Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

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

   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  	 	 if (!$this->autoCommit && $this->_transactionID) {
 338  	 	 	 $conn = $this->_transactionID;
 339  	 	 	 $docommit = false;
 340  	 	 } else {
 341  	 	 	 $conn = $this->_connectionID;
 342  	 	 	 $docommit = true;
 343  	 	 }
 344  	 	 if (is_array($sql)) {
 345  	 	 	 $fn = 'ibase_execute';
 346  	 	 	 $sql = $sql[1];
 347  	 	 	 if (is_array($iarr)) {
 348  	 	 	 	 if (!isset($iarr[0])) {
 349  	 	 	 	 	 $iarr[0] = '';  // PHP5 compat hack
 350  	 	 	 	 }
 351  	 	 	 	 $fnarr = array_merge(array($sql), $iarr);
 352  	 	 	 	 $ret = call_user_func_array($fn, $fnarr);
 353  	 	 	 } else {
 354  	 	 	 	 $ret = $fn($sql);
 355  	 	 	 }
 356  	 	 } else {
 357  	 	 	 $fn = 'ibase_query';
 358  
 359  	 	 	 if (is_array($iarr)) {
 360  	 	 	 	 if (sizeof($iarr) == 0) {
 361  	 	 	 	 	 $iarr[0] = ''; // PHP5 compat hack
 362  	 	 	 	 }
 363  	 	 	 	 $fnarr = array_merge(array($conn, $sql), $iarr);
 364  	 	 	 	 $ret = call_user_func_array($fn, $fnarr);
 365  	 	 	 } else {
 366  	 	 	 	 $ret = $fn($conn, $sql);
 367  	 	 	 }
 368  	 	 }
 369  
 370  	 	 // ibase_query() and ibase_execute() return number of affected rows
 371  	 	 // ADOConnection::_Execute() expects true for INSERT/UPDATE/DELETE
 372  	 	 if (is_numeric($ret)) {
 373  	 	 	 $ret = true;
 374  	 	 }
 375  
 376  	 	 if ($docommit && $ret === true) {
 377  	 	 	 ibase_commit($this->_connectionID);
 378  	 	 }
 379  
 380  	 	 $this->_handleerror();
 381  	 	 return $ret;
 382  	 }
 383  
 384  	 // returns true or false
 385  	function _close()
 386  	 {
 387  	 	 if (!$this->autoCommit) {
 388  	 	 	 @ibase_rollback($this->_connectionID);
 389  	 	 }
 390  	 	 return @ibase_close($this->_connectionID);
 391  	 }
 392  
 393  	 //OPN STUFF start
 394  	function _ConvertFieldType(&$fld, $ftype, $flen, $fscale, $fsubtype, $fprecision, $dialect3)
 395  	 {
 396  	 	 $fscale = abs($fscale);
 397  	 	 $fld->max_length = $flen;
 398  	 	 $fld->scale = null;
 399  	 	 switch($ftype){
 400  	 	 	 case 7:
 401  	 	 	 case 8:
 402  	 	 	 	 if ($dialect3) {
 403  	 	 	 	 	 switch($fsubtype){
 404  	 	 	 	 	 	 case 0:
 405  	 	 	 	 	 	 	 $fld->type = ($ftype == 7 ? 'smallint' : 'integer');
 406  	 	 	 	 	 	 	 break;
 407  	 	 	 	 	 	 case 1:
 408  	 	 	 	 	 	 	 $fld->type = 'numeric';
 409  	 	 	 	 	 	 	 $fld->max_length = $fprecision;
 410  	 	 	 	 	 	 	 $fld->scale = $fscale;
 411  	 	 	 	 	 	 	 break;
 412  	 	 	 	 	 	 case 2:
 413  	 	 	 	 	 	 	 $fld->type = 'decimal';
 414  	 	 	 	 	 	 	 $fld->max_length = $fprecision;
 415  	 	 	 	 	 	 	 $fld->scale = $fscale;
 416  	 	 	 	 	 	 	 break;
 417  	 	 	 	 	 } // switch
 418  	 	 	 	 } else {
 419  	 	 	 	 	 if ($fscale !=0) {
 420  	 	 	 	 	 	 $fld->type = 'decimal';
 421  	 	 	 	 	 	 $fld->scale = $fscale;
 422  	 	 	 	 	 	 $fld->max_length = ($ftype == 7 ? 4 : 9);
 423  	 	 	 	 	 } else {
 424  	 	 	 	 	 	 $fld->type = ($ftype == 7 ? 'smallint' : 'integer');
 425  	 	 	 	 	 }
 426  	 	 	 	 }
 427  	 	 	 	 break;
 428  	 	 	 case 16:
 429  	 	 	 	 if ($dialect3) {
 430  	 	 	 	 	 switch($fsubtype){
 431  	 	 	 	 	 	 case 0:
 432  	 	 	 	 	 	 	 $fld->type = 'decimal';
 433  	 	 	 	 	 	 	 $fld->max_length = 18;
 434  	 	 	 	 	 	 	 $fld->scale = 0;
 435  	 	 	 	 	 	 	 break;
 436  	 	 	 	 	 	 case 1:
 437  	 	 	 	 	 	 	 $fld->type = 'numeric';
 438  	 	 	 	 	 	 	 $fld->max_length = $fprecision;
 439  	 	 	 	 	 	 	 $fld->scale = $fscale;
 440  	 	 	 	 	 	 	 break;
 441  	 	 	 	 	 	 case 2:
 442  	 	 	 	 	 	 	 $fld->type = 'decimal';
 443  	 	 	 	 	 	 	 $fld->max_length = $fprecision;
 444  	 	 	 	 	 	 	 $fld->scale = $fscale;
 445  	 	 	 	 	 	 	 break;
 446  	 	 	 	 	 } // switch
 447  	 	 	 	 }
 448  	 	 	 	 break;
 449  	 	 	 case 10:
 450  	 	 	 	 $fld->type = 'float';
 451  	 	 	 	 break;
 452  	 	 	 case 14:
 453  	 	 	 	 $fld->type = 'char';
 454  	 	 	 	 break;
 455  	 	 	 case 27:
 456  	 	 	 	 if ($fscale !=0) {
 457  	 	 	 	 	 $fld->type = 'decimal';
 458  	 	 	 	 	 $fld->max_length = 15;
 459  	 	 	 	 	 $fld->scale = 5;
 460  	 	 	 	 } else {
 461  	 	 	 	 	 $fld->type = 'double';
 462  	 	 	 	 }
 463  	 	 	 	 break;
 464  	 	 	 case 35:
 465  	 	 	 	 if ($dialect3) {
 466  	 	 	 	 	 $fld->type = 'timestamp';
 467  	 	 	 	 } else {
 468  	 	 	 	 	 $fld->type = 'date';
 469  	 	 	 	 }
 470  	 	 	 	 break;
 471  	 	 	 case 12:
 472  	 	 	 	 $fld->type = 'date';
 473  	 	 	 	 break;
 474  	 	 	 case 13:
 475  	 	 	 	 $fld->type = 'time';
 476  	 	 	 	 break;
 477  	 	 	 case 37:
 478  	 	 	 	 $fld->type = 'varchar';
 479  	 	 	 	 break;
 480  	 	 	 case 40:
 481  	 	 	 	 $fld->type = 'cstring';
 482  	 	 	 	 break;
 483  	 	 	 case 261:
 484  	 	 	 	 $fld->type = 'blob';
 485  	 	 	 	 $fld->max_length = -1;
 486  	 	 	 	 break;
 487  	 	 } // switch
 488  	 }
 489  	 //OPN STUFF end
 490  
 491  	 // returns array of ADOFieldObjects for current table
 492  	function MetaColumns($table, $normalize=true)
 493  	 {
 494  	 global $ADODB_FETCH_MODE;
 495  
 496  	 	 $save = $ADODB_FETCH_MODE;
 497  	 	 $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
 498  
 499  	 	 $rs = $this->Execute(sprintf($this->metaColumnsSQL,strtoupper($table)));
 500  
 501  	 	 $ADODB_FETCH_MODE = $save;
 502  	 	 $false = false;
 503  	 	 if ($rs === false) {
 504  	 	 	 return $false;
 505  	 	 }
 506  
 507  	 	 $retarr = array();
 508  	 	 //OPN STUFF start
 509  	 	 $dialect3 = ($this->dialect==3 ? true : false);
 510  	 	 //OPN STUFF end
 511  	 	 while (!$rs->EOF) { //print_r($rs->fields);
 512  	 	 	 $fld = new ADOFieldObject();
 513  	 	 	 $fld->name = trim($rs->fields[0]);
 514  	 	 	 //OPN STUFF start
 515  	 	 	 $this->_ConvertFieldType($fld, $rs->fields[7], $rs->fields[3], $rs->fields[4], $rs->fields[5], $rs->fields[6], $dialect3);
 516  	 	 	 if (isset($rs->fields[1]) && $rs->fields[1]) {
 517  	 	 	 	 $fld->not_null = true;
 518  	 	 	 }
 519  	 	 	 if (isset($rs->fields[2])) {
 520  
 521  	 	 	 	 $fld->has_default = true;
 522  	 	 	 	 $d = substr($rs->fields[2],strlen('default '));
 523  	 	 	 	 switch ($fld->type) {
 524  	 	 	 	 	 case 'smallint':
 525  	 	 	 	 	 case 'integer':
 526  	 	 	 	 	 	 $fld->default_value = (int)$d;
 527  	 	 	 	 	 	 break;
 528  	 	 	 	 	 case 'char':
 529  	 	 	 	 	 case 'blob':
 530  	 	 	 	 	 case 'text':
 531  	 	 	 	 	 case 'varchar':
 532  	 	 	 	 	 	 $fld->default_value = (string)substr($d, 1, strlen($d) - 2);
 533  	 	 	 	 	 	 break;
 534  	 	 	 	 	 case 'double':
 535  	 	 	 	 	 case 'float':
 536  	 	 	 	 	 	 $fld->default_value = (float)$d;
 537  	 	 	 	 	 	 break;
 538  	 	 	 	 	 default:
 539  	 	 	 	 	 	 $fld->default_value = $d;
 540  	 	 	 	 	 	 break;
 541  	 	 	 	 }
 542  	 	 //	 case 35:$tt = 'TIMESTAMP'; break;
 543  	 	 	 }
 544  	 	 	 if ((isset($rs->fields[5])) && ($fld->type == 'blob')) {
 545  	 	 	 	 $fld->sub_type = $rs->fields[5];
 546  	 	 	 } else {
 547  	 	 	 	 $fld->sub_type = null;
 548  	 	 	 }
 549  	 	 	 //OPN STUFF end
 550  	 	 	 if ($ADODB_FETCH_MODE == ADODB_FETCH_NUM) $retarr[] = $fld;
 551  	 	 	 else $retarr[strtoupper($fld->name)] = $fld;
 552  
 553  	 	 	 $rs->MoveNext();
 554  	 	 }
 555  	 	 $rs->Close();
 556  	 	 if ( empty($retarr)) return $false;
 557  	 	 else return $retarr;
 558  	 }
 559  
 560  	function BlobEncode( $blob )
 561  	 {
 562  	 	 $blobid = ibase_blob_create( $this->_connectionID);
 563  	 	 ibase_blob_add( $blobid, $blob );
 564  	 	 return ibase_blob_close( $blobid );
 565  	 }
 566  
 567  	 // since we auto-decode all blob's since 2.42,
 568  	 // BlobDecode should not do any transforms
 569  	function BlobDecode($blob)
 570  	 {
 571  	 	 return $blob;
 572  	 }
 573  
 574  	 // old blobdecode function
 575  	 // still used to auto-decode all blob's
 576  	function _BlobDecode_old( $blob )
 577  	 {
 578  	 	 $blobid = ibase_blob_open($this->_connectionID, $blob );
 579  	 	 $realblob = ibase_blob_get( $blobid,$this->maxblobsize); // 2nd param is max size of blob -- Kevin Boillet <kevinboillet@yahoo.fr>
 580  	 	 while($string = ibase_blob_get($blobid, 8192)){
 581  	 	 	 $realblob .= $string;
 582  	 	 }
 583  	 	 ibase_blob_close( $blobid );
 584  
 585  	 	 return( $realblob );
 586  	 }
 587  
 588  	function _BlobDecode( $blob )
 589  	 {
 590  	 	 $blob_data = ibase_blob_info($this->_connectionID, $blob );
 591  	 	 $blobid    = ibase_blob_open($this->_connectionID, $blob );
 592  
 593  	 	 if( $blob_data[0] > $this->maxblobsize ) {
 594  
 595  	 	 	 $realblob = ibase_blob_get($blobid, $this->maxblobsize);
 596  
 597  	 	 	 while($string = ibase_blob_get($blobid, 8192)){
 598  	 	 	 	 $realblob .= $string;
 599  	 	 	 }
 600  	 	 } else {
 601  	 	 	 $realblob = ibase_blob_get($blobid, $blob_data[0]);
 602  	 	 }
 603  
 604  	 	 ibase_blob_close( $blobid );
 605  	 	 return( $realblob );
 606  	 }
 607  
 608  	function UpdateBlobFile($table,$column,$path,$where,$blobtype='BLOB')
 609  	 {
 610  	 	 $fd = fopen($path,'rb');
 611  	 	 if ($fd === false) return false;
 612  	 	 $blob_id = ibase_blob_create($this->_connectionID);
 613  
 614  	 	 /* fill with data */
 615  
 616  	 	 while ($val = fread($fd,32768)){
 617  	 	 	 ibase_blob_add($blob_id, $val);
 618  	 	 }
 619  
 620  	 	 /* close and get $blob_id_str for inserting into table */
 621  	 	 $blob_id_str = ibase_blob_close($blob_id);
 622  
 623  	 	 fclose($fd);
 624  	 	 return $this->Execute("UPDATE $table SET $column=(?) WHERE $where",array($blob_id_str)) != false;
 625  	 }
 626  
 627  	 /*
 628  	 	 Insert a null into the blob field of the table first.
 629  	 	 Then use UpdateBlob to store the blob.
 630  
 631  	 	 Usage:
 632  
 633  	 	 $conn->Execute('INSERT INTO blobtable (id, blobcol) VALUES (1, null)');
 634  	 	 $conn->UpdateBlob('blobtable','blobcol',$blob,'id=1');
 635  	 */
 636  	function UpdateBlob($table,$column,$val,$where,$blobtype='BLOB')
 637  	 {
 638  	 	 $blob_id = ibase_blob_create($this->_connectionID);
 639  
 640  	 	 // ibase_blob_add($blob_id, $val);
 641  
 642  	 	 // replacement that solves the problem by which only the first modulus 64K /
 643  	 	 // of $val are stored at the blob field ////////////////////////////////////
 644  	 	 // Thx Abel Berenstein  aberenstein#afip.gov.ar
 645  	 	 $len = strlen($val);
 646  	 	 $chunk_size = 32768;
 647  	 	 $tail_size = $len % $chunk_size;
 648  	 	 $n_chunks = ($len - $tail_size) / $chunk_size;
 649  
 650  	 	 for ($n = 0; $n < $n_chunks; $n++) {
 651  	 	 	 $start = $n * $chunk_size;
 652  	 	 	 $data = substr($val, $start, $chunk_size);
 653  	 	 	 ibase_blob_add($blob_id, $data);
 654  	 	 }
 655  
 656  	 	 if ($tail_size) {
 657  	 	 	 $start = $n_chunks * $chunk_size;
 658  	 	 	 $data = substr($val, $start, $tail_size);
 659  	 	 	 ibase_blob_add($blob_id, $data);
 660  	 	 }
 661  	 	 // end replacement /////////////////////////////////////////////////////////
 662  
 663  	 	 $blob_id_str = ibase_blob_close($blob_id);
 664  
 665  	 	 return $this->Execute("UPDATE $table SET $column=(?) WHERE $where", array($blob_id_str)) != false;
 666  
 667  	 }
 668  
 669  
 670  	function OldUpdateBlob($table,$column,$val,$where,$blobtype='BLOB')
 671  	 {
 672  	 	 $blob_id = ibase_blob_create($this->_connectionID);
 673  	 	 ibase_blob_add($blob_id, $val);
 674  	 	 $blob_id_str = ibase_blob_close($blob_id);
 675  	 	 return $this->Execute("UPDATE $table SET $column=(?) WHERE $where",array($blob_id_str)) != false;
 676  	 }
 677  
 678  	 // Format date column in sql string given an input format that understands Y M D
 679  	 // Only since Interbase 6.0 - uses EXTRACT
 680  	 // problem - does not zero-fill the day and month yet
 681  	function SQLDate($fmt, $col=false)
 682  	 {
 683  	 	 if (!$col) $col = $this->sysDate;
 684  	 	 $s = '';
 685  
 686  	 	 $len = strlen($fmt);
 687  	 	 for ($i=0; $i < $len; $i++) {
 688  	 	 	 if ($s) $s .= '||';
 689  	 	 	 $ch = $fmt[$i];
 690  	 	 	 switch($ch) {
 691  	 	 	 case 'Y':
 692  	 	 	 case 'y':
 693  	 	 	 	 $s .= "extract(year from $col)";
 694  	 	 	 	 break;
 695  	 	 	 case 'M':
 696  	 	 	 case 'm':
 697  	 	 	 	 $s .= "extract(month from $col)";
 698  	 	 	 	 break;
 699  	 	 	 case 'Q':
 700  	 	 	 case 'q':
 701  	 	 	 	 $s .= "cast(((extract(month from $col)+2) / 3) as integer)";
 702  	 	 	 	 break;
 703  	 	 	 case 'D':
 704  	 	 	 case 'd':
 705  	 	 	 	 $s .= "(extract(day from $col))";
 706  	 	 	 	 break;
 707  	 	 	 case 'H':
 708  	 	 	 case 'h':
 709  	 	 	 	 $s .= "(extract(hour from $col))";
 710  	 	 	 	 break;
 711  	 	 	 case 'I':
 712  	 	 	 case 'i':
 713  	 	 	 	 $s .= "(extract(minute from $col))";
 714  	 	 	 	 break;
 715  	 	 	 case 'S':
 716  	 	 	 case 's':
 717  	 	 	 	 $s .= "CAST((extract(second from $col)) AS INTEGER)";
 718  	 	 	 	 break;
 719  
 720  	 	 	 default:
 721  	 	 	 	 if ($ch == '\\') {
 722  	 	 	 	 	 $i++;
 723  	 	 	 	 	 $ch = substr($fmt,$i,1);
 724  	 	 	 	 }
 725  	 	 	 	 $s .= $this->qstr($ch);
 726  	 	 	 	 break;
 727  	 	 	 }
 728  	 	 }
 729  	 	 return $s;
 730  	 }
 731  }
 732  
 733  /*--------------------------------------------------------------------------------------
 734  	 Class Name: Recordset
 735  --------------------------------------------------------------------------------------*/
 736  
 737  class ADORecordset_ibase extends ADORecordSet
 738  {
 739  
 740  	 var $databaseType = "ibase";
 741  	 var $bind=false;
 742  	 var $_cacheType;
 743  
 744  	function __construct($id,$mode=false)
 745  	 {
 746  	 global $ADODB_FETCH_MODE;
 747  
 748  	 	 	 $this->fetchMode = ($mode === false) ? $ADODB_FETCH_MODE : $mode;
 749  	 	 	 parent::__construct($id);
 750  	 }
 751  
 752  	 /*	 	 Returns: an object containing field information.
 753  	 	 	 Get column information in the Recordset object. fetchField() can be used in order to obtain information about
 754  	 	 	 fields in a certain query result. If the field offset isn't specified, the next field that wasn't yet retrieved by
 755  	 	 	 fetchField() is retrieved.	 	 */
 756  
 757  	function FetchField($fieldOffset = -1)
 758  	 {
 759  	 	 	 $fld = new ADOFieldObject;
 760  	 	 	 $ibf = ibase_field_info($this->_queryID,$fieldOffset);
 761  
 762  	 	 	 $name = empty($ibf['alias']) ? $ibf['name'] : $ibf['alias'];
 763  
 764  	 	 	 switch (ADODB_ASSOC_CASE) {
 765  	 	 	 	 case ADODB_ASSOC_CASE_UPPER:
 766  	 	 	 	 	 $fld->name = strtoupper($name);
 767  	 	 	 	 	 break;
 768  	 	 	 	 case ADODB_ASSOC_CASE_LOWER:
 769  	 	 	 	 	 $fld->name = strtolower($name);
 770  	 	 	 	 	 break;
 771  	 	 	 	 case ADODB_ASSOC_CASE_NATIVE:
 772  	 	 	 	 default:
 773  	 	 	 	 	 $fld->name = $name;
 774  	 	 	 	 	 break;
 775  	 	 	 }
 776  
 777  	 	 	 $fld->type = $ibf['type'];
 778  	 	 	 $fld->max_length = $ibf['length'];
 779  
 780  	 	 	 /*       This needs to be populated from the metadata */
 781  	 	 	 $fld->not_null = false;
 782  	 	 	 $fld->has_default = false;
 783  	 	 	 $fld->default_value = 'null';
 784  	 	 	 return $fld;
 785  	 }
 786  
 787  	function _initrs()
 788  	 {
 789  	 	 $this->_numOfRows = -1;
 790  	 	 $this->_numOfFields = @ibase_num_fields($this->_queryID);
 791  
 792  	 	 // cache types for blob decode check
 793  	 	 for ($i=0, $max = $this->_numOfFields; $i < $max; $i++) {
 794  	 	 	 $f1 = $this->FetchField($i);
 795  	 	 	 $this->_cacheType[] = $f1->type;
 796  	 	 }
 797  	 }
 798  
 799  	function _seek($row)
 800  	 {
 801  	 	 return false;
 802  	 }
 803  
 804  	function _fetch()
 805  	 {
 806  	 	 $f = @ibase_fetch_row($this->_queryID);
 807  	 	 if ($f === false) {
 808  	 	 	 $this->fields = false;
 809  	 	 	 return false;
 810  	 	 }
 811  	 	 // OPN stuff start - optimized
 812  	 	 // fix missing nulls and decode blobs automatically
 813  
 814  	 	 global $ADODB_ANSI_PADDING_OFF;
 815  	 	 //$ADODB_ANSI_PADDING_OFF=1;
 816  	 	 $rtrim = !empty($ADODB_ANSI_PADDING_OFF);
 817  
 818  	 	 for ($i=0, $max = $this->_numOfFields; $i < $max; $i++) {
 819  	 	 	 if ($this->_cacheType[$i]=="BLOB") {
 820  	 	 	 	 if (isset($f[$i])) {
 821  	 	 	 	 	 $f[$i] = $this->connection->_BlobDecode($f[$i]);
 822  	 	 	 	 } else {
 823  	 	 	 	 	 $f[$i] = null;
 824  	 	 	 	 }
 825  	 	 	 } else {
 826  	 	 	 	 if (!isset($f[$i])) {
 827  	 	 	 	 	 $f[$i] = null;
 828  	 	 	 	 } else if ($rtrim && is_string($f[$i])) {
 829  	 	 	 	 	 $f[$i] = rtrim($f[$i]);
 830  	 	 	 	 }
 831  	 	 	 }
 832  	 	 }
 833  	 	 // OPN stuff end
 834  
 835  	 	 $this->fields = $f;
 836  	 	 if ($this->fetchMode == ADODB_FETCH_ASSOC) {
 837  	 	 	 $this->fields = $this->GetRowAssoc();
 838  	 	 } else if ($this->fetchMode == ADODB_FETCH_BOTH) {
 839  	 	 	 $this->fields = array_merge($this->fields,$this->GetRowAssoc());
 840  	 	 }
 841  	 	 return true;
 842  	 }
 843  
 844  	 /* Use associative array to get fields array */
 845  	function Fields($colname)
 846  	 {
 847  	 	 if ($this->fetchMode & ADODB_FETCH_ASSOC) return $this->fields[$colname];
 848  	 	 if (!$this->bind) {
 849  	 	 	 $this->bind = array();
 850  	 	 	 for ($i=0; $i < $this->_numOfFields; $i++) {
 851  	 	 	 	 $o = $this->FetchField($i);
 852  	 	 	 	 $this->bind[strtoupper($o->name)] = $i;
 853  	 	 	 }
 854  	 	 }
 855  
 856  	 	 return $this->fields[$this->bind[strtoupper($colname)]];
 857  	 }
 858  
 859  
 860  	function _close()
 861  	 {
 862  	 	 return @ibase_free_result($this->_queryID);
 863  	 }
 864  
 865  	function MetaType($t,$len=-1,$fieldobj=false)
 866  	 {
 867  	 	 if (is_object($t)) {
 868  	 	 	 $fieldobj = $t;
 869  	 	 	 $t = $fieldobj->type;
 870  	 	 	 $len = $fieldobj->max_length;
 871  	 	 }
 872  
 873  	 	 $t = strtoupper($t);
 874  
 875  	 	 if (array_key_exists($t, $this->connection->customActualTypes)) {
 876  	 	 	 return $this->connection->customActualTypes[$t];
 877  	 	 }
 878  
 879  	 	 switch ($t) {
 880  	 	 	 case 'CHAR':
 881  	 	 	 	 return 'C';
 882  
 883  	 	 	 case 'TEXT':
 884  	 	 	 case 'VARCHAR':
 885  	 	 	 case 'VARYING':
 886  	 	 	 	 if ($len <= $this->blobSize) {
 887  	 	 	 	 	 return 'C';
 888  	 	 	 	 }
 889  	 	 	 	 return 'X';
 890  	 	 	 case 'BLOB':
 891  	 	 	 	 return 'B';
 892  
 893  	 	 	 case 'TIMESTAMP':
 894  	 	 	 case 'DATE':
 895  	 	 	 	 return 'D';
 896  	 	 	 case 'TIME':
 897  	 	 	 	 return 'T';
 898  	 	 	 //case 'T': return 'T';
 899  
 900  	 	 	 //case 'L': return 'L';
 901  	 	 	 case 'INT':
 902  	 	 	 case 'SHORT':
 903  	 	 	 case 'INTEGER':
 904  	 	 	 	 return 'I';
 905  	 	 	 default:
 906  	 	 	 	 return ADODB_DEFAULT_METATYPE;
 907  	 	 }
 908  	 }
 909  
 910  }