]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiDB/adodb/drivers/adodb-pdo_sqlite.inc.php
Upgrade adodb
[SourceForge/phpwiki.git] / lib / WikiDB / adodb / drivers / adodb-pdo_sqlite.inc.php
1 <?php
2
3 /* 
4  V5.18 3 Sep 2012  (c) 2000-2012 John Lim (jlim#natsoft.com). All rights reserved.
5   Released under both BSD license and Lesser GPL library license. 
6   Whenever there is any discrepancy between the two licenses, 
7   the BSD license will take precedence. See License.txt. 
8   Set tabs to 4 for best viewing.
9   
10   Latest version is available at http://adodb.sourceforge.net
11   
12   Thanks Diogo Toscano (diogo#scriptcase.net) for the code.
13         And also Sid Dunayer [sdunayer#interserv.com] for extensive fixes.
14 */
15
16 class ADODB_pdo_sqlite extends ADODB_pdo {
17         var $metaTablesSQL   = "SELECT name FROM sqlite_master WHERE type='table'";
18         var $sysDate         = 'current_date';
19         var $sysTimeStamp    = 'current_timestamp';
20         var $nameQuote       = '`';
21         var $replaceQuote    = "''";
22         var $hasGenID        = true;
23         var $_genIDSQL       = "UPDATE %s SET id=id+1 WHERE id=%s";
24         var $_genSeqSQL      = "CREATE TABLE %s (id integer)";
25         var $_genSeqCountSQL = 'SELECT COUNT(*) FROM %s';
26         var $_genSeq2SQL     = 'INSERT INTO %s VALUES(%s)';
27         var $_dropSeqSQL     = 'DROP TABLE %s';
28         var $concat_operator = '||';
29     var $pdoDriver       = false;
30         var $random='abs(random())';
31     
32         function _init($parentDriver)
33         {
34                 $this->pdoDriver = $parentDriver;
35                 $parentDriver->_bindInputArray = true;
36                 $parentDriver->hasTransactions = false; // // should be set to false because of PDO SQLite driver not supporting changing autocommit mode
37                 $parentDriver->hasInsertID = true;
38         }
39
40         function ServerInfo()
41         {
42                 $parent = $this->pdoDriver;
43                 @($ver = array_pop($parent->GetCol("SELECT sqlite_version()")));
44                 @($enc = array_pop($parent->GetCol("PRAGMA encoding")));
45
46                 $arr['version']     = $ver;
47                 $arr['description'] = 'SQLite ';
48                 $arr['encoding']    = $enc;
49
50                 return $arr;
51         }
52         
53         function SelectLimit($sql,$nrows=-1,$offset=-1,$inputarr=false,$secs2cache=0) 
54         {
55                 $parent = $this->pdoDriver;
56                 $offsetStr = ($offset >= 0) ? " OFFSET $offset" : '';
57                 $limitStr  = ($nrows >= 0)  ? " LIMIT $nrows" : ($offset >= 0 ? ' LIMIT 999999999' : '');
58                 if ($secs2cache)
59                         $rs = $parent->CacheExecute($secs2cache,$sql."$limitStr$offsetStr",$inputarr);
60                 else
61                         $rs = $parent->Execute($sql."$limitStr$offsetStr",$inputarr);
62
63                 return $rs;
64         }
65
66         function GenID($seq='adodbseq',$start=1)
67         {
68                 $parent = $this->pdoDriver;
69                 // if you have to modify the parameter below, your database is overloaded,
70                 // or you need to implement generation of id's yourself!
71                 $MAXLOOPS = 100;
72                 while (--$MAXLOOPS>=0) {
73                         @($num = array_pop($parent->GetCol("SELECT id FROM {$seq}")));
74                         if ($num === false || !is_numeric($num)) {
75                                 @$parent->Execute(sprintf($this->_genSeqSQL ,$seq));
76                                 $start -= 1;
77                                 $num = '0';
78                                 $cnt = $parent->GetOne(sprintf($this->_genSeqCountSQL,$seq));
79                                 if (!$cnt) {
80                                         $ok = $parent->Execute(sprintf($this->_genSeq2SQL,$seq,$start));
81                                 }
82                                 if (!$ok) return false;
83                         }
84                         $parent->Execute(sprintf($this->_genIDSQL,$seq,$num));
85
86                         if ($parent->affected_rows() > 0) {
87                                 $num += 1;
88                                 $parent->genID = intval($num);
89                                 return intval($num);
90                         }
91                 }
92                 if ($fn = $parent->raiseErrorFn) {
93                         $fn($parent->databaseType,'GENID',-32000,"Unable to generate unique id after $MAXLOOPS attempts",$seq,$num);
94                 }
95                 return false;
96         }
97
98         function CreateSequence($seqname='adodbseq',$start=1)
99         {
100                 $parent = $this->pdoDriver;
101                 $ok = $parent->Execute(sprintf($this->_genSeqSQL,$seqname));
102                 if (!$ok) return false;
103                 $start -= 1;
104                 return $parent->Execute("insert into $seqname values($start)");
105         }
106
107         function SetTransactionMode($transaction_mode)
108         {
109                 $parent = $this->pdoDriver;
110                 $parent->_transmode = strtoupper($transaction_mode);
111         }
112
113         function BeginTrans()
114         {       
115                 $parent = $this->pdoDriver;
116                 if ($parent->transOff) return true; 
117                 $parent->transCnt += 1;
118                 $parent->_autocommit = false;
119                 return $parent->Execute("BEGIN {$parent->_transmode}");
120         }
121         
122         function CommitTrans($ok=true) 
123         { 
124                 $parent = $this->pdoDriver;
125                 if ($parent->transOff) return true; 
126                 if (!$ok) return $parent->RollbackTrans();
127                 if ($parent->transCnt) $parent->transCnt -= 1;
128                 $parent->_autocommit = true;
129                 
130                 $ret = $parent->Execute('COMMIT');
131                 return $ret;
132         }
133         
134         function RollbackTrans()
135         {
136                 $parent = $this->pdoDriver;
137                 if ($parent->transOff) return true; 
138                 if ($parent->transCnt) $parent->transCnt -= 1;
139                 $parent->_autocommit = true;
140                 
141                 $ret = $parent->Execute('ROLLBACK');
142                 return $ret;
143         }
144
145
146     // mark newnham
147         function MetaColumns($tab,$normalize=true)
148         {
149           global $ADODB_FETCH_MODE;
150
151           $parent = $this->pdoDriver;
152           $false = false;
153           $save = $ADODB_FETCH_MODE;
154           $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
155           if ($parent->fetchMode !== false) $savem = $parent->SetFetchMode(false);
156           $rs = $parent->Execute("PRAGMA table_info('$tab')");
157           if (isset($savem)) $parent->SetFetchMode($savem);
158           if (!$rs) {
159             $ADODB_FETCH_MODE = $save; 
160             return $false;
161           }
162           $arr = array();
163           while ($r = $rs->FetchRow()) {
164             $type = explode('(',$r['type']);
165             $size = '';
166             if (sizeof($type)==2)
167             $size = trim($type[1],')');
168             $fn = strtoupper($r['name']);
169             $fld = new ADOFieldObject;
170             $fld->name = $r['name'];
171             $fld->type = $type[0];
172             $fld->max_length = $size;
173             $fld->not_null = $r['notnull'];
174             $fld->primary_key = $r['pk'];
175             $fld->default_value = $r['dflt_value'];
176             $fld->scale = 0;
177             if ($save == ADODB_FETCH_NUM) $arr[] = $fld;        
178             else $arr[strtoupper($fld->name)] = $fld;
179           }
180           $rs->Close();
181           $ADODB_FETCH_MODE = $save;
182           return $arr;
183         }
184
185         function MetaTables($ttype=false,$showSchema=false,$mask=false)
186         {
187                 $parent = $this->pdoDriver;
188                 
189                 if ($mask) {
190                         $save = $this->metaTablesSQL;
191                         $mask = $this->qstr(strtoupper($mask));
192                         $this->metaTablesSQL .= " AND name LIKE $mask";
193                 }
194                 
195                 $ret = $parent->GetCol($this->metaTablesSQL);
196                 
197                 if ($mask) {
198                         $this->metaTablesSQL = $save;
199                 }
200                 return $ret;
201    }
202 }
203 ?>