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
28 class DatabaseSqlite
extends DatabaseBase
{
29 /** @var bool Whether full text is enabled */
30 private static $fulltextEnabled = null;
32 /** @var string Directory */
35 /** @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;
45 protected $mLastResult;
50 /** @var FSLockManager (hopefully on the same server as the DB) */
54 * Additional params include:
55 * - dbDirectory : directory containing the DB and the lock file directory
56 * [defaults to $wgSQLiteDataDir]
57 * - dbFilePath : use this to force the path of the DB file
58 * - trxMode : one of (deferred, immediate, exclusive)
61 function __construct( array $p ) {
62 if ( isset( $p['dbFilePath'] ) ) {
63 parent
::__construct( $p );
64 // Standalone .sqlite file mode.
65 // Super doesn't open when $user is false, but we can work with $dbName,
66 // which is derived from the file path in this case.
67 $this->openFile( $p['dbFilePath'] );
68 $lockDomain = md5( $p['dbFilePath'] );
69 } elseif ( !isset( $p['dbDirectory'] ) ) {
70 throw new InvalidArgumentException( "Need 'dbDirectory' or 'dbFilePath' parameter." );
72 $this->dbDir
= $p['dbDirectory'];
73 $this->mDBname
= $p['dbname'];
74 $lockDomain = $this->mDBname
;
75 // Stock wiki mode using standard file names per DB.
76 parent
::__construct( $p );
77 // Super doesn't open when $user is false, but we can work with $dbName
78 if ( $p['dbname'] && !$this->isOpen() ) {
79 if ( $this->open( $p['host'], $p['user'], $p['password'], $p['dbname'] ) ) {
81 foreach ( $this->tableAliases
as $params ) {
82 if ( isset( $done[$params['dbname']] ) ) {
85 $this->attachDatabase( $params['dbname'] );
86 $done[$params['dbname']] = 1;
92 $this->trxMode
= isset( $p['trxMode'] ) ?
strtoupper( $p['trxMode'] ) : null;
93 if ( $this->trxMode
&&
94 !in_array( $this->trxMode
, [ 'DEFERRED', 'IMMEDIATE', 'EXCLUSIVE' ] )
96 $this->trxMode
= null;
97 $this->queryLogger
->warning( "Invalid SQLite transaction mode provided." );
100 $this->lockMgr
= new FSLockManager( [
101 'domain' => $lockDomain,
102 'lockDirectory' => "{$this->dbDir}/locks"
107 * @param string $filename
108 * @param array $p Options map; supports:
109 * - flags : (same as __construct counterpart)
110 * - trxMode : (same as __construct counterpart)
111 * - dbDirectory : (same as __construct counterpart)
112 * @return DatabaseSqlite
115 public static function newStandaloneInstance( $filename, array $p = [] ) {
116 $p['dbFilePath'] = $filename;
117 $p['schema'] = false;
118 $p['tablePrefix'] = '';
120 return DatabaseBase
::factory( 'sqlite', $p );
131 * @todo Check if it should be true like parent class
135 function implicitGroupby() {
139 /** Open an SQLite database and return a resource handle to it
140 * NOTE: only $dbName is used, the other parameters are irrelevant for SQLite databases
142 * @param string $server
143 * @param string $user
144 * @param string $pass
145 * @param string $dbName
147 * @throws DBConnectionError
150 function open( $server, $user, $pass, $dbName ) {
152 $fileName = self
::generateFileName( $this->dbDir
, $dbName );
153 if ( !is_readable( $fileName ) ) {
154 $this->mConn
= false;
155 throw new DBConnectionError( $this, "SQLite database not accessible" );
157 $this->openFile( $fileName );
163 * Opens a database file
165 * @param string $fileName
166 * @throws DBConnectionError
167 * @return PDO|bool SQL connection or false if failed
169 protected function openFile( $fileName ) {
172 $this->dbPath
= $fileName;
174 if ( $this->mFlags
& DBO_PERSISTENT
) {
175 $this->mConn
= new PDO( "sqlite:$fileName", '', '',
176 [ PDO
::ATTR_PERSISTENT
=> true ] );
178 $this->mConn
= new PDO( "sqlite:$fileName", '', '' );
180 } catch ( PDOException
$e ) {
181 $err = $e->getMessage();
184 if ( !$this->mConn
) {
185 $this->queryLogger
->debug( "DB connection error: $err\n" );
186 throw new DBConnectionError( $this, $err );
189 $this->mOpened
= !!$this->mConn
;
190 if ( $this->mOpened
) {
191 # Set error codes only, don't raise exceptions
192 $this->mConn
->setAttribute( PDO
::ATTR_ERRMODE
, PDO
::ERRMODE_SILENT
);
193 # Enforce LIKE to be case sensitive, just like MySQL
194 $this->query( 'PRAGMA case_sensitive_like = 1' );
203 * @return string SQLite DB file path
206 public function getDbFilePath() {
207 return $this->dbPath
;
211 * Does not actually close the connection, just destroys the reference for GC to do its work
214 protected function closeConnection() {
221 * Generates a database file name. Explicitly public for installer.
222 * @param string $dir Directory where database resides
223 * @param string $dbName Database name
226 public static function generateFileName( $dir, $dbName ) {
227 return "$dir/$dbName.sqlite";
231 * Check if the searchindext table is FTS enabled.
232 * @return bool False if not enabled.
234 function checkForEnabledSearch() {
235 if ( self
::$fulltextEnabled === null ) {
236 self
::$fulltextEnabled = false;
237 $table = $this->tableName( 'searchindex' );
238 $res = $this->query( "SELECT sql FROM sqlite_master WHERE tbl_name = '$table'", __METHOD__
);
240 $row = $res->fetchRow();
241 self
::$fulltextEnabled = stristr( $row['sql'], 'fts' ) !== false;
245 return self
::$fulltextEnabled;
249 * Returns version of currently supported SQLite fulltext search module or false if none present.
252 static function getFulltextSearchModule() {
253 static $cachedResult = null;
254 if ( $cachedResult !== null ) {
255 return $cachedResult;
257 $cachedResult = false;
258 $table = 'dummy_search_test';
260 $db = self
::newStandaloneInstance( ':memory:' );
261 if ( $db->query( "CREATE VIRTUAL TABLE $table USING FTS3(dummy_field)", __METHOD__
, true ) ) {
262 $cachedResult = 'FTS3';
266 return $cachedResult;
270 * Attaches external database to our connection, see http://sqlite.org/lang_attach.html
273 * @param string $name Database name to be used in queries like
274 * SELECT foo FROM dbname.table
275 * @param bool|string $file Database file name. If omitted, will be generated
276 * using $name and configured data directory
277 * @param string $fname Calling function name
278 * @return ResultWrapper
280 function attachDatabase( $name, $file = false, $fname = __METHOD__
) {
282 $file = self
::generateFileName( $this->dbDir
, $name );
284 $file = $this->addQuotes( $file );
286 return $this->query( "ATTACH DATABASE $file AS $name", $fname );
289 function isWriteQuery( $sql ) {
290 return parent
::isWriteQuery( $sql ) && !preg_match( '/^(ATTACH|PRAGMA)\b/i', $sql );
294 * SQLite doesn't allow buffered results or data seeking etc, so we'll use fetchAll as the result
297 * @return bool|ResultWrapper
299 protected function doQuery( $sql ) {
300 $res = $this->mConn
->query( $sql );
301 if ( $res === false ) {
305 $r = $res instanceof ResultWrapper ?
$res->result
: $res;
306 $this->mAffectedRows
= $r->rowCount();
307 $res = new ResultWrapper( $this, $r->fetchAll() );
313 * @param ResultWrapper|mixed $res
315 function freeResult( $res ) {
316 if ( $res instanceof ResultWrapper
) {
324 * @param ResultWrapper|array $res
325 * @return stdClass|bool
327 function fetchObject( $res ) {
328 if ( $res instanceof ResultWrapper
) {
334 $cur = current( $r );
335 if ( is_array( $cur ) ) {
338 foreach ( $cur as $k => $v ) {
339 if ( !is_numeric( $k ) ) {
351 * @param ResultWrapper|mixed $res
354 function fetchRow( $res ) {
355 if ( $res instanceof ResultWrapper
) {
360 $cur = current( $r );
361 if ( is_array( $cur ) ) {
371 * The PDO::Statement class implements the array interface so count() will work
373 * @param ResultWrapper|array $res
376 function numRows( $res ) {
377 $r = $res instanceof ResultWrapper ?
$res->result
: $res;
383 * @param ResultWrapper $res
386 function numFields( $res ) {
387 $r = $res instanceof ResultWrapper ?
$res->result
: $res;
388 if ( is_array( $r ) && count( $r ) > 0 ) {
389 // The size of the result array is twice the number of fields. (Bug: 65578)
390 return count( $r[0] ) / 2;
392 // If the result is empty return 0
398 * @param ResultWrapper $res
402 function fieldName( $res, $n ) {
403 $r = $res instanceof ResultWrapper ?
$res->result
: $res;
404 if ( is_array( $r ) ) {
405 $keys = array_keys( $r[0] );
414 * Use MySQL's naming (accounts for prefix etc) but remove surrounding backticks
416 * @param string $name
417 * @param string $format
420 function tableName( $name, $format = 'quoted' ) {
421 // table names starting with sqlite_ are reserved
422 if ( strpos( $name, 'sqlite_' ) === 0 ) {
426 return str_replace( '"', '', parent
::tableName( $name, $format ) );
430 * Index names have DB scope
432 * @param string $index
435 protected function indexName( $index ) {
440 * This must be called after nextSequenceVal
444 function insertId() {
445 // PDO::lastInsertId yields a string :(
446 return intval( $this->mConn
->lastInsertId() );
450 * @param ResultWrapper|array $res
453 function dataSeek( $res, $row ) {
454 if ( $res instanceof ResultWrapper
) {
461 for ( $i = 0; $i < $row; $i++
) {
470 function lastError() {
471 if ( !is_object( $this->mConn
) ) {
472 return "Cannot return last error, no db connection";
474 $e = $this->mConn
->errorInfo();
476 return isset( $e[2] ) ?
$e[2] : '';
482 function lastErrno() {
483 if ( !is_object( $this->mConn
) ) {
484 return "Cannot return last error, no db connection";
486 $info = $this->mConn
->errorInfo();
495 function affectedRows() {
496 return $this->mAffectedRows
;
500 * Returns information about an index
501 * Returns false if the index does not exist
502 * - if errors are explicitly ignored, returns NULL on failure
504 * @param string $table
505 * @param string $index
506 * @param string $fname
509 function indexInfo( $table, $index, $fname = __METHOD__
) {
510 $sql = 'PRAGMA index_info(' . $this->addQuotes( $this->indexName( $index ) ) . ')';
511 $res = $this->query( $sql, $fname );
515 if ( $res->numRows() == 0 ) {
519 foreach ( $res as $row ) {
520 $info[] = $row->name
;
527 * @param string $table
528 * @param string $index
529 * @param string $fname
532 function indexUnique( $table, $index, $fname = __METHOD__
) {
533 $row = $this->selectRow( 'sqlite_master', '*',
536 'name' => $this->indexName( $index ),
538 if ( !$row ||
!isset( $row->sql
) ) {
542 // $row->sql will be of the form CREATE [UNIQUE] INDEX ...
543 $indexPos = strpos( $row->sql
, 'INDEX' );
544 if ( $indexPos === false ) {
547 $firstPart = substr( $row->sql
, 0, $indexPos );
548 $options = explode( ' ', $firstPart );
550 return in_array( 'UNIQUE', $options );
554 * Filter the options used in SELECT statements
556 * @param array $options
559 function makeSelectOptions( $options ) {
560 foreach ( $options as $k => $v ) {
561 if ( is_numeric( $k ) && ( $v == 'FOR UPDATE' ||
$v == 'LOCK IN SHARE MODE' ) ) {
566 return parent
::makeSelectOptions( $options );
570 * @param array $options
573 protected function makeUpdateOptionsArray( $options ) {
574 $options = parent
::makeUpdateOptionsArray( $options );
575 $options = self
::fixIgnore( $options );
581 * @param array $options
584 static function fixIgnore( $options ) {
585 # SQLite uses OR IGNORE not just IGNORE
586 foreach ( $options as $k => $v ) {
587 if ( $v == 'IGNORE' ) {
588 $options[$k] = 'OR IGNORE';
596 * @param array $options
599 function makeInsertOptions( $options ) {
600 $options = self
::fixIgnore( $options );
602 return parent
::makeInsertOptions( $options );
606 * Based on generic method (parent) with some prior SQLite-sepcific adjustments
607 * @param string $table
609 * @param string $fname
610 * @param array $options
613 function insert( $table, $a, $fname = __METHOD__
, $options = [] ) {
614 if ( !count( $a ) ) {
618 # SQLite can't handle multi-row inserts, so divide up into multiple single-row inserts
619 if ( isset( $a[0] ) && is_array( $a[0] ) ) {
621 foreach ( $a as $v ) {
622 if ( !parent
::insert( $table, $v, "$fname/multi-row", $options ) ) {
627 $ret = parent
::insert( $table, $a, "$fname/single-row", $options );
634 * @param string $table
635 * @param array $uniqueIndexes Unused
636 * @param string|array $rows
637 * @param string $fname
638 * @return bool|ResultWrapper
640 function replace( $table, $uniqueIndexes, $rows, $fname = __METHOD__
) {
641 if ( !count( $rows ) ) {
645 # SQLite can't handle multi-row replaces, so divide up into multiple single-row queries
646 if ( isset( $rows[0] ) && is_array( $rows[0] ) ) {
648 foreach ( $rows as $v ) {
649 if ( !$this->nativeReplace( $table, $v, "$fname/multi-row" ) ) {
654 $ret = $this->nativeReplace( $table, $rows, "$fname/single-row" );
661 * Returns the size of a text field, or -1 for "unlimited"
662 * In SQLite this is SQLITE_MAX_LENGTH, by default 1GB. No way to query it though.
664 * @param string $table
665 * @param string $field
668 function textFieldSize( $table, $field ) {
675 function unionSupportsOrderAndLimit() {
680 * @param string $sqls
681 * @param bool $all Whether to "UNION ALL" or not
684 function unionQueries( $sqls, $all ) {
685 $glue = $all ?
' UNION ALL ' : ' UNION ';
687 return implode( $glue, $sqls );
693 function wasDeadlock() {
694 return $this->lastErrno() == 5; // SQLITE_BUSY
700 function wasErrorReissuable() {
701 return $this->lastErrno() == 17; // SQLITE_SCHEMA;
707 function wasReadOnlyError() {
708 return $this->lastErrno() == 8; // SQLITE_READONLY;
712 * @return string Wikitext of a link to the server software's web site
714 public function getSoftwareLink() {
715 return "[{{int:version-db-sqlite-url}} SQLite]";
719 * @return string Version information from the database
721 function getServerVersion() {
722 $ver = $this->mConn
->getAttribute( PDO
::ATTR_SERVER_VERSION
);
728 * Get information about a given field
729 * Returns false if the field does not exist.
731 * @param string $table
732 * @param string $field
733 * @return SQLiteField|bool False on failure
735 function fieldInfo( $table, $field ) {
736 $tableName = $this->tableName( $table );
737 $sql = 'PRAGMA table_info(' . $this->addQuotes( $tableName ) . ')';
738 $res = $this->query( $sql, __METHOD__
);
739 foreach ( $res as $row ) {
740 if ( $row->name
== $field ) {
741 return new SQLiteField( $row, $tableName );
748 protected function doBegin( $fname = '' ) {
749 if ( $this->trxMode
) {
750 $this->query( "BEGIN {$this->trxMode}", $fname );
752 $this->query( 'BEGIN', $fname );
754 $this->mTrxLevel
= 1;
761 function strencode( $s ) {
762 return substr( $this->addQuotes( $s ), 1, -1 );
769 function encodeBlob( $b ) {
770 return new Blob( $b );
774 * @param Blob|string $b
777 function decodeBlob( $b ) {
778 if ( $b instanceof Blob
) {
786 * @param Blob|string $s
789 function addQuotes( $s ) {
790 if ( $s instanceof Blob
) {
791 return "x'" . bin2hex( $s->fetch() ) . "'";
792 } elseif ( is_bool( $s ) ) {
794 } elseif ( strpos( $s, "\0" ) !== false ) {
795 // SQLite doesn't support \0 in strings, so use the hex representation as a workaround.
796 // This is a known limitation of SQLite's mprintf function which PDO
797 // should work around, but doesn't. I have reported this to php.net as bug #63419:
798 // https://bugs.php.net/bug.php?id=63419
799 // There was already a similar report for SQLite3::escapeString, bug #62361:
800 // https://bugs.php.net/bug.php?id=62361
801 // There is an additional bug regarding sorting this data after insert
802 // on older versions of sqlite shipped with ubuntu 12.04
803 // https://phabricator.wikimedia.org/T74367
804 $this->queryLogger
->debug(
806 ': Quoting value containing null byte. ' .
807 'For consistency all binary data should have been ' .
808 'first processed with self::encodeBlob()'
810 return "x'" . bin2hex( $s ) . "'";
812 return $this->mConn
->quote( $s );
819 function buildLike() {
820 $params = func_get_args();
821 if ( count( $params ) > 0 && is_array( $params[0] ) ) {
822 $params = $params[0];
825 return parent
::buildLike( $params ) . "ESCAPE '\' ";
829 * @param string $field Field or column to cast
833 public function buildStringCast( $field ) {
834 return 'CAST ( ' . $field . ' AS TEXT )';
838 * No-op version of deadlockLoop
842 public function deadlockLoop( /*...*/ ) {
843 $args = func_get_args();
844 $function = array_shift( $args );
846 return call_user_func_array( $function, $args );
853 protected function replaceVars( $s ) {
854 $s = parent
::replaceVars( $s );
855 if ( preg_match( '/^\s*(CREATE|ALTER) TABLE/i', $s ) ) {
856 // CREATE TABLE hacks to allow schema file sharing with MySQL
858 // binary/varbinary column type -> blob
859 $s = preg_replace( '/\b(var)?binary(\(\d+\))/i', 'BLOB', $s );
860 // no such thing as unsigned
861 $s = preg_replace( '/\b(un)?signed\b/i', '', $s );
863 $s = preg_replace( '/\b(tiny|small|medium|big|)int(\s*\(\s*\d+\s*\)|\b)/i', 'INTEGER', $s );
864 // floating point types -> REAL
866 '/\b(float|double(\s+precision)?)(\s*\(\s*\d+\s*(,\s*\d+\s*)?\)|\b)/i',
871 $s = preg_replace( '/\b(var)?char\s*\(.*?\)/i', 'TEXT', $s );
872 // TEXT normalization
873 $s = preg_replace( '/\b(tiny|medium|long)text\b/i', 'TEXT', $s );
874 // BLOB normalization
875 $s = preg_replace( '/\b(tiny|small|medium|long|)blob\b/i', 'BLOB', $s );
877 $s = preg_replace( '/\bbool(ean)?\b/i', 'INTEGER', $s );
879 $s = preg_replace( '/\b(datetime|timestamp)\b/i', 'TEXT', $s );
881 $s = preg_replace( '/\benum\s*\([^)]*\)/i', 'TEXT', $s );
882 // binary collation type -> nothing
883 $s = preg_replace( '/\bbinary\b/i', '', $s );
884 // auto_increment -> autoincrement
885 $s = preg_replace( '/\bauto_increment\b/i', 'AUTOINCREMENT', $s );
886 // No explicit options
887 $s = preg_replace( '/\)[^);]*(;?)\s*$/', ')\1', $s );
888 // AUTOINCREMENT should immedidately follow PRIMARY KEY
889 $s = preg_replace( '/primary key (.*?) autoincrement/i', 'PRIMARY KEY AUTOINCREMENT $1', $s );
890 } elseif ( preg_match( '/^\s*CREATE (\s*(?:UNIQUE|FULLTEXT)\s+)?INDEX/i', $s ) ) {
891 // No truncated indexes
892 $s = preg_replace( '/\(\d+\)/', '', $s );
894 $s = preg_replace( '/\bfulltext\b/i', '', $s );
895 } elseif ( preg_match( '/^\s*DROP INDEX/i', $s ) ) {
896 // DROP INDEX is database-wide, not table-specific, so no ON <table> clause.
897 $s = preg_replace( '/\sON\s+[^\s]*/i', '', $s );
898 } elseif ( preg_match( '/^\s*INSERT IGNORE\b/i', $s ) ) {
899 // INSERT IGNORE --> INSERT OR IGNORE
900 $s = preg_replace( '/^\s*INSERT IGNORE\b/i', 'INSERT OR IGNORE', $s );
906 public function lock( $lockName, $method, $timeout = 5 ) {
907 if ( !is_dir( "{$this->dbDir}/locks" ) ) { // create dir as needed
908 if ( !is_writable( $this->dbDir
) ||
!mkdir( "{$this->dbDir}/locks" ) ) {
909 throw new DBError( $this, "Cannot create directory \"{$this->dbDir}/locks\"." );
913 return $this->lockMgr
->lock( [ $lockName ], LockManager
::LOCK_EX
, $timeout )->isOK();
916 public function unlock( $lockName, $method ) {
917 return $this->lockMgr
->unlock( [ $lockName ], LockManager
::LOCK_EX
)->isOK();
921 * Build a concatenation list to feed into a SQL query
923 * @param string[] $stringList
926 function buildConcat( $stringList ) {
927 return '(' . implode( ') || (', $stringList ) . ')';
930 public function buildGroupConcatField(
931 $delim, $table, $field, $conds = '', $join_conds = []
933 $fld = "group_concat($field," . $this->addQuotes( $delim ) . ')';
935 return '(' . $this->selectSQLText( $table, $fld, $conds, null, [], $join_conds ) . ')';
939 * @param string $oldName
940 * @param string $newName
941 * @param bool $temporary
942 * @param string $fname
943 * @return bool|ResultWrapper
944 * @throws RuntimeException
946 function duplicateTableStructure( $oldName, $newName, $temporary = false, $fname = __METHOD__
) {
947 $res = $this->query( "SELECT sql FROM sqlite_master WHERE tbl_name=" .
948 $this->addQuotes( $oldName ) . " AND type='table'", $fname );
949 $obj = $this->fetchObject( $res );
951 throw new RuntimeException( "Couldn't retrieve structure for table $oldName" );
955 '/(?<=\W)"?' . preg_quote( trim( $this->addIdentifierQuotes( $oldName ), '"' ) ) . '"?(?=\W)/',
956 $this->addIdentifierQuotes( $newName ),
961 if ( preg_match( '/^\\s*CREATE\\s+VIRTUAL\\s+TABLE\b/i', $sql ) ) {
962 $this->queryLogger
->debug(
963 "Table $oldName is virtual, can't create a temporary duplicate.\n" );
965 $sql = str_replace( 'CREATE TABLE', 'CREATE TEMPORARY TABLE', $sql );
969 $res = $this->query( $sql, $fname );
972 $indexList = $this->query( 'PRAGMA INDEX_LIST(' . $this->addQuotes( $oldName ) . ')' );
973 foreach ( $indexList as $index ) {
974 if ( strpos( $index->name
, 'sqlite_autoindex' ) === 0 ) {
978 if ( $index->unique
) {
979 $sql = 'CREATE UNIQUE INDEX';
981 $sql = 'CREATE INDEX';
983 // Try to come up with a new index name, given indexes have database scope in SQLite
984 $indexName = $newName . '_' . $index->name
;
985 $sql .= ' ' . $indexName . ' ON ' . $newName;
987 $indexInfo = $this->query( 'PRAGMA INDEX_INFO(' . $this->addQuotes( $index->name
) . ')' );
989 foreach ( $indexInfo as $indexInfoRow ) {
990 $fields[$indexInfoRow->seqno
] = $indexInfoRow->name
;
993 $sql .= '(' . implode( ',', $fields ) . ')';
995 $this->query( $sql );
1002 * List all tables on the database
1004 * @param string $prefix Only show tables with this prefix, e.g. mw_
1005 * @param string $fname Calling function name
1009 function listTables( $prefix = null, $fname = __METHOD__
) {
1010 $result = $this->select(
1018 foreach ( $result as $table ) {
1019 $vars = get_object_vars( $table );
1020 $table = array_pop( $vars );
1022 if ( !$prefix ||
strpos( $table, $prefix ) === 0 ) {
1023 if ( strpos( $table, 'sqlite_' ) !== 0 ) {
1024 $endArray[] = $table;
1033 * Override due to no CASCADE support
1035 * @param string $tableName
1036 * @param string $fName
1037 * @return bool|ResultWrapper
1038 * @throws DBReadOnlyError
1040 public function dropTable( $tableName, $fName = __METHOD__
) {
1041 if ( !$this->tableExists( $tableName, $fName ) ) {
1044 $sql = "DROP TABLE " . $this->tableName( $tableName );
1046 return $this->query( $sql, $fName );
1049 protected function requiresDatabaseUser() {
1050 return false; // just a file
1056 public function __toString() {
1057 return 'SQLite ' . (string)$this->mConn
->getAttribute( PDO
::ATTR_SERVER_VERSION
);