Documentation TYPO3 par Ameos

adodb-sqlite.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   SQLite info: http://www.hwaci.com/sw/sqlite/
00011     
00012   Install Instructions:
00013   ====================
00014   1. Place this in adodb/drivers
00015   2. Rename the file, remove the .txt prefix.
00016 */
00017 
00018 // security - hide paths
00019 if (!defined('ADODB_DIR')) die();
00020 
00021 class ADODB_sqlite extends ADOConnection {
00022         var $databaseType = "sqlite";
00023         var $replaceQuote = "''"; // string to use to replace quotes
00024         var $concat_operator='||';
00025         var $_errorNo = 0;
00026         var $hasLimit = true;   
00027         var $hasInsertID = true;                
00028         var $hasAffectedRows = true;    
00029         var $metaTablesSQL = "SELECT name FROM sqlite_master WHERE type='table' ORDER BY name";
00030         var $sysDate = "adodb_date('Y-m-d')";
00031         var $sysTimeStamp = "adodb_date('Y-m-d H:i:s')";
00032         var $fmtTimeStamp = "'Y-m-d H:i:s'";
00033         
00034         function ADODB_sqlite() 
00035         {
00036         }
00037         
00038 /*
00039   function __get($name) 
00040   {
00041         switch($name) {
00042         case 'sysDate': return "'".date($this->fmtDate)."'";
00043         case 'sysTimeStamp' : return "'".date($this->sysTimeStamp)."'";
00044         }
00045   }*/
00046         
00047         function ServerInfo()
00048         {
00049                 $arr['version'] = sqlite_libversion();
00050                 $arr['description'] = 'SQLite ';
00051                 $arr['encoding'] = sqlite_libencoding();
00052                 return $arr;
00053         }
00054         
00055         function BeginTrans()
00056         {         
00057                  if ($this->transOff) return true; 
00058                  $ret = $this->Execute("BEGIN TRANSACTION");
00059                  $this->transCnt += 1;
00060                  return true;
00061         }
00062         
00063         function CommitTrans($ok=true) 
00064         { 
00065                 if ($this->transOff) return true; 
00066                 if (!$ok) return $this->RollbackTrans();
00067                 $ret = $this->Execute("COMMIT");
00068                 if ($this->transCnt>0)$this->transCnt -= 1;
00069                 return !empty($ret);
00070         }
00071         
00072         function RollbackTrans()
00073         {
00074                 if ($this->transOff) return true; 
00075                 $ret = $this->Execute("ROLLBACK");
00076                 if ($this->transCnt>0)$this->transCnt -= 1;
00077                 return !empty($ret);
00078         }
00079         
00080         // mark newnham
00081         function &MetaColumns($tab)
00082         {
00083           global $ADODB_FETCH_MODE;
00084           $false = false;
00085           $save = $ADODB_FETCH_MODE;
00086           $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
00087           if ($this->fetchMode !== false) $savem = $this->SetFetchMode(false);
00088           $rs = $this->Execute("PRAGMA table_info('$tab')");
00089           if (isset($savem)) $this->SetFetchMode($savem);
00090           if (!$rs) {
00091             $ADODB_FETCH_MODE = $save; 
00092             return $false;
00093           }
00094           $arr = array();
00095           while ($r = $rs->FetchRow()) {
00096             $type = explode('(',$r['type']);
00097             $size = '';
00098             if (sizeof($type)==2)
00099             $size = trim($type[1],')');
00100             $fn = strtoupper($r['name']);
00101             $fld = new ADOFieldObject;
00102             $fld->name = $r['name'];
00103             $fld->type = $type[0];
00104             $fld->max_length = $size;
00105             $fld->not_null = $r['notnull'];
00106             $fld->default_value = $r['dflt_value'];
00107             $fld->scale = 0;
00108             if ($save == ADODB_FETCH_NUM) $arr[] = $fld;        
00109             else $arr[strtoupper($fld->name)] = $fld;
00110           }
00111           $rs->Close();
00112           $ADODB_FETCH_MODE = $save;
00113           return $arr;
00114         }
00115         
00116         function _init($parentDriver)
00117         {
00118         
00119                 $parentDriver->hasTransactions = false;
00120                 $parentDriver->hasInsertID = true;
00121         }
00122 
00123         function _insertid()
00124         {
00125                 return sqlite_last_insert_rowid($this->_connectionID);
00126         }
00127         
00128         function _affectedrows()
00129         {
00130         return sqlite_changes($this->_connectionID);
00131     }
00132         
00133         function ErrorMsg() 
00134         {
00135                 if ($this->_logsql) return $this->_errorMsg;
00136                 return ($this->_errorNo) ? sqlite_error_string($this->_errorNo) : '';
00137         }
00138  
00139         function ErrorNo() 
00140         {
00141                 return $this->_errorNo;
00142         }
00143         
00144         function SQLDate($fmt, $col=false)
00145         {
00146                 $fmt = $this->qstr($fmt);
00147                 return ($col) ? "adodb_date2($fmt,$col)" : "adodb_date($fmt)";
00148         }
00149         
00150         
00151         function _createFunctions()
00152         {
00153                 @sqlite_create_function($this->_connectionID, 'adodb_date', 'adodb_date', 1);
00154                 @sqlite_create_function($this->_connectionID, 'adodb_date2', 'adodb_date2', 2);
00155         }
00156         
00157 
00158         // returns true or false
00159         function _connect($argHostname, $argUsername, $argPassword, $argDatabasename)
00160         {
00161                 if (!function_exists('sqlite_open')) return null;
00162                 if (empty($argHostname) && $argDatabasename) $argHostname = $argDatabasename;
00163                 
00164                 $this->_connectionID = sqlite_open($argHostname);
00165                 if ($this->_connectionID === false) return false;
00166                 $this->_createFunctions();
00167                 return true;
00168         }
00169         
00170         // returns true or false
00171         function _pconnect($argHostname, $argUsername, $argPassword, $argDatabasename)
00172         {
00173                 if (!function_exists('sqlite_open')) return null;
00174                 if (empty($argHostname) && $argDatabasename) $argHostname = $argDatabasename;
00175                 
00176                 $this->_connectionID = sqlite_popen($argHostname);
00177                 if ($this->_connectionID === false) return false;
00178                 $this->_createFunctions();
00179                 return true;
00180         }
00181 
00182         // returns query ID if successful, otherwise false
00183         function _query($sql,$inputarr=false)
00184         {
00185                 $rez = sqlite_query($sql,$this->_connectionID);
00186                 if (!$rez) {
00187                         $this->_errorNo = sqlite_last_error($this->_connectionID);
00188                 }
00189                 
00190                 return $rez;
00191         }
00192         
00193         function &SelectLimit($sql,$nrows=-1,$offset=-1,$inputarr=false,$secs2cache=0) 
00194         {
00195                 $offsetStr = ($offset >= 0) ? " OFFSET $offset" : '';
00196                 $limitStr  = ($nrows >= 0)  ? " LIMIT $nrows" : ($offset >= 0 ? ' LIMIT 999999999' : '');
00197                 if ($secs2cache)
00198                         $rs =& $this->CacheExecute($secs2cache,$sql."$limitStr$offsetStr",$inputarr);
00199                 else
00200                         $rs =& $this->Execute($sql."$limitStr$offsetStr",$inputarr);
00201                         
00202                 return $rs;
00203         }
00204         
00205         /*
00206                 This algorithm is not very efficient, but works even if table locking
00207                 is not available.
00208                 
00209                 Will return false if unable to generate an ID after $MAXLOOPS attempts.
00210         */
00211         var $_genSeqSQL = "create table %s (id integer)";
00212         
00213         function GenID($seq='adodbseq',$start=1)
00214         {       
00215                 // if you have to modify the parameter below, your database is overloaded,
00216                 // or you need to implement generation of id's yourself!
00217                 $MAXLOOPS = 100;
00218                 //$this->debug=1;
00219                 while (--$MAXLOOPS>=0) {
00220                         @($num = $this->GetOne("select id from $seq"));
00221                         if ($num === false) {
00222                                 $this->Execute(sprintf($this->_genSeqSQL ,$seq));       
00223                                 $start -= 1;
00224                                 $num = '0';
00225                                 $ok = $this->Execute("insert into $seq values($start)");
00226                                 if (!$ok) return false;
00227                         } 
00228                         $this->Execute("update $seq set id=id+1 where id=$num");
00229                         
00230                         if ($this->affected_rows() > 0) {
00231                                 $num += 1;
00232                                 $this->genID = $num;
00233                                 return $num;
00234                         }
00235                 }
00236                 if ($fn = $this->raiseErrorFn) {
00237                         $fn($this->databaseType,'GENID',-32000,"Unable to generate unique id after $MAXLOOPS attempts",$seq,$num);
00238                 }
00239                 return false;
00240         }
00241 
00242         function CreateSequence($seqname='adodbseq',$start=1)
00243         {
00244                 if (empty($this->_genSeqSQL)) return false;
00245                 $ok = $this->Execute(sprintf($this->_genSeqSQL,$seqname));
00246                 if (!$ok) return false;
00247                 $start -= 1;
00248                 return $this->Execute("insert into $seqname values($start)");
00249         }
00250         
00251         var $_dropSeqSQL = 'drop table %s';
00252         function DropSequence($seqname)
00253         {
00254                 if (empty($this->_dropSeqSQL)) return false;
00255                 return $this->Execute(sprintf($this->_dropSeqSQL,$seqname));
00256         }
00257         
00258         // returns true or false
00259         function _close()
00260         {
00261                 return @sqlite_close($this->_connectionID);
00262         }
00263 
00264         function &MetaIndexes($table, $primary = FALSE, $owner=false)
00265         {
00266                 $false = false;
00267                 // save old fetch mode
00268         global $ADODB_FETCH_MODE;
00269         $save = $ADODB_FETCH_MODE;
00270         $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
00271         if ($this->fetchMode !== FALSE) {
00272                $savem = $this->SetFetchMode(FALSE);
00273         }
00274                 $SQL=sprintf("SELECT name,sql FROM sqlite_master WHERE type='index' AND tbl_name='%s'", strtolower($table));
00275         $rs = $this->Execute($SQL);
00276         if (!is_object($rs)) {
00277                         if (isset($savem)) 
00278                                 $this->SetFetchMode($savem);
00279                         $ADODB_FETCH_MODE = $save;
00280             return $false;
00281         }
00282 
00283                 $indexes = array ();
00284                 while ($row = $rs->FetchRow()) {
00285                         if ($primary && preg_match("/primary/i",$row[1]) == 0) continue;
00286             if (!isset($indexes[$row[0]])) {
00287 
00288                         $indexes[$row[0]] = array(
00289                                    'unique' => preg_match("/unique/i",$row[1]),
00290                                    'columns' => array());
00291                         }
00298                         $cols = explode("(",$row[1]);
00299                         $cols = explode(")",$cols[1]);
00300                         array_pop($cols);
00301                         $indexes[$row[0]]['columns'] = $cols;
00302                 }
00303                 if (isset($savem)) { 
00304             $this->SetFetchMode($savem);
00305                         $ADODB_FETCH_MODE = $save;
00306                 }
00307         return $indexes;
00308         }
00309 
00310 }
00311 
00312 /*--------------------------------------------------------------------------------------
00313                  Class Name: Recordset
00314 --------------------------------------------------------------------------------------*/
00315 
00316 class ADORecordset_sqlite extends ADORecordSet {
00317 
00318         var $databaseType = "sqlite";
00319         var $bind = false;
00320 
00321         function ADORecordset_sqlite($queryID,$mode=false)
00322         {
00323                 
00324                 if ($mode === false) { 
00325                         global $ADODB_FETCH_MODE;
00326                         $mode = $ADODB_FETCH_MODE;
00327                 }
00328                 switch($mode) {
00329                 case ADODB_FETCH_NUM: $this->fetchMode = SQLITE_NUM; break;
00330                 case ADODB_FETCH_ASSOC: $this->fetchMode = SQLITE_ASSOC; break;
00331                 default: $this->fetchMode = SQLITE_BOTH; break;
00332                 }
00333                 $this->adodbFetchMode = $mode;
00334                 
00335                 $this->_queryID = $queryID;
00336         
00337                 $this->_inited = true;
00338                 $this->fields = array();
00339                 if ($queryID) {
00340                         $this->_currentRow = 0;
00341                         $this->EOF = !$this->_fetch();
00342                         @$this->_initrs();
00343                 } else {
00344                         $this->_numOfRows = 0;
00345                         $this->_numOfFields = 0;
00346                         $this->EOF = true;
00347                 }
00348                 
00349                 return $this->_queryID;
00350         }
00351 
00352 
00353         function &FetchField($fieldOffset = -1)
00354         {
00355                 $fld = new ADOFieldObject;
00356                 $fld->name = sqlite_field_name($this->_queryID, $fieldOffset);
00357                 $fld->type = 'VARCHAR';
00358                 $fld->max_length = -1;
00359                 return $fld;
00360         }
00361         
00362    function _initrs()
00363    {
00364                 $this->_numOfRows = @sqlite_num_rows($this->_queryID);
00365                 $this->_numOfFields = @sqlite_num_fields($this->_queryID);
00366    }
00367 
00368         function Fields($colname)
00369         {
00370                 if ($this->fetchMode != SQLITE_NUM) return $this->fields[$colname];
00371                 if (!$this->bind) {
00372                         $this->bind = array();
00373                         for ($i=0; $i < $this->_numOfFields; $i++) {
00374                                 $o = $this->FetchField($i);
00375                                 $this->bind[strtoupper($o->name)] = $i;
00376                         }
00377                 }
00378                 
00379                  return $this->fields[$this->bind[strtoupper($colname)]];
00380         }
00381         
00382    function _seek($row)
00383    {
00384                 return sqlite_seek($this->_queryID, $row);
00385    }
00386 
00387         function _fetch($ignore_fields=false) 
00388         {
00389                 $this->fields = @sqlite_fetch_array($this->_queryID,$this->fetchMode);
00390                 return !empty($this->fields);
00391         }
00392         
00393         function _close() 
00394         {
00395         }
00396 
00397 }
00398 ?>


Généré par TYPO3 Ameos avec  doxygen 1.4.6