3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
6 * The PEAR DB driver for PHP's sqlite extension
7 * for interacting with SQLite databases
11 * LICENSE: This source file is subject to version 3.0 of the PHP license
12 * that is available through the world-wide-web at the following URI:
13 * http://www.php.net/license/3_0.txt. If you did not receive a copy of
14 * the PHP License and are unable to obtain it through the web, please
15 * send a note to license@php.net so we can mail you a copy immediately.
19 * @author Urs Gehrig <urs@circle.ch>
20 * @author Mika Tuupola <tuupola@appelsiini.net>
21 * @author Daniel Convissor <danielc@php.net>
22 * @copyright 1997-2007 The PHP Group
23 * @license http://www.php.net/license/3_0.txt PHP License 3.0 3.0
25 * @link http://pear.php.net/package/DB
29 * Obtain the DB_common class so it can be extended from
31 //require_once 'DB/common.php';
32 require_once 'common.php';
35 * The methods PEAR DB uses to interact with PHP's sqlite extension
36 * for interacting with SQLite databases
38 * These methods overload the ones declared in DB_common.
40 * NOTICE: This driver needs PHP's track_errors ini setting to be on.
41 * It is automatically turned on when connecting to the database.
42 * Make sure your scripts don't turn it off.
46 * @author Urs Gehrig <urs@circle.ch>
47 * @author Mika Tuupola <tuupola@appelsiini.net>
48 * @author Daniel Convissor <danielc@php.net>
49 * @copyright 1997-2007 The PHP Group
50 * @license http://www.php.net/license/3_0.txt PHP License 3.0 3.0
51 * @version Release: 1.9.2
52 * @link http://pear.php.net/package/DB
54 class DB_sqlite extends DB_common
59 * The DB driver type (mysql, oci8, odbc, etc.)
62 public $phptype = 'sqlite';
65 * The database syntax variant to be used (db2, access, etc.), if any
68 public $dbsyntax = 'sqlite';
71 * The capabilities of this DB implementation
73 * The 'new_link' element contains the PHP version that first provided
74 * new_link support for this DBMS. Contains false if it's unsupported.
76 * Meaning of the 'limit' element:
77 * + 'emulate' = emulate with fetch row by number
78 * + 'alter' = alter the query
83 public $features = array(
90 'transactions' => false,
94 * A mapping of native error codes to DB error codes
96 * {@internal Error codes according to sqlite_exec. See the online
97 * manual at http://sqlite.org/c_interface.html for info.
98 * This error handling based on sqlite_exec is not yet implemented.}}
102 public $errorcode_map = array();
105 * The raw database connection created by PHP
111 * The DSN information for connecting to a database
114 public $dsn = array();
120 * @link http://www.sqlite.org/datatypes.html
124 public $keywords = array(
144 * The most recent error message from $php_errormsg
148 public $_lasterror = '';
155 * This constructor calls <kbd>parent::__construct()</kbd>
159 public function __construct()
161 parent::__construct();
168 * Connect to the database server, log in and open the database
170 * Don't call this method directly. Use DB::connect() instead.
172 * PEAR DB's sqlite driver supports the following extra DSN options:
173 * + mode The permissions for the database file, in four digit
174 * chmod octal format (eg "0600").
176 * Example of connecting to a database in read-only mode:
178 * require_once 'DB.php';
180 * $dsn = 'sqlite:///path/and/name/of/db/file?mode=0400';
182 * 'portability' => DB_PORTABILITY_ALL,
185 * $db = DB::connect($dsn, $options);
186 * if ((new PEAR)->isError($db)) {
187 * die($db->getMessage());
191 * @param array $dsn the data source name
192 * @param bool $persistent should the connection be persistent?
196 public function connect($dsn, $persistent = false)
198 if (!PEAR::loadExtension('sqlite')) {
199 return $this->raiseError(DB_ERROR_EXTENSION_NOT_FOUND);
203 if ($dsn['dbsyntax']) {
204 $this->dbsyntax = $dsn['dbsyntax'];
207 if (!$dsn['database']) {
208 return $this->sqliteRaiseError(DB_ERROR_ACCESS_VIOLATION);
211 if ($dsn['database'] !== ':memory:') {
212 if (!file_exists($dsn['database'])) {
213 if (!touch($dsn['database'])) {
214 return $this->sqliteRaiseError(DB_ERROR_NOT_FOUND);
216 if (!isset($dsn['mode']) ||
217 !is_numeric($dsn['mode'])) {
220 $mode = octdec($dsn['mode']);
222 if (!chmod($dsn['database'], $mode)) {
223 return $this->sqliteRaiseError(DB_ERROR_NOT_FOUND);
225 if (!file_exists($dsn['database'])) {
226 return $this->sqliteRaiseError(DB_ERROR_NOT_FOUND);
229 if (!is_file($dsn['database'])) {
230 return $this->sqliteRaiseError(DB_ERROR_INVALID);
232 if (!is_readable($dsn['database'])) {
233 return $this->sqliteRaiseError(DB_ERROR_ACCESS_VIOLATION);
237 $connect_function = $persistent ? 'sqlite_popen' : 'sqlite_open';
239 // track_errors must remain on for simpleQuery()
240 @ini_set('track_errors', 1);
243 if (!$this->connection = @$connect_function($dsn['database'])) {
244 return $this->raiseError(
245 DB_ERROR_NODBSELECTED,
259 * Produces a DB_Error object regarding the current problem
261 * @param int $errno if the error is being manually raised pass a
262 * DB_ERROR* constant here. If this isn't passed
263 * the error information gathered from the DBMS.
265 * @return object the DB_Error object
267 * @see DB_common::raiseError(),
268 * DB_sqlite::errorNative(), DB_sqlite::errorCode()
270 public function sqliteRaiseError($errno = null)
272 $native = $this->errorNative();
273 if ($errno === null) {
274 $errno = $this->errorCode($native);
277 $errorcode = @sqlite_last_error($this->connection);
278 $userinfo = "$errorcode ** $this->last_query";
280 return $this->raiseError($errno, null, null, $userinfo, $native);
287 * Gets the DBMS' native error message produced by the last query
289 * {@internal This is used to retrieve more meaningfull error messages
290 * because sqlite_last_error() does not provide adequate info.}}
292 * @return string the DBMS' error message
294 public function errorNative()
296 return $this->_lasterror;
303 * Determines PEAR::DB error code from the database's text error message
305 * @param string $errormsg the error message returned from the database
307 * @return integer the DB error number
309 public function errorCode($errormsg)
311 static $error_regexps;
313 // PHP 5.2+ prepends the function name to $php_errormsg, so we need
314 // this hack to work around it, per bug #9599.
315 $errormsg = preg_replace('/^sqlite[a-z_]+\(\): /', '', $errormsg);
317 if (!isset($error_regexps)) {
318 $error_regexps = array(
319 '/^no such table:/' => DB_ERROR_NOSUCHTABLE,
320 '/^no such index:/' => DB_ERROR_NOT_FOUND,
321 '/^(table|index) .* already exists$/' => DB_ERROR_ALREADY_EXISTS,
322 '/PRIMARY KEY must be unique/i' => DB_ERROR_CONSTRAINT,
323 '/is not unique/' => DB_ERROR_CONSTRAINT,
324 '/columns .* are not unique/i' => DB_ERROR_CONSTRAINT,
325 '/uniqueness constraint failed/' => DB_ERROR_CONSTRAINT,
326 '/may not be NULL/' => DB_ERROR_CONSTRAINT_NOT_NULL,
327 '/^no such column:/' => DB_ERROR_NOSUCHFIELD,
328 '/no column named/' => DB_ERROR_NOSUCHFIELD,
329 '/column not present in both tables/i' => DB_ERROR_NOSUCHFIELD,
330 '/^near ".*": syntax error$/' => DB_ERROR_SYNTAX,
331 '/[0-9]+ values for [0-9]+ columns/i' => DB_ERROR_VALUE_COUNT_ON_ROW,
334 foreach ($error_regexps as $regexp => $code) {
335 if (preg_match($regexp, $errormsg)) {
339 // Fall back to DB_ERROR if there was no mapping.
347 * Disconnects from the database server
351 public function disconnect()
353 $ret = @sqlite_close($this->connection);
354 $this->connection = null;
362 * Sends a query to the database server
364 * NOTICE: This method needs PHP's track_errors ini setting to be on.
365 * It is automatically turned on when connecting to the database.
366 * Make sure your scripts don't turn it off.
368 * @param string the SQL query string
370 * @return mixed + a PHP result resrouce for successful SELECT queries
371 * + the DB_OK constant for other successful queries
372 * + a DB_Error object on failure
374 public function simpleQuery($query)
376 $ismanip = $this->_checkManip($query);
377 $this->last_query = $query;
378 $query = $this->modifyQuery($query);
382 $result = @sqlite_query($query, $this->connection);
383 $this->_lasterror = $php_errormsg ? $php_errormsg : '';
385 $this->result = $result;
386 if (!$this->result) {
387 return $this->sqliteRaiseError(null);
390 // sqlite_query() seems to allways return a resource
391 // so cant use that. Using $ismanip instead
393 $numRows = $this->numRows($result);
394 if (is_object($numRows)) {
395 // we've got PEAR_Error
407 * Changes a query string for various DBMS specific reasons
409 * This little hack lets you know how many rows were deleted
410 * when running a "DELETE FROM table" query. Only implemented
411 * if the DB_PORTABILITY_DELETE_COUNT portability option is on.
413 * @param string $query the query string to modify
415 * @return string the modified query string
418 * @see DB_common::setOption()
420 public function modifyQuery($query)
422 if ($this->options['portability'] & DB_PORTABILITY_DELETE_COUNT) {
423 if (preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i', $query)) {
424 $query = preg_replace(
425 '/^\s*DELETE\s+FROM\s+(\S+)\s*$/',
426 'DELETE FROM \1 WHERE 1=1',
438 * Gets the number of rows in a result set
440 * This method is not meant to be called directly. Use
441 * DB_result::numRows() instead. It can't be declared "protected"
442 * because DB_result is a separate object.
444 * @param resource $result PHP's query result resource
448 * @see DB_result::numRows()
450 public function numRows($result)
452 $rows = @sqlite_num_rows($result);
453 if ($rows === null) {
454 return $this->sqliteRaiseError();
463 * Move the internal sqlite result pointer to the next available result
465 * @param resource $result the valid sqlite result resource
467 * @return bool true if a result is available otherwise return false
469 public function nextResult($result)
475 // {{{ dropSequence()
478 * Places a row from the result set into the given array
480 * Formating of the array and the data therein are configurable.
481 * See DB_result::fetchInto() for more information.
483 * This method is not meant to be called directly. Use
484 * DB_result::fetchInto() instead. It can't be declared "protected"
485 * because DB_result is a separate object.
487 * @param resource $result the query result resource
488 * @param array $arr the referenced array to put the data in
489 * @param int $fetchmode how the resulting array should be indexed
490 * @param int $rownum the row number to fetch (0 = first row)
492 * @return mixed DB_OK on success, NULL when the end of a result set is
493 * reached or on failure
495 * @see DB_result::fetchInto()
497 public function fetchInto($result, &$arr, $fetchmode, $rownum = null)
499 if ($rownum !== null) {
500 if (!@sqlite_seek($this->result, $rownum)) {
504 if ($fetchmode & DB_FETCHMODE_ASSOC) {
505 $arr = @sqlite_fetch_array($result, SQLITE_ASSOC);
506 if ($this->options['portability'] & DB_PORTABILITY_LOWERCASE && $arr) {
507 $arr = array_change_key_case($arr, CASE_LOWER);
510 /* Remove extraneous " characters from the fields in the result.
511 * Fixes bug #11716. */
512 if (is_array($arr) && count($arr) > 0) {
513 $strippedArr = array();
514 foreach ($arr as $field => $value) {
515 $strippedArr[trim($field, '"')] = $value;
520 $arr = @sqlite_fetch_array($result, SQLITE_NUM);
525 if ($this->options['portability'] & DB_PORTABILITY_RTRIM) {
527 * Even though this DBMS already trims output, we do this because
528 * a field might have intentional whitespace at the end that
529 * gets removed by DB_PORTABILITY_RTRIM under another driver.
531 $this->_rtrimArrayValues($arr);
533 if ($this->options['portability'] & DB_PORTABILITY_NULL_TO_EMPTY) {
534 $this->_convertNullArrayValuesToEmpty($arr);
540 * Deletes the result set and frees the memory occupied by the result set
542 * This method is not meant to be called directly. Use
543 * DB_result::free() instead. It can't be declared "protected"
544 * because DB_result is a separate object.
546 * @param resource $result PHP's query result resource
548 * @return bool TRUE on success, FALSE if $result is invalid
550 * @see DB_result::free()
552 public function freeResult(&$result)
554 // XXX No native free?
555 if (!is_resource($result)) {
566 * Gets the number of columns in a result set
568 * This method is not meant to be called directly. Use
569 * DB_result::numCols() instead. It can't be declared "protected"
570 * because DB_result is a separate object.
572 * @param resource $result PHP's query result resource
576 * @see DB_result::numCols()
578 public function numCols($result)
580 $cols = @sqlite_num_fields($result);
582 return $this->sqliteRaiseError();
588 // {{{ getDbFileStats()
591 * Determines the number of rows affected by a data maniuplation query
593 * 0 is returned for queries that don't manipulate data.
595 * @return int the number of rows. A DB_Error object on failure.
597 public function affectedRows()
599 return @sqlite_changes($this->connection);
603 // {{{ escapeSimple()
608 * @param string $seq_name name of the sequence to be deleted
610 * @return int DB_OK on success. A DB_Error object on failure.
612 * @see DB_common::dropSequence(), DB_common::getSequenceName(),
613 * DB_sqlite::nextID(), DB_sqlite::createSequence()
615 public function dropSequence($seq_name)
617 return $this->query('DROP TABLE ' . $this->getSequenceName($seq_name));
621 // {{{ modifyLimitQuery()
624 * Returns the next free id in a sequence
626 * @param string $seq_name name of the sequence
627 * @param boolean $ondemand when true, the seqence is automatically
628 * created if it does not exist
631 * A DB_Error object on failure.
633 * @see DB_common::nextID(), DB_common::getSequenceName(),
634 * DB_sqlite::createSequence(), DB_sqlite::dropSequence()
636 public function nextId($seq_name, $ondemand = true)
638 $seqname = $this->getSequenceName($seq_name);
642 $this->pushErrorHandling(PEAR_ERROR_RETURN);
643 $result = $this->query("INSERT INTO $seqname (id) VALUES (NULL)");
644 $this->popErrorHandling();
645 if ($result === DB_OK) {
646 $id = @sqlite_last_insert_rowid($this->connection);
650 } elseif ($ondemand && DB::isError($result) &&
651 $result->getCode() == DB_ERROR_NOSUCHTABLE) {
652 $result = $this->createSequence($seq_name);
653 if (DB::isError($result)) {
654 return $this->raiseError($result);
661 return $this->raiseError($result);
668 * Creates a new sequence
670 * @param string $seq_name name of the new sequence
672 * @return int DB_OK on success. A DB_Error object on failure.
674 * @see DB_common::createSequence(), DB_common::getSequenceName(),
675 * DB_sqlite::nextID(), DB_sqlite::dropSequence()
677 public function createSequence($seq_name)
679 $seqname = $this->getSequenceName($seq_name);
680 $query = 'CREATE TABLE ' . $seqname .
681 ' (id INTEGER UNSIGNED PRIMARY KEY) ';
682 $result = $this->query($query);
683 if (DB::isError($result)) {
686 $query = "CREATE TRIGGER ${seqname}_cleanup AFTER INSERT ON $seqname
688 DELETE FROM $seqname WHERE id<LAST_INSERT_ROWID();
690 $result = $this->query($query);
691 //if (DB::isError($result)) {
697 // {{{ sqliteRaiseError()
700 * Get the file stats for the current database
702 * Possible arguments are dev, ino, mode, nlink, uid, gid, rdev, size,
703 * atime, mtime, ctime, blksize, blocks or a numeric key between
706 * @param string $arg the array key for stats()
708 * @return mixed an array on an unspecified key, integer on a passed
709 * arg and false at a stats error
711 public function getDbFileStats($arg = '')
713 $stats = stat($this->dsn['database']);
714 if ($stats == false) {
717 if (is_array($stats)) {
718 if (is_numeric($arg)) {
719 if (((int)$arg <= 12) & ((int)$arg >= 0)) {
724 if (array_key_exists(trim($arg), $stats)) {
735 * Escapes a string according to the current DBMS's standards
737 * In SQLite, this makes things safe for inserts/updates, but may
738 * cause problems when performing text comparisons against columns
739 * containing binary data. See the
740 * {@link http://php.net/sqlite_escape_string PHP manual} for more info.
742 * @param string $str the string to be escaped
744 * @return string the escaped string
746 * @since Method available since Release 1.6.1
747 * @see DB_common::escapeSimple()
749 public function escapeSimple($str)
751 return @sqlite_escape_string($str);
758 * Adds LIMIT clauses to a query string according to current DBMS standards
760 * @param string $query the query to modify
761 * @param int $from the row to start to fetching (0 = the first row)
762 * @param int $count the numbers of rows to fetch
763 * @param mixed $params array, string or numeric data to be used in
764 * execution of the statement. Quantity of items
765 * passed must match quantity of placeholders in
766 * query: meaning 1 placeholder for non-array
767 * parameters or 1 placeholder per array element.
769 * @return string the query string with LIMIT clauses added
773 public function modifyLimitQuery($query, $from, $count, $params = array())
775 return "$query LIMIT $count OFFSET $from";
782 * Returns information about a table
784 * @param string $result a string containing the name of a table
785 * @param int $mode a valid tableInfo mode
787 * @return array|object
788 * A DB_Error object on failure.
790 * @see DB_common::tableInfo()
791 * @since Method available since Release 1.7.0
793 public function tableInfo($result, $mode = null)
795 if (is_string($result)) {
797 * Probably received a table name.
798 * Create a result resource identifier.
800 $id = @sqlite_array_query(
802 "PRAGMA table_info('$result');",
807 $this->last_query = '';
808 return $this->raiseError(
809 DB_ERROR_NOT_CAPABLE,
813 'This DBMS can not obtain tableInfo' .
818 if ($this->options['portability'] & DB_PORTABILITY_LOWERCASE) {
819 $case_func = 'strtolower';
821 $case_func = 'strval';
828 $res['num_fields'] = $count;
831 for ($i = 0; $i < $count; $i++) {
832 if (strpos($id[$i]['type'], '(') !== false) {
833 $bits = explode('(', $id[$i]['type']);
835 $len = rtrim($bits[1], ')');
837 $type = $id[$i]['type'];
843 $flags .= 'primary_key ';
844 if (strtoupper($type) == 'INTEGER') {
845 $flags .= 'auto_increment ';
848 if ($id[$i]['notnull']) {
849 $flags .= 'not_null ';
851 if ($id[$i]['dflt_value'] !== null) {
852 $flags .= 'default_' . rawurlencode($id[$i]['dflt_value']);
854 $flags = trim($flags);
857 'table' => $case_func($result),
858 'name' => $case_func($id[$i]['name']),
864 if ($mode & DB_TABLEINFO_ORDER) {
865 $res['order'][$res[$i]['name']] = $i;
867 if ($mode & DB_TABLEINFO_ORDERTABLE) {
868 $res['ordertable'][$res[$i]['table']][$res[$i]['name']] = $i;
876 // {{{ getSpecialQuery()
879 * Obtains the query string needed for listing a given type of objects
881 * @param string $type the kind of objects you want to retrieve
882 * @param array $args SQLITE DRIVER ONLY: a private array of arguments
883 * used by the getSpecialQuery(). Do not use
886 * @return string the SQL query string or null if the driver doesn't
887 * support the object type requested
890 * @see DB_common::getListOf()
892 public function getSpecialQuery($type, $args = array())
894 if (!is_array($args)) {
895 return $this->raiseError(
900 'Argument has to be an array.'
906 return 'SELECT * FROM sqlite_master;';
908 return "SELECT name FROM sqlite_master WHERE type='table' "
909 . 'UNION ALL SELECT name FROM sqlite_temp_master '
910 . "WHERE type='table' ORDER BY name;";
912 return 'SELECT sql FROM (SELECT * FROM sqlite_master '
913 . 'UNION ALL SELECT * FROM sqlite_temp_master) '
914 . "WHERE type!='meta' "
915 . 'ORDER BY tbl_name, type DESC, name;';
920 * $res = $db->query($db->getSpecialQuery('schema_x',
921 * array('table' => 'table3')));
923 return 'SELECT sql FROM (SELECT * FROM sqlite_master '
924 . 'UNION ALL SELECT * FROM sqlite_temp_master) '
925 . "WHERE tbl_name LIKE '{$args['table']}' "
926 . "AND type!='meta' "
927 . 'ORDER BY type DESC, name;';
930 * SQLite does not support ALTER TABLE; this is a helper query
931 * to handle this. 'table' represents the table name, 'rows'
932 * the news rows to create, 'save' the row(s) to keep _with_
938 * 'rows' => "id INTEGER PRIMARY KEY, firstname TEXT, surname TEXT, datetime TEXT",
939 * 'save' => "NULL, titel, content, datetime"
941 * $res = $db->query( $db->getSpecialQuery('alter', $args));
943 $rows = strtr($args['rows'], $this->keywords);
947 "CREATE TEMPORARY TABLE {$args['table']}_backup ({$args['rows']})",
948 "INSERT INTO {$args['table']}_backup SELECT {$args['save']} FROM {$args['table']}",
949 "DROP TABLE {$args['table']}",
950 "CREATE TABLE {$args['table']} ({$args['rows']})",
951 "INSERT INTO {$args['table']} SELECT {$rows} FROM {$args['table']}_backup",
952 "DROP TABLE {$args['table']}_backup",
957 * This is a dirty hack, since the above query will not get
958 * executed with a single query call so here the query method
959 * will be called directly and return a select instead.
961 foreach ($q as $query) {
962 $this->query($query);
964 return "SELECT * FROM {$args['table']};";