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 namespace Wikimedia\Rdbms
;
30 use DBConnectionError
;
32 use InvalidArgumentException
;
40 class DatabaseSqlite
extends Database
{
41 /** @var bool Whether full text is enabled */
42 private static $fulltextEnabled = null;
44 /** @var string Directory */
46 /** @var string File name for SQLite database file */
48 /** @var string Transaction mode */
51 /** @var int The number of rows affected as an integer */
52 protected $mAffectedRows;
54 protected $mLastResult;
59 /** @var FSLockManager (hopefully on the same server as the DB) */
63 * Additional params include:
64 * - dbDirectory : directory containing the DB and the lock file directory
65 * [defaults to $wgSQLiteDataDir]
66 * - dbFilePath : use this to force the path of the DB file
67 * - trxMode : one of (deferred, immediate, exclusive)
70 function __construct( array $p ) {
71 if ( isset( $p['dbFilePath'] ) ) {
72 parent
::__construct( $p );
73 // Standalone .sqlite file mode.
74 // Super doesn't open when $user is false, but we can work with $dbName,
75 // which is derived from the file path in this case.
76 $this->openFile( $p['dbFilePath'] );
77 $lockDomain = md5( $p['dbFilePath'] );
78 } elseif ( !isset( $p['dbDirectory'] ) ) {
79 throw new InvalidArgumentException( "Need 'dbDirectory' or 'dbFilePath' parameter." );
81 $this->dbDir
= $p['dbDirectory'];
82 $this->mDBname
= $p['dbname'];
83 $lockDomain = $this->mDBname
;
84 // Stock wiki mode using standard file names per DB.
85 parent
::__construct( $p );
86 // Super doesn't open when $user is false, but we can work with $dbName
87 if ( $p['dbname'] && !$this->isOpen() ) {
88 if ( $this->open( $p['host'], $p['user'], $p['password'], $p['dbname'] ) ) {
90 foreach ( $this->tableAliases
as $params ) {
91 if ( isset( $done[$params['dbname']] ) ) {
94 $this->attachDatabase( $params['dbname'] );
95 $done[$params['dbname']] = 1;
101 $this->trxMode
= isset( $p['trxMode'] ) ?
strtoupper( $p['trxMode'] ) : null;
102 if ( $this->trxMode
&&
103 !in_array( $this->trxMode
, [ 'DEFERRED', 'IMMEDIATE', 'EXCLUSIVE' ] )
105 $this->trxMode
= null;
106 $this->queryLogger
->warning( "Invalid SQLite transaction mode provided." );
109 $this->lockMgr
= new FSLockManager( [
110 'domain' => $lockDomain,
111 'lockDirectory' => "{$this->dbDir}/locks"
116 * @param string $filename
117 * @param array $p Options map; supports:
118 * - flags : (same as __construct counterpart)
119 * - trxMode : (same as __construct counterpart)
120 * - dbDirectory : (same as __construct counterpart)
121 * @return DatabaseSqlite
124 public static function newStandaloneInstance( $filename, array $p = [] ) {
125 $p['dbFilePath'] = $filename;
126 $p['schema'] = false;
127 $p['tablePrefix'] = '';
128 /** @var DatabaseSqlite $db */
129 $db = Database
::factory( 'sqlite', $p );
142 * @todo Check if it should be true like parent class
146 function implicitGroupby() {
150 /** Open an SQLite database and return a resource handle to it
151 * NOTE: only $dbName is used, the other parameters are irrelevant for SQLite databases
153 * @param string $server
154 * @param string $user
155 * @param string $pass
156 * @param string $dbName
158 * @throws DBConnectionError
161 function open( $server, $user, $pass, $dbName ) {
163 $fileName = self
::generateFileName( $this->dbDir
, $dbName );
164 if ( !is_readable( $fileName ) ) {
165 $this->mConn
= false;
166 throw new DBConnectionError( $this, "SQLite database not accessible" );
168 $this->openFile( $fileName );
170 return (bool)$this->mConn
;
174 * Opens a database file
176 * @param string $fileName
177 * @throws DBConnectionError
178 * @return PDO|bool SQL connection or false if failed
180 protected function openFile( $fileName ) {
183 $this->dbPath
= $fileName;
185 if ( $this->mFlags
& self
::DBO_PERSISTENT
) {
186 $this->mConn
= new PDO( "sqlite:$fileName", '', '',
187 [ PDO
::ATTR_PERSISTENT
=> true ] );
189 $this->mConn
= new PDO( "sqlite:$fileName", '', '' );
191 } catch ( PDOException
$e ) {
192 $err = $e->getMessage();
195 if ( !$this->mConn
) {
196 $this->queryLogger
->debug( "DB connection error: $err\n" );
197 throw new DBConnectionError( $this, $err );
200 $this->mOpened
= !!$this->mConn
;
201 if ( $this->mOpened
) {
202 # Set error codes only, don't raise exceptions
203 $this->mConn
->setAttribute( PDO
::ATTR_ERRMODE
, PDO
::ERRMODE_SILENT
);
204 # Enforce LIKE to be case sensitive, just like MySQL
205 $this->query( 'PRAGMA case_sensitive_like = 1' );
213 public function selectDB( $db ) {
214 return false; // doesn't make sense
218 * @return string SQLite DB file path
221 public function getDbFilePath() {
222 return $this->dbPath
;
226 * Does not actually close the connection, just destroys the reference for GC to do its work
229 protected function closeConnection() {
236 * Generates a database file name. Explicitly public for installer.
237 * @param string $dir Directory where database resides
238 * @param string $dbName Database name
241 public static function generateFileName( $dir, $dbName ) {
242 return "$dir/$dbName.sqlite";
246 * Check if the searchindext table is FTS enabled.
247 * @return bool False if not enabled.
249 function checkForEnabledSearch() {
250 if ( self
::$fulltextEnabled === null ) {
251 self
::$fulltextEnabled = false;
252 $table = $this->tableName( 'searchindex' );
253 $res = $this->query( "SELECT sql FROM sqlite_master WHERE tbl_name = '$table'", __METHOD__
);
255 $row = $res->fetchRow();
256 self
::$fulltextEnabled = stristr( $row['sql'], 'fts' ) !== false;
260 return self
::$fulltextEnabled;
264 * Returns version of currently supported SQLite fulltext search module or false if none present.
267 static function getFulltextSearchModule() {
268 static $cachedResult = null;
269 if ( $cachedResult !== null ) {
270 return $cachedResult;
272 $cachedResult = false;
273 $table = 'dummy_search_test';
275 $db = self
::newStandaloneInstance( ':memory:' );
276 if ( $db->query( "CREATE VIRTUAL TABLE $table USING FTS3(dummy_field)", __METHOD__
, true ) ) {
277 $cachedResult = 'FTS3';
281 return $cachedResult;
285 * Attaches external database to our connection, see https://sqlite.org/lang_attach.html
288 * @param string $name Database name to be used in queries like
289 * SELECT foo FROM dbname.table
290 * @param bool|string $file Database file name. If omitted, will be generated
291 * using $name and configured data directory
292 * @param string $fname Calling function name
293 * @return ResultWrapper
295 function attachDatabase( $name, $file = false, $fname = __METHOD__
) {
297 $file = self
::generateFileName( $this->dbDir
, $name );
299 $file = $this->addQuotes( $file );
301 return $this->query( "ATTACH DATABASE $file AS $name", $fname );
304 function isWriteQuery( $sql ) {
305 return parent
::isWriteQuery( $sql ) && !preg_match( '/^(ATTACH|PRAGMA)\b/i', $sql );
309 * SQLite doesn't allow buffered results or data seeking etc, so we'll use fetchAll as the result
312 * @return bool|ResultWrapper
314 protected function doQuery( $sql ) {
315 $res = $this->mConn
->query( $sql );
316 if ( $res === false ) {
320 $r = $res instanceof ResultWrapper ?
$res->result
: $res;
321 $this->mAffectedRows
= $r->rowCount();
322 $res = new ResultWrapper( $this, $r->fetchAll() );
328 * @param ResultWrapper|mixed $res
330 function freeResult( $res ) {
331 if ( $res instanceof ResultWrapper
) {
339 * @param ResultWrapper|array $res
340 * @return stdClass|bool
342 function fetchObject( $res ) {
343 if ( $res instanceof ResultWrapper
) {
349 $cur = current( $r );
350 if ( is_array( $cur ) ) {
353 foreach ( $cur as $k => $v ) {
354 if ( !is_numeric( $k ) ) {
366 * @param ResultWrapper|mixed $res
369 function fetchRow( $res ) {
370 if ( $res instanceof ResultWrapper
) {
375 $cur = current( $r );
376 if ( is_array( $cur ) ) {
386 * The PDO::Statement class implements the array interface so count() will work
388 * @param ResultWrapper|array $res
391 function numRows( $res ) {
392 $r = $res instanceof ResultWrapper ?
$res->result
: $res;
398 * @param ResultWrapper $res
401 function numFields( $res ) {
402 $r = $res instanceof ResultWrapper ?
$res->result
: $res;
403 if ( is_array( $r ) && count( $r ) > 0 ) {
404 // The size of the result array is twice the number of fields. (Bug: 65578)
405 return count( $r[0] ) / 2;
407 // If the result is empty return 0
413 * @param ResultWrapper $res
417 function fieldName( $res, $n ) {
418 $r = $res instanceof ResultWrapper ?
$res->result
: $res;
419 if ( is_array( $r ) ) {
420 $keys = array_keys( $r[0] );
429 * Use MySQL's naming (accounts for prefix etc) but remove surrounding backticks
431 * @param string $name
432 * @param string $format
435 function tableName( $name, $format = 'quoted' ) {
436 // table names starting with sqlite_ are reserved
437 if ( strpos( $name, 'sqlite_' ) === 0 ) {
441 return str_replace( '"', '', parent
::tableName( $name, $format ) );
445 * This must be called after nextSequenceVal
449 function insertId() {
450 // PDO::lastInsertId yields a string :(
451 return intval( $this->mConn
->lastInsertId() );
455 * @param ResultWrapper|array $res
458 function dataSeek( $res, $row ) {
459 if ( $res instanceof ResultWrapper
) {
466 for ( $i = 0; $i < $row; $i++
) {
475 function lastError() {
476 if ( !is_object( $this->mConn
) ) {
477 return "Cannot return last error, no db connection";
479 $e = $this->mConn
->errorInfo();
481 return isset( $e[2] ) ?
$e[2] : '';
487 function lastErrno() {
488 if ( !is_object( $this->mConn
) ) {
489 return "Cannot return last error, no db connection";
491 $info = $this->mConn
->errorInfo();
500 function affectedRows() {
501 return $this->mAffectedRows
;
505 * Returns information about an index
506 * Returns false if the index does not exist
507 * - if errors are explicitly ignored, returns NULL on failure
509 * @param string $table
510 * @param string $index
511 * @param string $fname
512 * @return array|false
514 function indexInfo( $table, $index, $fname = __METHOD__
) {
515 $sql = 'PRAGMA index_info(' . $this->addQuotes( $this->indexName( $index ) ) . ')';
516 $res = $this->query( $sql, $fname );
517 if ( !$res ||
$res->numRows() == 0 ) {
521 foreach ( $res as $row ) {
522 $info[] = $row->name
;
529 * @param string $table
530 * @param string $index
531 * @param string $fname
534 function indexUnique( $table, $index, $fname = __METHOD__
) {
535 $row = $this->selectRow( 'sqlite_master', '*',
538 'name' => $this->indexName( $index ),
540 if ( !$row ||
!isset( $row->sql
) ) {
544 // $row->sql will be of the form CREATE [UNIQUE] INDEX ...
545 $indexPos = strpos( $row->sql
, 'INDEX' );
546 if ( $indexPos === false ) {
549 $firstPart = substr( $row->sql
, 0, $indexPos );
550 $options = explode( ' ', $firstPart );
552 return in_array( 'UNIQUE', $options );
556 * Filter the options used in SELECT statements
558 * @param array $options
561 function makeSelectOptions( $options ) {
562 foreach ( $options as $k => $v ) {
563 if ( is_numeric( $k ) && ( $v == 'FOR UPDATE' ||
$v == 'LOCK IN SHARE MODE' ) ) {
568 return parent
::makeSelectOptions( $options );
572 * @param array $options
575 protected function makeUpdateOptionsArray( $options ) {
576 $options = parent
::makeUpdateOptionsArray( $options );
577 $options = self
::fixIgnore( $options );
583 * @param array $options
586 static function fixIgnore( $options ) {
587 # SQLite uses OR IGNORE not just IGNORE
588 foreach ( $options as $k => $v ) {
589 if ( $v == 'IGNORE' ) {
590 $options[$k] = 'OR IGNORE';
598 * @param array $options
601 function makeInsertOptions( $options ) {
602 $options = self
::fixIgnore( $options );
604 return parent
::makeInsertOptions( $options );
608 * Based on generic method (parent) with some prior SQLite-sepcific adjustments
609 * @param string $table
611 * @param string $fname
612 * @param array $options
615 function insert( $table, $a, $fname = __METHOD__
, $options = [] ) {
616 if ( !count( $a ) ) {
620 # SQLite can't handle multi-row inserts, so divide up into multiple single-row inserts
621 if ( isset( $a[0] ) && is_array( $a[0] ) ) {
623 foreach ( $a as $v ) {
624 if ( !parent
::insert( $table, $v, "$fname/multi-row", $options ) ) {
629 $ret = parent
::insert( $table, $a, "$fname/single-row", $options );
636 * @param string $table
637 * @param array $uniqueIndexes Unused
638 * @param string|array $rows
639 * @param string $fname
640 * @return bool|ResultWrapper
642 function replace( $table, $uniqueIndexes, $rows, $fname = __METHOD__
) {
643 if ( !count( $rows ) ) {
647 # SQLite can't handle multi-row replaces, so divide up into multiple single-row queries
648 if ( isset( $rows[0] ) && is_array( $rows[0] ) ) {
650 foreach ( $rows as $v ) {
651 if ( !$this->nativeReplace( $table, $v, "$fname/multi-row" ) ) {
656 $ret = $this->nativeReplace( $table, $rows, "$fname/single-row" );
663 * Returns the size of a text field, or -1 for "unlimited"
664 * In SQLite this is SQLITE_MAX_LENGTH, by default 1GB. No way to query it though.
666 * @param string $table
667 * @param string $field
670 function textFieldSize( $table, $field ) {
677 function unionSupportsOrderAndLimit() {
682 * @param string[] $sqls
683 * @param bool $all Whether to "UNION ALL" or not
686 function unionQueries( $sqls, $all ) {
687 $glue = $all ?
' UNION ALL ' : ' UNION ';
689 return implode( $glue, $sqls );
695 function wasDeadlock() {
696 return $this->lastErrno() == 5; // SQLITE_BUSY
702 function wasErrorReissuable() {
703 return $this->lastErrno() == 17; // SQLITE_SCHEMA;
709 function wasReadOnlyError() {
710 return $this->lastErrno() == 8; // SQLITE_READONLY;
714 * @return string Wikitext of a link to the server software's web site
716 public function getSoftwareLink() {
717 return "[{{int:version-db-sqlite-url}} SQLite]";
721 * @return string Version information from the database
723 function getServerVersion() {
724 $ver = $this->mConn
->getAttribute( PDO
::ATTR_SERVER_VERSION
);
730 * Get information about a given field
731 * Returns false if the field does not exist.
733 * @param string $table
734 * @param string $field
735 * @return SQLiteField|bool False on failure
737 function fieldInfo( $table, $field ) {
738 $tableName = $this->tableName( $table );
739 $sql = 'PRAGMA table_info(' . $this->addQuotes( $tableName ) . ')';
740 $res = $this->query( $sql, __METHOD__
);
741 foreach ( $res as $row ) {
742 if ( $row->name
== $field ) {
743 return new SQLiteField( $row, $tableName );
750 protected function doBegin( $fname = '' ) {
751 if ( $this->trxMode
) {
752 $this->query( "BEGIN {$this->trxMode}", $fname );
754 $this->query( 'BEGIN', $fname );
756 $this->mTrxLevel
= 1;
763 function strencode( $s ) {
764 return substr( $this->addQuotes( $s ), 1, -1 );
771 function encodeBlob( $b ) {
772 return new Blob( $b );
776 * @param Blob|string $b
779 function decodeBlob( $b ) {
780 if ( $b instanceof Blob
) {
788 * @param string|int|null|bool|Blob $s
791 function addQuotes( $s ) {
792 if ( $s instanceof Blob
) {
793 return "x'" . bin2hex( $s->fetch() ) . "'";
794 } elseif ( is_bool( $s ) ) {
796 } elseif ( strpos( $s, "\0" ) !== false ) {
797 // SQLite doesn't support \0 in strings, so use the hex representation as a workaround.
798 // This is a known limitation of SQLite's mprintf function which PDO
799 // should work around, but doesn't. I have reported this to php.net as bug #63419:
800 // https://bugs.php.net/bug.php?id=63419
801 // There was already a similar report for SQLite3::escapeString, bug #62361:
802 // https://bugs.php.net/bug.php?id=62361
803 // There is an additional bug regarding sorting this data after insert
804 // on older versions of sqlite shipped with ubuntu 12.04
805 // https://phabricator.wikimedia.org/T74367
806 $this->queryLogger
->debug(
808 ': Quoting value containing null byte. ' .
809 'For consistency all binary data should have been ' .
810 'first processed with self::encodeBlob()'
812 return "x'" . bin2hex( $s ) . "'";
814 return $this->mConn
->quote( $s );
821 function buildLike() {
822 $params = func_get_args();
823 if ( count( $params ) > 0 && is_array( $params[0] ) ) {
824 $params = $params[0];
827 return parent
::buildLike( $params ) . "ESCAPE '\' ";
831 * @param string $field Field or column to cast
835 public function buildStringCast( $field ) {
836 return 'CAST ( ' . $field . ' AS TEXT )';
840 * No-op version of deadlockLoop
844 public function deadlockLoop( /*...*/ ) {
845 $args = func_get_args();
846 $function = array_shift( $args );
848 return call_user_func_array( $function, $args );
855 protected function replaceVars( $s ) {
856 $s = parent
::replaceVars( $s );
857 if ( preg_match( '/^\s*(CREATE|ALTER) TABLE/i', $s ) ) {
858 // CREATE TABLE hacks to allow schema file sharing with MySQL
860 // binary/varbinary column type -> blob
861 $s = preg_replace( '/\b(var)?binary(\(\d+\))/i', 'BLOB', $s );
862 // no such thing as unsigned
863 $s = preg_replace( '/\b(un)?signed\b/i', '', $s );
865 $s = preg_replace( '/\b(tiny|small|medium|big|)int(\s*\(\s*\d+\s*\)|\b)/i', 'INTEGER', $s );
866 // floating point types -> REAL
868 '/\b(float|double(\s+precision)?)(\s*\(\s*\d+\s*(,\s*\d+\s*)?\)|\b)/i',
873 $s = preg_replace( '/\b(var)?char\s*\(.*?\)/i', 'TEXT', $s );
874 // TEXT normalization
875 $s = preg_replace( '/\b(tiny|medium|long)text\b/i', 'TEXT', $s );
876 // BLOB normalization
877 $s = preg_replace( '/\b(tiny|small|medium|long|)blob\b/i', 'BLOB', $s );
879 $s = preg_replace( '/\bbool(ean)?\b/i', 'INTEGER', $s );
881 $s = preg_replace( '/\b(datetime|timestamp)\b/i', 'TEXT', $s );
883 $s = preg_replace( '/\benum\s*\([^)]*\)/i', 'TEXT', $s );
884 // binary collation type -> nothing
885 $s = preg_replace( '/\bbinary\b/i', '', $s );
886 // auto_increment -> autoincrement
887 $s = preg_replace( '/\bauto_increment\b/i', 'AUTOINCREMENT', $s );
888 // No explicit options
889 $s = preg_replace( '/\)[^);]*(;?)\s*$/', ')\1', $s );
890 // AUTOINCREMENT should immedidately follow PRIMARY KEY
891 $s = preg_replace( '/primary key (.*?) autoincrement/i', 'PRIMARY KEY AUTOINCREMENT $1', $s );
892 } elseif ( preg_match( '/^\s*CREATE (\s*(?:UNIQUE|FULLTEXT)\s+)?INDEX/i', $s ) ) {
893 // No truncated indexes
894 $s = preg_replace( '/\(\d+\)/', '', $s );
896 $s = preg_replace( '/\bfulltext\b/i', '', $s );
897 } elseif ( preg_match( '/^\s*DROP INDEX/i', $s ) ) {
898 // DROP INDEX is database-wide, not table-specific, so no ON <table> clause.
899 $s = preg_replace( '/\sON\s+[^\s]*/i', '', $s );
900 } elseif ( preg_match( '/^\s*INSERT IGNORE\b/i', $s ) ) {
901 // INSERT IGNORE --> INSERT OR IGNORE
902 $s = preg_replace( '/^\s*INSERT IGNORE\b/i', 'INSERT OR IGNORE', $s );
908 public function lock( $lockName, $method, $timeout = 5 ) {
909 if ( !is_dir( "{$this->dbDir}/locks" ) ) { // create dir as needed
910 if ( !is_writable( $this->dbDir
) ||
!mkdir( "{$this->dbDir}/locks" ) ) {
911 throw new DBError( $this, "Cannot create directory \"{$this->dbDir}/locks\"." );
915 return $this->lockMgr
->lock( [ $lockName ], LockManager
::LOCK_EX
, $timeout )->isOK();
918 public function unlock( $lockName, $method ) {
919 return $this->lockMgr
->unlock( [ $lockName ], LockManager
::LOCK_EX
)->isOK();
923 * Build a concatenation list to feed into a SQL query
925 * @param string[] $stringList
928 function buildConcat( $stringList ) {
929 return '(' . implode( ') || (', $stringList ) . ')';
932 public function buildGroupConcatField(
933 $delim, $table, $field, $conds = '', $join_conds = []
935 $fld = "group_concat($field," . $this->addQuotes( $delim ) . ')';
937 return '(' . $this->selectSQLText( $table, $fld, $conds, null, [], $join_conds ) . ')';
941 * @param string $oldName
942 * @param string $newName
943 * @param bool $temporary
944 * @param string $fname
945 * @return bool|ResultWrapper
946 * @throws RuntimeException
948 function duplicateTableStructure( $oldName, $newName, $temporary = false, $fname = __METHOD__
) {
949 $res = $this->query( "SELECT sql FROM sqlite_master WHERE tbl_name=" .
950 $this->addQuotes( $oldName ) . " AND type='table'", $fname );
951 $obj = $this->fetchObject( $res );
953 throw new RuntimeException( "Couldn't retrieve structure for table $oldName" );
957 '/(?<=\W)"?' . preg_quote( trim( $this->addIdentifierQuotes( $oldName ), '"' ) ) . '"?(?=\W)/',
958 $this->addIdentifierQuotes( $newName ),
963 if ( preg_match( '/^\\s*CREATE\\s+VIRTUAL\\s+TABLE\b/i', $sql ) ) {
964 $this->queryLogger
->debug(
965 "Table $oldName is virtual, can't create a temporary duplicate.\n" );
967 $sql = str_replace( 'CREATE TABLE', 'CREATE TEMPORARY TABLE', $sql );
971 $res = $this->query( $sql, $fname );
974 $indexList = $this->query( 'PRAGMA INDEX_LIST(' . $this->addQuotes( $oldName ) . ')' );
975 foreach ( $indexList as $index ) {
976 if ( strpos( $index->name
, 'sqlite_autoindex' ) === 0 ) {
980 if ( $index->unique
) {
981 $sql = 'CREATE UNIQUE INDEX';
983 $sql = 'CREATE INDEX';
985 // Try to come up with a new index name, given indexes have database scope in SQLite
986 $indexName = $newName . '_' . $index->name
;
987 $sql .= ' ' . $indexName . ' ON ' . $newName;
989 $indexInfo = $this->query( 'PRAGMA INDEX_INFO(' . $this->addQuotes( $index->name
) . ')' );
991 foreach ( $indexInfo as $indexInfoRow ) {
992 $fields[$indexInfoRow->seqno
] = $indexInfoRow->name
;
995 $sql .= '(' . implode( ',', $fields ) . ')';
997 $this->query( $sql );
1004 * List all tables on the database
1006 * @param string $prefix Only show tables with this prefix, e.g. mw_
1007 * @param string $fname Calling function name
1011 function listTables( $prefix = null, $fname = __METHOD__
) {
1012 $result = $this->select(
1020 foreach ( $result as $table ) {
1021 $vars = get_object_vars( $table );
1022 $table = array_pop( $vars );
1024 if ( !$prefix ||
strpos( $table, $prefix ) === 0 ) {
1025 if ( strpos( $table, 'sqlite_' ) !== 0 ) {
1026 $endArray[] = $table;
1035 * Override due to no CASCADE support
1037 * @param string $tableName
1038 * @param string $fName
1039 * @return bool|ResultWrapper
1040 * @throws DBReadOnlyError
1042 public function dropTable( $tableName, $fName = __METHOD__
) {
1043 if ( !$this->tableExists( $tableName, $fName ) ) {
1046 $sql = "DROP TABLE " . $this->tableName( $tableName );
1048 return $this->query( $sql, $fName );
1051 protected function requiresDatabaseUser() {
1052 return false; // just a file
1058 public function __toString() {
1059 return 'SQLite ' . (string)$this->mConn
->getAttribute( PDO
::ATTR_SERVER_VERSION
);
1063 class_alias( DatabaseSqlite
::class, 'DatabaseSqlite' );