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