<?php class SQLiteErrorException extends Exception {
public $errno;
public $errstring;
public $query;
function __construct ($errno, $errstring, $query) {
$this->errno = $errno;
$this->errstring = $errstring;
$this->query = $query;
}
function __toString () { return 'errno (' . $this->errno . '), errstring (' . $this->errstring . '), query (' . $this->query . ')'; }
}
class NoDatabaseException extends Exception {};
class RecordNotFound extends Exception {};
class SQLiteConnect implements IteratorAggregate {
public $dbhandle;
public $lastoffset = 0;
protected $lastresult;
static function createIfNotExist ($dbpath, $creation=null) {
if(!file_exists ($dbpath)) {
$n = new SQLiteConnect (sqlite_open ($dbpath));
if($creation) foreach ($creation as $table => $columndef) $n->query ('CREATE TABLE "' . $table . '" (' . $columndef . ')');
return $n;
}
else return new SQLiteConnect ($dbpath);
}
function __construct ($db) {
if(is_resource ($db)) $this->dbhandle = $db;
else {
if(file_exists ($db)) $this->dbhandle = sqlite_open ($db);
else throw new NoDatabaseException ($db);
}
}
function query ($query, $offset=null) {
$n = sqlite_query ($this->dbhandle, $query);
if($errno = sqlite_last_error ($this->dbhandle)) throw new SQLiteErrorException ($errno, sqlite_error_string ($errno), $query);
$this->lastoffset = $offset ? (int) $offset : 0;
return $this->lastresult = $n;
}
function fetch ($mixed=null) {
if(is_null ($mixed)) $mixed = $this->lastresult;
else if(is_string ($mixed)) $mixed = $this->query ($mixed); // string query
return sqlite_fetch_array ($mixed, SQLITE_ASSOC);
}
function replace ($table, $assoc) { $this->insert ($table, $assoc, true); }
function insert ($table, $assoc, $replace = false) {
foreach ($assoc as $key => $value) {
$keys [] = "'" . $key . "'";
$sql [] = "'" . sqlite_escape_string ($value) . "'";
}
$this->query (($replace ? 'REPLACE' : 'INSERT') . ' INTO ' . $table . ' (' . implode (', ', $keys) . ') VALUES (' . implode (', ', $sql) . ')');
return sqlite_last_insert_rowid ($this->dbhandle);
}
function update ($table, $assoc, $id) {
foreach ($assoc as $key => $value) $sql [$key] = '"' . $key . '"=\'' . sqlite_escape_string ($value) . '\'';
$this->query ('UPDATE "' . $table . '" SET ' . implode (', ', $sql) . ' WHERE "id"="' . $id . '"');
return sqlite_changes ($this->dbhandle) ? true : false;
}
function delete ($table, $id) {
$this->query ('DELETE FROM "' . $table . '" WHERE "id"="' . $id . '"');
return sqlite_changes ($this->dbhandle) ? true : false;
}
function getIterator () {
return new SQLiteIterator ($this->lastresult, $this->lastoffset);
}
}
class SQLiteIterator implements Iterator, SeekableIterator {
public $result;
public $offset = 0;
public $position;
protected $current;
function __construct ($result, $offset=null) {
$this->offset = $offset;
$this->result = $result;
}
function seek ($offset) {
$sqliteoffset = $offset - $this->offset;
$this->rewind ();
if($sqliteoffset < 0 || !@sqlite_seek ($this->result, $sqliteoffset)) throw new RecordNotFound ('offset: ' . $offset);
else {
$this->current = sqlite_current ($this->result, SQLITE_ASSOC);
$this->position = $offset;
}
}
function field ($name) { return isset ($this->current [$name]) ? $this->current [$name] : null; }
function rewind () {
if(is_resource ($this->result) && sqlite_num_rows($this->result)) {
sqlite_rewind ($this->result);
$this->current = sqlite_current ($this->result, SQLITE_ASSOC);
}
if($this->valid ()) $this->position = $this->offset or $this->position = 0;
}
function valid () { return is_resource ($this->result) && !empty ($this->current); }
function next () {
sqlite_next ($this->result);
$this->current = sqlite_current ($this->result, SQLITE_ASSOC);
if($this->valid ()) ++$this->position;
}
function current () { return $this->current; }
function key () { return $this->position; }
} ?>
|
|