Documentation TYPO3 par Ameos

adodb-ibase.inc.php

00001 <?php
00002 /*
00003 V4.80 8 Mar 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 = "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( $blob ) 
00560         {
00561                 $blobid = ibase_blob_open( $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 UpdateBlobFile($table,$column,$path,$where,$blobtype='BLOB') 
00572         { 
00573                 $fd = fopen($path,'rb'); 
00574                 if ($fd === false) return false; 
00575                 $blob_id = ibase_blob_create($this->_connectionID); 
00576                 
00577                 /* fill with data */ 
00578                 
00579                 while ($val = fread($fd,32768)){ 
00580                         ibase_blob_add($blob_id, $val); 
00581                 } 
00582                 
00583                 /* close and get $blob_id_str for inserting into table */ 
00584                 $blob_id_str = ibase_blob_close($blob_id); 
00585                 
00586                 fclose($fd); 
00587                 return $this->Execute("UPDATE $table SET $column=(?) WHERE $where",array($blob_id_str)) != false; 
00588         } 
00589         
00590         /*
00591                 Insert a null into the blob field of the table first.
00592                 Then use UpdateBlob to store the blob.
00593                 
00594                 Usage:
00595                  
00596                 $conn->Execute('INSERT INTO blobtable (id, blobcol) VALUES (1, null)');
00597                 $conn->UpdateBlob('blobtable','blobcol',$blob,'id=1');
00598         */
00599         function UpdateBlob($table,$column,$val,$where,$blobtype='BLOB') 
00600         { 
00601         $blob_id = ibase_blob_create($this->_connectionID); 
00602         
00603         // ibase_blob_add($blob_id, $val); 
00604         
00605         // replacement that solves the problem by which only the first modulus 64K / 
00606         // of $val are stored at the blob field //////////////////////////////////// 
00607         // Thx Abel Berenstein  aberenstein#afip.gov.ar
00608         $len = strlen($val); 
00609         $chunk_size = 32768; 
00610         $tail_size = $len % $chunk_size; 
00611         $n_chunks = ($len - $tail_size) / $chunk_size; 
00612         
00613         for ($n = 0; $n < $n_chunks; $n++) { 
00614                 $start = $n * $chunk_size; 
00615                 $data = substr($val, $start, $chunk_size); 
00616                 ibase_blob_add($blob_id, $data); 
00617         } 
00618         
00619         if ($tail_size) {
00620                 $start = $n_chunks * $chunk_size; 
00621                 $data = substr($val, $start, $tail_size); 
00622                 ibase_blob_add($blob_id, $data); 
00623         }
00624         // end replacement ///////////////////////////////////////////////////////// 
00625         
00626         $blob_id_str = ibase_blob_close($blob_id); 
00627         
00628         return $this->Execute("UPDATE $table SET $column=(?) WHERE $where",array($blob_id_str)) != false; 
00629         
00630         } 
00631         
00632         
00633         function OldUpdateBlob($table,$column,$val,$where,$blobtype='BLOB')
00634         {
00635                 $blob_id = ibase_blob_create($this->_connectionID);
00636                 ibase_blob_add($blob_id, $val);
00637                 $blob_id_str = ibase_blob_close($blob_id);
00638                 return $this->Execute("UPDATE $table SET $column=(?) WHERE $where",array($blob_id_str)) != false;
00639         }
00640         
00641         // Format date column in sql string given an input format that understands Y M D
00642         // Only since Interbase 6.0 - uses EXTRACT
00643         // problem - does not zero-fill the day and month yet
00644         function SQLDate($fmt, $col=false)
00645         {       
00646                 if (!$col) $col = $this->sysDate;
00647                 $s = '';
00648                 
00649                 $len = strlen($fmt);
00650                 for ($i=0; $i < $len; $i++) {
00651                         if ($s) $s .= '||';
00652                         $ch = $fmt[$i];
00653                         switch($ch) {
00654                         case 'Y':
00655                         case 'y':
00656                                 $s .= "extract(year from $col)";
00657                                 break;
00658                         case 'M':
00659                         case 'm':
00660                                 $s .= "extract(month from $col)";
00661                                 break;
00662                         case 'Q':
00663                         case 'q':
00664                                 $s .= "cast(((extract(month from $col)+2) / 3) as integer)";
00665                                 break;
00666                         case 'D':
00667                         case 'd':
00668                                 $s .= "(extract(day from $col))";
00669                                 break;
00670                         case 'H':
00671                         case 'h':
00672                           $s .= "(extract(hour from $col))";
00673                           break;                        
00674                         case 'I':
00675                         case 'i':
00676                           $s .= "(extract(minute from $col))";
00677                           break;                
00678                         case 'S':
00679                         case 's':
00680                           $s .= "CAST((extract(second from $col)) AS INTEGER)";
00681                           break;        
00682 
00683                         default:
00684                                 if ($ch == '\\') {
00685                                         $i++;
00686                                         $ch = substr($fmt,$i,1);
00687                                 }
00688                                 $s .= $this->qstr($ch);
00689                                 break;
00690                         }
00691                 }
00692                 return $s;
00693         }
00694 }
00695 
00696 /*--------------------------------------------------------------------------------------
00697                  Class Name: Recordset
00698 --------------------------------------------------------------------------------------*/
00699 
00700 class ADORecordset_ibase extends ADORecordSet
00701 {
00702 
00703         var $databaseType = "ibase";
00704         var $bind=false;
00705         var $_cacheType;
00706         
00707         function ADORecordset_ibase($id,$mode=false)
00708         {
00709         global $ADODB_FETCH_MODE;
00710         
00711                         $this->fetchMode = ($mode === false) ? $ADODB_FETCH_MODE : $mode;
00712                         $this->ADORecordSet($id);
00713         }
00714 
00715         /*              Returns: an object containing field information.
00716                         Get column information in the Recordset object. fetchField() can be used in order to obtain information about
00717                         fields in a certain query result. If the field offset isn't specified, the next field that wasn't yet retrieved by
00718                         fetchField() is retrieved.              */
00719 
00720         function &FetchField($fieldOffset = -1)
00721         {
00722                          $fld = new ADOFieldObject;
00723                          $ibf = ibase_field_info($this->_queryID,$fieldOffset);
00724                          switch (ADODB_ASSOC_CASE) {
00725                          case 2: // the default
00726                                 $fld->name = ($ibf['alias']);
00727                                  if (empty($fld->name)) $fld->name = ($ibf['name']);
00728                                  break;
00729                          case 0: 
00730                                  $fld->name = strtoupper($ibf['alias']);
00731                                  if (empty($fld->name)) $fld->name = strtoupper($ibf['name']);
00732                                  break;
00733                          case 1: 
00734                                 $fld->name = strtolower($ibf['alias']);
00735                                  if (empty($fld->name)) $fld->name = strtolower($ibf['name']);
00736                                  break;
00737                          }
00738                          
00739                          $fld->type = $ibf['type'];
00740                          $fld->max_length = $ibf['length'];
00741                          
00742                          /*       This needs to be populated from the metadata */ 
00743                          $fld->not_null = false;
00744                          $fld->has_default = false;
00745                          $fld->default_value = 'null';
00746                          return $fld;
00747         }
00748 
00749         function _initrs()
00750         {
00751                 $this->_numOfRows = -1;
00752                 $this->_numOfFields = @ibase_num_fields($this->_queryID);
00753 
00754                 // cache types for blob decode check
00755                 for ($i=0, $max = $this->_numOfFields; $i < $max; $i++) { 
00756                         $f1 = $this->FetchField($i); 
00757                         $this->_cacheType[] = $f1->type;
00758                 }                               
00759         }
00760 
00761         function _seek($row)
00762         {
00763                 return false;
00764         }
00765         
00766         function _fetch() 
00767         {
00768                 $f = @ibase_fetch_row($this->_queryID); 
00769                 if ($f === false) {
00770                         $this->fields = false;
00771                         return false;
00772                 }
00773                 // OPN stuff start - optimized
00774                 // fix missing nulls and decode blobs automatically
00775         
00776                 global $ADODB_ANSI_PADDING_OFF;
00777                 //$ADODB_ANSI_PADDING_OFF=1;
00778                 $rtrim = !empty($ADODB_ANSI_PADDING_OFF);
00779                 
00780                 for ($i=0, $max = $this->_numOfFields; $i < $max; $i++) { 
00781                         if ($this->_cacheType[$i]=="BLOB") {
00782                                 if (isset($f[$i])) { 
00783                                         $f[$i] = $this->connection->_BlobDecode($f[$i]); 
00784                                 } else { 
00785                                         $f[$i] = null; 
00786                                 } 
00787                         } else { 
00788                                 if (!isset($f[$i])) { 
00789                                         $f[$i] = null; 
00790                                 } else if ($rtrim && is_string($f[$i])) {
00791                                         $f[$i] = rtrim($f[$i]);
00792                                 }
00793                         } 
00794                 } 
00795                 // OPN stuff end 
00796                 
00797                 $this->fields = $f;
00798                 if ($this->fetchMode == ADODB_FETCH_ASSOC) {
00799                         $this->fields = &$this->GetRowAssoc(ADODB_ASSOC_CASE);
00800                 } else if ($this->fetchMode == ADODB_FETCH_BOTH) {
00801                         $this->fields =& array_merge($this->fields,$this->GetRowAssoc(ADODB_ASSOC_CASE));
00802                 }
00803                 return true;
00804         }
00805 
00806         /* Use associative array to get fields array */
00807         function Fields($colname)
00808         {
00809                 if ($this->fetchMode & ADODB_FETCH_ASSOC) return $this->fields[$colname];
00810                 if (!$this->bind) {
00811                         $this->bind = array();
00812                         for ($i=0; $i < $this->_numOfFields; $i++) {
00813                                 $o = $this->FetchField($i);
00814                                 $this->bind[strtoupper($o->name)] = $i;
00815                         }
00816                 }
00817                 
00818                 return $this->fields[$this->bind[strtoupper($colname)]];
00819                 
00820         }
00821         
00822 
00823         function _close() 
00824         {
00825                         return @ibase_free_result($this->_queryID);
00826         }
00827 
00828         function MetaType($t,$len=-1,$fieldobj=false)
00829         {
00830                 if (is_object($t)) {
00831                         $fieldobj = $t;
00832                         $t = $fieldobj->type;
00833                         $len = $fieldobj->max_length;
00834                 }
00835                 switch (strtoupper($t)) {
00836                 case 'CHAR':
00837                         return 'C';
00838                         
00839                 case 'TEXT':
00840                 case 'VARCHAR':
00841                 case 'VARYING':
00842                 if ($len <= $this->blobSize) return 'C';
00843                         return 'X';
00844                 case 'BLOB':
00845                         return 'B';
00846                            
00847                 case 'TIMESTAMP':
00848                 case 'DATE': return 'D';
00849                 case 'TIME': return 'T';
00850                                 //case 'T': return 'T';
00851 
00852                                 //case 'L': return 'L';
00853                 case 'INT': 
00854                 case 'SHORT':
00855                 case 'INTEGER': return 'I';
00856                 default: return 'N';
00857                 }
00858         }
00859 
00860 }
00861 ?>


Généré par Les spécialistes TYPO3 avec  doxygen 1.4.6