Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

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

   1  <?php
   2  /*
   3  @version   v5.21.0  2021-02-27
   4  @copyright (c) 2000-2013 John Lim (jlim#natsoft.com). All rights reserved.
   5  @copyright (c) 2014      Damien Regad, Mark Newnham and the ADOdb community
   6    Released under both BSD license and Lesser GPL library license.
   7    Whenever there is any discrepancy between the two licenses,
   8    the BSD license will take precedence.
   9  Set tabs to 4 for best viewing.
  10  
  11    Latest version is available at https://adodb.org/
  12  
  13    firebird data driver. Requires firebird client. Works on Windows and Unix.
  14  
  15  */
  16  
  17  // security - hide paths
  18  if (!defined('ADODB_DIR')) die();
  19  
  20  class ADODB_firebird extends ADOConnection {
  21  	 var $databaseType = "firebird";
  22  	 var $dataProvider = "firebird";
  23  	 var $replaceQuote = "''"; // string to use to replace quotes
  24  	 var $fbird_datefmt = '%Y-%m-%d'; // For hours,mins,secs change to '%Y-%m-%d %H:%M:%S';
  25  	 var $fmtDate = "'Y-m-d'";
  26  	 var $fbird_timestampfmt = "%Y-%m-%d %H:%M:%S";
  27  	 var $fbird_timefmt = "%H:%M:%S";
  28  	 var $fmtTimeStamp = "'Y-m-d, H:i:s'";
  29  	 var $concat_operator='||';
  30  	 var $_transactionID;
  31  	 var $metaTablesSQL = "select lower(rdb\$relation_name) from rdb\$relations where rdb\$relation_name not like 'RDB\$%'";
  32  	 //OPN STUFF start
  33  	 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";
  34  	 //OPN STUFF end
  35  	 var $ibasetrans;
  36  	 var $hasGenID = true;
  37  	 var $_bindInputArray = true;
  38  	 var $buffers = 0;
  39  	 var $dialect = 3;
  40  	 var $sysDate = "cast('TODAY' as timestamp)";
  41  	 var $sysTimeStamp = "CURRENT_TIMESTAMP"; //"cast('NOW' as timestamp)";
  42  	 var $ansiOuter = true;
  43  	 var $hasAffectedRows = true;
  44  	 var $poorAffectedRows = false;
  45  	 var $blobEncodeType = 'C';
  46  	 var $role = false;
  47  	 var $nameQuote = '';	 	 /// string to use to quote identifiers and names
  48  
  49  	function __construct()
  50  	 {
  51  	 // Ignore IBASE_DEFAULT we want a more practical transaction!
  52  	 //	 if (defined('IBASE_DEFAULT')) $this->ibasetrans = IBASE_DEFAULT;
  53  	 //	 else
  54  	 	 $this->ibasetrans = IBASE_WAIT | IBASE_REC_VERSION | IBASE_COMMITTED;
  55  	 }
  56  
  57  
  58  	 // returns true or false
  59  	function _connect($argHostname, $argUsername, $argPassword, $argDatabasename,$persist=false)
  60  	 {
  61  	 	 if (!function_exists('fbird_pconnect')) return null;
  62  	 	 if ($argDatabasename) $argHostname .= ':'.$argDatabasename;
  63  	 	 $fn = ($persist) ? 'fbird_pconnect':'fbird_connect';
  64  	 	 if ($this->role)
  65  	 	 	 $this->_connectionID = $fn($argHostname,$argUsername,$argPassword,
  66  	 	 	 	 	 $this->charSet,$this->buffers,$this->dialect,$this->role);
  67  	 	 else
  68  	 	 	 $this->_connectionID = $fn($argHostname,$argUsername,$argPassword,
  69  	 	 	 	 	 $this->charSet,$this->buffers,$this->dialect);
  70  
  71  	 	 if ($this->dialect != 1) { // http://www.ibphoenix.com/ibp_60_del_id_ds.html
  72  	 	 	 $this->replaceQuote = "''";
  73  	 	 }
  74  	 	 if ($this->_connectionID === false) {
  75  	 	 	 $this->_handleerror();
  76  	 	 	 return false;
  77  	 	 }
  78  
  79  	 	 // PHP5 change.
  80  	 	 if (function_exists('fbird_timefmt')) {
  81  	 	 	 fbird_timefmt($this->fbird_datefmt,fbird_DATE );
  82  	 	 	 if ($this->dialect == 1) {
  83  	 	 	 	 fbird_timefmt($this->fbird_datefmt,fbird_TIMESTAMP );
  84  	 	 	 } else {
  85  	 	 	 	 fbird_timefmt($this->fbird_timestampfmt,fbird_TIMESTAMP );
  86  	 	 	 }
  87  	 	 	 fbird_timefmt($this->fbird_timefmt,fbird_TIME );
  88  
  89  	 	 } else {
  90  	 	 	 ini_set("ibase.timestampformat", $this->fbird_timestampfmt);
  91  	 	 	 ini_set("ibase.dateformat", $this->fbird_datefmt);
  92  	 	 	 ini_set("ibase.timeformat", $this->fbird_timefmt);
  93  	 	 }
  94  	 	 return true;
  95  	 }
  96  
  97  	 // returns true or false
  98  	function _pconnect($argHostname, $argUsername, $argPassword, $argDatabasename)
  99  	 {
 100  	 	 return $this->_connect($argHostname, $argUsername, $argPassword, $argDatabasename,true);
 101  	 }
 102  
 103  
 104  	function MetaPrimaryKeys($table,$owner_notused=false,$internalKey=false)
 105  	 {
 106  	 	 if ($internalKey) {
 107  	 	 	 return array('RDB$DB_KEY');
 108  	 	 }
 109  
 110  	 	 $table = strtoupper($table);
 111  
 112  	 	 $sql = 'SELECT S.RDB$FIELD_NAME AFIELDNAME
 113  	 FROM RDB$INDICES I JOIN RDB$INDEX_SEGMENTS S ON I.RDB$INDEX_NAME=S.RDB$INDEX_NAME
 114  	 WHERE I.RDB$RELATION_NAME=\''.$table.'\' and I.RDB$INDEX_NAME like \'RDB$PRIMARY%\'
 115  	 ORDER BY I.RDB$INDEX_NAME,S.RDB$FIELD_POSITION';
 116  
 117  	 	 $a = $this->GetCol($sql,false,true);
 118  	 	 if ($a && sizeof($a)>0) return $a;
 119  	 	 return false;
 120  	 }
 121  
 122  	function ServerInfo()
 123  	 {
 124  	 	 $arr['dialect'] = $this->dialect;
 125  	 	 switch($arr['dialect']) {
 126  	 	 case '':
 127  	 	 case '1': $s = 'Firebird Dialect 1'; break;
 128  	 	 case '2': $s = 'Firebird Dialect 2'; break;
 129  	 	 default:
 130  	 	 case '3': $s = 'Firebird Dialect 3'; break;
 131  	 	 }
 132  	 	 $arr['version'] = ADOConnection::_findvers($s);
 133  	 	 $arr['description'] = $s;
 134  	 	 return $arr;
 135  	 }
 136  
 137  	function BeginTrans()
 138  	 {
 139  	 	 if ($this->transOff) return true;
 140  	 	 $this->transCnt += 1;
 141  	 	 $this->autoCommit = false;
 142  	 	 $this->_transactionID = fbird_trans( $this->ibasetrans, $this->_connectionID );
 143  	 	 return $this->_transactionID;
 144  	 }
 145  
 146  	function CommitTrans($ok=true)
 147  	 {
 148  	 	 if (!$ok) {
 149  	 	 	 return $this->RollbackTrans();
 150  	 	 }
 151  	 	 if ($this->transOff) {
 152  	 	 	 return true;
 153  	 	 }
 154  	 	 if ($this->transCnt) {
 155  	 	 	 $this->transCnt -= 1;
 156  	 	 }
 157  	 	 $ret = false;
 158  	 	 $this->autoCommit = true;
 159  	 	 if ($this->_transactionID) {
 160  	 	 	 //print ' commit ';
 161  	 	 	 $ret = fbird_commit($this->_transactionID);
 162  	 	 }
 163  	 	 $this->_transactionID = false;
 164  	 	 return $ret;
 165  	 }
 166  
 167  	function _affectedrows()
 168  	 {
 169  	 	 	 return fbird_affected_rows( $this->_transactionID ? $this->_transactionID : $this->_connectionID );
 170  	 }
 171  
 172  	 // there are some compat problems with ADODB_COUNTRECS=false and $this->_logsql currently.
 173  	 // it appears that ibase extension cannot support multiple concurrent queryid's
 174  	function _Execute($sql,$inputarr=false) {
 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 = fbird_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(("CREATE GENERATOR $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  	 	 return $this->Execute("DROP GENERATOR $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(("CREATE GENERATOR $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 = fbird_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 = fbird_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->isConnected() ) return false;
 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 = 'fbird_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 = 'fbird_query';
 359  	 	 	 if (is_array($iarr)) 
 360  	 	 	 {
 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  	 	 	 fbird_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  	 	 	 @fbird_rollback($this->_connectionID);
 383  	 	 }
 384  	 	 return @fbird_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 = fbird_blob_create( $this->_connectionID);
 550  	 	 fbird_blob_add( $blobid, $blob );
 551  	 	 return fbird_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  	 // old blobdecode function
 562  	 // still used to auto-decode all blob's
 563  	function _BlobDecode_old( $blob )
 564  	 {
 565  	 	 $blobid = fbird_blob_open($this->_connectionID, $blob );
 566  	 	 $realblob = fbird_blob_get( $blobid,$this->maxblobsize); // 2nd param is max size of blob -- Kevin Boillet <kevinboillet@yahoo.fr>
 567  	 	 while($string = fbird_blob_get($blobid, 8192)){
 568  	 	 	 $realblob .= $string;
 569  	 	 }
 570  	 	 fbird_blob_close( $blobid );
 571  
 572  	 	 return( $realblob );
 573  	 }
 574  
 575  	function _BlobDecode( $blob )
 576  	 {
 577  	 	 $blob_data = fbird_blob_info($this->_connectionID, $blob );
 578  	 	 $blobid    = fbird_blob_open($this->_connectionID, $blob );
 579  
 580  	 	 if( $blob_data[0] > $this->maxblobsize ) {
 581  	 	 	 $realblob = fbird_blob_get($blobid, $this->maxblobsize);
 582  
 583  	 	 	 while($string = fbird_blob_get($blobid, 8192)) {
 584  	 	 	 	 $realblob .= $string;
 585  	 	 	 }
 586  	 	 } else {
 587  	 	 	 $realblob = fbird_blob_get($blobid, $blob_data[0]);
 588  	 	 }
 589  
 590  	 	 fbird_blob_close( $blobid );
 591  	 	 return( $realblob );
 592  	 }
 593  
 594  	function UpdateBlobFile($table,$column,$path,$where,$blobtype='BLOB')
 595  	 {
 596  	 	 $fd = fopen($path,'rb');
 597  	 	 if ($fd === false) return false;
 598  	 	 $blob_id = fbird_blob_create($this->_connectionID);
 599  
 600  	 	 /* fill with data */
 601  
 602  	 	 while ($val = fread($fd,32768)){
 603  	 	 	 fbird_blob_add($blob_id, $val);
 604  	 	 }
 605  
 606  	 	 /* close and get $blob_id_str for inserting into table */
 607  	 	 $blob_id_str = fbird_blob_close($blob_id);
 608  
 609  	 	 fclose($fd);
 610  	 	 return $this->Execute("UPDATE $table SET $column=(?) WHERE $where",array($blob_id_str)) != false;
 611  	 }
 612  
 613  	 /*
 614  	 	 Insert a null into the blob field of the table first.
 615  	 	 Then use UpdateBlob to store the blob.
 616  
 617  	 	 Usage:
 618  
 619  	 	 $conn->Execute('INSERT INTO blobtable (id, blobcol) VALUES (1, null)');
 620  	 	 $conn->UpdateBlob('blobtable','blobcol',$blob,'id=1');
 621  	 */
 622  	function UpdateBlob($table,$column,$val,$where,$blobtype='BLOB')
 623  	 {
 624  	 $blob_id = fbird_blob_create($this->_connectionID);
 625  
 626  	 // fbird_blob_add($blob_id, $val);
 627  
 628  	 // replacement that solves the problem by which only the first modulus 64K /
 629  	 // of $val are stored at the blob field ////////////////////////////////////
 630  	 // Thx Abel Berenstein  aberenstein#afip.gov.ar
 631  	 $len = strlen($val);
 632  	 $chunk_size = 32768;
 633  	 $tail_size = $len % $chunk_size;
 634  	 $n_chunks = ($len - $tail_size) / $chunk_size;
 635  
 636  	 for ($n = 0; $n < $n_chunks; $n++) {
 637  	 	 $start = $n * $chunk_size;
 638  	 	 $data = substr($val, $start, $chunk_size);
 639  	 	 fbird_blob_add($blob_id, $data);
 640  	 }
 641  
 642  	 if ($tail_size) {
 643  	 	 $start = $n_chunks * $chunk_size;
 644  	 	 $data = substr($val, $start, $tail_size);
 645  	 	 fbird_blob_add($blob_id, $data);
 646  	 }
 647  	 // end replacement /////////////////////////////////////////////////////////
 648  
 649  	 $blob_id_str = fbird_blob_close($blob_id);
 650  
 651  	 return $this->Execute("UPDATE $table SET $column=(?) WHERE $where",array($blob_id_str)) != false;
 652  
 653  	 }
 654  
 655  
 656  	function OldUpdateBlob($table,$column,$val,$where,$blobtype='BLOB')
 657  	 {
 658  	 	 $blob_id = fbird_blob_create($this->_connectionID);
 659  	 	 fbird_blob_add($blob_id, $val);
 660  	 	 $blob_id_str = fbird_blob_close($blob_id);
 661  	 	 return $this->Execute("UPDATE $table SET $column=(?) WHERE $where",array($blob_id_str)) != false;
 662  	 }
 663  
 664  	 // Format date column in sql string given an input format that understands Y M D
 665  	 // Only since Interbase 6.0 - uses EXTRACT
 666  	 // problem - does not zero-fill the day and month yet
 667  	function SQLDate($fmt, $col=false)
 668  	 {
 669  	 	 if (!$col) $col = $this->sysDate;
 670  	 	 $s = '';
 671  
 672  	 	 $len = strlen($fmt);
 673  	 	 for ($i=0; $i < $len; $i++) {
 674  	 	 	 if ($s) $s .= '||';
 675  	 	 	 $ch = $fmt[$i];
 676  	 	 	 switch($ch) {
 677  	 	 	 case 'Y':
 678  	 	 	 case 'y':
 679  	 	 	 	 $s .= "extract(year from $col)";
 680  	 	 	 	 break;
 681  	 	 	 case 'M':
 682  	 	 	 case 'm':
 683  	 	 	 	 $s .= "extract(month from $col)";
 684  	 	 	 	 break;
 685  	 	 	 case 'W':
 686  	 	 	 case 'w':
 687  	 	 	 	 // The more accurate way of doing this is with a stored procedure
 688  	 	 	 	 // See http://wiki.firebirdsql.org/wiki/index.php?page=DATE+Handling+Functions for details
 689  	 	 	 	 $s .= "((extract(yearday from $col) - extract(weekday from $col - 1) + 7) / 7)";
 690  	 	 	 	 break;
 691  	 	 	 case 'Q':
 692  	 	 	 case 'q':
 693  	 	 	 	 $s .= "cast(((extract(month from $col)+2) / 3) as integer)";
 694  	 	 	 	 break;
 695  	 	 	 case 'D':
 696  	 	 	 case 'd':
 697  	 	 	 	 $s .= "(extract(day from $col))";
 698  	 	 	 	 break;
 699  	 	 	 case 'H':
 700  	 	 	 case 'h':
 701  	 	 	 	 $s .= "(extract(hour from $col))";
 702  	 	 	 	 break;
 703  	 	 	 case 'I':
 704  	 	 	 case 'i':
 705  	 	 	 	 $s .= "(extract(minute from $col))";
 706  	 	 	 	 break;
 707  	 	 	 case 'S':
 708  	 	 	 case 's':
 709  	 	 	 	 $s .= "CAST((extract(second from $col)) AS INTEGER)";
 710  	 	 	 	 break;
 711  
 712  	 	 	 default:
 713  	 	 	 	 if ($ch == '\\') {
 714  	 	 	 	 	 $i++;
 715  	 	 	 	 	 $ch = substr($fmt,$i,1);
 716  	 	 	 	 }
 717  	 	 	 	 $s .= $this->qstr($ch);
 718  	 	 	 	 break;
 719  	 	 	 }
 720  	 	 }
 721  	 	 return $s;
 722  	 }
 723  
 724  	 // Note that Interbase 6.5 uses this ROWS instead - don't you love forking wars!
 725  	 // 	 	 SELECT col1, col2 FROM table ROWS 5 -- get 5 rows
 726  	 //	 	 SELECT col1, col2 FROM TABLE ORDER BY col1 ROWS 3 TO 7 -- first 5 skip 2
 727  	 function &SelectLimit($sql,$nrows=-1,$offset=-1,$inputarr=false, $secs=0)
 728  	 {
 729  	 	 $nrows = (integer) $nrows;
 730  	 	 $offset = (integer) $offset;
 731  	 	 $str = 'SELECT ';
 732  	 	 if ($nrows >= 0) $str .= "FIRST $nrows ";
 733  	 	 $str .=($offset>=0) ? "SKIP $offset " : '';
 734  
 735  	 	 $sql = preg_replace('/^[ \t]*select/i',$str,$sql);
 736  	 	 if ($secs)
 737  	 	 	 $rs = $this->CacheExecute($secs,$sql,$inputarr);
 738  	 	 else
 739  	 	 	 $rs = $this->Execute($sql,$inputarr);
 740  
 741  	 	 return $rs;
 742  	 }
 743  
 744  }
 745  
 746  /*--------------------------------------------------------------------------------------
 747  	 	  Class Name: Recordset
 748  --------------------------------------------------------------------------------------*/
 749  
 750  class  ADORecordset_firebird extends ADORecordSet
 751  {
 752  
 753  	 var $databaseType = "firebird";
 754  	 var $bind=false;
 755  	 var $_cacheType;
 756  
 757  	function __construct($id,$mode=false)
 758  	 {
 759  	 global $ADODB_FETCH_MODE;
 760  
 761  	 	 	 $this->fetchMode = ($mode === false) ? $ADODB_FETCH_MODE : $mode;
 762  	 	 	 parent::__construct($id);
 763  	 }
 764  
 765  	 /**
 766  	  * Get column information in the Recordset object.
 767  	  * fetchField() can be used in order to obtain information about fields in
 768  	  * a certain query result. If the field offset isn't specified, the next
 769  	  * field that wasn't yet retrieved by fetchField() is retrieved.
 770  	  * @return object containing field information.
 771  	 */
 772  	function FetchField($fieldOffset = -1)
 773  	 {
 774  	 	 	 $fld = new ADOFieldObject;
 775  	 	 	  $ibf = fbird_field_info($this->_queryID,$fieldOffset);
 776  
 777  	 	 	 $name = empty($ibf['alias']) ? $ibf['name'] : $ibf['alias'];
 778  
 779  	 	 	 switch (ADODB_ASSOC_CASE) {
 780  	 	 	 	 case ADODB_ASSOC_CASE_UPPER:
 781  	 	 	 	 	 $fld->name = strtoupper($name);
 782  	 	 	 	 	 break;
 783  	 	 	 	 case ADODB_ASSOC_CASE_LOWER:
 784  	 	 	 	 	 $fld->name = strtolower($name);
 785  	 	 	 	 	 break;
 786  	 	 	 	 case ADODB_ASSOC_CASE_NATIVE:
 787  	 	 	 	 default:
 788  	 	 	 	 	 $fld->name = $name;
 789  	 	 	 	 	 break;
 790  	 	 	 }
 791  
 792  	 	 	 $fld->type = $ibf['type'];
 793  	 	 	 $fld->max_length = $ibf['length'];
 794  
 795  	 	 	 /*       This needs to be populated from the metadata */
 796  	 	 	 $fld->not_null = false;
 797  	 	 	 $fld->has_default = false;
 798  	 	 	 $fld->default_value = 'null';
 799  	 	 	 return $fld;
 800  	 }
 801  
 802  	function _initrs()
 803  	 {
 804  	 	 $this->_numOfRows = -1;
 805  	 	 $this->_numOfFields = @fbird_num_fields($this->_queryID);
 806  
 807  	 	 // cache types for blob decode check
 808  	 	 for ($i=0, $max = $this->_numOfFields; $i < $max; $i++) {
 809  	 	 	 $f1 = $this->FetchField($i);
 810  	 	 	 $this->_cacheType[] = $f1->type;
 811  	 	 }
 812  	 }
 813  
 814  	function _seek($row)
 815  	 {
 816  	 	 return false;
 817  	 }
 818  
 819  	function _fetch()
 820  	 {
 821  	 	 $f = @fbird_fetch_row($this->_queryID);
 822  	 	 if ($f === false) {
 823  	 	 	 $this->fields = false;
 824  	 	 	 return false;
 825  	 	 }
 826  	 	 // OPN stuff start - optimized
 827  	 	 // fix missing nulls and decode blobs automatically
 828  
 829  	 	 global $ADODB_ANSI_PADDING_OFF;
 830  	 	 //$ADODB_ANSI_PADDING_OFF=1;
 831  	 	 $rtrim = !empty($ADODB_ANSI_PADDING_OFF);
 832  
 833  	 	 for ($i=0, $max = $this->_numOfFields; $i < $max; $i++) {
 834  	 	 	 if ($this->_cacheType[$i]=="BLOB") {
 835  	 	 	 	 if (isset($f[$i])) {
 836  	 	 	 	 	 $f[$i] = $this->connection->_BlobDecode($f[$i]);
 837  	 	 	 	 } else {
 838  	 	 	 	 	 $f[$i] = null;
 839  	 	 	 	 }
 840  	 	 	 } else {
 841  	 	 	 	 if (!isset($f[$i])) {
 842  	 	 	 	 	 $f[$i] = null;
 843  	 	 	 	 } else if ($rtrim && is_string($f[$i])) {
 844  	 	 	 	 	 $f[$i] = rtrim($f[$i]);
 845  	 	 	 	 }
 846  	 	 	 }
 847  	 	 }
 848  	 	 // OPN stuff end
 849  
 850  	 	 $this->fields = $f;
 851  	 	 if ($this->fetchMode == ADODB_FETCH_ASSOC) {
 852  	 	 	 $this->fields = $this->GetRowAssoc();
 853  	 	 } else if ($this->fetchMode == ADODB_FETCH_BOTH) {
 854  	 	 	 $this->fields = array_merge($this->fields,$this->GetRowAssoc());
 855  	 	 }
 856  	 	 return true;
 857  	 }
 858  
 859  	 /* Use associative array to get fields array */
 860  	function Fields($colname)
 861  	 {
 862  	 	 if ($this->fetchMode & ADODB_FETCH_ASSOC) return $this->fields[$colname];
 863  	 	 if (!$this->bind) {
 864  	 	 	 $this->bind = array();
 865  	 	 	 for ($i=0; $i < $this->_numOfFields; $i++) {
 866  	 	 	 	 $o = $this->FetchField($i);
 867  	 	 	 	 $this->bind[strtoupper($o->name)] = $i;
 868  	 	 	 }
 869  	 	 }
 870  
 871  	 	 return $this->fields[$this->bind[strtoupper($colname)]];
 872  
 873  	 }
 874  
 875  
 876  	function _close()
 877  	 {
 878  	 	 	 return @fbird_free_result($this->_queryID);
 879  	 }
 880  
 881  	function MetaType($t,$len=-1,$fieldobj=false)
 882  	 {
 883  	 	 if (is_object($t)) {
 884  	 	 	 $fieldobj = $t;
 885  	 	 	 $t = $fieldobj->type;
 886  	 	 	 $len = $fieldobj->max_length;
 887  	 	 }
 888  	 	 switch (strtoupper($t)) {
 889  	 	 case 'CHAR':
 890  	 	 	 return 'C';
 891  
 892  	 	 case 'TEXT':
 893  	 	 case 'VARCHAR':
 894  	 	 case 'VARYING':
 895  	 	 if ($len <= $this->blobSize) return 'C';
 896  	 	 	 return 'X';
 897  	 	 case 'BLOB':
 898  	 	 	 return 'B';
 899  
 900  	 	 case 'TIMESTAMP':
 901  	 	 case 'DATE': return 'D';
 902  	 	 case 'TIME': return 'T';
 903  	 	 	 	 //case 'T': return 'T';
 904  
 905  	 	 	 	 //case 'L': return 'L';
 906  	 	 case 'INT':
 907  	 	 case 'SHORT':
 908  	 	 case 'INTEGER': return 'I';
 909  	 	 default: return ADODB_DEFAULT_METATYPE;
 910  	 	 }
 911  	 }
 912  
 913  }