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