Source for file EDB_SQLITE3.php
Documentation is available at EDB_SQLITE3.php
* Project: EDB_SQLITE3 :: SQLITE3 abstraction layer
* File: EDB/EDB_SQLITE3.php
* The EDB_SQLITE3 class is sqlite3 abstraction layer that used internally
* @subpackage EDB_ABSTRACT
* @author JoungKyun.Kim <http://oops.org>
* @copyright (c) 2018, JoungKyun.Kim
* @link http://pear.oops.org/package/EDB
* SQLite3 engine for EDB API
* This class support abstracttion DB layer for SQLite3 Engine
* db handler of EDB_SQLITE3 class
* SQLITE3 STMT object of EDB_SQLITE3 class
* The number of query parameter
* The number of query parameter
private $field = array ();
* number of query result rows
// {{{ (object) EDB_SQLITE3::__construct ($path, $flag = SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE)
* Instantiates an EDB_SQLITE3 object and opens an SQLite 3 database
* $db = new EDB_SQLITE3 ('sqlite3:///path/file.db');
* $db = new EDB_SQLITE3 ('sqlite3:///path/file.db', SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE);
* $db = new EDB_SQLITE3 ('sqlite3://:memory:', SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE);
* @param string $path sqlite3 database file
* @param int $flags (optinal) open flags of sqlite3. See also {@link http://manual.phpdoc.org/HTMLSmartyConverter/PHP/phpDocumentor/tutorial_tags.inlinelink.pkg.html SQLite3::__construct}.
throw new myException ('sqlite3 extension is not loaded on PHP!', E_USER_ERROR);
$argv = is_array ($_argv[0]) ? $_argv[0] : $_argv;;
$o->flag = SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE;
// for persistent connection. sqlite3 don't support
$this->db = new SQLite3 ($o->path, $o->flag);
} catch ( Exception $e ) {
throw new myException ($e->getMessage (), $e->getCode(), $e);
// {{{ (string) EDB_SQLITE3::get_charset (void)
* Get character set of current database
* This method is not allow on SQLite3 Engine
* @return string Current character set name
#throw new myException ('Unsupported method on SQLITE3 engine', E_ERROR);
// {{{ (bool) EDB_SQLITE3::set_charset ($charset)
* Set character set of current database
* This method is not allow on SQLite3 Engine, and always
* @return bool always retuns true
* @param string $char name of character set that supported from database
#throw new myException ('Unsupported method on SQLITE3 engine', E_ERROR);
// {{{ (string) EDB_SQLITE3::escape ($string)
* Escape special characters in a string for use in an SQL statement
* @param string The string that is to be escaped.
return $this->db->escapeString ($string);
// {{{ (int) EDB_SQLITE3::query ($query, $param_type, $param1, $param2 ...)
* Performs a query on the database
* Executes an SQL query, returning number of affected rows
* @return integer The number of affected rows or false. If is not delete/insert/update
* query, always returns 0.
* @param string $query The query strings
* @param string $type (optional) Bind parameter type. See also
* {@link http://www.php.net/manual/en/sqlite3stmt.bindparam.php SQLite3Stmt::bindparam()}.
* i => integer SQLITE3_INTEGER
* d => double SQLITE3_FLOAT
* s => string SQLITE3_TEXT
* @param mixed $param1 (optional) Bind parameter 1
* @param mixed $param2,... (optional) Bind parameter 2 ..
$argv = is_array ($_argv[0]) ? $_argv[0] : $_argv;;
// store query in log variable
return $this->no_bind_query ($sql);
return $this->bind_query ($sql, $argv);
} catch ( Exception $e ) {
throw new myException ($e->getMessage (), $e->getCode(), $e);
// {{{ (int) EDB_SQLITE3::lastId (void)
* 가장 마지막 입력 row ID를 반환한다.
return $this->db->lastInsertRowID ();
// {{{ (bool) EDB_SQLITE3::seek ($offset)
* Adjusts the result pointer to an arbitrary row in the result
* @param integer Must be between zero and the total number of rows minus one
function seek ($offset) {
if ( $offset >= $this->nums )
while ( $this->result->fetchArray (SQLITE3_ASSOC) !== false ) {
} catch ( Exception $e ) {
throw new myException ($e->getMessage (), $e->getCode(), $e);
// {{{ (object) EDB_SQLITE3::fetch (void)
* Fetch a result row as an associative object
* @return object The object of fetched a result row or false
* @param boolean (optional) fetch 수행 후 result를 free한다.
* (기본값: false) EDB >= 2.0.3
function fetch ($free = false) {
$r = $this->result->fetchArray (SQLITE3_ASSOC);
return is_array ($r) ? (object) $r : false;
} catch ( Exception $e ) {
throw new myException ($e->getMessage (), $e->getCode(), $e);
// {{{ (array) EDB_SQLITE3::fetch_all ($free = true)
* Fetch all result rows as an associative object
* @return array The fetched result rows
* @param boolean (optional) free result set after fetch.
while ( ($row = $this->result->fetchArray (SQLITE3_ASSOC)) !== false )
} catch ( Exception $e ) {
throw new myException ($e->getMessage (), $e->getCode(), $e);
// {{{ (bool) EDB_SQLITE3::free_result (void)
* Frees stored result memory for the given statement handle
* @return boolean always returns true
if ( ! $this->free ) return true;
if ( $this->stmt instanceof SQLite3Stmt )
} catch ( Exception $e ) {
throw new myException ($e->getMessage (), $e->getCode(), $e);
// {{{ (string) EDB_SQLITE3::field_name ($index)
* Returns the name of the column specified by the column_number.
* @param integer The numeric zero-based index of the column.
return $this->result->columnName ($index);
} catch ( Exception $e ) {
throw new myException ($e->getMessage (), $e->getCode(), $e);
// {{{ (string) EDB_SQLITE3::field_type ($index)
* Get the type of the specified field in a result
* @param integer The numeric zero-based index of the column.
$r = $this->result->columnType ($index);
//throw new myException ('Unknown. This is libsqlite3 bug!', E_USER_WARNING);
return 'unknown, maybe libsqlite3 bug?';
} catch ( Exception $e ) {
throw new myException ($e->getMessage (), $e->getCode(), $e);
// {{{ (int) EDB_SQLITE3::num_fields (void)
* Get number of fields in result
return $this->result->numColumns ();
} catch ( Exception $e ) {
throw new myException ($e->getMessage (), $e->getCode(), $e);
// {{{ (void) EDB_SQLITE3::trstart (void)
$this->db->query ('BEGIN TRANSACTION');
// {{{ (void) EDB_SQLITE3::trend ($v)
* @param bool false일경우 rollback을 수행한다.
function trend ($v = true) {
$sql = ($v === false) ? 'ROLLBACK' : 'COMMIT';
$this->db->query ($sql . ' TRANSACTION');
// {{{ (void) EDB_SQLITE3::close (void)
// {{{ private (int) EDB_SQLITE3::num_rows ()
* Returns the number of rows in the result set
* SQLite3 extension don't support num_rows method. Why???
while ( ($r->fetchArray (SQLITE3_ASSOC)) !== false )
} catch ( Exception $e ) {
throw new myException ($e->getMessage (), $e->getCode(), $e);
// {{{ private (int) EDB_SQLITE3::no_bind_query ($sql)
* Performs a query on the database
* @return integer The number of affected rows or false only update|insert|delete.
* other row is returned -1.
* @param string The query strings
private function no_bind_query ($sql) {
if ( ($this->result = $this->db->query ($sql)) === false ) {
throw new myException ($this->db->lastErrorMsg (), E_USER_WARNING);
} catch ( Exception $e ) {
throw new myException ($e->getMessage (), $e->getCode(), $e);
return $this->db->changes ();
return $this->num_rows ();
// {{{ private (int) EDB_SQLITE3::bind_query ($sql, $parameters)
* Performs a bind query on the database
* @return integer The number of affected rows or false
* @param string The query strings
* @param array (optional) Bind parameter type
private function bind_query ($sql, $params) {
$this->stmt = $this->db->prepare ($sql);
} catch ( Exception $e ) {
throw new myException ($e->getMessage (), $e->getCode(), $e);
if ( $this->pno != count ($params) || $this->check_param ($params) === false ) {
'Number of elements in query doesn\'t match number of bind variables',
for ( $i= 0; $i< count ($params); $i++ )
for ( $i= 1; $i< $this->pno+ 1; $i++ ) {
switch ($param[0][$i- 1]) {
$data = is_object ($param[$i]) ? $param[$i]->data : $param[$i];
$this->stmt->bindParam ($i, $data, SQLITE3_BLOB);
$this->stmt->bindParam ($i, $param[$i], SQLITE3_INTEGER);
$this->stmt->bindParam ($i, $param[$i], SQLITE3_FLOAT);
$this->stmt->bindParam ($i, $param[$i], SQLITE3_NULL);
$this->stmt->bindParam ($i, $param[$i]);
$this->result = $this->stmt->execute ();
} catch ( Exception $e ) {
throw new myException ($e->getMessage (), $e->getCode(), $e);
return $this->db->changes ();
return $this->num_rows ();
} catch ( Exception $e ) { }
* vim: set filetype=php noet sw=4 ts=4 fdm=marker:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
|