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