Direct Messaging update: add CCR transmit, more meaningful attachment filenames
[openemr.git] / library / adodb / drivers / adodb-sqlite.inc.php
blobd45d21b1da3256eb39c0406caad9909a13db9ddc
1 <?php
2 /*
3 V5.14 8 Sept 2011 (c) 2000-2011 John Lim (jlim#natsoft.com). All rights reserved.
4 Released under both BSD license and Lesser GPL library license.
5 Whenever there is any discrepancy between the two licenses,
6 the BSD license will take precedence.
8 Latest version is available at http://adodb.sourceforge.net
10 SQLite info: http://www.hwaci.com/sw/sqlite/
12 Install Instructions:
13 ====================
14 1. Place this in adodb/drivers
15 2. Rename the file, remove the .txt prefix.
18 // security - hide paths
19 if (!defined('ADODB_DIR')) die();
21 class ADODB_sqlite extends ADOConnection {
22 var $databaseType = "sqlite";
23 var $replaceQuote = "''"; // string to use to replace quotes
24 var $concat_operator='||';
25 var $_errorNo = 0;
26 var $hasLimit = true;
27 var $hasInsertID = true; /// supports autoincrement ID?
28 var $hasAffectedRows = true; /// supports affected rows for update/delete?
29 var $metaTablesSQL = "SELECT name FROM sqlite_master WHERE type='table' ORDER BY name";
30 var $sysDate = "adodb_date('Y-m-d')";
31 var $sysTimeStamp = "adodb_date('Y-m-d H:i:s')";
32 var $fmtTimeStamp = "'Y-m-d H:i:s'";
34 function ADODB_sqlite()
39 function __get($name)
41 switch($name) {
42 case 'sysDate': return "'".date($this->fmtDate)."'";
43 case 'sysTimeStamp' : return "'".date($this->sysTimeStamp)."'";
45 }*/
47 function ServerInfo()
49 $arr['version'] = sqlite_libversion();
50 $arr['description'] = 'SQLite ';
51 $arr['encoding'] = sqlite_libencoding();
52 return $arr;
55 function BeginTrans()
57 if ($this->transOff) return true;
58 $ret = $this->Execute("BEGIN TRANSACTION");
59 $this->transCnt += 1;
60 return true;
63 function CommitTrans($ok=true)
65 if ($this->transOff) return true;
66 if (!$ok) return $this->RollbackTrans();
67 $ret = $this->Execute("COMMIT");
68 if ($this->transCnt>0)$this->transCnt -= 1;
69 return !empty($ret);
72 function RollbackTrans()
74 if ($this->transOff) return true;
75 $ret = $this->Execute("ROLLBACK");
76 if ($this->transCnt>0)$this->transCnt -= 1;
77 return !empty($ret);
80 // mark newnham
81 function MetaColumns($table, $normalize=true)
83 global $ADODB_FETCH_MODE;
84 $false = false;
85 $save = $ADODB_FETCH_MODE;
86 $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
87 if ($this->fetchMode !== false) $savem = $this->SetFetchMode(false);
88 $rs = $this->Execute("PRAGMA table_info('$table')");
89 if (isset($savem)) $this->SetFetchMode($savem);
90 if (!$rs) {
91 $ADODB_FETCH_MODE = $save;
92 return $false;
94 $arr = array();
95 while ($r = $rs->FetchRow()) {
96 $type = explode('(',$r['type']);
97 $size = '';
98 if (sizeof($type)==2)
99 $size = trim($type[1],')');
100 $fn = strtoupper($r['name']);
101 $fld = new ADOFieldObject;
102 $fld->name = $r['name'];
103 $fld->type = $type[0];
104 $fld->max_length = $size;
105 $fld->not_null = $r['notnull'];
106 $fld->default_value = $r['dflt_value'];
107 $fld->scale = 0;
108 if (isset($r['pk']) && $r['pk']) $fld->primary_key=1;
109 if ($save == ADODB_FETCH_NUM) $arr[] = $fld;
110 else $arr[strtoupper($fld->name)] = $fld;
112 $rs->Close();
113 $ADODB_FETCH_MODE = $save;
114 return $arr;
117 function _init($parentDriver)
120 $parentDriver->hasTransactions = false;
121 $parentDriver->hasInsertID = true;
124 function _insertid()
126 return sqlite_last_insert_rowid($this->_connectionID);
129 function _affectedrows()
131 return sqlite_changes($this->_connectionID);
134 function ErrorMsg()
136 if ($this->_logsql) return $this->_errorMsg;
137 return ($this->_errorNo) ? sqlite_error_string($this->_errorNo) : '';
140 function ErrorNo()
142 return $this->_errorNo;
145 function SQLDate($fmt, $col=false)
147 $fmt = $this->qstr($fmt);
148 return ($col) ? "adodb_date2($fmt,$col)" : "adodb_date($fmt)";
152 function _createFunctions()
154 @sqlite_create_function($this->_connectionID, 'adodb_date', 'adodb_date', 1);
155 @sqlite_create_function($this->_connectionID, 'adodb_date2', 'adodb_date2', 2);
159 // returns true or false
160 function _connect($argHostname, $argUsername, $argPassword, $argDatabasename)
162 if (!function_exists('sqlite_open')) return null;
163 if (empty($argHostname) && $argDatabasename) $argHostname = $argDatabasename;
165 $this->_connectionID = sqlite_open($argHostname);
166 if ($this->_connectionID === false) return false;
167 $this->_createFunctions();
168 return true;
171 // returns true or false
172 function _pconnect($argHostname, $argUsername, $argPassword, $argDatabasename)
174 if (!function_exists('sqlite_open')) return null;
175 if (empty($argHostname) && $argDatabasename) $argHostname = $argDatabasename;
177 $this->_connectionID = sqlite_popen($argHostname);
178 if ($this->_connectionID === false) return false;
179 $this->_createFunctions();
180 return true;
183 // returns query ID if successful, otherwise false
184 function _query($sql,$inputarr=false)
186 $rez = sqlite_query($sql,$this->_connectionID);
187 if (!$rez) {
188 $this->_errorNo = sqlite_last_error($this->_connectionID);
191 return $rez;
194 function SelectLimit($sql,$nrows=-1,$offset=-1,$inputarr=false,$secs2cache=0)
196 $offsetStr = ($offset >= 0) ? " OFFSET $offset" : '';
197 $limitStr = ($nrows >= 0) ? " LIMIT $nrows" : ($offset >= 0 ? ' LIMIT 999999999' : '');
198 if ($secs2cache)
199 $rs = $this->CacheExecute($secs2cache,$sql."$limitStr$offsetStr",$inputarr);
200 else
201 $rs = $this->Execute($sql."$limitStr$offsetStr",$inputarr);
203 return $rs;
207 This algorithm is not very efficient, but works even if table locking
208 is not available.
210 Will return false if unable to generate an ID after $MAXLOOPS attempts.
212 var $_genSeqSQL = "create table %s (id integer)";
214 function GenID($seq='adodbseq',$start=1)
216 // if you have to modify the parameter below, your database is overloaded,
217 // or you need to implement generation of id's yourself!
218 $MAXLOOPS = 100;
219 //$this->debug=1;
220 while (--$MAXLOOPS>=0) {
221 @($num = $this->GetOne("select id from $seq"));
222 if ($num === false) {
223 $this->Execute(sprintf($this->_genSeqSQL ,$seq));
224 $start -= 1;
225 $num = '0';
226 $ok = $this->Execute("insert into $seq values($start)");
227 if (!$ok) return false;
229 $this->Execute("update $seq set id=id+1 where id=$num");
231 if ($this->affected_rows() > 0) {
232 $num += 1;
233 $this->genID = $num;
234 return $num;
237 if ($fn = $this->raiseErrorFn) {
238 $fn($this->databaseType,'GENID',-32000,"Unable to generate unique id after $MAXLOOPS attempts",$seq,$num);
240 return false;
243 function CreateSequence($seqname='adodbseq',$start=1)
245 if (empty($this->_genSeqSQL)) return false;
246 $ok = $this->Execute(sprintf($this->_genSeqSQL,$seqname));
247 if (!$ok) return false;
248 $start -= 1;
249 return $this->Execute("insert into $seqname values($start)");
252 var $_dropSeqSQL = 'drop table %s';
253 function DropSequence($seqname)
255 if (empty($this->_dropSeqSQL)) return false;
256 return $this->Execute(sprintf($this->_dropSeqSQL,$seqname));
259 // returns true or false
260 function _close()
262 return @sqlite_close($this->_connectionID);
265 function MetaIndexes($table, $primary = FALSE, $owner=false, $owner = false)
267 $false = false;
268 // save old fetch mode
269 global $ADODB_FETCH_MODE;
270 $save = $ADODB_FETCH_MODE;
271 $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
272 if ($this->fetchMode !== FALSE) {
273 $savem = $this->SetFetchMode(FALSE);
275 $SQL=sprintf("SELECT name,sql FROM sqlite_master WHERE type='index' AND tbl_name='%s'", strtolower($table));
276 $rs = $this->Execute($SQL);
277 if (!is_object($rs)) {
278 if (isset($savem))
279 $this->SetFetchMode($savem);
280 $ADODB_FETCH_MODE = $save;
281 return $false;
284 $indexes = array ();
285 while ($row = $rs->FetchRow()) {
286 if ($primary && preg_match("/primary/i",$row[1]) == 0) continue;
287 if (!isset($indexes[$row[0]])) {
289 $indexes[$row[0]] = array(
290 'unique' => preg_match("/unique/i",$row[1]),
291 'columns' => array());
294 * There must be a more elegant way of doing this,
295 * the index elements appear in the SQL statement
296 * in cols[1] between parentheses
297 * e.g CREATE UNIQUE INDEX ware_0 ON warehouse (org,warehouse)
299 $cols = explode("(",$row[1]);
300 $cols = explode(")",$cols[1]);
301 array_pop($cols);
302 $indexes[$row[0]]['columns'] = $cols;
304 if (isset($savem)) {
305 $this->SetFetchMode($savem);
306 $ADODB_FETCH_MODE = $save;
308 return $indexes;
313 /*--------------------------------------------------------------------------------------
314 Class Name: Recordset
315 --------------------------------------------------------------------------------------*/
317 class ADORecordset_sqlite extends ADORecordSet {
319 var $databaseType = "sqlite";
320 var $bind = false;
322 function ADORecordset_sqlite($queryID,$mode=false)
325 if ($mode === false) {
326 global $ADODB_FETCH_MODE;
327 $mode = $ADODB_FETCH_MODE;
329 switch($mode) {
330 case ADODB_FETCH_NUM: $this->fetchMode = SQLITE_NUM; break;
331 case ADODB_FETCH_ASSOC: $this->fetchMode = SQLITE_ASSOC; break;
332 default: $this->fetchMode = SQLITE_BOTH; break;
334 $this->adodbFetchMode = $mode;
336 $this->_queryID = $queryID;
338 $this->_inited = true;
339 $this->fields = array();
340 if ($queryID) {
341 $this->_currentRow = 0;
342 $this->EOF = !$this->_fetch();
343 @$this->_initrs();
344 } else {
345 $this->_numOfRows = 0;
346 $this->_numOfFields = 0;
347 $this->EOF = true;
350 return $this->_queryID;
354 function FetchField($fieldOffset = -1)
356 $fld = new ADOFieldObject;
357 $fld->name = sqlite_field_name($this->_queryID, $fieldOffset);
358 $fld->type = 'VARCHAR';
359 $fld->max_length = -1;
360 return $fld;
363 function _initrs()
365 $this->_numOfRows = @sqlite_num_rows($this->_queryID);
366 $this->_numOfFields = @sqlite_num_fields($this->_queryID);
369 function Fields($colname)
371 if ($this->fetchMode != SQLITE_NUM) return $this->fields[$colname];
372 if (!$this->bind) {
373 $this->bind = array();
374 for ($i=0; $i < $this->_numOfFields; $i++) {
375 $o = $this->FetchField($i);
376 $this->bind[strtoupper($o->name)] = $i;
380 return $this->fields[$this->bind[strtoupper($colname)]];
383 function _seek($row)
385 return sqlite_seek($this->_queryID, $row);
388 function _fetch($ignore_fields=false)
390 $this->fields = @sqlite_fetch_array($this->_queryID,$this->fetchMode);
391 return !empty($this->fields);
394 function _close()