31bb26b75c2c2194168be0ab89d1688b68b69efe
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 Database
{
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;
47 /** @var $mConn PDO */
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 );
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' );
202 * @return string SQLite DB file path
205 public function getDbFilePath() {
206 return $this->dbPath
;
210 * Does not actually close the connection, just destroys the reference for GC to do its work
213 protected function closeConnection() {
220 * Generates a database file name. Explicitly public for installer.
221 * @param string $dir Directory where database resides
222 * @param string $dbName Database name
225 public static function generateFileName( $dir, $dbName ) {
226 return "$dir/$dbName.sqlite";
230 * Check if the searchindext table is FTS enabled.
231 * @return bool False if not enabled.
233 function checkForEnabledSearch() {
234 if ( self
::$fulltextEnabled === null ) {
235 self
::$fulltextEnabled = false;
236 $table = $this->tableName( 'searchindex' );
237 $res = $this->query( "SELECT sql FROM sqlite_master WHERE tbl_name = '$table'", __METHOD__
);
239 $row = $res->fetchRow();
240 self
::$fulltextEnabled = stristr( $row['sql'], 'fts' ) !== false;
244 return self
::$fulltextEnabled;
248 * Returns version of currently supported SQLite fulltext search module or false if none present.
251 static function getFulltextSearchModule() {
252 static $cachedResult = null;
253 if ( $cachedResult !== null ) {
254 return $cachedResult;
256 $cachedResult = false;
257 $table = 'dummy_search_test';
259 $db = self
::newStandaloneInstance( ':memory:' );
260 if ( $db->query( "CREATE VIRTUAL TABLE $table USING FTS3(dummy_field)", __METHOD__
, true ) ) {
261 $cachedResult = 'FTS3';
265 return $cachedResult;
269 * Attaches external database to our connection, see http://sqlite.org/lang_attach.html
272 * @param string $name Database name to be used in queries like
273 * SELECT foo FROM dbname.table
274 * @param bool|string $file Database file name. If omitted, will be generated
275 * using $name and configured data directory
276 * @param string $fname Calling function name
277 * @return ResultWrapper
279 function attachDatabase( $name, $file = false, $fname = __METHOD__
) {
281 $file = self
::generateFileName( $this->dbDir
, $name );
283 $file = $this->addQuotes( $file );
285 return $this->query( "ATTACH DATABASE $file AS $name", $fname );
288 function isWriteQuery( $sql ) {
289 return parent
::isWriteQuery( $sql ) && !preg_match( '/^(ATTACH|PRAGMA)\b/i', $sql );
293 * SQLite doesn't allow buffered results or data seeking etc, so we'll use fetchAll as the result
296 * @return bool|ResultWrapper
298 protected function doQuery( $sql ) {
299 $res = $this->mConn
->query( $sql );
300 if ( $res === false ) {
304 $r = $res instanceof ResultWrapper ?
$res->result
: $res;
305 $this->mAffectedRows
= $r->rowCount();
306 $res = new ResultWrapper( $this, $r->fetchAll() );
312 * @param ResultWrapper|mixed $res
314 function freeResult( $res ) {
315 if ( $res instanceof ResultWrapper
) {
323 * @param ResultWrapper|array $res
324 * @return stdClass|bool
326 function fetchObject( $res ) {
327 if ( $res instanceof ResultWrapper
) {
333 $cur = current( $r );
334 if ( is_array( $cur ) ) {
337 foreach ( $cur as $k => $v ) {
338 if ( !is_numeric( $k ) ) {
350 * @param ResultWrapper|mixed $res
353 function fetchRow( $res ) {
354 if ( $res instanceof ResultWrapper
) {
359 $cur = current( $r );
360 if ( is_array( $cur ) ) {
370 * The PDO::Statement class implements the array interface so count() will work
372 * @param ResultWrapper|array $res
375 function numRows( $res ) {
376 $r = $res instanceof ResultWrapper ?
$res->result
: $res;
382 * @param ResultWrapper $res
385 function numFields( $res ) {
386 $r = $res instanceof ResultWrapper ?
$res->result
: $res;
387 if ( is_array( $r ) && count( $r ) > 0 ) {
388 // The size of the result array is twice the number of fields. (Bug: 65578)
389 return count( $r[0] ) / 2;
391 // If the result is empty return 0
397 * @param ResultWrapper $res
401 function fieldName( $res, $n ) {
402 $r = $res instanceof ResultWrapper ?
$res->result
: $res;
403 if ( is_array( $r ) ) {
404 $keys = array_keys( $r[0] );
413 * Use MySQL's naming (accounts for prefix etc) but remove surrounding backticks
415 * @param string $name
416 * @param string $format
419 function tableName( $name, $format = 'quoted' ) {
420 // table names starting with sqlite_ are reserved
421 if ( strpos( $name, 'sqlite_' ) === 0 ) {
425 return str_replace( '"', '', parent
::tableName( $name, $format ) );
429 * This must be called after nextSequenceVal
433 function insertId() {
434 // PDO::lastInsertId yields a string :(
435 return intval( $this->mConn
->lastInsertId() );
439 * @param ResultWrapper|array $res
442 function dataSeek( $res, $row ) {
443 if ( $res instanceof ResultWrapper
) {
450 for ( $i = 0; $i < $row; $i++
) {
459 function lastError() {
460 if ( !is_object( $this->mConn
) ) {
461 return "Cannot return last error, no db connection";
463 $e = $this->mConn
->errorInfo();
465 return isset( $e[2] ) ?
$e[2] : '';
471 function lastErrno() {
472 if ( !is_object( $this->mConn
) ) {
473 return "Cannot return last error, no db connection";
475 $info = $this->mConn
->errorInfo();
484 function affectedRows() {
485 return $this->mAffectedRows
;
489 * Returns information about an index
490 * Returns false if the index does not exist
491 * - if errors are explicitly ignored, returns NULL on failure
493 * @param string $table
494 * @param string $index
495 * @param string $fname
498 function indexInfo( $table, $index, $fname = __METHOD__
) {
499 $sql = 'PRAGMA index_info(' . $this->addQuotes( $this->indexName( $index ) ) . ')';
500 $res = $this->query( $sql, $fname );
504 if ( $res->numRows() == 0 ) {
508 foreach ( $res as $row ) {
509 $info[] = $row->name
;
516 * @param string $table
517 * @param string $index
518 * @param string $fname
521 function indexUnique( $table, $index, $fname = __METHOD__
) {
522 $row = $this->selectRow( 'sqlite_master', '*',
525 'name' => $this->indexName( $index ),
527 if ( !$row ||
!isset( $row->sql
) ) {
531 // $row->sql will be of the form CREATE [UNIQUE] INDEX ...
532 $indexPos = strpos( $row->sql
, 'INDEX' );
533 if ( $indexPos === false ) {
536 $firstPart = substr( $row->sql
, 0, $indexPos );
537 $options = explode( ' ', $firstPart );
539 return in_array( 'UNIQUE', $options );
543 * Filter the options used in SELECT statements
545 * @param array $options
548 function makeSelectOptions( $options ) {
549 foreach ( $options as $k => $v ) {
550 if ( is_numeric( $k ) && ( $v == 'FOR UPDATE' ||
$v == 'LOCK IN SHARE MODE' ) ) {
555 return parent
::makeSelectOptions( $options );
559 * @param array $options
562 protected function makeUpdateOptionsArray( $options ) {
563 $options = parent
::makeUpdateOptionsArray( $options );
564 $options = self
::fixIgnore( $options );
570 * @param array $options
573 static function fixIgnore( $options ) {
574 # SQLite uses OR IGNORE not just IGNORE
575 foreach ( $options as $k => $v ) {
576 if ( $v == 'IGNORE' ) {
577 $options[$k] = 'OR IGNORE';
585 * @param array $options
588 function makeInsertOptions( $options ) {
589 $options = self
::fixIgnore( $options );
591 return parent
::makeInsertOptions( $options );
595 * Based on generic method (parent) with some prior SQLite-sepcific adjustments
596 * @param string $table
598 * @param string $fname
599 * @param array $options
602 function insert( $table, $a, $fname = __METHOD__
, $options = [] ) {
603 if ( !count( $a ) ) {
607 # SQLite can't handle multi-row inserts, so divide up into multiple single-row inserts
608 if ( isset( $a[0] ) && is_array( $a[0] ) ) {
610 foreach ( $a as $v ) {
611 if ( !parent
::insert( $table, $v, "$fname/multi-row", $options ) ) {
616 $ret = parent
::insert( $table, $a, "$fname/single-row", $options );
623 * @param string $table
624 * @param array $uniqueIndexes Unused
625 * @param string|array $rows
626 * @param string $fname
627 * @return bool|ResultWrapper
629 function replace( $table, $uniqueIndexes, $rows, $fname = __METHOD__
) {
630 if ( !count( $rows ) ) {
634 # SQLite can't handle multi-row replaces, so divide up into multiple single-row queries
635 if ( isset( $rows[0] ) && is_array( $rows[0] ) ) {
637 foreach ( $rows as $v ) {
638 if ( !$this->nativeReplace( $table, $v, "$fname/multi-row" ) ) {
643 $ret = $this->nativeReplace( $table, $rows, "$fname/single-row" );
650 * Returns the size of a text field, or -1 for "unlimited"
651 * In SQLite this is SQLITE_MAX_LENGTH, by default 1GB. No way to query it though.
653 * @param string $table
654 * @param string $field
657 function textFieldSize( $table, $field ) {
664 function unionSupportsOrderAndLimit() {
669 * @param string $sqls
670 * @param bool $all Whether to "UNION ALL" or not
673 function unionQueries( $sqls, $all ) {
674 $glue = $all ?
' UNION ALL ' : ' UNION ';
676 return implode( $glue, $sqls );
682 function wasDeadlock() {
683 return $this->lastErrno() == 5; // SQLITE_BUSY
689 function wasErrorReissuable() {
690 return $this->lastErrno() == 17; // SQLITE_SCHEMA;
696 function wasReadOnlyError() {
697 return $this->lastErrno() == 8; // SQLITE_READONLY;
701 * @return string Wikitext of a link to the server software's web site
703 public function getSoftwareLink() {
704 return "[{{int:version-db-sqlite-url}} SQLite]";
708 * @return string Version information from the database
710 function getServerVersion() {
711 $ver = $this->mConn
->getAttribute( PDO
::ATTR_SERVER_VERSION
);
717 * Get information about a given field
718 * Returns false if the field does not exist.
720 * @param string $table
721 * @param string $field
722 * @return SQLiteField|bool False on failure
724 function fieldInfo( $table, $field ) {
725 $tableName = $this->tableName( $table );
726 $sql = 'PRAGMA table_info(' . $this->addQuotes( $tableName ) . ')';
727 $res = $this->query( $sql, __METHOD__
);
728 foreach ( $res as $row ) {
729 if ( $row->name
== $field ) {
730 return new SQLiteField( $row, $tableName );
737 protected function doBegin( $fname = '' ) {
738 if ( $this->trxMode
) {
739 $this->query( "BEGIN {$this->trxMode}", $fname );
741 $this->query( 'BEGIN', $fname );
743 $this->mTrxLevel
= 1;
750 function strencode( $s ) {
751 return substr( $this->addQuotes( $s ), 1, -1 );
758 function encodeBlob( $b ) {
759 return new Blob( $b );
763 * @param Blob|string $b
766 function decodeBlob( $b ) {
767 if ( $b instanceof Blob
) {
775 * @param string|int|null|bool|Blob $s
778 function addQuotes( $s ) {
779 if ( $s instanceof Blob
) {
780 return "x'" . bin2hex( $s->fetch() ) . "'";
781 } elseif ( is_bool( $s ) ) {
783 } elseif ( strpos( $s, "\0" ) !== false ) {
784 // SQLite doesn't support \0 in strings, so use the hex representation as a workaround.
785 // This is a known limitation of SQLite's mprintf function which PDO
786 // should work around, but doesn't. I have reported this to php.net as bug #63419:
787 // https://bugs.php.net/bug.php?id=63419
788 // There was already a similar report for SQLite3::escapeString, bug #62361:
789 // https://bugs.php.net/bug.php?id=62361
790 // There is an additional bug regarding sorting this data after insert
791 // on older versions of sqlite shipped with ubuntu 12.04
792 // https://phabricator.wikimedia.org/T74367
793 $this->queryLogger
->debug(
795 ': Quoting value containing null byte. ' .
796 'For consistency all binary data should have been ' .
797 'first processed with self::encodeBlob()'
799 return "x'" . bin2hex( $s ) . "'";
801 return $this->mConn
->quote( $s );
808 function buildLike() {
809 $params = func_get_args();
810 if ( count( $params ) > 0 && is_array( $params[0] ) ) {
811 $params = $params[0];
814 return parent
::buildLike( $params ) . "ESCAPE '\' ";
818 * @param string $field Field or column to cast
822 public function buildStringCast( $field ) {
823 return 'CAST ( ' . $field . ' AS TEXT )';
827 * No-op version of deadlockLoop
831 public function deadlockLoop( /*...*/ ) {
832 $args = func_get_args();
833 $function = array_shift( $args );
835 return call_user_func_array( $function, $args );
842 protected function replaceVars( $s ) {
843 $s = parent
::replaceVars( $s );
844 if ( preg_match( '/^\s*(CREATE|ALTER) TABLE/i', $s ) ) {
845 // CREATE TABLE hacks to allow schema file sharing with MySQL
847 // binary/varbinary column type -> blob
848 $s = preg_replace( '/\b(var)?binary(\(\d+\))/i', 'BLOB', $s );
849 // no such thing as unsigned
850 $s = preg_replace( '/\b(un)?signed\b/i', '', $s );
852 $s = preg_replace( '/\b(tiny|small|medium|big|)int(\s*\(\s*\d+\s*\)|\b)/i', 'INTEGER', $s );
853 // floating point types -> REAL
855 '/\b(float|double(\s+precision)?)(\s*\(\s*\d+\s*(,\s*\d+\s*)?\)|\b)/i',
860 $s = preg_replace( '/\b(var)?char\s*\(.*?\)/i', 'TEXT', $s );
861 // TEXT normalization
862 $s = preg_replace( '/\b(tiny|medium|long)text\b/i', 'TEXT', $s );
863 // BLOB normalization
864 $s = preg_replace( '/\b(tiny|small|medium|long|)blob\b/i', 'BLOB', $s );
866 $s = preg_replace( '/\bbool(ean)?\b/i', 'INTEGER', $s );
868 $s = preg_replace( '/\b(datetime|timestamp)\b/i', 'TEXT', $s );
870 $s = preg_replace( '/\benum\s*\([^)]*\)/i', 'TEXT', $s );
871 // binary collation type -> nothing
872 $s = preg_replace( '/\bbinary\b/i', '', $s );
873 // auto_increment -> autoincrement
874 $s = preg_replace( '/\bauto_increment\b/i', 'AUTOINCREMENT', $s );
875 // No explicit options
876 $s = preg_replace( '/\)[^);]*(;?)\s*$/', ')\1', $s );
877 // AUTOINCREMENT should immedidately follow PRIMARY KEY
878 $s = preg_replace( '/primary key (.*?) autoincrement/i', 'PRIMARY KEY AUTOINCREMENT $1', $s );
879 } elseif ( preg_match( '/^\s*CREATE (\s*(?:UNIQUE|FULLTEXT)\s+)?INDEX/i', $s ) ) {
880 // No truncated indexes
881 $s = preg_replace( '/\(\d+\)/', '', $s );
883 $s = preg_replace( '/\bfulltext\b/i', '', $s );
884 } elseif ( preg_match( '/^\s*DROP INDEX/i', $s ) ) {
885 // DROP INDEX is database-wide, not table-specific, so no ON <table> clause.
886 $s = preg_replace( '/\sON\s+[^\s]*/i', '', $s );
887 } elseif ( preg_match( '/^\s*INSERT IGNORE\b/i', $s ) ) {
888 // INSERT IGNORE --> INSERT OR IGNORE
889 $s = preg_replace( '/^\s*INSERT IGNORE\b/i', 'INSERT OR IGNORE', $s );
895 public function lock( $lockName, $method, $timeout = 5 ) {
896 if ( !is_dir( "{$this->dbDir}/locks" ) ) { // create dir as needed
897 if ( !is_writable( $this->dbDir
) ||
!mkdir( "{$this->dbDir}/locks" ) ) {
898 throw new DBError( $this, "Cannot create directory \"{$this->dbDir}/locks\"." );
902 return $this->lockMgr
->lock( [ $lockName ], LockManager
::LOCK_EX
, $timeout )->isOK();
905 public function unlock( $lockName, $method ) {
906 return $this->lockMgr
->unlock( [ $lockName ], LockManager
::LOCK_EX
)->isOK();
910 * Build a concatenation list to feed into a SQL query
912 * @param string[] $stringList
915 function buildConcat( $stringList ) {
916 return '(' . implode( ') || (', $stringList ) . ')';
919 public function buildGroupConcatField(
920 $delim, $table, $field, $conds = '', $join_conds = []
922 $fld = "group_concat($field," . $this->addQuotes( $delim ) . ')';
924 return '(' . $this->selectSQLText( $table, $fld, $conds, null, [], $join_conds ) . ')';
928 * @param string $oldName
929 * @param string $newName
930 * @param bool $temporary
931 * @param string $fname
932 * @return bool|ResultWrapper
933 * @throws RuntimeException
935 function duplicateTableStructure( $oldName, $newName, $temporary = false, $fname = __METHOD__
) {
936 $res = $this->query( "SELECT sql FROM sqlite_master WHERE tbl_name=" .
937 $this->addQuotes( $oldName ) . " AND type='table'", $fname );
938 $obj = $this->fetchObject( $res );
940 throw new RuntimeException( "Couldn't retrieve structure for table $oldName" );
944 '/(?<=\W)"?' . preg_quote( trim( $this->addIdentifierQuotes( $oldName ), '"' ) ) . '"?(?=\W)/',
945 $this->addIdentifierQuotes( $newName ),
950 if ( preg_match( '/^\\s*CREATE\\s+VIRTUAL\\s+TABLE\b/i', $sql ) ) {
951 $this->queryLogger
->debug(
952 "Table $oldName is virtual, can't create a temporary duplicate.\n" );
954 $sql = str_replace( 'CREATE TABLE', 'CREATE TEMPORARY TABLE', $sql );
958 $res = $this->query( $sql, $fname );
961 $indexList = $this->query( 'PRAGMA INDEX_LIST(' . $this->addQuotes( $oldName ) . ')' );
962 foreach ( $indexList as $index ) {
963 if ( strpos( $index->name
, 'sqlite_autoindex' ) === 0 ) {
967 if ( $index->unique
) {
968 $sql = 'CREATE UNIQUE INDEX';
970 $sql = 'CREATE INDEX';
972 // Try to come up with a new index name, given indexes have database scope in SQLite
973 $indexName = $newName . '_' . $index->name
;
974 $sql .= ' ' . $indexName . ' ON ' . $newName;
976 $indexInfo = $this->query( 'PRAGMA INDEX_INFO(' . $this->addQuotes( $index->name
) . ')' );
978 foreach ( $indexInfo as $indexInfoRow ) {
979 $fields[$indexInfoRow->seqno
] = $indexInfoRow->name
;
982 $sql .= '(' . implode( ',', $fields ) . ')';
984 $this->query( $sql );
991 * List all tables on the database
993 * @param string $prefix Only show tables with this prefix, e.g. mw_
994 * @param string $fname Calling function name
998 function listTables( $prefix = null, $fname = __METHOD__
) {
999 $result = $this->select(
1007 foreach ( $result as $table ) {
1008 $vars = get_object_vars( $table );
1009 $table = array_pop( $vars );
1011 if ( !$prefix ||
strpos( $table, $prefix ) === 0 ) {
1012 if ( strpos( $table, 'sqlite_' ) !== 0 ) {
1013 $endArray[] = $table;
1022 * Override due to no CASCADE support
1024 * @param string $tableName
1025 * @param string $fName
1026 * @return bool|ResultWrapper
1027 * @throws DBReadOnlyError
1029 public function dropTable( $tableName, $fName = __METHOD__
) {
1030 if ( !$this->tableExists( $tableName, $fName ) ) {
1033 $sql = "DROP TABLE " . $this->tableName( $tableName );
1035 return $this->query( $sql, $fName );
1038 protected function requiresDatabaseUser() {
1039 return false; // just a file
1045 public function __toString() {
1046 return 'SQLite ' . (string)$this->mConn
->getAttribute( PDO
::ATTR_SERVER_VERSION
);