Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 310 and 400] [Versions 311 and 400] [Versions 39 and 400] [Versions 400 and 401] [Versions 400 and 402] [Versions 400 and 403]

   1  <?php
   2  /**
   3   * Interbase driver.
   4   *
   5   * Requires interbase client. Works on Windows and Unix.
   6   *
   7   * This file is part of ADOdb, a Database Abstraction Layer library for PHP.
   8   *
   9   * @package ADOdb
  10   * @link https://adodb.org Project's web site and documentation
  11   * @link https://github.com/ADOdb/ADOdb Source code and issue tracker
  12   *
  13   * The ADOdb Library is dual-licensed, released under both the BSD 3-Clause
  14   * and the GNU Lesser General Public Licence (LGPL) v2.1 or, at your option,
  15   * any later version. This means you can use it in proprietary products.
  16   * See the LICENSE.md file distributed with this source code for details.
  17   * @license BSD-3-Clause
  18   * @license LGPL-2.1-or-later
  19   *
  20   * @copyright 2000-2013 John Lim
  21   * @copyright 2014 Damien Regad, Mark Newnham and the ADOdb community
  22   */
  23  
  24  // security - hide paths
  25  if (!defined('ADODB_DIR')) die();
  26  
  27  class ADODB_ibase extends ADOConnection {
  28  	 var $databaseType = "ibase";
  29  	 var $dataProvider = "ibase";
  30  	 var $replaceQuote = "''"; // string to use to replace quotes
  31  	 var $ibase_datefmt = '%Y-%m-%d'; // For hours,mins,secs change to '%Y-%m-%d %H:%M:%S';
  32  	 var $fmtDate = "'Y-m-d'";
  33  	 var $ibase_timestampfmt = "%Y-%m-%d %H:%M:%S";
  34  	 var $ibase_timefmt = "%H:%M:%S";
  35  	 var $fmtTimeStamp = "'Y-m-d, H:i:s'";
  36  	 var $concat_operator='||';
  37  	 var $_transactionID;
  38  	 var $metaTablesSQL = "select rdb\$relation_name from rdb\$relations where rdb\$relation_name not like 'RDB\$%'";
  39  	 //OPN STUFF start
  40  	 var $metaColumnsSQL = "select a.rdb\$field_name, a.rdb\$null_flag, a.rdb\$default_source, b.rdb\$field_length, b.rdb\$field_scale, b.rdb\$field_sub_type, b.rdb\$field_precision, b.rdb\$field_type from rdb\$relation_fields a, rdb\$fields b where a.rdb\$field_source = b.rdb\$field_name and a.rdb\$relation_name = '%s' order by a.rdb\$field_position asc";
  41  	 //OPN STUFF end
  42  	 var $ibasetrans;
  43  	 var $hasGenID = true;
  44  	 var $_bindInputArray = true;
  45  	 var $buffers = 0;
  46  	 var $dialect = 1;
  47  	 var $sysDate = "cast('TODAY' as timestamp)";
  48  	 var $sysTimeStamp = "CURRENT_TIMESTAMP"; //"cast('NOW' as timestamp)";
  49  	 var $ansiOuter = true;
  50  	 var $hasAffectedRows = false;
  51  	 var $poorAffectedRows = true;
  52  	 var $blobEncodeType = 'C';
  53  	 var $role = false;
  54  
  55  	function __construct()
  56  	 {
  57  	 	 if (defined('IBASE_DEFAULT')) $this->ibasetrans = IBASE_DEFAULT;
  58  	 }
  59  
  60  
  61  	 // returns true or false
  62  	function _connect($argHostname, $argUsername, $argPassword, $argDatabasename,$persist=false)
  63  	 {
  64  	 	 if (!function_exists('ibase_pconnect')) return null;
  65  	 	 if ($argDatabasename) $argHostname .= ':'.$argDatabasename;
  66  	 	 $fn = ($persist) ? 'ibase_pconnect':'ibase_connect';
  67  	 	 if ($this->role)
  68  	 	 	 $this->_connectionID = $fn($argHostname,$argUsername,$argPassword,
  69  	 	 	 	 	 $this->charSet,$this->buffers,$this->dialect,$this->role);
  70  	 	 else
  71  	 	 	 $this->_connectionID = $fn($argHostname,$argUsername,$argPassword,
  72  	 	 	 	 	 $this->charSet,$this->buffers,$this->dialect);
  73  
  74  	 	 if ($this->dialect != 1) { // http://www.ibphoenix.com/ibp_60_del_id_ds.html
  75  	 	 	 $this->replaceQuote = "''";
  76  	 	 }
  77  	 	 if ($this->_connectionID === false) {
  78  	 	 	 $this->_handleerror();
  79  	 	 	 return false;
  80  	 	 }
  81  
  82  	 	 // PHP5 change.
  83  	 	 if (function_exists('ibase_timefmt')) {
  84  	 	 	 ibase_timefmt($this->ibase_datefmt,IBASE_DATE );
  85  	 	 	 if ($this->dialect == 1) {
  86  	 	 	 	 ibase_timefmt($this->ibase_datefmt,IBASE_TIMESTAMP );
  87  	 	 	 }
  88  	 	 	 else {
  89  	 	 	 	 ibase_timefmt($this->ibase_timestampfmt,IBASE_TIMESTAMP );
  90  	 	 	 }
  91  	 	 	 ibase_timefmt($this->ibase_timefmt,IBASE_TIME );
  92  
  93  	 	 } else {
  94  	 	 	 ini_set("ibase.timestampformat", $this->ibase_timestampfmt);
  95  	 	 	 ini_set("ibase.dateformat", $this->ibase_datefmt);
  96  	 	 	 ini_set("ibase.timeformat", $this->ibase_timefmt);
  97  	 	 }
  98  	 	 return true;
  99  	 }
 100  
 101  	 // returns true or false
 102  	function _pconnect($argHostname, $argUsername, $argPassword, $argDatabasename)
 103  	 {
 104  	 	 return $this->_connect($argHostname, $argUsername, $argPassword, $argDatabasename,true);
 105  	 }
 106  
 107  
 108  	function MetaPrimaryKeys($table,$owner_notused=false,$internalKey=false)
 109  	 {
 110  	 	 if ($internalKey) {
 111  	 	 	 return array('RDB$DB_KEY');
 112  	 	 }
 113  
 114  	 	 $table = strtoupper($table);
 115  
 116  	 	 $sql = 'SELECT S.RDB$FIELD_NAME AFIELDNAME
 117  	 FROM RDB$INDICES I JOIN RDB$INDEX_SEGMENTS S ON I.RDB$INDEX_NAME=S.RDB$INDEX_NAME
 118  	 WHERE I.RDB$RELATION_NAME=\''.$table.'\' and I.RDB$INDEX_NAME like \'RDB$PRIMARY%\'
 119  	 ORDER BY I.RDB$INDEX_NAME,S.RDB$FIELD_POSITION';
 120  
 121  	 	 $a = $this->GetCol($sql,false,true);
 122  	 	 if ($a && sizeof($a)>0) return $a;
 123  	 	 return false;
 124  	 }
 125  
 126  	function ServerInfo()
 127  	 {
 128  	 	 $arr['dialect'] = $this->dialect;
 129  	 	 switch($arr['dialect']) {
 130  	 	 case '':
 131  	 	 case '1': $s = 'Interbase 5.5 or earlier'; break;
 132  	 	 case '2': $s = 'Interbase 5.6'; break;
 133  	 	 default:
 134  	 	 case '3': $s = 'Interbase 6.0'; break;
 135  	 	 }
 136  	 	 $arr['version'] = ADOConnection::_findvers($s);
 137  	 	 $arr['description'] = $s;
 138  	 	 return $arr;
 139  	 }
 140  
 141  	function BeginTrans()
 142  	 {
 143  	 	 if ($this->transOff) return true;
 144  	 	 $this->transCnt += 1;
 145  	 	 $this->autoCommit = false;
 146  	 	 $this->_transactionID = $this->_connectionID;//ibase_trans($this->ibasetrans, $this->_connectionID);
 147  	 	 return $this->_transactionID;
 148  	 }
 149  
 150  	function CommitTrans($ok=true)
 151  	 {
 152  	 	 if (!$ok) {
 153  	 	 	 return $this->RollbackTrans();
 154  	 	 }
 155  	 	 if ($this->transOff) {
 156  	 	 	 return true;
 157  	 	 }
 158  	 	 if ($this->transCnt) {
 159  	 	 	 $this->transCnt -= 1;
 160  	 	 }
 161  	 	 $ret = false;
 162  	 	 $this->autoCommit = true;
 163  	 	 if ($this->_transactionID) {
 164  	 	 	 //print ' commit ';
 165  	 	 	 $ret = ibase_commit($this->_transactionID);
 166  	 	 }
 167  	 	 $this->_transactionID = false;
 168  	 	 return $ret;
 169  	 }
 170  
 171  	 // there are some compat problems with ADODB_COUNTRECS=false and $this->_logsql currently.
 172  	 // it appears that ibase extension cannot support multiple concurrent queryid's
 173  	function _Execute($sql,$inputarr=false)
 174  	 {
 175  	 global $ADODB_COUNTRECS;
 176  
 177  	 	 if ($this->_logsql) {
 178  	 	 	 $savecrecs = $ADODB_COUNTRECS;
 179  	 	 	 $ADODB_COUNTRECS = true; // force countrecs
 180  	 	 	 $ret = ADOConnection::_Execute($sql,$inputarr);
 181  	 	 	 $ADODB_COUNTRECS = $savecrecs;
 182  	 	 } else {
 183  	 	 	 $ret = ADOConnection::_Execute($sql,$inputarr);
 184  	 	 }
 185  	 	 return $ret;
 186  	 }
 187  
 188  	function RollbackTrans()
 189  	 {
 190  	 	 if ($this->transOff) return true;
 191  	 	 if ($this->transCnt) $this->transCnt -= 1;
 192  	 	 $ret = false;
 193  	 	 $this->autoCommit = true;
 194  	 	 if ($this->_transactionID) {
 195  	 	 	 $ret = ibase_rollback($this->_transactionID);
 196  	 	 }
 197  	 	 $this->_transactionID = false;
 198  
 199  	 	 return $ret;
 200  	 }
 201  
 202  	function MetaIndexes ($table, $primary = FALSE, $owner=false)
 203  	 {
 204  	 	 // save old fetch mode
 205  	 	 global $ADODB_FETCH_MODE;
 206  	 	 $false = false;
 207  	 	 $save = $ADODB_FETCH_MODE;
 208  	 	 $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
 209  	 	 if ($this->fetchMode !== FALSE) {
 210  	 	 	 	 $savem = $this->SetFetchMode(FALSE);
 211  	 	 }
 212  	 	 $table = strtoupper($table);
 213  	 	 $sql = "SELECT * FROM RDB\$INDICES WHERE RDB\$RELATION_NAME = '".$table."'";
 214  	 	 if (!$primary) {
 215  	 	 	 $sql .= " AND RDB\$INDEX_NAME NOT LIKE 'RDB\$%'";
 216  	 	 } else {
 217  	 	 	 $sql .= " AND RDB\$INDEX_NAME NOT LIKE 'RDB\$FOREIGN%'";
 218  	 	 }
 219  	 	 // get index details
 220  	 	 $rs = $this->Execute($sql);
 221  	 	 if (!is_object($rs)) {
 222  	 	 	 // restore fetchmode
 223  	 	 	 if (isset($savem)) {
 224  	 	 	 	 $this->SetFetchMode($savem);
 225  	 	 	 }
 226  	 	 	 $ADODB_FETCH_MODE = $save;
 227  	 	 	 return $false;
 228  	 	 }
 229  
 230  	 	 $indexes = array();
 231  	 	 while ($row = $rs->FetchRow()) {
 232  	 	 	 $index = $row[0];
 233  	 	 	 if (!isset($indexes[$index])) {
 234  	 	 	 	 if (is_null($row[3])) {
 235  	 	 	 	 	 $row[3] = 0;
 236  	 	 	 	 }
 237  	 	 	 	 $indexes[$index] = array(
 238  	 	 	 	 	 'unique' => ($row[3] == 1),
 239  	 	 	 	 	 'columns' => array()
 240  	 	 	 	 );
 241  	 	 	 }
 242  	 	 	 $sql = "SELECT * FROM RDB\$INDEX_SEGMENTS WHERE RDB\$INDEX_NAME = '".$index."' ORDER BY RDB\$FIELD_POSITION ASC";
 243  	 	 	 $rs1 = $this->Execute($sql);
 244  	 	 	 while ($row1 = $rs1->FetchRow()) {
 245  	 	 	 	 $indexes[$index]['columns'][$row1[2]] = $row1[1];
 246  	 	 	 }
 247  	 	 }
 248  	 	 // restore fetchmode
 249  	 	 if (isset($savem)) {
 250  	 	 	 $this->SetFetchMode($savem);
 251  	 	 }
 252  	 	 $ADODB_FETCH_MODE = $save;
 253  
 254  	 	 return $indexes;
 255  	 }
 256  
 257  
 258  	 // See http://community.borland.com/article/0,1410,25844,00.html
 259  	function RowLock($tables,$where,$col=false)
 260  	 {
 261  	 	 if ($this->autoCommit) {
 262  	 	 	 $this->BeginTrans();
 263  	 	 }
 264  	 	 $this->Execute("UPDATE $table SET $col=$col WHERE $where "); // is this correct - jlim?
 265  	 	 return 1;
 266  	 }
 267  
 268  
 269  	function CreateSequence($seqname = 'adodbseq', $startID = 1)
 270  	 {
 271  	 	 $ok = $this->Execute(("INSERT INTO RDB\$GENERATORS (RDB\$GENERATOR_NAME) VALUES (UPPER('$seqname'))" ));
 272  	 	 if (!$ok) return false;
 273  	 	 return $this->Execute("SET GENERATOR $seqname TO ".($startID-1).';');
 274  	 }
 275  
 276  	function DropSequence($seqname = 'adodbseq')
 277  	 {
 278  	 	 $seqname = strtoupper($seqname);
 279  	 	 $this->Execute("delete from RDB\$GENERATORS where RDB\$GENERATOR_NAME='$seqname'");
 280  	 }
 281  
 282  	function GenID($seqname='adodbseq',$startID=1)
 283  	 {
 284  	 	 $getnext = ("SELECT Gen_ID($seqname,1) FROM RDB\$DATABASE");
 285  	 	 $rs = @$this->Execute($getnext);
 286  	 	 if (!$rs) {
 287  	 	 	 $this->Execute(("INSERT INTO RDB\$GENERATORS (RDB\$GENERATOR_NAME) VALUES (UPPER('$seqname'))" ));
 288  	 	 	 $this->Execute("SET GENERATOR $seqname TO ".($startID-1).';');
 289  	 	 	 $rs = $this->Execute($getnext);
 290  	 	 }
 291  	 	 if ($rs && !$rs->EOF) {
 292  	 	 	 $this->genID = (integer) reset($rs->fields);
 293  	 	 }
 294  	 	 else {
 295  	 	 	 $this->genID = 0; // false
 296  	 	 }
 297  
 298  	 	 if ($rs) {
 299  	 	 	 $rs->Close();
 300  	 	 }
 301  
 302  	 	 return $this->genID;
 303  	 }
 304  
 305  	function SelectDB($dbName)
 306  	 {
 307  	 	 return false;
 308  	 }
 309  
 310  	function _handleerror()
 311  	 {
 312  	 	 $this->_errorMsg = ibase_errmsg();
 313  	 }
 314  
 315  	function ErrorNo()
 316  	 {
 317  	 	 if (preg_match('/error code = ([\-0-9]*)/i', $this->_errorMsg,$arr)) return (integer) $arr[1];
 318  	 	 else return 0;
 319  	 }
 320  
 321  	function ErrorMsg()
 322  	 {
 323  	 	 	 return $this->_errorMsg;
 324  	 }
 325  
 326  	function Prepare($sql)
 327  	 {
 328  	 	 $stmt = ibase_prepare($this->_connectionID,$sql);
 329  	 	 if (!$stmt) return false;
 330  	 	 return array($sql,$stmt);
 331  	 }
 332  
 333  	 // returns query ID if successful, otherwise false
 334  	 // there have been reports of problems with nested queries - the code is probably not re-entrant?
 335  	function _query($sql,$iarr=false)
 336  	 {
 337  
 338  	 	 if (!$this->autoCommit && $this->_transactionID) {
 339  	 	 	 $conn = $this->_transactionID;
 340  	 	 	 $docommit = false;
 341  	 	 } else {
 342  	 	 	 $conn = $this->_connectionID;
 343  	 	 	 $docommit = true;
 344  	 	 }
 345  	 	 if (is_array($sql)) {
 346  	 	 	 $fn = 'ibase_execute';
 347  	 	 	 $sql = $sql[1];
 348  	 	 	 if (is_array($iarr)) {
 349  	 	 	 	 if ( !isset($iarr[0]) ) 
 350  	 	 	 	 	 $iarr[0] = ''; // PHP5 compat hack
 351  	 	 	 	 $fnarr = array_merge( array($sql) , $iarr);
 352  	 	 	 	 $ret = call_user_func_array($fn,$fnarr);
 353  	 	 	 }
 354  	 	 	 else {
 355  	 	 	 	 $ret = $fn($sql);
 356  	 	 	 }
 357  	 	 } else {
 358  	 	 	 $fn = 'ibase_query';
 359  
 360  	 	 	 if (is_array($iarr)) {
 361  	 	 	 	 if (sizeof($iarr) == 0) 
 362  	 	 	 	 	 $iarr[0] = ''; // PHP5 compat hack
 363  	 	 	 	 $fnarr = array_merge( array($conn,$sql) , $iarr);
 364  	 	 	 	 $ret = call_user_func_array($fn,$fnarr);
 365  	 	 	 }
 366  	 	 	 else {
 367  	 	 	 	 $ret = $fn($conn, $sql);
 368  	 	 	 }
 369  	 	 }
 370  	 	 if ($docommit && $ret === true) {
 371  	 	 	 ibase_commit($this->_connectionID);
 372  	 	 }
 373  
 374  	 	 $this->_handleerror();
 375  	 	 return $ret;
 376  	 }
 377  
 378  	 // returns true or false
 379  	function _close()
 380  	 {
 381  	 	 if (!$this->autoCommit) {
 382  	 	 	 @ibase_rollback($this->_connectionID);
 383  	 	 }
 384  	 	 return @ibase_close($this->_connectionID);
 385  	 }
 386  
 387  	 //OPN STUFF start
 388  	function _ConvertFieldType(&$fld, $ftype, $flen, $fscale, $fsubtype, $fprecision, $dialect3)
 389  	 {
 390  	 	 $fscale = abs($fscale);
 391  	 	 $fld->max_length = $flen;
 392  	 	 $fld->scale = null;
 393  	 	 switch($ftype){
 394  	 	 	 case 7:
 395  	 	 	 case 8:
 396  	 	 	 	 if ($dialect3) {
 397  	 	 	 	 	 switch($fsubtype){
 398  	 	 	 	 	 	 case 0:
 399  	 	 	 	 	 	 	 $fld->type = ($ftype == 7 ? 'smallint' : 'integer');
 400  	 	 	 	 	 	 	 break;
 401  	 	 	 	 	 	 case 1:
 402  	 	 	 	 	 	 	 $fld->type = 'numeric';
 403  	 	 	 	 	 	 	 $fld->max_length = $fprecision;
 404  	 	 	 	 	 	 	 $fld->scale = $fscale;
 405  	 	 	 	 	 	 	 break;
 406  	 	 	 	 	 	 case 2:
 407  	 	 	 	 	 	 	 $fld->type = 'decimal';
 408  	 	 	 	 	 	 	 $fld->max_length = $fprecision;
 409  	 	 	 	 	 	 	 $fld->scale = $fscale;
 410  	 	 	 	 	 	 	 break;
 411  	 	 	 	 	 } // switch
 412  	 	 	 	 } else {
 413  	 	 	 	 	 if ($fscale !=0) {
 414  	 	 	 	 	 	 $fld->type = 'decimal';
 415  	 	 	 	 	 	 $fld->scale = $fscale;
 416  	 	 	 	 	 	 $fld->max_length = ($ftype == 7 ? 4 : 9);
 417  	 	 	 	 	 } else {
 418  	 	 	 	 	 	 $fld->type = ($ftype == 7 ? 'smallint' : 'integer');
 419  	 	 	 	 	 }
 420  	 	 	 	 }
 421  	 	 	 	 break;
 422  	 	 	 case 16:
 423  	 	 	 	 if ($dialect3) {
 424  	 	 	 	 	 switch($fsubtype){
 425  	 	 	 	 	 	 case 0:
 426  	 	 	 	 	 	 	 $fld->type = 'decimal';
 427  	 	 	 	 	 	 	 $fld->max_length = 18;
 428  	 	 	 	 	 	 	 $fld->scale = 0;
 429  	 	 	 	 	 	 	 break;
 430  	 	 	 	 	 	 case 1:
 431  	 	 	 	 	 	 	 $fld->type = 'numeric';
 432  	 	 	 	 	 	 	 $fld->max_length = $fprecision;
 433  	 	 	 	 	 	 	 $fld->scale = $fscale;
 434  	 	 	 	 	 	 	 break;
 435  	 	 	 	 	 	 case 2:
 436  	 	 	 	 	 	 	 $fld->type = 'decimal';
 437  	 	 	 	 	 	 	 $fld->max_length = $fprecision;
 438  	 	 	 	 	 	 	 $fld->scale = $fscale;
 439  	 	 	 	 	 	 	 break;
 440  	 	 	 	 	 } // switch
 441  	 	 	 	 }
 442  	 	 	 	 break;
 443  	 	 	 case 10:
 444  	 	 	 	 $fld->type = 'float';
 445  	 	 	 	 break;
 446  	 	 	 case 14:
 447  	 	 	 	 $fld->type = 'char';
 448  	 	 	 	 break;
 449  	 	 	 case 27:
 450  	 	 	 	 if ($fscale !=0) {
 451  	 	 	 	 	 $fld->type = 'decimal';
 452  	 	 	 	 	 $fld->max_length = 15;
 453  	 	 	 	 	 $fld->scale = 5;
 454  	 	 	 	 } else {
 455  	 	 	 	 	 $fld->type = 'double';
 456  	 	 	 	 }
 457  	 	 	 	 break;
 458  	 	 	 case 35:
 459  	 	 	 	 if ($dialect3) {
 460  	 	 	 	 	 $fld->type = 'timestamp';
 461  	 	 	 	 } else {
 462  	 	 	 	 	 $fld->type = 'date';
 463  	 	 	 	 }
 464  	 	 	 	 break;
 465  	 	 	 case 12:
 466  	 	 	 	 $fld->type = 'date';
 467  	 	 	 	 break;
 468  	 	 	 case 13:
 469  	 	 	 	 $fld->type = 'time';
 470  	 	 	 	 break;
 471  	 	 	 case 37:
 472  	 	 	 	 $fld->type = 'varchar';
 473  	 	 	 	 break;
 474  	 	 	 case 40:
 475  	 	 	 	 $fld->type = 'cstring';
 476  	 	 	 	 break;
 477  	 	 	 case 261:
 478  	 	 	 	 $fld->type = 'blob';
 479  	 	 	 	 $fld->max_length = -1;
 480  	 	 	 	 break;
 481  	 	 } // switch
 482  	 }
 483  	 //OPN STUFF end
 484  
 485  	 // returns array of ADOFieldObjects for current table
 486  	function MetaColumns($table, $normalize=true)
 487  	 {
 488  	 global $ADODB_FETCH_MODE;
 489  
 490  	 	 $save = $ADODB_FETCH_MODE;
 491  	 	 $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
 492  
 493  	 	 $rs = $this->Execute(sprintf($this->metaColumnsSQL,strtoupper($table)));
 494  
 495  	 	 $ADODB_FETCH_MODE = $save;
 496  	 	 $false = false;
 497  	 	 if ($rs === false) {
 498  	 	 	 return $false;
 499  	 	 }
 500  
 501  	 	 $retarr = array();
 502  	 	 //OPN STUFF start
 503  	 	 $dialect3 = ($this->dialect==3 ? true : false);
 504  	 	 //OPN STUFF end
 505  	 	 while (!$rs->EOF) { //print_r($rs->fields);
 506  	 	 	 $fld = new ADOFieldObject();
 507  	 	 	 $fld->name = trim($rs->fields[0]);
 508  	 	 	 //OPN STUFF start
 509  	 	 	 $this->_ConvertFieldType($fld, $rs->fields[7], $rs->fields[3], $rs->fields[4], $rs->fields[5], $rs->fields[6], $dialect3);
 510  	 	 	 if (isset($rs->fields[1]) && $rs->fields[1]) {
 511  	 	 	 	 $fld->not_null = true;
 512  	 	 	 }
 513  	 	 	 if (isset($rs->fields[2])) {
 514  
 515  	 	 	 	 $fld->has_default = true;
 516  	 	 	 	 $d = substr($rs->fields[2],strlen('default '));
 517  	 	 	 	 switch ($fld->type)
 518  	 	 	 	 {
 519  	 	 	 	 case 'smallint':
 520  	 	 	 	 case 'integer': $fld->default_value = (int) $d; break;
 521  	 	 	 	 case 'char':
 522  	 	 	 	 case 'blob':
 523  	 	 	 	 case 'text':
 524  	 	 	 	 case 'varchar': $fld->default_value = (string) substr($d,1,strlen($d)-2); break;
 525  	 	 	 	 case 'double':
 526  	 	 	 	 case 'float': $fld->default_value = (float) $d; break;
 527  	 	 	 	 default: $fld->default_value = $d; break;
 528  	 	 	 	 }
 529  	 	 //	 case 35:$tt = 'TIMESTAMP'; break;
 530  	 	 	 }
 531  	 	 	 if ((isset($rs->fields[5])) && ($fld->type == 'blob')) {
 532  	 	 	 	 $fld->sub_type = $rs->fields[5];
 533  	 	 	 } else {
 534  	 	 	 	 $fld->sub_type = null;
 535  	 	 	 }
 536  	 	 	 //OPN STUFF end
 537  	 	 	 if ($ADODB_FETCH_MODE == ADODB_FETCH_NUM) $retarr[] = $fld;
 538  	 	 	 else $retarr[strtoupper($fld->name)] = $fld;
 539  
 540  	 	 	 $rs->MoveNext();
 541  	 	 }
 542  	 	 $rs->Close();
 543  	 	 if ( empty($retarr)) return $false;
 544  	 	 else return $retarr;
 545  	 }
 546  
 547  	function BlobEncode( $blob )
 548  	 {
 549  	 	 $blobid = ibase_blob_create( $this->_connectionID);
 550  	 	 ibase_blob_add( $blobid, $blob );
 551  	 	 return ibase_blob_close( $blobid );
 552  	 }
 553  
 554  	 // since we auto-decode all blob's since 2.42,
 555  	 // BlobDecode should not do any transforms
 556  	function BlobDecode($blob)
 557  	 {
 558  	 	 return $blob;
 559  	 }
 560  
 561  
 562  
 563  
 564  	 // old blobdecode function
 565  	 // still used to auto-decode all blob's
 566  	function _BlobDecode_old( $blob )
 567  	 {
 568  	 	 $blobid = ibase_blob_open($this->_connectionID, $blob );
 569  	 	 $realblob = ibase_blob_get( $blobid,$this->maxblobsize); // 2nd param is max size of blob -- Kevin Boillet <kevinboillet@yahoo.fr>
 570  	 	 while($string = ibase_blob_get($blobid, 8192)){
 571  	 	 	 $realblob .= $string;
 572  	 	 }
 573  	 	 ibase_blob_close( $blobid );
 574  
 575  	 	 return( $realblob );
 576  	 }
 577  
 578  	function _BlobDecode( $blob )
 579  	 {
 580  	 	 $blob_data = ibase_blob_info($this->_connectionID, $blob );
 581  	 	 $blobid    = ibase_blob_open($this->_connectionID, $blob );
 582  
 583  	 	 if( $blob_data[0] > $this->maxblobsize ) {
 584  
 585  	 	 	 $realblob = ibase_blob_get($blobid, $this->maxblobsize);
 586  
 587  	 	 	 while($string = ibase_blob_get($blobid, 8192)){
 588  	 	 	 	 $realblob .= $string;
 589  	 	 	 }
 590  	 	 } else {
 591  	 	 	 $realblob = ibase_blob_get($blobid, $blob_data[0]);
 592  	 	 }
 593  
 594  	 	 ibase_blob_close( $blobid );
 595  	 	 return( $realblob );
 596  	 }
 597  
 598  	function UpdateBlobFile($table,$column,$path,$where,$blobtype='BLOB')
 599  	 {
 600  	 	 $fd = fopen($path,'rb');
 601  	 	 if ($fd === false) return false;
 602  	 	 $blob_id = ibase_blob_create($this->_connectionID);
 603  
 604  	 	 /* fill with data */
 605  
 606  	 	 while ($val = fread($fd,32768)){
 607  	 	 	 ibase_blob_add($blob_id, $val);
 608  	 	 }
 609  
 610  	 	 /* close and get $blob_id_str for inserting into table */
 611  	 	 $blob_id_str = ibase_blob_close($blob_id);
 612  
 613  	 	 fclose($fd);
 614  	 	 return $this->Execute("UPDATE $table SET $column=(?) WHERE $where",array($blob_id_str)) != false;
 615  	 }
 616  
 617  	 /*
 618  	 	 Insert a null into the blob field of the table first.
 619  	 	 Then use UpdateBlob to store the blob.
 620  
 621  	 	 Usage:
 622  
 623  	 	 $conn->Execute('INSERT INTO blobtable (id, blobcol) VALUES (1, null)');
 624  	 	 $conn->UpdateBlob('blobtable','blobcol',$blob,'id=1');
 625  	 */
 626  	function UpdateBlob($table,$column,$val,$where,$blobtype='BLOB')
 627  	 {
 628  	 $blob_id = ibase_blob_create($this->_connectionID);
 629  
 630  	 // ibase_blob_add($blob_id, $val);
 631  
 632  	 // replacement that solves the problem by which only the first modulus 64K /
 633  	 // of $val are stored at the blob field ////////////////////////////////////
 634  	 // Thx Abel Berenstein  aberenstein#afip.gov.ar
 635  	 $len = strlen($val);
 636  	 $chunk_size = 32768;
 637  	 $tail_size = $len % $chunk_size;
 638  	 $n_chunks = ($len - $tail_size) / $chunk_size;
 639  
 640  	 for ($n = 0; $n < $n_chunks; $n++) {
 641  	 	 $start = $n * $chunk_size;
 642  	 	 $data = substr($val, $start, $chunk_size);
 643  	 	 ibase_blob_add($blob_id, $data);
 644  	 }
 645  
 646  	 if ($tail_size) {
 647  	 	 $start = $n_chunks * $chunk_size;
 648  	 	 $data = substr($val, $start, $tail_size);
 649  	 	 ibase_blob_add($blob_id, $data);
 650  	 }
 651  	 // end replacement /////////////////////////////////////////////////////////
 652  
 653  	 $blob_id_str = ibase_blob_close($blob_id);
 654  
 655  	 return $this->Execute("UPDATE $table SET $column=(?) WHERE $where",array($blob_id_str)) != false;
 656  
 657  	 }
 658  
 659  
 660  	function OldUpdateBlob($table,$column,$val,$where,$blobtype='BLOB')
 661  	 {
 662  	 	 $blob_id = ibase_blob_create($this->_connectionID);
 663  	 	 ibase_blob_add($blob_id, $val);
 664  	 	 $blob_id_str = ibase_blob_close($blob_id);
 665  	 	 return $this->Execute("UPDATE $table SET $column=(?) WHERE $where",array($blob_id_str)) != false;
 666  	 }
 667  
 668  	 // Format date column in sql string given an input format that understands Y M D
 669  	 // Only since Interbase 6.0 - uses EXTRACT
 670  	 // problem - does not zero-fill the day and month yet
 671  	function SQLDate($fmt, $col=false)
 672  	 {
 673  	 	 if (!$col) $col = $this->sysDate;
 674  	 	 $s = '';
 675  
 676  	 	 $len = strlen($fmt);
 677  	 	 for ($i=0; $i < $len; $i++) {
 678  	 	 	 if ($s) $s .= '||';
 679  	 	 	 $ch = $fmt[$i];
 680  	 	 	 switch($ch) {
 681  	 	 	 case 'Y':
 682  	 	 	 case 'y':
 683  	 	 	 	 $s .= "extract(year from $col)";
 684  	 	 	 	 break;
 685  	 	 	 case 'M':
 686  	 	 	 case 'm':
 687  	 	 	 	 $s .= "extract(month from $col)";
 688  	 	 	 	 break;
 689  	 	 	 case 'Q':
 690  	 	 	 case 'q':
 691  	 	 	 	 $s .= "cast(((extract(month from $col)+2) / 3) as integer)";
 692  	 	 	 	 break;
 693  	 	 	 case 'D':
 694  	 	 	 case 'd':
 695  	 	 	 	 $s .= "(extract(day from $col))";
 696  	 	 	 	 break;
 697  	 	 	 case 'H':
 698  	 	 	 case 'h':
 699  	 	 	 	 $s .= "(extract(hour from $col))";
 700  	 	 	 	 break;
 701  	 	 	 case 'I':
 702  	 	 	 case 'i':
 703  	 	 	 	 $s .= "(extract(minute from $col))";
 704  	 	 	 	 break;
 705  	 	 	 case 'S':
 706  	 	 	 case 's':
 707  	 	 	 	 $s .= "CAST((extract(second from $col)) AS INTEGER)";
 708  	 	 	 	 break;
 709  
 710  	 	 	 default:
 711  	 	 	 	 if ($ch == '\\') {
 712  	 	 	 	 	 $i++;
 713  	 	 	 	 	 $ch = substr($fmt,$i,1);
 714  	 	 	 	 }
 715  	 	 	 	 $s .= $this->qstr($ch);
 716  	 	 	 	 break;
 717  	 	 	 }
 718  	 	 }
 719  	 	 return $s;
 720  	 }
 721  }
 722  
 723  /*--------------------------------------------------------------------------------------
 724  	 Class Name: Recordset
 725  --------------------------------------------------------------------------------------*/
 726  
 727  class ADORecordset_ibase extends ADORecordSet
 728  {
 729  
 730  	 var $databaseType = "ibase";
 731  	 var $bind=false;
 732  	 var $_cacheType;
 733  
 734  	function __construct($id,$mode=false)
 735  	 {
 736  	 global $ADODB_FETCH_MODE;
 737  
 738  	 	 	 $this->fetchMode = ($mode === false) ? $ADODB_FETCH_MODE : $mode;
 739  	 	 	 parent::__construct($id);
 740  	 }
 741  
 742  	 /*	 	 Returns: an object containing field information.
 743  	 	 	 Get column information in the Recordset object. fetchField() can be used in order to obtain information about
 744  	 	 	 fields in a certain query result. If the field offset isn't specified, the next field that wasn't yet retrieved by
 745  	 	 	 fetchField() is retrieved.	 	 */
 746  
 747  	function FetchField($fieldOffset = -1)
 748  	 {
 749  	 	 	 $fld = new ADOFieldObject;
 750  	 	 	 $ibf = ibase_field_info($this->_queryID,$fieldOffset);
 751  
 752  	 	 	 $name = empty($ibf['alias']) ? $ibf['name'] : $ibf['alias'];
 753  
 754  	 	 	 switch (ADODB_ASSOC_CASE) {
 755  	 	 	 	 case ADODB_ASSOC_CASE_UPPER:
 756  	 	 	 	 	 $fld->name = strtoupper($name);
 757  	 	 	 	 	 break;
 758  	 	 	 	 case ADODB_ASSOC_CASE_LOWER:
 759  	 	 	 	 	 $fld->name = strtolower($name);
 760  	 	 	 	 	 break;
 761  	 	 	 	 case ADODB_ASSOC_CASE_NATIVE:
 762  	 	 	 	 default:
 763  	 	 	 	 	 $fld->name = $name;
 764  	 	 	 	 	 break;
 765  	 	 	 }
 766  
 767  	 	 	 $fld->type = $ibf['type'];
 768  	 	 	 $fld->max_length = $ibf['length'];
 769  
 770  	 	 	 /*       This needs to be populated from the metadata */
 771  	 	 	 $fld->not_null = false;
 772  	 	 	 $fld->has_default = false;
 773  	 	 	 $fld->default_value = 'null';
 774  	 	 	 return $fld;
 775  	 }
 776  
 777  	function _initrs()
 778  	 {
 779  	 	 $this->_numOfRows = -1;
 780  	 	 $this->_numOfFields = @ibase_num_fields($this->_queryID);
 781  
 782  	 	 // cache types for blob decode check
 783  	 	 for ($i=0, $max = $this->_numOfFields; $i < $max; $i++) {
 784  	 	 	 $f1 = $this->FetchField($i);
 785  	 	 	 $this->_cacheType[] = $f1->type;
 786  	 	 }
 787  	 }
 788  
 789  	function _seek($row)
 790  	 {
 791  	 	 return false;
 792  	 }
 793  
 794  	function _fetch()
 795  	 {
 796  	 	 $f = @ibase_fetch_row($this->_queryID);
 797  	 	 if ($f === false) {
 798  	 	 	 $this->fields = false;
 799  	 	 	 return false;
 800  	 	 }
 801  	 	 // OPN stuff start - optimized
 802  	 	 // fix missing nulls and decode blobs automatically
 803  
 804  	 	 global $ADODB_ANSI_PADDING_OFF;
 805  	 	 //$ADODB_ANSI_PADDING_OFF=1;
 806  	 	 $rtrim = !empty($ADODB_ANSI_PADDING_OFF);
 807  
 808  	 	 for ($i=0, $max = $this->_numOfFields; $i < $max; $i++) {
 809  	 	 	 if ($this->_cacheType[$i]=="BLOB") {
 810  	 	 	 	 if (isset($f[$i])) {
 811  	 	 	 	 	 $f[$i] = $this->connection->_BlobDecode($f[$i]);
 812  	 	 	 	 } else {
 813  	 	 	 	 	 $f[$i] = null;
 814  	 	 	 	 }
 815  	 	 	 } else {
 816  	 	 	 	 if (!isset($f[$i])) {
 817  	 	 	 	 	 $f[$i] = null;
 818  	 	 	 	 } else if ($rtrim && is_string($f[$i])) {
 819  	 	 	 	 	 $f[$i] = rtrim($f[$i]);
 820  	 	 	 	 }
 821  	 	 	 }
 822  	 	 }
 823  	 	 // OPN stuff end
 824  
 825  	 	 $this->fields = $f;
 826  	 	 if ($this->fetchMode == ADODB_FETCH_ASSOC) {
 827  	 	 	 $this->fields = $this->GetRowAssoc();
 828  	 	 } else if ($this->fetchMode == ADODB_FETCH_BOTH) {
 829  	 	 	 $this->fields = array_merge($this->fields,$this->GetRowAssoc());
 830  	 	 }
 831  	 	 return true;
 832  	 }
 833  
 834  	 /* Use associative array to get fields array */
 835  	function Fields($colname)
 836  	 {
 837  	 	 if ($this->fetchMode & ADODB_FETCH_ASSOC) return $this->fields[$colname];
 838  	 	 if (!$this->bind) {
 839  	 	 	 $this->bind = array();
 840  	 	 	 for ($i=0; $i < $this->_numOfFields; $i++) {
 841  	 	 	 	 $o = $this->FetchField($i);
 842  	 	 	 	 $this->bind[strtoupper($o->name)] = $i;
 843  	 	 	 }
 844  	 	 }
 845  
 846  	 	 return $this->fields[$this->bind[strtoupper($colname)]];
 847  
 848  	 }
 849  
 850  
 851  	function _close()
 852  	 {
 853  	 	 	 return @ibase_free_result($this->_queryID);
 854  	 }
 855  
 856  	function MetaType($t,$len=-1,$fieldobj=false)
 857  	 {
 858  	 	 if (is_object($t)) {
 859  	 	 	 $fieldobj = $t;
 860  	 	 	 $t = $fieldobj->type;
 861  	 	 	 $len = $fieldobj->max_length;
 862  	 	 }
 863  	 	 switch (strtoupper($t)) {
 864  	 	 case 'CHAR':
 865  	 	 	 return 'C';
 866  
 867  	 	 case 'TEXT':
 868  	 	 case 'VARCHAR':
 869  	 	 case 'VARYING':
 870  	 	 if ($len <= $this->blobSize) return 'C';
 871  	 	 	 return 'X';
 872  	 	 case 'BLOB':
 873  	 	 	 return 'B';
 874  
 875  	 	 case 'TIMESTAMP':
 876  	 	 case 'DATE': return 'D';
 877  	 	 case 'TIME': return 'T';
 878  	 	 	 	 //case 'T': return 'T';
 879  
 880  	 	 	 	 //case 'L': return 'L';
 881  	 	 case 'INT':
 882  	 	 case 'SHORT':
 883  	 	 case 'INTEGER': return 'I';
 884  	 	 default: return ADODB_DEFAULT_METATYPE;
 885  	 	 }
 886  	 }
 887  
 888  }