Direct Messaging update: add CCR transmit, more meaningful attachment filenames
[openemr.git] / library / adodb / drivers / adodb-pdo_sqlite.inc.php
blob80c954866cb692dd9c2ff69e9967b16147819257
1 <?php
3 /*
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';
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())';
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;
40 function ServerInfo()
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;
50 return $arr;
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' : '');
58 if ($secs2cache)
59 $rs = $parent->CacheExecute($secs2cache,$sql."$limitStr$offsetStr",$inputarr);
60 else
61 $rs = $parent->Execute($sql."$limitStr$offsetStr",$inputarr);
63 return $rs;
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!
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));
82 if (!$ok) return false;
84 $parent->Execute(sprintf($this->_genIDSQL,$seq,$num));
86 if ($parent->affected_rows() > 0) {
87 $num += 1;
88 $parent->genID = intval($num);
89 return intval($num);
92 if ($fn = $parent->raiseErrorFn) {
93 $fn($parent->databaseType,'GENID',-32000,"Unable to generate unique id after $MAXLOOPS attempts",$seq,$num);
95 return false;
98 function CreateSequence($seqname='adodbseq',$start=1)
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)");
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');
131 return $ret;
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');
142 return $ret;
146 // mark newnham
147 function MetaColumns($tab,$normalize=true)
149 global $ADODB_FETCH_MODE;
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;
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;
180 $rs->Close();
181 $ADODB_FETCH_MODE = $save;
182 return $arr;
185 function MetaTables($ttype=false,$showSchema=false,$mask=false)
187 $parent = $this->pdoDriver;
189 if ($mask) {
190 $save = $this->metaTablesSQL;
191 $mask = $this->qstr(strtoupper($mask));
192 $this->metaTablesSQL .= " AND name LIKE $mask";
195 $ret = $parent->GetCol($this->metaTablesSQL);
197 if ($mask) {
198 $this->metaTablesSQL = $save;
200 return $ret;