Documentation TYPO3 par Ameos

adodb-ibase.inc.php

00001 <?php
00002 /*
00003 V4.93 10 Oct 2006  (c) 2000-2006 John Lim (jlim#natsoft.com.my). All rights reserved.  
00004   Released under both BSD license and Lesser GPL library license. 
00005   Whenever there is any discrepancy between the two licenses, 
00006   the BSD license will take precedence.
00007 
00008   Latest version is available at http://adodb.sourceforge.net
00009   
00010   Interbase data driver. Requires interbase client. Works on Windows and Unix.
00011 
00012   3 Jan 2002 -- suggestions by Hans-Peter Oeri <kampfcaspar75@oeri.ch>
00013         changed transaction handling and added experimental blob stuff
00014   
00015   Docs to interbase at the website
00016    http://www.synectics.co.za/php3/tutorial/IB_PHP3_API.html
00017    
00018   To use gen_id(), see
00019    http://www.volny.cz/iprenosil/interbase/ip_ib_code.htm#_code_creategen
00020    
00021    $rs = $conn->Execute('select gen_id(adodb,1) from rdb$database');
00022    $id = $rs->fields[0];
00023    $conn->Execute("insert into table (id, col1,...) values ($id, $val1,...)");
00024 */
00025 
00026 // security - hide paths
00027 if (!defined('ADODB_DIR')) die();
00028 
00029 class ADODB_ibase extends ADOConnection {
00030         var $databaseType = "ibase";
00031         var $dataProvider = "ibase";
00032         var $replaceQuote = "''"; // string to use to replace quotes
00033         var $ibase_datefmt = '%Y-%m-%d'; // For hours,mins,secs change to '%Y-%m-%d %H:%M:%S';
00034         var $fmtDate = "'Y-m-d'";
00035         var $ibase_timestampfmt = "%Y-%m-%d %H:%M:%S";
00036         var $ibase_timefmt = "%H:%M:%S";
00037         var $fmtTimeStamp = "'Y-m-d, H:i:s'";
00038         var $concat_operator='||';
00039         var $_transactionID;
00040         var $metaTablesSQL = "select rdb\$relation_name from rdb\$relations where rdb\$relation_name not like 'RDB\$%'";
00041         //OPN STUFF start
00042         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";
00043         //OPN STUFF end
00044         var $ibasetrans;
00045         var $hasGenID = true;
00046         var $_bindInputArray = true;
00047         var $buffers = 0;
00048         var $dialect = 1;
00049         var $sysDate = "cast('TODAY' as timestamp)";
00050         var $sysTimeStamp = "CURRENT_TIMESTAMP"; //"cast('NOW' as timestamp)";
00051         var $ansiOuter = true;
00052         var $hasAffectedRows = false;
00053         var $poorAffectedRows = true;
00054         var $blobEncodeType = 'C';
00055         var $role = false;
00056         
00057         function ADODB_ibase() 
00058         {
00059                  if (defined('IBASE_DEFAULT')) $this->ibasetrans = IBASE_DEFAULT;
00060         }
00061         
00062         
00063            // returns true or false
00064         function _connect($argHostname, $argUsername, $argPassword, $argDatabasename,$persist=false)
00065         {  
00066                 if (!function_exists('ibase_pconnect')) return null;
00067                 if ($argDatabasename) $argHostname .= ':'.$argDatabasename;
00068                 $fn = ($persist) ? 'ibase_pconnect':'ibase_connect';
00069                 if ($this->role)
00070                         $this->_connectionID = $fn($argHostname,$argUsername,$argPassword,
00071                                         $this->charSet,$this->buffers,$this->dialect,$this->role);
00072                 else    
00073                         $this->_connectionID = $fn($argHostname,$argUsername,$argPassword,
00074                                         $this->charSet,$this->buffers,$this->dialect);
00075                 
00076                 if ($this->dialect != 1) { // http://www.ibphoenix.com/ibp_60_del_id_ds.html
00077                         $this->replaceQuote = "''";
00078                 }
00079                 if ($this->_connectionID === false) {
00080                         $this->_handleerror();
00081                         return false;
00082                 }
00083                 
00084                 // PHP5 change.
00085                 if (function_exists('ibase_timefmt')) {
00086                         ibase_timefmt($this->ibase_datefmt,IBASE_DATE );
00087                         if ($this->dialect == 1) ibase_timefmt($this->ibase_datefmt,IBASE_TIMESTAMP );
00088                         else ibase_timefmt($this->ibase_timestampfmt,IBASE_TIMESTAMP );
00089                         ibase_timefmt($this->ibase_timefmt,IBASE_TIME );
00090                         
00091                 } else {
00092                         ini_set("ibase.timestampformat", $this->ibase_timestampfmt);
00093                         ini_set("ibase.dateformat", $this->ibase_datefmt);
00094                         ini_set("ibase.timeformat", $this->ibase_timefmt);
00095                 }
00096                 return true;
00097         }
00098            // returns true or false
00099         function _pconnect($argHostname, $argUsername, $argPassword, $argDatabasename)
00100         {
00101                 return $this->_connect($argHostname, $argUsername, $argPassword, $argDatabasename,true);
00102         }       
00103         
00104         
00105         function MetaPrimaryKeys($table,$owner_notused=false,$internalKey=false)
00106         {       
00107                 if ($internalKey) return array('RDB$DB_KEY');
00108                 
00109                 $table = strtoupper($table);
00110                 
00111                 $sql = 'SELECT S.RDB$FIELD_NAME AFIELDNAME
00112         FROM RDB$INDICES I JOIN RDB$INDEX_SEGMENTS S ON I.RDB$INDEX_NAME=S.RDB$INDEX_NAME  
00113         WHERE I.RDB$RELATION_NAME=\''.$table.'\' and I.RDB$INDEX_NAME like \'RDB$PRIMARY%\'
00114         ORDER BY I.RDB$INDEX_NAME,S.RDB$FIELD_POSITION';
00115 
00116                 $a = $this->GetCol($sql,false,true);
00117                 if ($a && sizeof($a)>0) return $a;
00118                 return false;     
00119         }
00120         
00121         function ServerInfo()
00122         {
00123                 $arr['dialect'] = $this->dialect;
00124                 switch($arr['dialect']) {
00125                 case '': 
00126                 case '1': $s = 'Interbase 5.5 or earlier'; break;
00127                 case '2': $s = 'Interbase 5.6'; break;
00128                 default:
00129                 case '3': $s = 'Interbase 6.0'; break;
00130                 }
00131                 $arr['version'] = ADOConnection::_findvers($s);
00132                 $arr['description'] = $s;
00133                 return $arr;
00134         }
00135 
00136         function BeginTrans()
00137         {        
00138                 if ($this->transOff) return true;
00139                 $this->transCnt += 1;
00140                 $this->autoCommit = false;
00141                 $this->_transactionID = $this->_connectionID;//ibase_trans($this->ibasetrans, $this->_connectionID);
00142                 return $this->_transactionID;
00143         }
00144         
00145         function CommitTrans($ok=true) 
00146         { 
00147                 if (!$ok) return $this->RollbackTrans();
00148                 if ($this->transOff) return true;
00149                 if ($this->transCnt) $this->transCnt -= 1;
00150                 $ret = false;
00151                 $this->autoCommit = true;
00152                 if ($this->_transactionID) {
00153                                         //print ' commit ';
00154                         $ret = ibase_commit($this->_transactionID);
00155                 }
00156                 $this->_transactionID = false;
00157                 return $ret;
00158         }
00159         
00160         // there are some compat problems with ADODB_COUNTRECS=false and $this->_logsql currently.
00161         // it appears that ibase extension cannot support multiple concurrent queryid's
00162         function &_Execute($sql,$inputarr=false) 
00163         {
00164         global $ADODB_COUNTRECS;
00165         
00166                 if ($this->_logsql) {
00167                         $savecrecs = $ADODB_COUNTRECS;
00168                         $ADODB_COUNTRECS = true; // force countrecs
00169                         $ret =& ADOConnection::_Execute($sql,$inputarr);
00170                         $ADODB_COUNTRECS = $savecrecs;
00171                 } else {
00172                         $ret =& ADOConnection::_Execute($sql,$inputarr);
00173                 }
00174                 return $ret;
00175         }
00176         
00177         function RollbackTrans()
00178         {
00179                 if ($this->transOff) return true;
00180                 if ($this->transCnt) $this->transCnt -= 1;
00181                 $ret = false;
00182                 $this->autoCommit = true;
00183                 if ($this->_transactionID) 
00184                                   $ret = ibase_rollback($this->_transactionID);
00185                 $this->_transactionID = false;   
00186                 
00187                 return $ret;
00188         }
00189         
00190         function &MetaIndexes ($table, $primary = FALSE, $owner=false)
00191         {
00192         // save old fetch mode
00193         global $ADODB_FETCH_MODE;
00194         $false = false;
00195         $save = $ADODB_FETCH_MODE;
00196         $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
00197         if ($this->fetchMode !== FALSE) {
00198                $savem = $this->SetFetchMode(FALSE);
00199         }
00200         $table = strtoupper($table);
00201         $sql = "SELECT * FROM RDB\$INDICES WHERE RDB\$RELATION_NAME = '".$table."'";
00202         if (!$primary) {
00203                 $sql .= " AND RDB\$INDEX_NAME NOT LIKE 'RDB\$%'";
00204         } else {
00205                 $sql .= " AND RDB\$INDEX_NAME NOT LIKE 'RDB\$FOREIGN%'";
00206         }
00207         // get index details
00208         $rs = $this->Execute($sql);
00209         if (!is_object($rs)) {
00210                 // restore fetchmode
00211                 if (isset($savem)) {
00212                     $this->SetFetchMode($savem);
00213                 }
00214                 $ADODB_FETCH_MODE = $save;
00215             return $false;
00216         }
00217         
00218         $indexes = array();
00219                 while ($row = $rs->FetchRow()) {
00220                         $index = $row[0];
00221              if (!isset($indexes[$index])) {
00222                         if (is_null($row[3])) {$row[3] = 0;}
00223                      $indexes[$index] = array(
00224                              'unique' => ($row[3] == 1),
00225                              'columns' => array()
00226                      );
00227              }
00228                         $sql = "SELECT * FROM RDB\$INDEX_SEGMENTS WHERE RDB\$INDEX_NAME = '".$index."' ORDER BY RDB\$FIELD_POSITION ASC";
00229                         $rs1 = $this->Execute($sql);
00230             while ($row1 = $rs1->FetchRow()) {
00231                 $indexes[$index]['columns'][$row1[2]] = $row1[1];
00232                 }
00233                 }
00234         // restore fetchmode
00235         if (isset($savem)) {
00236             $this->SetFetchMode($savem);
00237         }
00238         $ADODB_FETCH_MODE = $save;
00239         
00240         return $indexes;
00241         }
00242 
00243         
00244         // See http://community.borland.com/article/0,1410,25844,00.html
00245         function RowLock($tables,$where,$col)
00246         {
00247                 if ($this->autoCommit) $this->BeginTrans();
00248                 $this->Execute("UPDATE $table SET $col=$col WHERE $where "); // is this correct - jlim?
00249                 return 1;
00250         }
00251         
00252         
00253         function CreateSequence($seqname,$startID=1)
00254         {
00255                 $ok = $this->Execute(("INSERT INTO RDB\$GENERATORS (RDB\$GENERATOR_NAME) VALUES (UPPER('$seqname'))" ));
00256                 if (!$ok) return false;
00257                 return $this->Execute("SET GENERATOR $seqname TO ".($startID-1).';');
00258         }
00259         
00260         function DropSequence($seqname)
00261         {
00262                 $seqname = strtoupper($seqname);
00263                 $this->Execute("delete from RDB\$GENERATORS where RDB\$GENERATOR_NAME='$seqname'");
00264         }
00265         
00266         function GenID($seqname='adodbseq',$startID=1)
00267         {
00268                 $getnext = ("SELECT Gen_ID($seqname,1) FROM RDB\$DATABASE");
00269                 $rs = @$this->Execute($getnext);
00270                 if (!$rs) {
00271                         $this->Execute(("INSERT INTO RDB\$GENERATORS (RDB\$GENERATOR_NAME) VALUES (UPPER('$seqname'))" ));
00272                         $this->Execute("SET GENERATOR $seqname TO ".($startID-1).';');
00273                         $rs = $this->Execute($getnext);
00274                 }
00275                 if ($rs && !$rs->EOF) $this->genID = (integer) reset($rs->fields);
00276                 else $this->genID = 0; // false
00277                 
00278                 if ($rs) $rs->Close();
00279                 
00280                 return $this->genID;
00281         }
00282 
00283         function SelectDB($dbName) 
00284         {
00285                    return false;
00286         }
00287 
00288         function _handleerror()
00289         {
00290                 $this->_errorMsg = ibase_errmsg();
00291         }
00292 
00293         function ErrorNo() 
00294         {
00295                 if (preg_match('/error code = ([\-0-9]*)/i', $this->_errorMsg,$arr)) return (integer) $arr[1];
00296                 else return 0;
00297         }
00298 
00299         function ErrorMsg() 
00300         {
00301                         return $this->_errorMsg;
00302         }
00303 
00304         function Prepare($sql)
00305         {
00306                 $stmt = ibase_prepare($this->_connectionID,$sql);
00307                 if (!$stmt) return false;
00308                 return array($sql,$stmt);
00309         }
00310 
00311            // returns query ID if successful, otherwise false
00312            // there have been reports of problems with nested queries - the code is probably not re-entrant?
00313         function _query($sql,$iarr=false)
00314         { 
00315 
00316                 if (!$this->autoCommit && $this->_transactionID) {
00317                         $conn = $this->_transactionID;
00318                         $docommit = false;
00319                 } else {
00320                         $conn = $this->_connectionID;
00321                         $docommit = true;
00322                 }
00323                 if (is_array($sql)) {
00324                         $fn = 'ibase_execute';
00325                         $sql = $sql[1];
00326                         if (is_array($iarr)) {
00327                                 if  (ADODB_PHPVER >= 0x4050) { // actually 4.0.4
00328                                         if ( !isset($iarr[0]) ) $iarr[0] = ''; // PHP5 compat hack
00329                                         $fnarr =& array_merge( array($sql) , $iarr);
00330                                         $ret = call_user_func_array($fn,$fnarr);
00331                                 } else {
00332                                         switch(sizeof($iarr)) {
00333                                         case 1: $ret = $fn($sql,$iarr[0]); break;
00334                                         case 2: $ret = $fn($sql,$iarr[0],$iarr[1]); break;
00335                                         case 3: $ret = $fn($sql,$iarr[0],$iarr[1],$iarr[2]); break;
00336                                         case 4: $ret = $fn($sql,$iarr[0],$iarr[1],$iarr[2],$iarr[3]); break;
00337                                         case 5: $ret = $fn($sql,$iarr[0],$iarr[1],$iarr[2],$iarr[3],$iarr[4]); break;
00338                                         case 6: $ret = $fn($sql,$iarr[0],$iarr[1],$iarr[2],$iarr[3],$iarr[4],$iarr[5]); break;
00339                                         case 7: $ret = $fn($sql,$iarr[0],$iarr[1],$iarr[2],$iarr[3],$iarr[4],$iarr[5],$iarr[6]); break;
00340                                         default: ADOConnection::outp( "Too many parameters to ibase query $sql");
00341                                         case 8: $ret = $fn($sql,$iarr[0],$iarr[1],$iarr[2],$iarr[3],$iarr[4],$iarr[5],$iarr[6],$iarr[7]); break;
00342                                         }
00343                                 }
00344                         } else $ret = $fn($sql); 
00345                 } else {
00346                         $fn = 'ibase_query';
00347                 
00348                         if (is_array($iarr)) {  
00349                                 if (ADODB_PHPVER >= 0x4050) { // actually 4.0.4
00350                                         if (sizeof($iarr) == 0) $iarr[0] = ''; // PHP5 compat hack
00351                                         $fnarr =& array_merge( array($conn,$sql) , $iarr);
00352                                         $ret = call_user_func_array($fn,$fnarr);
00353                                 } else {
00354                                         switch(sizeof($iarr)) {
00355                                         case 1: $ret = $fn($conn,$sql,$iarr[0]); break;
00356                                         case 2: $ret = $fn($conn,$sql,$iarr[0],$iarr[1]); break;
00357                                         case 3: $ret = $fn($conn,$sql,$iarr[0],$iarr[1],$iarr[2]); break;
00358                                         case 4: $ret = $fn($conn,$sql,$iarr[0],$iarr[1],$iarr[2],$iarr[3]); break;
00359                                         case 5: $ret = $fn($conn,$sql,$iarr[0],$iarr[1],$iarr[2],$iarr[3],$iarr[4]); break;
00360                                         case 6: $ret = $fn($conn,$sql,$iarr[0],$iarr[1],$iarr[2],$iarr[3],$iarr[4],$iarr[5]); break;
00361                                         case 7: $ret = $fn($conn,$sql,$iarr[0],$iarr[1],$iarr[2],$iarr[3],$iarr[4],$iarr[5],$iarr[6]); break;
00362                                         default: ADOConnection::outp( "Too many parameters to ibase query $sql");
00363                                         case 8: $ret = $fn($conn,$sql,$iarr[0],$iarr[1],$iarr[2],$iarr[3],$iarr[4],$iarr[5],$iarr[6],$iarr[7]); break;
00364                                         }
00365                                 }
00366                         } else $ret = $fn($conn,$sql); 
00367                 }
00368                 if ($docommit && $ret === true) ibase_commit($this->_connectionID);
00369 
00370                 $this->_handleerror();
00371                 return $ret;
00372         }
00373 
00374          // returns true or false
00375          function _close()
00376          {         
00377                 if (!$this->autoCommit) @ibase_rollback($this->_connectionID);
00378                 return @ibase_close($this->_connectionID);
00379          }
00380         
00381         //OPN STUFF start
00382         function _ConvertFieldType(&$fld, $ftype, $flen, $fscale, $fsubtype, $fprecision, $dialect3)
00383         {
00384                 $fscale = abs($fscale);
00385                 $fld->max_length = $flen;
00386                 $fld->scale = null;
00387                 switch($ftype){
00388                         case 7: 
00389                         case 8:
00390                                 if ($dialect3) {
00391                                     switch($fsubtype){
00392                                         case 0: 
00393                                                 $fld->type = ($ftype == 7 ? 'smallint' : 'integer');
00394                                                 break;
00395                                         case 1: 
00396                                                 $fld->type = 'numeric';
00397                                                         $fld->max_length = $fprecision;
00398                                                         $fld->scale = $fscale;
00399                                                 break;
00400                                         case 2:
00401                                                 $fld->type = 'decimal';
00402                                                         $fld->max_length = $fprecision;
00403                                                         $fld->scale = $fscale;
00404                                                 break;
00405                                     } // switch
00406                                 } else {
00407                                         if ($fscale !=0) {
00408                                             $fld->type = 'decimal';
00409                                                 $fld->scale = $fscale;
00410                                                 $fld->max_length = ($ftype == 7 ? 4 : 9);
00411                                         } else {
00412                                                 $fld->type = ($ftype == 7 ? 'smallint' : 'integer');
00413                                         }
00414                                 }
00415                                 break;
00416                         case 16: 
00417                                 if ($dialect3) {
00418                                     switch($fsubtype){
00419                                         case 0: 
00420                                                 $fld->type = 'decimal';
00421                                                         $fld->max_length = 18;
00422                                                         $fld->scale = 0;
00423                                                 break;
00424                                         case 1: 
00425                                                 $fld->type = 'numeric';
00426                                                         $fld->max_length = $fprecision;
00427                                                         $fld->scale = $fscale;
00428                                                 break;
00429                                         case 2:
00430                                                 $fld->type = 'decimal';
00431                                                         $fld->max_length = $fprecision;
00432                                                         $fld->scale = $fscale;
00433                                                 break;
00434                                     } // switch
00435                                 }
00436                                 break;
00437                         case 10:
00438                                 $fld->type = 'float';
00439                                 break;
00440                         case 14:
00441                                 $fld->type = 'char';
00442                                 break;
00443                         case 27:
00444                                 if ($fscale !=0) {
00445                                     $fld->type = 'decimal';
00446                                         $fld->max_length = 15;
00447                                         $fld->scale = 5;
00448                                 } else {
00449                                         $fld->type = 'double';
00450                                 }
00451                                 break;
00452                         case 35:
00453                                 if ($dialect3) {
00454                                     $fld->type = 'timestamp';
00455                                 } else {
00456                                         $fld->type = 'date';
00457                                 }
00458                                 break;
00459                         case 12:
00460                                 $fld->type = 'date';
00461                                 break;
00462                         case 13:
00463                                 $fld->type = 'time';
00464                                 break;
00465                         case 37:
00466                                 $fld->type = 'varchar';
00467                                 break;
00468                         case 40:
00469                                 $fld->type = 'cstring';
00470                                 break;
00471                         case 261:
00472                                 $fld->type = 'blob';
00473                                 $fld->max_length = -1;
00474                                 break;
00475                 } // switch
00476         }
00477         //OPN STUFF end
00478                 // returns array of ADOFieldObjects for current table
00479         function &MetaColumns($table) 
00480         {
00481         global $ADODB_FETCH_MODE;
00482                 
00483                 $save = $ADODB_FETCH_MODE;
00484                 $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
00485         
00486                 $rs = $this->Execute(sprintf($this->metaColumnsSQL,strtoupper($table)));
00487         
00488                 $ADODB_FETCH_MODE = $save;
00489                 $false = false;
00490                 if ($rs === false) {
00491                         return $false;
00492                 }
00493                 
00494                 $retarr = array();
00495                 //OPN STUFF start
00496                 $dialect3 = ($this->dialect==3 ? true : false);
00497                 //OPN STUFF end
00498                 while (!$rs->EOF) { //print_r($rs->fields);
00499                         $fld = new ADOFieldObject();
00500                         $fld->name = trim($rs->fields[0]);
00501                         //OPN STUFF start
00502                         $this->_ConvertFieldType($fld, $rs->fields[7], $rs->fields[3], $rs->fields[4], $rs->fields[5], $rs->fields[6], $dialect3);
00503                         if (isset($rs->fields[1]) && $rs->fields[1]) {
00504                                 $fld->not_null = true;
00505                         }                               
00506                         if (isset($rs->fields[2])) {
00507                                 
00508                                 $fld->has_default = true;
00509                                 $d = substr($rs->fields[2],strlen('default '));
00510                                 switch ($fld->type)
00511                                 {
00512                                 case 'smallint':
00513                                 case 'integer': $fld->default_value = (int) $d; break;
00514                                 case 'char': 
00515                                 case 'blob':
00516                                 case 'text':
00517                                 case 'varchar': $fld->default_value = (string) substr($d,1,strlen($d)-2); break;
00518                                 case 'double':
00519                                 case 'float': $fld->default_value = (float) $d; break;
00520                                 default: $fld->default_value = $d; break;
00521                                 }
00522                 //      case 35:$tt = 'TIMESTAMP'; break;
00523                         }
00524                         if ((isset($rs->fields[5])) && ($fld->type == 'blob')) {
00525                                 $fld->sub_type = $rs->fields[5];
00526                         } else {
00527                                 $fld->sub_type = null;
00528                         }
00529                         //OPN STUFF end
00530                         if ($ADODB_FETCH_MODE == ADODB_FETCH_NUM) $retarr[] = $fld;     
00531                         else $retarr[strtoupper($fld->name)] = $fld;
00532                         
00533                         $rs->MoveNext();
00534                 }
00535                 $rs->Close();
00536                 if ( empty($retarr)) return $false;
00537                 else return $retarr;    
00538         }
00539         
00540         function BlobEncode( $blob ) 
00541         {
00542                 $blobid = ibase_blob_create( $this->_connectionID);
00543                 ibase_blob_add( $blobid, $blob );
00544                 return ibase_blob_close( $blobid );
00545         }
00546         
00547         // since we auto-decode all blob's since 2.42, 
00548         // BlobDecode should not do any transforms
00549         function BlobDecode($blob)
00550         {
00551                 return $blob; 
00552         }
00553         
00554         
00555         
00556         
00557         // old blobdecode function
00558         // still used to auto-decode all blob's
00559         function _BlobDecode_old( $blob ) 
00560         {
00561                 $blobid = ibase_blob_open($this->_connectionID, $blob );
00562                 $realblob = ibase_blob_get( $blobid,$this->maxblobsize); // 2nd param is max size of blob -- Kevin Boillet <kevinboillet@yahoo.fr>
00563                 while($string = ibase_blob_get($blobid, 8192)){ 
00564                         $realblob .= $string; 
00565                 }
00566                 ibase_blob_close( $blobid );
00567 
00568                 return( $realblob );
00569         } 
00570         
00571         function _BlobDecode( $blob ) 
00572     {
00573         if  (ADODB_PHPVER >= 0x5000) {
00574             $blob_data = ibase_blob_info($this->_connectionID, $blob );
00575             $blobid = ibase_blob_open($this->_connectionID, $blob );
00576         } else {
00577 
00578             $blob_data = ibase_blob_info( $blob );
00579             $blobid = ibase_blob_open( $blob );
00580         }
00581 
00582         if( $blob_data[0] > $this->maxblobsize ) {
00583 
00584             $realblob = ibase_blob_get($blobid, $this->maxblobsize);
00585 
00586             while($string = ibase_blob_get($blobid, 8192)){
00587                 $realblob .= $string; 
00588             }
00589         } else {
00590             $realblob = ibase_blob_get($blobid, $blob_data[0]);
00591         }
00592 
00593         ibase_blob_close( $blobid );
00594         return( $realblob );
00595         }
00596         
00597         function UpdateBlobFile($table,$column,$path,$where,$blobtype='BLOB') 
00598         { 
00599                 $fd = fopen($path,'rb'); 
00600                 if ($fd === false) return false; 
00601                 $blob_id = ibase_blob_create($this->_connectionID); 
00602                 
00603                 /* fill with data */ 
00604                 
00605                 while ($val = fread($fd,32768)){ 
00606                         ibase_blob_add($blob_id, $val); 
00607                 } 
00608                 
00609                 /* close and get $blob_id_str for inserting into table */ 
00610                 $blob_id_str = ibase_blob_close($blob_id); 
00611                 
00612                 fclose($fd); 
00613                 return $this->Execute("UPDATE $table SET $column=(?) WHERE $where",array($blob_id_str)) != false; 
00614         } 
00615         
00616         /*
00617                 Insert a null into the blob field of the table first.
00618                 Then use UpdateBlob to store the blob.
00619                 
00620                 Usage:
00621                  
00622                 $conn->Execute('INSERT INTO blobtable (id, blobcol) VALUES (1, null)');
00623                 $conn->UpdateBlob('blobtable','blobcol',$blob,'id=1');
00624         */
00625         function UpdateBlob($table,$column,$val,$where,$blobtype='BLOB') 
00626         { 
00627         $blob_id = ibase_blob_create($this->_connectionID); 
00628         
00629         // ibase_blob_add($blob_id, $val); 
00630         
00631         // replacement that solves the problem by which only the first modulus 64K / 
00632         // of $val are stored at the blob field //////////////////////////////////// 
00633         // Thx Abel Berenstein  aberenstein#afip.gov.ar
00634         $len = strlen($val); 
00635         $chunk_size = 32768; 
00636         $tail_size = $len % $chunk_size; 
00637         $n_chunks = ($len - $tail_size) / $chunk_size; 
00638         
00639         for ($n = 0; $n < $n_chunks; $n++) { 
00640                 $start = $n * $chunk_size; 
00641                 $data = substr($val, $start, $chunk_size); 
00642                 ibase_blob_add($blob_id, $data); 
00643         } 
00644         
00645         if ($tail_size) {
00646                 $start = $n_chunks * $chunk_size; 
00647                 $data = substr($val, $start, $tail_size); 
00648                 ibase_blob_add($blob_id, $data); 
00649         }
00650         // end replacement ///////////////////////////////////////////////////////// 
00651         
00652         $blob_id_str = ibase_blob_close($blob_id); 
00653         
00654         return $this->Execute("UPDATE $table SET $column=(?) WHERE $where",array($blob_id_str)) != false; 
00655         
00656         } 
00657         
00658         
00659         function OldUpdateBlob($table,$column,$val,$where,$blobtype='BLOB')
00660         {
00661                 $blob_id = ibase_blob_create($this->_connectionID);
00662                 ibase_blob_add($blob_id, $val);
00663                 $blob_id_str = ibase_blob_close($blob_id);
00664                 return $this->Execute("UPDATE $table SET $column=(?) WHERE $where",array($blob_id_str)) != false;
00665         }
00666         
00667         // Format date column in sql string given an input format that understands Y M D
00668         // Only since Interbase 6.0 - uses EXTRACT
00669         // problem - does not zero-fill the day and month yet
00670         function SQLDate($fmt, $col=false)
00671         {       
00672                 if (!$col) $col = $this->sysDate;
00673                 $s = '';
00674                 
00675                 $len = strlen($fmt);
00676                 for ($i=0; $i < $len; $i++) {
00677                         if ($s) $s .= '||';
00678                         $ch = $fmt[$i];
00679                         switch($ch) {
00680                         case 'Y':
00681                         case 'y':
00682                                 $s .= "extract(year from $col)";
00683                                 break;
00684                         case 'M':
00685                         case 'm':
00686                                 $s .= "extract(month from $col)";
00687                                 break;
00688                         case 'Q':
00689                         case 'q':
00690                                 $s .= "cast(((extract(month from $col)+2) / 3) as integer)";
00691                                 break;
00692                         case 'D':
00693                         case 'd':
00694                                 $s .= "(extract(day from $col))";
00695                                 break;
00696                         case 'H':
00697                         case 'h':
00698                           $s .= "(extract(hour from $col))";
00699                           break;                        
00700                         case 'I':
00701                         case 'i':
00702                           $s .= "(extract(minute from $col))";
00703                           break;                
00704                         case 'S':
00705                         case 's':
00706                           $s .= "CAST((extract(second from $col)) AS INTEGER)";
00707                           break;        
00708 
00709                         default:
00710                                 if ($ch == '\\') {
00711                                         $i++;
00712                                         $ch = substr($fmt,$i,1);
00713                                 }
00714                                 $s .= $this->qstr($ch);
00715                                 break;
00716                         }
00717                 }
00718                 return $s;
00719         }
00720 }
00721 
00722 /*--------------------------------------------------------------------------------------
00723                  Class Name: Recordset
00724 --------------------------------------------------------------------------------------*/
00725 
00726 class ADORecordset_ibase extends ADORecordSet
00727 {
00728 
00729         var $databaseType = "ibase";
00730         var $bind=false;
00731         var $_cacheType;
00732         
00733         function ADORecordset_ibase($id,$mode=false)
00734         {
00735         global $ADODB_FETCH_MODE;
00736         
00737                         $this->fetchMode = ($mode === false) ? $ADODB_FETCH_MODE : $mode;
00738                         $this->ADORecordSet($id);
00739         }
00740 
00741         /*              Returns: an object containing field information.
00742                         Get column information in the Recordset object. fetchField() can be used in order to obtain information about
00743                         fields in a certain query result. If the field offset isn't specified, the next field that wasn't yet retrieved by
00744                         fetchField() is retrieved.              */
00745 
00746         function &FetchField($fieldOffset = -1)
00747         {
00748                          $fld = new ADOFieldObject;
00749                          $ibf = ibase_field_info($this->_queryID,$fieldOffset);
00750                          switch (ADODB_ASSOC_CASE) {
00751                          case 2: // the default
00752                                 $fld->name = ($ibf['alias']);
00753                                  if (empty($fld->name)) $fld->name = ($ibf['name']);
00754                                  break;
00755                          case 0: 
00756                                  $fld->name = strtoupper($ibf['alias']);
00757                                  if (empty($fld->name)) $fld->name = strtoupper($ibf['name']);
00758                                  break;
00759                          case 1: 
00760                                 $fld->name = strtolower($ibf['alias']);
00761                                  if (empty($fld->name)) $fld->name = strtolower($ibf['name']);
00762                                  break;
00763                          }
00764                          
00765                          $fld->type = $ibf['type'];
00766                          $fld->max_length = $ibf['length'];
00767                          
00768                          /*       This needs to be populated from the metadata */ 
00769                          $fld->not_null = false;
00770                          $fld->has_default = false;
00771                          $fld->default_value = 'null';
00772                          return $fld;
00773         }
00774 
00775         function _initrs()
00776         {
00777                 $this->_numOfRows = -1;
00778                 $this->_numOfFields = @ibase_num_fields($this->_queryID);
00779 
00780                 // cache types for blob decode check
00781                 for ($i=0, $max = $this->_numOfFields; $i < $max; $i++) { 
00782                         $f1 = $this->FetchField($i); 
00783                         $this->_cacheType[] = $f1->type;
00784                 }                               
00785         }
00786 
00787         function _seek($row)
00788         {
00789                 return false;
00790         }
00791         
00792         function _fetch() 
00793         {
00794                 $f = @ibase_fetch_row($this->_queryID); 
00795                 if ($f === false) {
00796                         $this->fields = false;
00797                         return false;
00798                 }
00799                 // OPN stuff start - optimized
00800                 // fix missing nulls and decode blobs automatically
00801         
00802                 global $ADODB_ANSI_PADDING_OFF;
00803                 //$ADODB_ANSI_PADDING_OFF=1;
00804                 $rtrim = !empty($ADODB_ANSI_PADDING_OFF);
00805                 
00806                 for ($i=0, $max = $this->_numOfFields; $i < $max; $i++) { 
00807                         if ($this->_cacheType[$i]=="BLOB") {
00808                                 if (isset($f[$i])) { 
00809                                         $f[$i] = $this->connection->_BlobDecode($f[$i]); 
00810                                 } else { 
00811                                         $f[$i] = null; 
00812                                 } 
00813                         } else { 
00814                                 if (!isset($f[$i])) { 
00815                                         $f[$i] = null; 
00816                                 } else if ($rtrim && is_string($f[$i])) {
00817                                         $f[$i] = rtrim($f[$i]);
00818                                 }
00819                         } 
00820                 } 
00821                 // OPN stuff end 
00822                 
00823                 $this->fields = $f;
00824                 if ($this->fetchMode == ADODB_FETCH_ASSOC) {
00825                         $this->fields = &$this->GetRowAssoc(ADODB_ASSOC_CASE);
00826                 } else if ($this->fetchMode == ADODB_FETCH_BOTH) {
00827                         $this->fields =& array_merge($this->fields,$this->GetRowAssoc(ADODB_ASSOC_CASE));
00828                 }
00829                 return true;
00830         }
00831 
00832         /* Use associative array to get fields array */
00833         function Fields($colname)
00834         {
00835                 if ($this->fetchMode & ADODB_FETCH_ASSOC) return $this->fields[$colname];
00836                 if (!$this->bind) {
00837                         $this->bind = array();
00838                         for ($i=0; $i < $this->_numOfFields; $i++) {
00839                                 $o = $this->FetchField($i);
00840                                 $this->bind[strtoupper($o->name)] = $i;
00841                         }
00842                 }
00843                 
00844                 return $this->fields[$this->bind[strtoupper($colname)]];
00845                 
00846         }
00847         
00848 
00849         function _close() 
00850         {
00851                         return @ibase_free_result($this->_queryID);
00852         }
00853 
00854         function MetaType($t,$len=-1,$fieldobj=false)
00855         {
00856                 if (is_object($t)) {
00857                         $fieldobj = $t;
00858                         $t = $fieldobj->type;
00859                         $len = $fieldobj->max_length;
00860                 }
00861                 switch (strtoupper($t)) {
00862                 case 'CHAR':
00863                         return 'C';
00864                         
00865                 case 'TEXT':
00866                 case 'VARCHAR':
00867                 case 'VARYING':
00868                 if ($len <= $this->blobSize) return 'C';
00869                         return 'X';
00870                 case 'BLOB':
00871                         return 'B';
00872                            
00873                 case 'TIMESTAMP':
00874                 case 'DATE': return 'D';
00875                 case 'TIME': return 'T';
00876                                 //case 'T': return 'T';
00877 
00878                                 //case 'L': return 'L';
00879                 case 'INT': 
00880                 case 'SHORT':
00881                 case 'INTEGER': return 'I';
00882                 default: return 'N';
00883                 }
00884         }
00885 
00886 }
00887 ?>


Généré par L'expert TYPO3 avec  doxygen 1.4.6