19 if (!defined(
'ADODB_DIR'))
die();
31 var
$metaTablesSQL =
"SELECT name FROM sqlite_master WHERE type='table' ORDER BY name";
52 $arr[
'version'] = $this->_connectionID->version();
53 $arr[
'description'] =
'SQLite 3';
60 if ($this->transOff)
return true;
61 $ret = $this->Execute(
"BEGIN TRANSACTION");
68 if ($this->transOff)
return true;
70 $ret = $this->Execute(
"COMMIT");
71 if ($this->transCnt>0)$this->transCnt -= 1;
77 if ($this->transOff)
return true;
78 $ret = $this->Execute(
"ROLLBACK");
79 if ($this->transCnt>0)$this->transCnt -= 1;
86 global $ADODB_FETCH_MODE;
88 $save = $ADODB_FETCH_MODE;
89 $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
90 if ($this->fetchMode !==
false) $savem = $this->SetFetchMode(
false);
91 $rs = $this->Execute(
"PRAGMA table_info('$table')");
92 if (isset($savem)) $this->SetFetchMode($savem);
94 $ADODB_FETCH_MODE = $save;
98 while ($r =
$rs->FetchRow()) {
100 $type = explode(
'(',$r[
'type']);
102 if (
sizeof($type)==2)
103 $size = trim($type[1],
')');
104 $fn = strtoupper($r[
'name']);
105 $fld =
new ADOFieldObject;
106 $fld->name = $r[
'name'];
107 $fld->type = $type[0];
108 $fld->max_length = $size;
109 $fld->not_null = $r[
'notnull'];
110 $fld->default_value = $r[
'dflt_value'];
112 if (isset($r[
'pk']) && $r[
'pk']) $fld->primary_key=1;
113 if ($save == ADODB_FETCH_NUM) $arr[] = $fld;
114 else $arr[strtoupper($fld->name)] = $fld;
117 $ADODB_FETCH_MODE = $save;
124 $parentDriver->hasTransactions =
false;
125 $parentDriver->hasInsertID =
true;
131 return $this->_connectionID->lastInsertRowID();
136 return $this->_connectionID->changes();
142 if ($this->_logsql)
return $this->_errorMsg;
144 return ($this->_errorNo) ? $this->
ErrorNo() :
'';
149 return $this->_connectionID->lastErrorCode();
154 $fmt = $this->qstr($fmt);
155 return ($col) ?
"adodb_date2($fmt,$col)" :
"adodb_date($fmt)";
162 $this->_connectionID->createFunction(
'adodb_date',
'adodb_date', 1);
165 $this->_connectionID->createFunction(
'adodb_date2',
'adodb_date2', 2);
170 function _connect($argHostname, $argUsername, $argPassword, $argDatabasename)
172 if (empty($argHostname) && $argDatabasename) $argHostname = $argDatabasename;
173 $this->_connectionID =
new SQLite3($argDatabasename);
189 function _pconnect($argHostname, $argUsername, $argPassword, $argDatabasename)
191 if (!function_exists(
'sqlite_open'))
return null;
192 if (empty($argHostname) && $argDatabasename) $argHostname = $argDatabasename;
194 $this->_connectionID = sqlite_popen($argHostname);
195 if ($this->_connectionID ===
false)
return false;
204 $rez = $this->_connectionID->query(
$sql);
207 $this->_connectionID->lastErrorCode();
215 $offsetStr = ($offset >= 0) ?
" OFFSET $offset" :
'';
216 $limitStr = ($nrows >= 0) ?
" LIMIT $nrows" : ($offset >= 0 ?
' LIMIT 999999999' :
'');
218 $rs = $this->CacheExecute($secs2cache,
$sql.
"$limitStr$offsetStr",$inputarr);
220 $rs = $this->Execute(
$sql.
"$limitStr$offsetStr",$inputarr);
233 function GenID($seq=
'adodbseq',$start=1)
239 while (--$MAXLOOPS>=0) {
240 @($num = $this->GetOne(
"select id from $seq"));
241 if ($num ===
false) {
242 $this->Execute(sprintf($this->_genSeqSQL ,$seq));
245 $ok = $this->Execute(
"insert into $seq values($start)");
246 if (!$ok)
return false;
248 $this->Execute(
"update $seq set id=id+1 where id=$num");
250 if ($this->affected_rows() > 0) {
256 if ($fn = $this->raiseErrorFn) {
257 $fn($this->databaseType,
'GENID',-32000,
"Unable to generate unique id after $MAXLOOPS attempts",$seq,$num);
264 if (empty($this->_genSeqSQL))
return false;
265 $ok = $this->Execute(sprintf($this->_genSeqSQL,$seqname));
266 if (!$ok)
return false;
268 return $this->Execute(
"insert into $seqname values($start)");
274 if (empty($this->_dropSeqSQL))
return false;
275 return $this->Execute(sprintf($this->_dropSeqSQL,$seqname));
282 return $this->_connectionID->close();
285 function MetaIndexes($table, $primary = FALSE, $owner=
false, $owner =
false)
289 global $ADODB_FETCH_MODE;
290 $save = $ADODB_FETCH_MODE;
291 $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
292 if ($this->fetchMode !== FALSE) {
293 $savem = $this->SetFetchMode(FALSE);
295 $SQL=sprintf(
"SELECT name,sql FROM sqlite_master WHERE type='index' AND tbl_name='%s'", strtolower($table));
296 $rs = $this->Execute($SQL);
297 if (!is_object(
$rs)) {
299 $this->SetFetchMode($savem);
300 $ADODB_FETCH_MODE = $save;
305 while ($row =
$rs->FetchRow()) {
306 if ($primary && preg_match(
"/primary/i",$row[1]) == 0)
continue;
307 if (!isset($indexes[$row[0]])) {
309 $indexes[$row[0]] = array(
310 'unique' => preg_match(
"/unique/i",$row[1]),
311 'columns' => array());
319 $cols = explode(
"(",$row[1]);
320 $cols = explode(
")",$cols[1]);
322 $indexes[$row[0]][
'columns'] = $cols;
325 $this->SetFetchMode($savem);
326 $ADODB_FETCH_MODE = $save;
348 if ($mode ===
false) {
349 global $ADODB_FETCH_MODE;
350 $mode = $ADODB_FETCH_MODE;
354 case ADODB_FETCH_NUM: $this->fetchMode = SQLITE3_NUM;
break;
356 case ADODB_FETCH_ASSOC: $this->fetchMode = SQLITE3_ASSOC;
break;
358 default: $this->fetchMode = SQLITE3_BOTH;
break;
360 $this->adodbFetchMode = $mode;
362 $this->_queryID = $queryID;
364 $this->_inited =
true;
365 $this->fields = array();
367 $this->_currentRow = 0;
368 $this->EOF = !$this->_fetch();
371 $this->_numOfRows = 0;
372 $this->_numOfFields = 0;
376 return $this->_queryID;
382 $fld =
new ADOFieldObject;
384 $fld->name->columnName($this->_queryID, $fieldOffset);
385 $fld->type =
'VARCHAR';
386 $fld->max_length = -1;
393 $this->_numOfRows = 1;
395 $this->_numOfFields = $this->_queryID->numColumns();
402 if ($this->fetchMode != SQLITE3_NUM)
return $this->fields[$colname];
404 $this->bind = array();
405 for ($i=0; $i < $this->_numOfFields; $i++) {
406 $o = $this->FetchField($i);
407 $this->bind[strtoupper($o->name)] = $i;
411 return $this->fields[$this->bind[strtoupper($colname)]];
416 return sqlite3_seek($this->_queryID, $row);
422 $this->fields = $this->_queryID->fetchArray($this->fetchMode);
423 return !empty($this->fields);
FetchField($fieldOffset=-1)
_fetch($ignore_fields=false)
$hasAffectedRows
supports autoincrement ID?
if(isset($_REQUEST['nrows'])) else $rs
MetaIndexes($table, $primary=FALSE, $owner=false, $owner=false)
$metaTablesSQL
supports affected rows for update/delete?
_query($sql, $inputarr=false)
ADORecordset_sqlite3($queryID, $mode=false)
_connect($argHostname, $argUsername, $argPassword, $argDatabasename)
SelectLimit($sql, $nrows=-1, $offset=-1, $inputarr=false, $secs2cache=0)
GenID($seq='adodbseq', $start=1)
SQLDate($fmt, $col=false)
_pconnect($argHostname, $argUsername, $argPassword, $argDatabasename)
MetaColumns($table, $normalize=true)
CreateSequence($seqname='adodbseq', $start=1)