4 V5.14 8 Sept 2011 (c) 2000-2011 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.
10 Latest version is available at http://adodb.sourceforge.net
12 Thanks Diogo Toscano (diogo#scriptcase.net) for the code.
13 And also Sid Dunayer [sdunayer#interserv.com] for extensive fixes.
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';
21 var $replaceQuote = "''";
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())';
32 function _init($parentDriver)
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;
42 $parent = $this->pdoDriver
;
43 @($ver = array_pop($parent->GetCol("SELECT sqlite_version()")));
44 @($enc = array_pop($parent->GetCol("PRAGMA encoding")));
46 $arr['version'] = $ver;
47 $arr['description'] = 'SQLite ';
48 $arr['encoding'] = $enc;
53 function SelectLimit($sql,$nrows=-1,$offset=-1,$inputarr=false,$secs2cache=0)
55 $parent = $this->pdoDriver
;
56 $offsetStr = ($offset >= 0) ?
" OFFSET $offset" : '';
57 $limitStr = ($nrows >= 0) ?
" LIMIT $nrows" : ($offset >= 0 ?
' LIMIT 999999999' : '');
59 $rs = $parent->CacheExecute($secs2cache,$sql."$limitStr$offsetStr",$inputarr);
61 $rs = $parent->Execute($sql."$limitStr$offsetStr",$inputarr);
66 function GenID($seq='adodbseq',$start=1)
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!
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));
78 $cnt = $parent->GetOne(sprintf($this->_genSeqCountSQL
,$seq));
80 $ok = $parent->Execute(sprintf($this->_genSeq2SQL
,$seq,$start));
82 if (!$ok) return false;
84 $parent->Execute(sprintf($this->_genIDSQL
,$seq,$num));
86 if ($parent->affected_rows() > 0) {
88 $parent->genID
= intval($num);
92 if ($fn = $parent->raiseErrorFn
) {
93 $fn($parent->databaseType
,'GENID',-32000,"Unable to generate unique id after $MAXLOOPS attempts",$seq,$num);
98 function CreateSequence($seqname='adodbseq',$start=1)
100 $parent = $this->pdoDriver
;
101 $ok = $parent->Execute(sprintf($this->_genSeqSQL
,$seqname));
102 if (!$ok) return false;
104 return $parent->Execute("insert into $seqname values($start)");
107 function SetTransactionMode($transaction_mode)
109 $parent = $this->pdoDriver
;
110 $parent->_transmode
= strtoupper($transaction_mode);
113 function BeginTrans()
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}");
122 function CommitTrans($ok=true)
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;
130 $ret = $parent->Execute('COMMIT');
134 function RollbackTrans()
136 $parent = $this->pdoDriver
;
137 if ($parent->transOff
) return true;
138 if ($parent->transCnt
) $parent->transCnt
-= 1;
139 $parent->_autocommit
= true;
141 $ret = $parent->Execute('ROLLBACK');
147 function MetaColumns($tab,$normalize=true)
149 global $ADODB_FETCH_MODE;
151 $parent = $this->pdoDriver
;
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);
159 $ADODB_FETCH_MODE = $save;
163 while ($r = $rs->FetchRow()) {
164 $type = explode('(',$r['type']);
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'];
177 if ($save == ADODB_FETCH_NUM
) $arr[] = $fld;
178 else $arr[strtoupper($fld->name
)] = $fld;
181 $ADODB_FETCH_MODE = $save;
185 function MetaTables($ttype=false,$showSchema=false,$mask=false)
187 $parent = $this->pdoDriver
;
190 $save = $this->metaTablesSQL
;
191 $mask = $this->qstr(strtoupper($mask));
192 $this->metaTablesSQL
.= " AND name LIKE $mask";
195 $ret = $parent->GetCol($this->metaTablesSQL
);
198 $this->metaTablesSQL
= $save;