3 * This is the SQLite database abstraction layer.
4 * See maintenance/sqlite/README for development notes and other specific information
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
24 use Wikimedia\Rdbms\Blob
;
25 use Wikimedia\Rdbms\SQLiteField
;
30 class DatabaseSqlite
extends Database
{
31 /** @var bool Whether full text is enabled */
32 private static $fulltextEnabled = null;
34 /** @var string Directory */
36 /** @var string File name for SQLite database file */
38 /** @var string Transaction mode */
41 /** @var int The number of rows affected as an integer */
42 protected $mAffectedRows;
44 protected $mLastResult;
49 /** @var FSLockManager (hopefully on the same server as the DB) */
53 * Additional params include:
54 * - dbDirectory : directory containing the DB and the lock file directory
55 * [defaults to $wgSQLiteDataDir]
56 * - dbFilePath : use this to force the path of the DB file
57 * - trxMode : one of (deferred, immediate, exclusive)
60 function __construct( array $p ) {
61 if ( isset( $p['dbFilePath'] ) ) {
62 parent
::__construct( $p );
63 // Standalone .sqlite file mode.
64 // Super doesn't open when $user is false, but we can work with $dbName,
65 // which is derived from the file path in this case.
66 $this->openFile( $p['dbFilePath'] );
67 $lockDomain = md5( $p['dbFilePath'] );
68 } elseif ( !isset( $p['dbDirectory'] ) ) {
69 throw new InvalidArgumentException( "Need 'dbDirectory' or 'dbFilePath' parameter." );
71 $this->dbDir
= $p['dbDirectory'];
72 $this->mDBname
= $p['dbname'];
73 $lockDomain = $this->mDBname
;
74 // Stock wiki mode using standard file names per DB.
75 parent
::__construct( $p );
76 // Super doesn't open when $user is false, but we can work with $dbName
77 if ( $p['dbname'] && !$this->isOpen() ) {
78 if ( $this->open( $p['host'], $p['user'], $p['password'], $p['dbname'] ) ) {
80 foreach ( $this->tableAliases
as $params ) {
81 if ( isset( $done[$params['dbname']] ) ) {
84 $this->attachDatabase( $params['dbname'] );
85 $done[$params['dbname']] = 1;
91 $this->trxMode
= isset( $p['trxMode'] ) ?
strtoupper( $p['trxMode'] ) : null;
92 if ( $this->trxMode
&&
93 !in_array( $this->trxMode
, [ 'DEFERRED', 'IMMEDIATE', 'EXCLUSIVE' ] )
95 $this->trxMode
= null;
96 $this->queryLogger
->warning( "Invalid SQLite transaction mode provided." );
99 $this->lockMgr
= new FSLockManager( [
100 'domain' => $lockDomain,
101 'lockDirectory' => "{$this->dbDir}/locks"
106 * @param string $filename
107 * @param array $p Options map; supports:
108 * - flags : (same as __construct counterpart)
109 * - trxMode : (same as __construct counterpart)
110 * - dbDirectory : (same as __construct counterpart)
111 * @return DatabaseSqlite
114 public static function newStandaloneInstance( $filename, array $p = [] ) {
115 $p['dbFilePath'] = $filename;
116 $p['schema'] = false;
117 $p['tablePrefix'] = '';
119 return Database
::factory( 'sqlite', $p );
130 * @todo Check if it should be true like parent class
134 function implicitGroupby() {
138 /** Open an SQLite database and return a resource handle to it
139 * NOTE: only $dbName is used, the other parameters are irrelevant for SQLite databases
141 * @param string $server
142 * @param string $user
143 * @param string $pass
144 * @param string $dbName
146 * @throws DBConnectionError
149 function open( $server, $user, $pass, $dbName ) {
151 $fileName = self
::generateFileName( $this->dbDir
, $dbName );
152 if ( !is_readable( $fileName ) ) {
153 $this->mConn
= false;
154 throw new DBConnectionError( $this, "SQLite database not accessible" );
156 $this->openFile( $fileName );
158 return (bool)$this->mConn
;
162 * Opens a database file
164 * @param string $fileName
165 * @throws DBConnectionError
166 * @return PDO|bool SQL connection or false if failed
168 protected function openFile( $fileName ) {
171 $this->dbPath
= $fileName;
173 if ( $this->mFlags
& self
::DBO_PERSISTENT
) {
174 $this->mConn
= new PDO( "sqlite:$fileName", '', '',
175 [ PDO
::ATTR_PERSISTENT
=> true ] );
177 $this->mConn
= new PDO( "sqlite:$fileName", '', '' );
179 } catch ( PDOException
$e ) {
180 $err = $e->getMessage();
183 if ( !$this->mConn
) {
184 $this->queryLogger
->debug( "DB connection error: $err\n" );
185 throw new DBConnectionError( $this, $err );
188 $this->mOpened
= !!$this->mConn
;
189 if ( $this->mOpened
) {
190 # Set error codes only, don't raise exceptions
191 $this->mConn
->setAttribute( PDO
::ATTR_ERRMODE
, PDO
::ERRMODE_SILENT
);
192 # Enforce LIKE to be case sensitive, just like MySQL
193 $this->query( 'PRAGMA case_sensitive_like = 1' );
201 public function selectDB( $db ) {
202 return false; // doesn't make sense
206 * @return string SQLite DB file path
209 public function getDbFilePath() {
210 return $this->dbPath
;
214 * Does not actually close the connection, just destroys the reference for GC to do its work
217 protected function closeConnection() {
224 * Generates a database file name. Explicitly public for installer.
225 * @param string $dir Directory where database resides
226 * @param string $dbName Database name
229 public static function generateFileName( $dir, $dbName ) {
230 return "$dir/$dbName.sqlite";
234 * Check if the searchindext table is FTS enabled.
235 * @return bool False if not enabled.
237 function checkForEnabledSearch() {
238 if ( self
::$fulltextEnabled === null ) {
239 self
::$fulltextEnabled = false;
240 $table = $this->tableName( 'searchindex' );
241 $res = $this->query( "SELECT sql FROM sqlite_master WHERE tbl_name = '$table'", __METHOD__
);
243 $row = $res->fetchRow();
244 self
::$fulltextEnabled = stristr( $row['sql'], 'fts' ) !== false;
248 return self
::$fulltextEnabled;
252 * Returns version of currently supported SQLite fulltext search module or false if none present.
255 static function getFulltextSearchModule() {
256 static $cachedResult = null;
257 if ( $cachedResult !== null ) {
258 return $cachedResult;
260 $cachedResult = false;
261 $table = 'dummy_search_test';
263 $db = self
::newStandaloneInstance( ':memory:' );
264 if ( $db->query( "CREATE VIRTUAL TABLE $table USING FTS3(dummy_field)", __METHOD__
, true ) ) {
265 $cachedResult = 'FTS3';
269 return $cachedResult;
273 * Attaches external database to our connection, see https://sqlite.org/lang_attach.html
276 * @param string $name Database name to be used in queries like
277 * SELECT foo FROM dbname.table
278 * @param bool|string $file Database file name. If omitted, will be generated
279 * using $name and configured data directory
280 * @param string $fname Calling function name
281 * @return ResultWrapper
283 function attachDatabase( $name, $file = false, $fname = __METHOD__
) {
285 $file = self
::generateFileName( $this->dbDir
, $name );
287 $file = $this->addQuotes( $file );
289 return $this->query( "ATTACH DATABASE $file AS $name", $fname );
292 function isWriteQuery( $sql ) {
293 return parent
::isWriteQuery( $sql ) && !preg_match( '/^(ATTACH|PRAGMA)\b/i', $sql );
297 * SQLite doesn't allow buffered results or data seeking etc, so we'll use fetchAll as the result
300 * @return bool|ResultWrapper
302 protected function doQuery( $sql ) {
303 $res = $this->mConn
->query( $sql );
304 if ( $res === false ) {
308 $r = $res instanceof ResultWrapper ?
$res->result
: $res;
309 $this->mAffectedRows
= $r->rowCount();
310 $res = new ResultWrapper( $this, $r->fetchAll() );
316 * @param ResultWrapper|mixed $res
318 function freeResult( $res ) {
319 if ( $res instanceof ResultWrapper
) {
327 * @param ResultWrapper|array $res
328 * @return stdClass|bool
330 function fetchObject( $res ) {
331 if ( $res instanceof ResultWrapper
) {
337 $cur = current( $r );
338 if ( is_array( $cur ) ) {
341 foreach ( $cur as $k => $v ) {
342 if ( !is_numeric( $k ) ) {
354 * @param ResultWrapper|mixed $res
357 function fetchRow( $res ) {
358 if ( $res instanceof ResultWrapper
) {
363 $cur = current( $r );
364 if ( is_array( $cur ) ) {
374 * The PDO::Statement class implements the array interface so count() will work
376 * @param ResultWrapper|array $res
379 function numRows( $res ) {
380 $r = $res instanceof ResultWrapper ?
$res->result
: $res;
386 * @param ResultWrapper $res
389 function numFields( $res ) {
390 $r = $res instanceof ResultWrapper ?
$res->result
: $res;
391 if ( is_array( $r ) && count( $r ) > 0 ) {
392 // The size of the result array is twice the number of fields. (Bug: 65578)
393 return count( $r[0] ) / 2;
395 // If the result is empty return 0
401 * @param ResultWrapper $res
405 function fieldName( $res, $n ) {
406 $r = $res instanceof ResultWrapper ?
$res->result
: $res;
407 if ( is_array( $r ) ) {
408 $keys = array_keys( $r[0] );
417 * Use MySQL's naming (accounts for prefix etc) but remove surrounding backticks
419 * @param string $name
420 * @param string $format
423 function tableName( $name, $format = 'quoted' ) {
424 // table names starting with sqlite_ are reserved
425 if ( strpos( $name, 'sqlite_' ) === 0 ) {
429 return str_replace( '"', '', parent
::tableName( $name, $format ) );
433 * This must be called after nextSequenceVal
437 function insertId() {
438 // PDO::lastInsertId yields a string :(
439 return intval( $this->mConn
->lastInsertId() );
443 * @param ResultWrapper|array $res
446 function dataSeek( $res, $row ) {
447 if ( $res instanceof ResultWrapper
) {
454 for ( $i = 0; $i < $row; $i++
) {
463 function lastError() {
464 if ( !is_object( $this->mConn
) ) {
465 return "Cannot return last error, no db connection";
467 $e = $this->mConn
->errorInfo();
469 return isset( $e[2] ) ?
$e[2] : '';
475 function lastErrno() {
476 if ( !is_object( $this->mConn
) ) {
477 return "Cannot return last error, no db connection";
479 $info = $this->mConn
->errorInfo();
488 function affectedRows() {
489 return $this->mAffectedRows
;
493 * Returns information about an index
494 * Returns false if the index does not exist
495 * - if errors are explicitly ignored, returns NULL on failure
497 * @param string $table
498 * @param string $index
499 * @param string $fname
500 * @return array|false
502 function indexInfo( $table, $index, $fname = __METHOD__
) {
503 $sql = 'PRAGMA index_info(' . $this->addQuotes( $this->indexName( $index ) ) . ')';
504 $res = $this->query( $sql, $fname );
505 if ( !$res ||
$res->numRows() == 0 ) {
509 foreach ( $res as $row ) {
510 $info[] = $row->name
;
517 * @param string $table
518 * @param string $index
519 * @param string $fname
522 function indexUnique( $table, $index, $fname = __METHOD__
) {
523 $row = $this->selectRow( 'sqlite_master', '*',
526 'name' => $this->indexName( $index ),
528 if ( !$row ||
!isset( $row->sql
) ) {
532 // $row->sql will be of the form CREATE [UNIQUE] INDEX ...
533 $indexPos = strpos( $row->sql
, 'INDEX' );
534 if ( $indexPos === false ) {
537 $firstPart = substr( $row->sql
, 0, $indexPos );
538 $options = explode( ' ', $firstPart );
540 return in_array( 'UNIQUE', $options );
544 * Filter the options used in SELECT statements
546 * @param array $options
549 function makeSelectOptions( $options ) {
550 foreach ( $options as $k => $v ) {
551 if ( is_numeric( $k ) && ( $v == 'FOR UPDATE' ||
$v == 'LOCK IN SHARE MODE' ) ) {
556 return parent
::makeSelectOptions( $options );
560 * @param array $options
563 protected function makeUpdateOptionsArray( $options ) {
564 $options = parent
::makeUpdateOptionsArray( $options );
565 $options = self
::fixIgnore( $options );
571 * @param array $options
574 static function fixIgnore( $options ) {
575 # SQLite uses OR IGNORE not just IGNORE
576 foreach ( $options as $k => $v ) {
577 if ( $v == 'IGNORE' ) {
578 $options[$k] = 'OR IGNORE';
586 * @param array $options
589 function makeInsertOptions( $options ) {
590 $options = self
::fixIgnore( $options );
592 return parent
::makeInsertOptions( $options );
596 * Based on generic method (parent) with some prior SQLite-sepcific adjustments
597 * @param string $table
599 * @param string $fname
600 * @param array $options
603 function insert( $table, $a, $fname = __METHOD__
, $options = [] ) {
604 if ( !count( $a ) ) {
608 # SQLite can't handle multi-row inserts, so divide up into multiple single-row inserts
609 if ( isset( $a[0] ) && is_array( $a[0] ) ) {
611 foreach ( $a as $v ) {
612 if ( !parent
::insert( $table, $v, "$fname/multi-row", $options ) ) {
617 $ret = parent
::insert( $table, $a, "$fname/single-row", $options );
624 * @param string $table
625 * @param array $uniqueIndexes Unused
626 * @param string|array $rows
627 * @param string $fname
628 * @return bool|ResultWrapper
630 function replace( $table, $uniqueIndexes, $rows, $fname = __METHOD__
) {
631 if ( !count( $rows ) ) {
635 # SQLite can't handle multi-row replaces, so divide up into multiple single-row queries
636 if ( isset( $rows[0] ) && is_array( $rows[0] ) ) {
638 foreach ( $rows as $v ) {
639 if ( !$this->nativeReplace( $table, $v, "$fname/multi-row" ) ) {
644 $ret = $this->nativeReplace( $table, $rows, "$fname/single-row" );
651 * Returns the size of a text field, or -1 for "unlimited"
652 * In SQLite this is SQLITE_MAX_LENGTH, by default 1GB. No way to query it though.
654 * @param string $table
655 * @param string $field
658 function textFieldSize( $table, $field ) {
665 function unionSupportsOrderAndLimit() {
670 * @param string[] $sqls
671 * @param bool $all Whether to "UNION ALL" or not
674 function unionQueries( $sqls, $all ) {
675 $glue = $all ?
' UNION ALL ' : ' UNION ';
677 return implode( $glue, $sqls );
683 function wasDeadlock() {
684 return $this->lastErrno() == 5; // SQLITE_BUSY
690 function wasErrorReissuable() {
691 return $this->lastErrno() == 17; // SQLITE_SCHEMA;
697 function wasReadOnlyError() {
698 return $this->lastErrno() == 8; // SQLITE_READONLY;
702 * @return string Wikitext of a link to the server software's web site
704 public function getSoftwareLink() {
705 return "[{{int:version-db-sqlite-url}} SQLite]";
709 * @return string Version information from the database
711 function getServerVersion() {
712 $ver = $this->mConn
->getAttribute( PDO
::ATTR_SERVER_VERSION
);
718 * Get information about a given field
719 * Returns false if the field does not exist.
721 * @param string $table
722 * @param string $field
723 * @return SQLiteField|bool False on failure
725 function fieldInfo( $table, $field ) {
726 $tableName = $this->tableName( $table );
727 $sql = 'PRAGMA table_info(' . $this->addQuotes( $tableName ) . ')';
728 $res = $this->query( $sql, __METHOD__
);
729 foreach ( $res as $row ) {
730 if ( $row->name
== $field ) {
731 return new SQLiteField( $row, $tableName );
738 protected function doBegin( $fname = '' ) {
739 if ( $this->trxMode
) {
740 $this->query( "BEGIN {$this->trxMode}", $fname );
742 $this->query( 'BEGIN', $fname );
744 $this->mTrxLevel
= 1;
751 function strencode( $s ) {
752 return substr( $this->addQuotes( $s ), 1, -1 );
759 function encodeBlob( $b ) {
760 return new Blob( $b );
764 * @param Blob|string $b
767 function decodeBlob( $b ) {
768 if ( $b instanceof Blob
) {
776 * @param string|int|null|bool|Blob $s
779 function addQuotes( $s ) {
780 if ( $s instanceof Blob
) {
781 return "x'" . bin2hex( $s->fetch() ) . "'";
782 } elseif ( is_bool( $s ) ) {
784 } elseif ( strpos( $s, "\0" ) !== false ) {
785 // SQLite doesn't support \0 in strings, so use the hex representation as a workaround.
786 // This is a known limitation of SQLite's mprintf function which PDO
787 // should work around, but doesn't. I have reported this to php.net as bug #63419:
788 // https://bugs.php.net/bug.php?id=63419
789 // There was already a similar report for SQLite3::escapeString, bug #62361:
790 // https://bugs.php.net/bug.php?id=62361
791 // There is an additional bug regarding sorting this data after insert
792 // on older versions of sqlite shipped with ubuntu 12.04
793 // https://phabricator.wikimedia.org/T74367
794 $this->queryLogger
->debug(
796 ': Quoting value containing null byte. ' .
797 'For consistency all binary data should have been ' .
798 'first processed with self::encodeBlob()'
800 return "x'" . bin2hex( $s ) . "'";
802 return $this->mConn
->quote( $s );
809 function buildLike() {
810 $params = func_get_args();
811 if ( count( $params ) > 0 && is_array( $params[0] ) ) {
812 $params = $params[0];
815 return parent
::buildLike( $params ) . "ESCAPE '\' ";
819 * @param string $field Field or column to cast
823 public function buildStringCast( $field ) {
824 return 'CAST ( ' . $field . ' AS TEXT )';
828 * No-op version of deadlockLoop
832 public function deadlockLoop( /*...*/ ) {
833 $args = func_get_args();
834 $function = array_shift( $args );
836 return call_user_func_array( $function, $args );
843 protected function replaceVars( $s ) {
844 $s = parent
::replaceVars( $s );
845 if ( preg_match( '/^\s*(CREATE|ALTER) TABLE/i', $s ) ) {
846 // CREATE TABLE hacks to allow schema file sharing with MySQL
848 // binary/varbinary column type -> blob
849 $s = preg_replace( '/\b(var)?binary(\(\d+\))/i', 'BLOB', $s );
850 // no such thing as unsigned
851 $s = preg_replace( '/\b(un)?signed\b/i', '', $s );
853 $s = preg_replace( '/\b(tiny|small|medium|big|)int(\s*\(\s*\d+\s*\)|\b)/i', 'INTEGER', $s );
854 // floating point types -> REAL
856 '/\b(float|double(\s+precision)?)(\s*\(\s*\d+\s*(,\s*\d+\s*)?\)|\b)/i',
861 $s = preg_replace( '/\b(var)?char\s*\(.*?\)/i', 'TEXT', $s );
862 // TEXT normalization
863 $s = preg_replace( '/\b(tiny|medium|long)text\b/i', 'TEXT', $s );
864 // BLOB normalization
865 $s = preg_replace( '/\b(tiny|small|medium|long|)blob\b/i', 'BLOB', $s );
867 $s = preg_replace( '/\bbool(ean)?\b/i', 'INTEGER', $s );
869 $s = preg_replace( '/\b(datetime|timestamp)\b/i', 'TEXT', $s );
871 $s = preg_replace( '/\benum\s*\([^)]*\)/i', 'TEXT', $s );
872 // binary collation type -> nothing
873 $s = preg_replace( '/\bbinary\b/i', '', $s );
874 // auto_increment -> autoincrement
875 $s = preg_replace( '/\bauto_increment\b/i', 'AUTOINCREMENT', $s );
876 // No explicit options
877 $s = preg_replace( '/\)[^);]*(;?)\s*$/', ')\1', $s );
878 // AUTOINCREMENT should immedidately follow PRIMARY KEY
879 $s = preg_replace( '/primary key (.*?) autoincrement/i', 'PRIMARY KEY AUTOINCREMENT $1', $s );
880 } elseif ( preg_match( '/^\s*CREATE (\s*(?:UNIQUE|FULLTEXT)\s+)?INDEX/i', $s ) ) {
881 // No truncated indexes
882 $s = preg_replace( '/\(\d+\)/', '', $s );
884 $s = preg_replace( '/\bfulltext\b/i', '', $s );
885 } elseif ( preg_match( '/^\s*DROP INDEX/i', $s ) ) {
886 // DROP INDEX is database-wide, not table-specific, so no ON <table> clause.
887 $s = preg_replace( '/\sON\s+[^\s]*/i', '', $s );
888 } elseif ( preg_match( '/^\s*INSERT IGNORE\b/i', $s ) ) {
889 // INSERT IGNORE --> INSERT OR IGNORE
890 $s = preg_replace( '/^\s*INSERT IGNORE\b/i', 'INSERT OR IGNORE', $s );
896 public function lock( $lockName, $method, $timeout = 5 ) {
897 if ( !is_dir( "{$this->dbDir}/locks" ) ) { // create dir as needed
898 if ( !is_writable( $this->dbDir
) ||
!mkdir( "{$this->dbDir}/locks" ) ) {
899 throw new DBError( $this, "Cannot create directory \"{$this->dbDir}/locks\"." );
903 return $this->lockMgr
->lock( [ $lockName ], LockManager
::LOCK_EX
, $timeout )->isOK();
906 public function unlock( $lockName, $method ) {
907 return $this->lockMgr
->unlock( [ $lockName ], LockManager
::LOCK_EX
)->isOK();
911 * Build a concatenation list to feed into a SQL query
913 * @param string[] $stringList
916 function buildConcat( $stringList ) {
917 return '(' . implode( ') || (', $stringList ) . ')';
920 public function buildGroupConcatField(
921 $delim, $table, $field, $conds = '', $join_conds = []
923 $fld = "group_concat($field," . $this->addQuotes( $delim ) . ')';
925 return '(' . $this->selectSQLText( $table, $fld, $conds, null, [], $join_conds ) . ')';
929 * @param string $oldName
930 * @param string $newName
931 * @param bool $temporary
932 * @param string $fname
933 * @return bool|ResultWrapper
934 * @throws RuntimeException
936 function duplicateTableStructure( $oldName, $newName, $temporary = false, $fname = __METHOD__
) {
937 $res = $this->query( "SELECT sql FROM sqlite_master WHERE tbl_name=" .
938 $this->addQuotes( $oldName ) . " AND type='table'", $fname );
939 $obj = $this->fetchObject( $res );
941 throw new RuntimeException( "Couldn't retrieve structure for table $oldName" );
945 '/(?<=\W)"?' . preg_quote( trim( $this->addIdentifierQuotes( $oldName ), '"' ) ) . '"?(?=\W)/',
946 $this->addIdentifierQuotes( $newName ),
951 if ( preg_match( '/^\\s*CREATE\\s+VIRTUAL\\s+TABLE\b/i', $sql ) ) {
952 $this->queryLogger
->debug(
953 "Table $oldName is virtual, can't create a temporary duplicate.\n" );
955 $sql = str_replace( 'CREATE TABLE', 'CREATE TEMPORARY TABLE', $sql );
959 $res = $this->query( $sql, $fname );
962 $indexList = $this->query( 'PRAGMA INDEX_LIST(' . $this->addQuotes( $oldName ) . ')' );
963 foreach ( $indexList as $index ) {
964 if ( strpos( $index->name
, 'sqlite_autoindex' ) === 0 ) {
968 if ( $index->unique
) {
969 $sql = 'CREATE UNIQUE INDEX';
971 $sql = 'CREATE INDEX';
973 // Try to come up with a new index name, given indexes have database scope in SQLite
974 $indexName = $newName . '_' . $index->name
;
975 $sql .= ' ' . $indexName . ' ON ' . $newName;
977 $indexInfo = $this->query( 'PRAGMA INDEX_INFO(' . $this->addQuotes( $index->name
) . ')' );
979 foreach ( $indexInfo as $indexInfoRow ) {
980 $fields[$indexInfoRow->seqno
] = $indexInfoRow->name
;
983 $sql .= '(' . implode( ',', $fields ) . ')';
985 $this->query( $sql );
992 * List all tables on the database
994 * @param string $prefix Only show tables with this prefix, e.g. mw_
995 * @param string $fname Calling function name
999 function listTables( $prefix = null, $fname = __METHOD__
) {
1000 $result = $this->select(
1008 foreach ( $result as $table ) {
1009 $vars = get_object_vars( $table );
1010 $table = array_pop( $vars );
1012 if ( !$prefix ||
strpos( $table, $prefix ) === 0 ) {
1013 if ( strpos( $table, 'sqlite_' ) !== 0 ) {
1014 $endArray[] = $table;
1023 * Override due to no CASCADE support
1025 * @param string $tableName
1026 * @param string $fName
1027 * @return bool|ResultWrapper
1028 * @throws DBReadOnlyError
1030 public function dropTable( $tableName, $fName = __METHOD__
) {
1031 if ( !$this->tableExists( $tableName, $fName ) ) {
1034 $sql = "DROP TABLE " . $this->tableName( $tableName );
1036 return $this->query( $sql, $fName );
1039 protected function requiresDatabaseUser() {
1040 return false; // just a file
1046 public function __toString() {
1047 return 'SQLite ' . (string)$this->mConn
->getAttribute( PDO
::ATTR_SERVER_VERSION
);