19 if (!defined(
'ADODB_DIR'))
die();
29 var
$metaTablesSQL =
"SELECT name FROM sqlite_master WHERE type='table' ORDER BY name";
49 $arr[
'version'] = sqlite_libversion();
50 $arr[
'description'] =
'SQLite ';
51 $arr[
'encoding'] = sqlite_libencoding();
57 if ($this->transOff)
return true;
58 $ret = $this->Execute(
"BEGIN TRANSACTION");
65 if ($this->transOff)
return true;
67 $ret = $this->Execute(
"COMMIT");
68 if ($this->transCnt>0)$this->transCnt -= 1;
74 if ($this->transOff)
return true;
75 $ret = $this->Execute(
"ROLLBACK");
76 if ($this->transCnt>0)$this->transCnt -= 1;
83 global $ADODB_FETCH_MODE;
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);
91 $ADODB_FETCH_MODE = $save;
95 while ($r =
$rs->FetchRow()) {
96 $type = explode(
'(',$r[
'type']);
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'];
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;
113 $ADODB_FETCH_MODE = $save;
120 $parentDriver->hasTransactions =
false;
121 $parentDriver->hasInsertID =
true;
126 return sqlite_last_insert_rowid($this->_connectionID);
131 return sqlite_changes($this->_connectionID);
136 if ($this->_logsql)
return $this->_errorMsg;
137 return ($this->_errorNo) ? sqlite_error_string($this->_errorNo) :
'';
147 $fmt = $this->qstr($fmt);
148 return ($col) ?
"adodb_date2($fmt,$col)" :
"adodb_date($fmt)";
154 @sqlite_create_function($this->_connectionID,
'adodb_date',
'adodb_date', 1);
155 @sqlite_create_function($this->_connectionID,
'adodb_date2',
'adodb_date2', 2);
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;
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;
186 $rez = sqlite_query(
$sql,$this->_connectionID);
188 $this->_errorNo = sqlite_last_error($this->_connectionID);
196 $offsetStr = ($offset >= 0) ?
" OFFSET $offset" :
'';
197 $limitStr = ($nrows >= 0) ?
" LIMIT $nrows" : ($offset >= 0 ?
' LIMIT 999999999' :
'');
199 $rs = $this->CacheExecute($secs2cache,
$sql.
"$limitStr$offsetStr",$inputarr);
201 $rs = $this->Execute(
$sql.
"$limitStr$offsetStr",$inputarr);
214 function GenID($seq=
'adodbseq',$start=1)
220 while (--$MAXLOOPS>=0) {
221 @($num = $this->GetOne(
"select id from $seq"));
222 if ($num ===
false) {
223 $this->Execute(sprintf($this->_genSeqSQL ,$seq));
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) {
237 if ($fn = $this->raiseErrorFn) {
238 $fn($this->databaseType,
'GENID',-32000,
"Unable to generate unique id after $MAXLOOPS attempts",$seq,$num);
245 if (empty($this->_genSeqSQL))
return false;
246 $ok = $this->Execute(sprintf($this->_genSeqSQL,$seqname));
247 if (!$ok)
return false;
249 return $this->Execute(
"insert into $seqname values($start)");
255 if (empty($this->_dropSeqSQL))
return false;
256 return $this->Execute(sprintf($this->_dropSeqSQL,$seqname));
262 return @sqlite_close($this->_connectionID);
265 function MetaIndexes($table, $primary = FALSE, $owner=
false, $owner =
false)
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)) {
279 $this->SetFetchMode($savem);
280 $ADODB_FETCH_MODE = $save;
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());
299 $cols = explode(
"(",$row[1]);
300 $cols = explode(
")",$cols[1]);
302 $indexes[$row[0]][
'columns'] = $cols;
305 $this->SetFetchMode($savem);
306 $ADODB_FETCH_MODE = $save;
325 if ($mode ===
false) {
326 global $ADODB_FETCH_MODE;
327 $mode = $ADODB_FETCH_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();
341 $this->_currentRow = 0;
342 $this->EOF = !$this->_fetch();
345 $this->_numOfRows = 0;
346 $this->_numOfFields = 0;
350 return $this->_queryID;
356 $fld =
new ADOFieldObject;
357 $fld->name = sqlite_field_name($this->_queryID, $fieldOffset);
358 $fld->type =
'VARCHAR';
359 $fld->max_length = -1;
365 $this->_numOfRows = @sqlite_num_rows($this->_queryID);
366 $this->_numOfFields = @sqlite_num_fields($this->_queryID);
371 if ($this->fetchMode != SQLITE_NUM)
return $this->fields[$colname];
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)]];
385 return sqlite_seek($this->_queryID, $row);
390 $this->fields = @sqlite_fetch_array($this->_queryID,$this->fetchMode);
391 return !empty($this->fields);
_fetch($ignore_fields=false)
$hasAffectedRows
supports autoincrement ID?
_connect($argHostname, $argUsername, $argPassword, $argDatabasename)
SelectLimit($sql, $nrows=-1, $offset=-1, $inputarr=false, $secs2cache=0)
if(isset($_REQUEST['nrows'])) else $rs
CreateSequence($seqname='adodbseq', $start=1)
FetchField($fieldOffset=-1)
$metaTablesSQL
supports affected rows for update/delete?
_query($sql, $inputarr=false)
MetaIndexes($table, $primary=FALSE, $owner=false, $owner=false)
SQLDate($fmt, $col=false)
ADORecordset_sqlite($queryID, $mode=false)
MetaColumns($table, $normalize=true)
GenID($seq='adodbseq', $start=1)
_pconnect($argHostname, $argUsername, $argPassword, $argDatabasename)