home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / adodb / drivers / adodb-sqlite.inc.php < prev    next >
Encoding:
PHP Script  |  2005-05-17  |  9.9 KB  |  373 lines

  1. <?php
  2. /*
  3. V4.63 17 May 2005  (c) 2000-2005 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) {
  113.             $false = false;
  114.             return $false;
  115.         }
  116.         $arr = array();
  117.         for ($i=0,$max=$rs->_numOfFields; $i < $max; $i++) {
  118.             $fld =& $rs->FetchField($i);
  119.             if ($ADODB_FETCH_MODE == ADODB_FETCH_NUM) $retarr[] =& $fld;    
  120.             else $arr[strtoupper($fld->name)] =& $fld;
  121.         }
  122.         $rs->Close();
  123.         return $arr;
  124.     }
  125.     
  126.     function _createFunctions()
  127.     {
  128.         @sqlite_create_function($this->_connectionID, 'adodb_date', 'adodb_date', 1);
  129.         @sqlite_create_function($this->_connectionID, 'adodb_date2', 'adodb_date2', 2);
  130.     }
  131.     
  132.  
  133.     // returns true or false
  134.     function _connect($argHostname, $argUsername, $argPassword, $argDatabasename)
  135.     {
  136.         if (!function_exists('sqlite_open')) return null;
  137.         if (empty($argHostname) && $argDatabasename) $argHostname = $argDatabasename;
  138.         
  139.         $this->_connectionID = sqlite_open($argHostname);
  140.         if ($this->_connectionID === false) return false;
  141.         $this->_createFunctions();
  142.         return true;
  143.     }
  144.     
  145.     // returns true or false
  146.     function _pconnect($argHostname, $argUsername, $argPassword, $argDatabasename)
  147.     {
  148.         if (!function_exists('sqlite_open')) return null;
  149.         if (empty($argHostname) && $argDatabasename) $argHostname = $argDatabasename;
  150.         
  151.         $this->_connectionID = sqlite_popen($argHostname);
  152.         if ($this->_connectionID === false) return false;
  153.         $this->_createFunctions();
  154.         return true;
  155.     }
  156.  
  157.     // returns query ID if successful, otherwise false
  158.     function _query($sql,$inputarr=false)
  159.     {
  160.         $rez = sqlite_query($sql,$this->_connectionID);
  161.         if (!$rez) {
  162.             $this->_errorNo = sqlite_last_error($this->_connectionID);
  163.         }
  164.         
  165.         return $rez;
  166.     }
  167.     
  168.     function &SelectLimit($sql,$nrows=-1,$offset=-1,$inputarr=false,$secs2cache=0) 
  169.     {
  170.         $offsetStr = ($offset >= 0) ? " OFFSET $offset" : '';
  171.         $limitStr  = ($nrows >= 0)  ? " LIMIT $nrows" : ($offset >= 0 ? ' LIMIT 999999999' : '');
  172.           if ($secs2cache)
  173.                $rs =& $this->CacheExecute($secs2cache,$sql."$limitStr$offsetStr",$inputarr);
  174.           else
  175.                $rs =& $this->Execute($sql."$limitStr$offsetStr",$inputarr);
  176.             
  177.         return $rs;
  178.     }
  179.     
  180.     /*
  181.         This algorithm is not very efficient, but works even if table locking
  182.         is not available.
  183.         
  184.         Will return false if unable to generate an ID after $MAXLOOPS attempts.
  185.     */
  186.     var $_genSeqSQL = "create table %s (id integer)";
  187.     
  188.     function GenID($seq='adodbseq',$start=1)
  189.     {    
  190.         // if you have to modify the parameter below, your database is overloaded,
  191.         // or you need to implement generation of id's yourself!
  192.         $MAXLOOPS = 100;
  193.         //$this->debug=1;
  194.         while (--$MAXLOOPS>=0) {
  195.             @($num = $this->GetOne("select id from $seq"));
  196.             if ($num === false) {
  197.                 $this->Execute(sprintf($this->_genSeqSQL ,$seq));    
  198.                 $start -= 1;
  199.                 $num = '0';
  200.                 $ok = $this->Execute("insert into $seq values($start)");
  201.                 if (!$ok) return false;
  202.             } 
  203.             $this->Execute("update $seq set id=id+1 where id=$num");
  204.             
  205.             if ($this->affected_rows() > 0) {
  206.                 $num += 1;
  207.                 $this->genID = $num;
  208.                 return $num;
  209.             }
  210.         }
  211.         if ($fn = $this->raiseErrorFn) {
  212.             $fn($this->databaseType,'GENID',-32000,"Unable to generate unique id after $MAXLOOPS attempts",$seq,$num);
  213.         }
  214.         return false;
  215.     }
  216.  
  217.     function CreateSequence($seqname='adodbseq',$start=1)
  218.     {
  219.         if (empty($this->_genSeqSQL)) return false;
  220.         $ok = $this->Execute(sprintf($this->_genSeqSQL,$seqname));
  221.         if (!$ok) return false;
  222.         $start -= 1;
  223.         return $this->Execute("insert into $seqname values($start)");
  224.     }
  225.     
  226.     var $_dropSeqSQL = 'drop table %s';
  227.     function DropSequence($seqname)
  228.     {
  229.         if (empty($this->_dropSeqSQL)) return false;
  230.         return $this->Execute(sprintf($this->_dropSeqSQL,$seqname));
  231.     }
  232.     
  233.     // returns true or false
  234.     function _close()
  235.     {
  236.         return @sqlite_close($this->_connectionID);
  237.     }
  238.  
  239.     function &MetaIndexes ($table, $primary = FALSE, $owner=false)
  240.     {
  241.         $false = false;
  242.         // save old fetch mode
  243.         global $ADODB_FETCH_MODE;
  244.         $save = $ADODB_FETCH_MODE;
  245.         $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
  246.         if ($this->fetchMode !== FALSE) {
  247.                $savem = $this->SetFetchMode(FALSE);
  248.         }
  249.         $SQL=sprintf("SELECT name,sql FROM sqlite_master WHERE type='index' AND tbl_name='%s'", strtolower($table));
  250.         $rs = $this->Execute($SQL);
  251.         if (!is_object($rs)) {
  252.             if (isset($savem)) 
  253.                 $this->SetFetchMode($savem);
  254.             $ADODB_FETCH_MODE = $save;
  255.             return $false;
  256.         }
  257.  
  258.         $indexes = array ();
  259.         while ($row = $rs->FetchRow()) {
  260.             if ($primary && preg_match("/primary/i",$row[1]) == 0) continue;
  261.             if (!isset($indexes[$row[0]])) {
  262.  
  263.             $indexes[$row[0]] = array(
  264.                    'unique' => preg_match("/unique/i",$row[1]),
  265.                    'columns' => array());
  266.             }
  267.             /**
  268.               * There must be a more elegant way of doing this,
  269.               * the index elements appear in the SQL statement
  270.               * in cols[1] between parentheses
  271.               * e.g CREATE UNIQUE INDEX ware_0 ON warehouse (org,warehouse)
  272.               */
  273.             $cols = explode("(",$row[1]);
  274.             $cols = explode(")",$cols[1]);
  275.             array_pop($cols);
  276.             $indexes[$row[0]]['columns'] = $cols;
  277.         }
  278.         if (isset($savem)) { 
  279.             $this->SetFetchMode($savem);
  280.             $ADODB_FETCH_MODE = $save;
  281.         }
  282.         return $indexes;
  283.     }
  284.  
  285. }
  286.  
  287. /*--------------------------------------------------------------------------------------
  288.          Class Name: Recordset
  289. --------------------------------------------------------------------------------------*/
  290.  
  291. class ADORecordset_sqlite extends ADORecordSet {
  292.  
  293.     var $databaseType = "sqlite";
  294.     var $bind = false;
  295.  
  296.     function ADORecordset_sqlite($queryID,$mode=false)
  297.     {
  298.         
  299.         if ($mode === false) { 
  300.             global $ADODB_FETCH_MODE;
  301.             $mode = $ADODB_FETCH_MODE;
  302.         }
  303.         switch($mode) {
  304.         case ADODB_FETCH_NUM: $this->fetchMode = SQLITE_NUM; break;
  305.         case ADODB_FETCH_ASSOC: $this->fetchMode = SQLITE_ASSOC; break;
  306.         default: $this->fetchMode = SQLITE_BOTH; break;
  307.         }
  308.         $this->adodbFetchMode = $mode;
  309.         
  310.         $this->_queryID = $queryID;
  311.     
  312.         $this->_inited = true;
  313.         $this->fields = array();
  314.         if ($queryID) {
  315.             $this->_currentRow = 0;
  316.             $this->EOF = !$this->_fetch();
  317.             @$this->_initrs();
  318.         } else {
  319.             $this->_numOfRows = 0;
  320.             $this->_numOfFields = 0;
  321.             $this->EOF = true;
  322.         }
  323.         
  324.         return $this->_queryID;
  325.     }
  326.  
  327.  
  328.     function &FetchField($fieldOffset = -1)
  329.     {
  330.         $fld = new ADOFieldObject;
  331.         $fld->name = sqlite_field_name($this->_queryID, $fieldOffset);
  332.         $fld->type = 'VARCHAR';
  333.         $fld->max_length = -1;
  334.         return $fld;
  335.     }
  336.     
  337.    function _initrs()
  338.    {
  339.         $this->_numOfRows = @sqlite_num_rows($this->_queryID);
  340.         $this->_numOfFields = @sqlite_num_fields($this->_queryID);
  341.    }
  342.  
  343.     function Fields($colname)
  344.     {
  345.         if ($this->fetchMode != SQLITE_NUM) return $this->fields[$colname];
  346.         if (!$this->bind) {
  347.             $this->bind = array();
  348.             for ($i=0; $i < $this->_numOfFields; $i++) {
  349.                 $o = $this->FetchField($i);
  350.                 $this->bind[strtoupper($o->name)] = $i;
  351.             }
  352.         }
  353.         
  354.          return $this->fields[$this->bind[strtoupper($colname)]];
  355.     }
  356.     
  357.    function _seek($row)
  358.    {
  359.            return sqlite_seek($this->_queryID, $row);
  360.    }
  361.  
  362.     function _fetch($ignore_fields=false) 
  363.     {
  364.         $this->fields = @sqlite_fetch_array($this->_queryID,$this->fetchMode);
  365.         return !empty($this->fields);
  366.     }
  367.     
  368.     function _close() 
  369.     {
  370.     }
  371.  
  372. }
  373. ?>