[ Index ]
 

Code source de Mantis 1.1.0rc3

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/core/adodb/drivers/ -> adodb-sqlite.inc.php.bak (source)

   1  <?php
   2  /*
   3  V4.53 14 Sept 2004  (c) 2000-2004 John Lim (jlim@natsoft.com.my). All rights reserved.
   4    Released under both BSD license and Lesser GPL library license.
   5    Whenever there is any discrepancy between the two licenses,
   6    the BSD license will take precedence.
   7  
   8    Latest version is available at http://adodb.sourceforge.net
   9  
  10    SQLite info: http://www.hwaci.com/sw/sqlite/
  11  
  12    Install Instructions:
  13    ====================
  14    1. Place this in adodb/drivers
  15    2. Rename the file, remove the .txt prefix.
  16  */
  17  
  18  // security - hide paths
  19  if (!defined('ADODB_DIR')) die();
  20  
  21  class ADODB_sqlite extends ADOConnection {
  22      var $databaseType = "sqlite";
  23      var $replaceQuote = "''"; // string to use to replace quotes
  24      var $concat_operator='||';
  25      var $_errorNo = 0;
  26      var $hasLimit = true;
  27      var $hasInsertID = true;         /// supports autoincrement ID?
  28      var $hasAffectedRows = true;     /// supports affected rows for update/delete?
  29      var $metaTablesSQL = "SELECT name FROM sqlite_master WHERE type='table' ORDER BY name";
  30      var $sysDate = "adodb_date('Y-m-d')";
  31      var $sysTimeStamp = "adodb_date('Y-m-d H:i:s')";
  32      var $fmtTimeStamp = "'Y-m-d H:i:s'";
  33  
  34  	function ADODB_sqlite()
  35      {
  36      }
  37  
  38  /*
  39    function __get($name)
  40    {
  41        switch($name) {
  42      case 'sysDate': return "'".date($this->fmtDate)."'";
  43      case 'sysTimeStamp' : return "'".date($this->sysTimeStamp)."'";
  44      }
  45    }*/
  46  
  47  	function ServerInfo()
  48      {
  49          $arr['version'] = sqlite_libversion();
  50          $arr['description'] = 'SQLite ';
  51          $arr['encoding'] = sqlite_libencoding();
  52          return $arr;
  53      }
  54  
  55  	function BeginTrans()
  56      {
  57           if ($this->transOff) return true;
  58           $ret = $this->Execute("BEGIN TRANSACTION");
  59           $this->transCnt += 1;
  60           return true;
  61      }
  62  
  63  	function CommitTrans($ok=true)
  64      {
  65          if ($this->transOff) return true;
  66          if (!$ok) return $this->RollbackTrans();
  67          $ret = $this->Execute("COMMIT");
  68          if ($this->transCnt>0)$this->transCnt -= 1;
  69          return !empty($ret);
  70      }
  71  
  72  	function RollbackTrans()
  73      {
  74          if ($this->transOff) return true;
  75          $ret = $this->Execute("ROLLBACK");
  76          if ($this->transCnt>0)$this->transCnt -= 1;
  77          return !empty($ret);
  78      }
  79  
  80  	function _insertid()
  81      {
  82          return sqlite_last_insert_rowid($this->_connectionID);
  83      }
  84  
  85  	function _affectedrows()
  86      {
  87          return sqlite_changes($this->_connectionID);
  88      }
  89  
  90  	function ErrorMsg()
  91       {
  92          if ($this->_logsql) return $this->_errorMsg;
  93          return ($this->_errorNo) ? sqlite_error_string($this->_errorNo) : '';
  94      }
  95  
  96  	function ErrorNo()
  97      {
  98          return $this->_errorNo;
  99      }
 100  
 101  	function SQLDate($fmt, $col=false)
 102      {
 103          $fmt = $this->qstr($fmt);
 104          return ($col) ? "adodb_date2($fmt,$col)" : "adodb_date($fmt)";
 105      }
 106  
 107      function &MetaColumns($tab)
 108      {
 109      global $ADODB_FETCH_MODE;
 110  
 111          $rs = $this->Execute("select * from $tab limit 1");
 112          if (!$rs) return false;
 113          $arr = array();
 114          for ($i=0,$max=$rs->_numOfFields; $i < $max; $i++) {
 115              $fld =& $rs->FetchField($i);
 116              if ($ADODB_FETCH_MODE == ADODB_FETCH_NUM) $retarr[] =& $fld;
 117              else $arr[strtoupper($fld->name)] =& $fld;
 118          }
 119          $rs->Close();
 120          return $arr;
 121      }
 122  
 123  	function _createFunctions()
 124      {
 125          @sqlite_create_function($this->_connectionID, 'adodb_date', 'adodb_date', 1);
 126          @sqlite_create_function($this->_connectionID, 'adodb_date2', 'adodb_date2', 2);
 127      }
 128  
 129  
 130      // returns true or false
 131  	function _connect($argHostname, $argUsername, $argPassword, $argDatabasename)
 132      {
 133          if (!function_exists('sqlite_open')) return null;
 134  
 135          $this->_connectionID = sqlite_open($argHostname);
 136          if ($this->_connectionID === false) return false;
 137          $this->_createFunctions();
 138          return true;
 139      }
 140  
 141      // returns true or false
 142  	function _pconnect($argHostname, $argUsername, $argPassword, $argDatabasename)
 143      {
 144          if (!function_exists('sqlite_open')) return null;
 145  
 146          $this->_connectionID = sqlite_popen($argHostname);
 147          if ($this->_connectionID === false) return false;
 148          $this->_createFunctions();
 149          return true;
 150      }
 151  
 152      // returns query ID if successful, otherwise false
 153  	function _query($sql,$inputarr=false)
 154      {
 155          $rez = sqlite_query($sql,$this->_connectionID);
 156          if (!$rez) {
 157              $this->_errorNo = sqlite_last_error($this->_connectionID);
 158          }
 159  
 160          return $rez;
 161      }
 162  
 163      function &SelectLimit($sql,$nrows=-1,$offset=-1,$inputarr=false,$secs2cache=0)
 164      {
 165          $offsetStr = ($offset >= 0) ? " OFFSET $offset" : '';
 166          $limitStr  = ($nrows >= 0)  ? " LIMIT $nrows" : ($offset >= 0 ? ' LIMIT 999999999' : '');
 167            if ($secs2cache)
 168                 $rs =& $this->CacheExecute($secs2cache,$sql."$limitStr$offsetStr",$inputarr);
 169            else
 170                 $rs =& $this->Execute($sql."$limitStr$offsetStr",$inputarr);
 171  
 172          return $rs;
 173      }
 174  
 175      /*
 176          This algorithm is not very efficient, but works even if table locking
 177          is not available.
 178  
 179          Will return false if unable to generate an ID after $MAXLOOPS attempts.
 180      */
 181      var $_genSeqSQL = "create table %s (id integer)";
 182  
 183  	function GenID($seq='adodbseq',$start=1)
 184      {
 185          // if you have to modify the parameter below, your database is overloaded,
 186          // or you need to implement generation of id's yourself!
 187          $MAXLOOPS = 100;
 188          //$this->debug=1;
 189          while (--$MAXLOOPS>=0) {
 190  <<<<<<< adodb-sqlite.inc.php
 191              $num = @$this->GetOne("select id from $seq");
 192  =======
 193              @($num = $this->GetOne("select id from $seq"));
 194  >>>>>>> 1.42
 195              if ($num === false) {
 196                  $this->Execute(sprintf($this->_genSeqSQL ,$seq));
 197                  $start -= 1;
 198                  $num = '0';
 199                  $ok = $this->Execute("insert into $seq values($start)");
 200                  if (!$ok) return false;
 201              }
 202              $this->Execute("update $seq set id=id+1 where id=$num");
 203  
 204              if ($this->affected_rows() > 0) {
 205                  $num += 1;
 206                  $this->genID = $num;
 207                  return $num;
 208              }
 209          }
 210          if ($fn = $this->raiseErrorFn) {
 211              $fn($this->databaseType,'GENID',-32000,"Unable to generate unique id after $MAXLOOPS attempts",$seq,$num);
 212          }
 213          return false;
 214      }
 215  
 216  	function CreateSequence($seqname='adodbseq',$start=1)
 217      {
 218          if (empty($this->_genSeqSQL)) return false;
 219          $ok = $this->Execute(sprintf($this->_genSeqSQL,$seqname));
 220          if (!$ok) return false;
 221          $start -= 1;
 222          return $this->Execute("insert into $seqname values($start)");
 223      }
 224  
 225      var $_dropSeqSQL = 'drop table %s';
 226  	function DropSequence($seqname)
 227      {
 228          if (empty($this->_dropSeqSQL)) return false;
 229          return $this->Execute(sprintf($this->_dropSeqSQL,$seqname));
 230      }
 231  
 232      // returns true or false
 233  	function _close()
 234      {
 235          return @sqlite_close($this->_connectionID);
 236      }
 237  
 238  
 239  }
 240  
 241  /*--------------------------------------------------------------------------------------
 242           Class Name: Recordset
 243  --------------------------------------------------------------------------------------*/
 244  
 245  class ADORecordset_sqlite extends ADORecordSet {
 246  
 247      var $databaseType = "sqlite";
 248      var $bind = false;
 249  
 250  	function ADORecordset_sqlite($queryID,$mode=false)
 251      {
 252  
 253          if ($mode === false) {
 254              global $ADODB_FETCH_MODE;
 255              $mode = $ADODB_FETCH_MODE;
 256          }
 257          switch($mode) {
 258          case ADODB_FETCH_NUM: $this->fetchMode = SQLITE_NUM; break;
 259          case ADODB_FETCH_ASSOC: $this->fetchMode = SQLITE_ASSOC; break;
 260          default: $this->fetchMode = SQLITE_BOTH; break;
 261          }
 262  
 263          $this->_queryID = $queryID;
 264  
 265          $this->_inited = true;
 266          $this->fields = array();
 267          if ($queryID) {
 268              $this->_currentRow = 0;
 269              $this->EOF = !$this->_fetch();
 270              @$this->_initrs();
 271          } else {
 272              $this->_numOfRows = 0;
 273              $this->_numOfFields = 0;
 274              $this->EOF = true;
 275          }
 276  
 277          return $this->_queryID;
 278      }
 279  
 280  
 281      function &FetchField($fieldOffset = -1)
 282      {
 283          $fld = new ADOFieldObject;
 284          $fld->name = sqlite_field_name($this->_queryID, $fieldOffset);
 285          $fld->type = 'VARCHAR';
 286          $fld->max_length = -1;
 287          return $fld;
 288      }
 289  
 290     function _initrs()
 291     {
 292          $this->_numOfRows = @sqlite_num_rows($this->_queryID);
 293          $this->_numOfFields = @sqlite_num_fields($this->_queryID);
 294     }
 295  
 296  	function Fields($colname)
 297      {
 298          if ($this->fetchMode != SQLITE_NUM) return $this->fields[$colname];
 299          if (!$this->bind) {
 300              $this->bind = array();
 301              for ($i=0; $i < $this->_numOfFields; $i++) {
 302                  $o = $this->FetchField($i);
 303                  $this->bind[strtoupper($o->name)] = $i;
 304              }
 305          }
 306  
 307           return $this->fields[$this->bind[strtoupper($colname)]];
 308      }
 309  
 310     function _seek($row)
 311     {
 312             return sqlite_seek($this->_queryID, $row);
 313     }
 314  
 315  	function _fetch($ignore_fields=false)
 316      {
 317          $this->fields = @sqlite_fetch_array($this->_queryID,$this->fetchMode);
 318          return !empty($this->fields);
 319      }
 320  
 321  	function _close()
 322      {
 323      }
 324  
 325  }
 326  ?>


Généré le : Thu Nov 29 09:42:17 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics