source: trunk/phpgwapi/inc/adodb/drivers/adodb-sqlite.inc.php @ 2

Revision 2, 7.9 KB checked in by niltonneto, 17 years ago (diff)

Removida todas as tags usadas pelo CVS ($Id, $Source).
Primeira versão no CVS externo.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1<?php
2/*
3V4.51 29 July 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
19if (!defined('ADODB_DIR')) die();
20
21class 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                        $num = $this->GetOne("select id from $seq");
191                        if ($num === false) {
192                                $this->Execute(sprintf($this->_genSeqSQL ,$seq));       
193                                $start -= 1;
194                                $num = '0';
195                                $ok = $this->Execute("insert into $seq values($start)");
196                                if (!$ok) return false;
197                        }
198                        $this->Execute("update $seq set id=id+1 where id=$num");
199                       
200                        if ($this->affected_rows() > 0) {
201                                $num += 1;
202                                $this->genID = $num;
203                                return $num;
204                        }
205                }
206                if ($fn = $this->raiseErrorFn) {
207                        $fn($this->databaseType,'GENID',-32000,"Unable to generate unique id after $MAXLOOPS attempts",$seq,$num);
208                }
209                return false;
210        }
211
212        function CreateSequence($seqname='adodbseq',$start=1)
213        {
214                if (empty($this->_genSeqSQL)) return false;
215                $ok = $this->Execute(sprintf($this->_genSeqSQL,$seqname));
216                if (!$ok) return false;
217                $start -= 1;
218                return $this->Execute("insert into $seqname values($start)");
219        }
220       
221        var $_dropSeqSQL = 'drop table %s';
222        function DropSequence($seqname)
223        {
224                if (empty($this->_dropSeqSQL)) return false;
225                return $this->Execute(sprintf($this->_dropSeqSQL,$seqname));
226        }
227       
228        // returns true or false
229        function _close()
230        {
231                return @sqlite_close($this->_connectionID);
232        }
233
234
235}
236
237/*--------------------------------------------------------------------------------------
238                 Class Name: Recordset
239--------------------------------------------------------------------------------------*/
240
241class ADORecordset_sqlite extends ADORecordSet {
242
243        var $databaseType = "sqlite";
244        var $bind = false;
245
246        function ADORecordset_sqlite($queryID,$mode=false)
247        {
248               
249                if ($mode === false) {
250                        global $ADODB_FETCH_MODE;
251                        $mode = $ADODB_FETCH_MODE;
252                }
253                switch($mode) {
254                case ADODB_FETCH_NUM: $this->fetchMode = SQLITE_NUM; break;
255                case ADODB_FETCH_ASSOC: $this->fetchMode = SQLITE_ASSOC; break;
256                default: $this->fetchMode = SQLITE_BOTH; break;
257                }
258               
259                $this->_queryID = $queryID;
260       
261                $this->_inited = true;
262                $this->fields = array();
263                if ($queryID) {
264                        $this->_currentRow = 0;
265                        $this->EOF = !$this->_fetch();
266                        @$this->_initrs();
267                } else {
268                        $this->_numOfRows = 0;
269                        $this->_numOfFields = 0;
270                        $this->EOF = true;
271                }
272               
273                return $this->_queryID;
274        }
275
276
277        function &FetchField($fieldOffset = -1)
278        {
279                $fld = new ADOFieldObject;
280                $fld->name = sqlite_field_name($this->_queryID, $fieldOffset);
281                $fld->type = 'VARCHAR';
282                $fld->max_length = -1;
283                return $fld;
284        }
285       
286   function _initrs()
287   {
288                $this->_numOfRows = @sqlite_num_rows($this->_queryID);
289                $this->_numOfFields = @sqlite_num_fields($this->_queryID);
290   }
291
292        function Fields($colname)
293        {
294                if ($this->fetchMode != SQLITE_NUM) return $this->fields[$colname];
295                if (!$this->bind) {
296                        $this->bind = array();
297                        for ($i=0; $i < $this->_numOfFields; $i++) {
298                                $o = $this->FetchField($i);
299                                $this->bind[strtoupper($o->name)] = $i;
300                        }
301                }
302               
303                 return $this->fields[$this->bind[strtoupper($colname)]];
304        }
305       
306   function _seek($row)
307   {
308                return sqlite_seek($this->_queryID, $row);
309   }
310
311        function _fetch($ignore_fields=false)
312        {
313                $this->fields = @sqlite_fetch_array($this->_queryID,$this->fetchMode);
314                return !empty($this->fields);
315        }
316       
317        function _close()
318        {
319        }
320
321}
322?>
Note: See TracBrowser for help on using the repository browser.