Updated adodb syysext to upstream version 5.0.8a.
[Packages/TYPO3.CMS.git] / typo3 / sysext / adodb / adodb / drivers / adodb-pdo_sqlite.inc.php
1 <?php
2
3 /*
4 V5.08 6 Apr 2009 (c) 2000-2009 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
31 function _init($parentDriver)
32 {
33 $this->pdoDriver = $parentDriver;
34 $parentDriver->_bindInputArray = true;
35 $parentDriver->hasTransactions = true;
36 $parentDriver->hasInsertID = true;
37 }
38
39 function ServerInfo()
40 {
41 $parent = $this->pdoDriver;
42 @($ver = array_pop($parent->GetCol("SELECT sqlite_version()")));
43 @($end = array_pop($parent->GetCol("PRAGMA encoding")));
44
45 $arr['version'] = $ver;
46 $arr['description'] = 'SQLite ';
47 $arr['encoding'] = $enc;
48
49 return $arr;
50 }
51
52 function SelectLimit($sql,$nrows=-1,$offset=-1,$inputarr=false,$secs2cache=0)
53 {
54 $parent = $this->pdoDriver;
55 $offsetStr = ($offset >= 0) ? " OFFSET $offset" : '';
56 $limitStr = ($nrows >= 0) ? " LIMIT $nrows" : ($offset >= 0 ? ' LIMIT 999999999' : '');
57 if ($secs2cache)
58 $rs = $parent->CacheExecute($secs2cache,$sql."$limitStr$offsetStr",$inputarr);
59 else
60 $rs = $parent->Execute($sql."$limitStr$offsetStr",$inputarr);
61
62 return $rs;
63 }
64
65 function GenID($seq='adodbseq',$start=1)
66 {
67 $parent = $this->pdoDriver;
68 // if you have to modify the parameter below, your database is overloaded,
69 // or you need to implement generation of id's yourself!
70 $MAXLOOPS = 100;
71 while (--$MAXLOOPS>=0) {
72 @($num = array_pop($parent->GetCol("SELECT id FROM {$seq}")));
73 if ($num === false || !is_numeric($num)) {
74 @$parent->Execute(sprintf($this->_genSeqSQL ,$seq));
75 $start -= 1;
76 $num = '0';
77 $cnt = $parent->GetOne(sprintf($this->_genSeqCountSQL,$seq));
78 if (!$cnt) {
79 $ok = $parent->Execute(sprintf($this->_genSeq2SQL,$seq,$start));
80 }
81 if (!$ok) return false;
82 }
83 $parent->Execute(sprintf($this->_genIDSQL,$seq,$num));
84
85 if ($parent->affected_rows() > 0) {
86 $num += 1;
87 $parent->genID = intval($num);
88 return intval($num);
89 }
90 }
91 if ($fn = $parent->raiseErrorFn) {
92 $fn($parent->databaseType,'GENID',-32000,"Unable to generate unique id after $MAXLOOPS attempts",$seq,$num);
93 }
94 return false;
95 }
96
97 function CreateSequence($seqname='adodbseq',$start=1)
98 {
99 $parent = $this->pdoDriver;
100 $ok = $parent->Execute(sprintf($this->_genSeqSQL,$seqname));
101 if (!$ok) return false;
102 $start -= 1;
103 return $parent->Execute("insert into $seqname values($start)");
104 }
105
106 function SetTransactionMode($transaction_mode)
107 {
108 $parent = $this->pdoDriver;
109 $parent->_transmode = strtoupper($transaction_mode);
110 }
111
112 function BeginTrans()
113 {
114 $parent = $this->pdoDriver;
115 if ($parent->transOff) return true;
116 $parent->transCnt += 1;
117 $parent->_autocommit = false;
118 return $parent->Execute("BEGIN {$parent->_transmode}");
119 }
120
121 function CommitTrans($ok=true)
122 {
123 $parent = $this->pdoDriver;
124 if ($parent->transOff) return true;
125 if (!$ok) return $parent->RollbackTrans();
126 if ($parent->transCnt) $parent->transCnt -= 1;
127 $parent->_autocommit = true;
128
129 $ret = $parent->Execute('COMMIT');
130 return $ret;
131 }
132
133 function RollbackTrans()
134 {
135 $parent = $this->pdoDriver;
136 if ($parent->transOff) return true;
137 if ($parent->transCnt) $parent->transCnt -= 1;
138 $parent->_autocommit = true;
139
140 $ret = $parent->Execute('ROLLBACK');
141 return $ret;
142 }
143
144
145 // mark newnham
146 function MetaColumns($tab,$normalize=true))
147 {
148 global $ADODB_FETCH_MODE;
149
150 $parent = $this->pdoDriver;
151 $false = false;
152 $save = $ADODB_FETCH_MODE;
153 $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
154 if ($parent->fetchMode !== false) $savem = $parent->SetFetchMode(false);
155 $rs = $parent->Execute("PRAGMA table_info('$tab')");
156 if (isset($savem)) $parent->SetFetchMode($savem);
157 if (!$rs) {
158 $ADODB_FETCH_MODE = $save;
159 return $false;
160 }
161 $arr = array();
162 while ($r = $rs->FetchRow()) {
163 $type = explode('(',$r['type']);
164 $size = '';
165 if (sizeof($type)==2)
166 $size = trim($type[1],')');
167 $fn = strtoupper($r['name']);
168 $fld = new ADOFieldObject;
169 $fld->name = $r['name'];
170 $fld->type = $type[0];
171 $fld->max_length = $size;
172 $fld->not_null = $r['notnull'];
173 $fld->primary_key = $r['pk'];
174 $fld->default_value = $r['dflt_value'];
175 $fld->scale = 0;
176 if ($save == ADODB_FETCH_NUM) $arr[] = $fld;
177 else $arr[strtoupper($fld->name)] = $fld;
178 }
179 $rs->Close();
180 $ADODB_FETCH_MODE = $save;
181 return $arr;
182 }
183
184 function MetaTables($ttype=false,$showSchema=false,$mask=false)
185 {
186 $parent = $this->pdoDriver;
187 return $parent->GetCol($this->metaTablesSQL);
188 }
189 }
190 ?>