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