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
;
38 class DatabaseSqlite
extends Database
{
39 /** @var string|null Directory for SQLite database files listed under their DB name */
41 /** @var string|null Explicit path for the SQLite database file */
43 /** @var string Transaction mode */
46 /** @var int The number of rows affected as an integer */
47 protected $lastAffectedRowCount;
49 protected $lastResultHandle;
54 /** @var FSLockManager (hopefully on the same server as the DB) */
57 /** @var array List of shared database already attached to this connection */
58 private $alreadyAttached = [];
60 /** @var string[] See https://www.sqlite.org/lang_transaction.html */
61 private static $VALID_TRX_MODES = [ '', 'DEFERRED', 'IMMEDIATE', 'EXCLUSIVE' ];
64 * Additional params include:
65 * - dbDirectory : directory containing the DB and the lock file directory
66 * - dbFilePath : use this to force the path of the DB file
67 * - trxMode : one of (deferred, immediate, exclusive)
70 public function __construct( array $p ) {
71 if ( isset( $p['dbFilePath'] ) ) {
72 $this->dbPath
= $p['dbFilePath'];
73 if ( !strlen( $p['dbname'] ) ) {
74 $p['dbname'] = self
::generateDatabaseName( $this->dbPath
);
76 } elseif ( isset( $p['dbDirectory'] ) ) {
77 $this->dbDir
= $p['dbDirectory'];
80 parent
::__construct( $p );
82 $this->trxMode
= strtoupper( $p['trxMode'] ??
'' );
84 $lockDirectory = $this->getLockFileDirectory();
85 if ( $lockDirectory !== null ) {
86 $this->lockMgr
= new FSLockManager( [
87 'domain' => $this->getDomainID(),
88 'lockDirectory' => $lockDirectory
91 $this->lockMgr
= new NullLockManager( [ 'domain' => $this->getDomainID() ] );
95 protected static function getAttributes() {
96 return [ self
::ATTR_DB_LEVEL_LOCKING
=> true ];
100 * @param string $filename
101 * @param array $p Options map; supports:
102 * - flags : (same as __construct counterpart)
103 * - trxMode : (same as __construct counterpart)
104 * - dbDirectory : (same as __construct counterpart)
105 * @return DatabaseSqlite
108 public static function newStandaloneInstance( $filename, array $p = [] ) {
109 $p['dbFilePath'] = $filename;
111 $p['tablePrefix'] = '';
112 /** @var DatabaseSqlite $db */
113 $db = Database
::factory( 'sqlite', $p );
121 public function getType() {
125 protected function open( $server, $user, $pass, $dbName, $schema, $tablePrefix ) {
128 // Note that for SQLite, $server, $user, and $pass are ignored
130 if ( $schema !== null ) {
131 throw $this->newExceptionAfterConnectError( "Got schema '$schema'; not supported." );
134 if ( $this->dbPath
!== null ) {
135 $path = $this->dbPath
;
136 } elseif ( $this->dbDir
!== null ) {
137 $path = self
::generateFileName( $this->dbDir
, $dbName );
139 throw $this->newExceptionAfterConnectError( "DB path or directory required" );
142 // Check if the database file already exists but is non-readable
144 !self
::isProcessMemoryPath( $path ) &&
145 file_exists( $path ) &&
146 !is_readable( $path )
148 throw $this->newExceptionAfterConnectError( 'SQLite database file is not readable' );
149 } elseif ( !in_array( $this->trxMode
, self
::$VALID_TRX_MODES, true ) ) {
150 throw $this->newExceptionAfterConnectError( "Got mode '{$this->trxMode}' for BEGIN" );
154 if ( $this->getFlag( self
::DBO_PERSISTENT
) ) {
155 // Persistent connections can avoid some schema index reading overhead.
156 // On the other hand, they can cause horrible contention with DBO_TRX.
157 if ( $this->getFlag( self
::DBO_TRX
) ||
$this->getFlag( self
::DBO_DEFAULT
) ) {
158 $this->connLogger
->warning(
159 __METHOD__
. ": ignoring DBO_PERSISTENT due to DBO_TRX or DBO_DEFAULT",
160 $this->getLogContext()
163 $attributes[PDO
::ATTR_PERSISTENT
] = true;
168 // Open the database file, creating it if it does not yet exist
169 $this->conn
= new PDO( "sqlite:$path", null, null, $attributes );
170 } catch ( PDOException
$e ) {
171 throw $this->newExceptionAfterConnectError( $e->getMessage() );
174 $this->currentDomain
= new DatabaseDomain( $dbName, null, $tablePrefix );
177 $flags = self
::QUERY_IGNORE_DBO_TRX | self
::QUERY_NO_RETRY
;
178 // Enforce LIKE to be case sensitive, just like MySQL
179 $this->query( 'PRAGMA case_sensitive_like = 1', __METHOD__
, $flags );
180 // Apply optimizations or requirements regarding fsync() usage
181 $sync = $this->connectionVariables
['synchronous'] ??
null;
182 if ( in_array( $sync, [ 'EXTRA', 'FULL', 'NORMAL', 'OFF' ], true ) ) {
183 $this->query( "PRAGMA synchronous = $sync", __METHOD__
, $flags );
185 } catch ( Exception
$e ) {
186 throw $this->newExceptionAfterConnectError( $e->getMessage() );
191 * @return string|null SQLite DB file path
192 * @throws DBUnexpectedError
195 public function getDbFilePath() {
196 return $this->dbPath ?? self
::generateFileName( $this->dbDir
, $this->getDBname() );
200 * @return string|null Lock file directory
202 public function getLockFileDirectory() {
203 if ( $this->dbPath
!== null && !self
::isProcessMemoryPath( $this->dbPath
) ) {
204 return dirname( $this->dbPath
) . '/locks';
205 } elseif ( $this->dbDir
!== null && !self
::isProcessMemoryPath( $this->dbDir
) ) {
206 return $this->dbDir
. '/locks';
213 * Does not actually close the connection, just destroys the reference for GC to do its work
216 protected function closeConnection() {
223 * Generates a database file name. Explicitly public for installer.
224 * @param string $dir Directory where database resides
225 * @param string|bool $dbName Database name (or false from Database::factory, validated here)
227 * @throws DBUnexpectedError
229 public static function generateFileName( $dir, $dbName ) {
231 throw new DBUnexpectedError( null, __CLASS__
. ": no DB directory specified" );
232 } elseif ( self
::isProcessMemoryPath( $dir ) ) {
233 throw new DBUnexpectedError(
235 __CLASS__
. ": cannot use process memory directory '$dir'"
237 } elseif ( !strlen( $dbName ) ) {
238 throw new DBUnexpectedError( null, __CLASS__
. ": no DB name specified" );
241 return "$dir/$dbName.sqlite";
245 * @param string $path
248 private static function generateDatabaseName( $path ) {
249 if ( preg_match( '/^(:memory:$|file::memory:)/', $path ) ) {
250 // E.g. "file::memory:?cache=shared" => ":memory":
252 } elseif ( preg_match( '/^file::([^?]+)\?mode=memory(&|$)/', $path, $m ) ) {
253 // E.g. "file:memdb1?mode=memory" => ":memdb1:"
256 // E.g. "/home/.../some_db.sqlite3" => "some_db"
257 return preg_replace( '/\.sqlite\d?$/', '', basename( $path ) );
262 * @param string $path
265 private static function isProcessMemoryPath( $path ) {
266 return preg_match( '/^(:memory:$|file:(:memory:|[^?]+\?mode=memory(&|$)))/', $path );
270 * Returns version of currently supported SQLite fulltext search module or false if none present.
273 static function getFulltextSearchModule() {
274 static $cachedResult = null;
275 if ( $cachedResult !== null ) {
276 return $cachedResult;
278 $cachedResult = false;
279 $table = 'dummy_search_test';
281 $db = self
::newStandaloneInstance( ':memory:' );
282 if ( $db->query( "CREATE VIRTUAL TABLE $table USING FTS3(dummy_field)", __METHOD__
, true ) ) {
283 $cachedResult = 'FTS3';
287 return $cachedResult;
291 * Attaches external database to the connection handle
293 * @see https://sqlite.org/lang_attach.html
295 * @param string $name Database name to be used in queries like
296 * SELECT foo FROM dbname.table
297 * @param bool|string $file Database file name. If omitted, will be generated
298 * using $name and configured data directory
299 * @param string $fname Calling function name
300 * @return IResultWrapper
302 public function attachDatabase( $name, $file = false, $fname = __METHOD__
) {
303 $file = is_string( $file ) ?
$file : self
::generateFileName( $this->dbDir
, $name );
304 $encFile = $this->addQuotes( $file );
307 "ATTACH DATABASE $encFile AS $name",
309 self
::QUERY_IGNORE_DBO_TRX
313 protected function isWriteQuery( $sql ) {
314 return parent
::isWriteQuery( $sql ) && !preg_match( '/^(ATTACH|PRAGMA)\b/i', $sql );
317 protected function isTransactableQuery( $sql ) {
318 return parent
::isTransactableQuery( $sql ) && !in_array(
319 $this->getQueryVerb( $sql ),
320 [ 'ATTACH', 'PRAGMA' ],
326 * SQLite doesn't allow buffered results or data seeking etc, so we'll use fetchAll as the result
329 * @return bool|IResultWrapper
331 protected function doQuery( $sql ) {
332 $res = $this->getBindingHandle()->query( $sql );
333 if ( $res === false ) {
337 $resource = ResultWrapper
::unwrap( $res );
338 $this->lastAffectedRowCount
= $resource->rowCount();
339 $res = new ResultWrapper( $this, $resource->fetchAll() );
345 * @param IResultWrapper|mixed $res
347 function freeResult( $res ) {
348 if ( $res instanceof ResultWrapper
) {
354 * @param IResultWrapper|array $res
355 * @return stdClass|bool
357 function fetchObject( $res ) {
358 $resource =& ResultWrapper
::unwrap( $res );
360 $cur = current( $resource );
361 if ( is_array( $cur ) ) {
364 foreach ( $cur as $k => $v ) {
365 if ( !is_numeric( $k ) ) {
377 * @param IResultWrapper|mixed $res
380 function fetchRow( $res ) {
381 $resource =& ResultWrapper
::unwrap( $res );
382 $cur = current( $resource );
383 if ( is_array( $cur ) ) {
393 * The PDO::Statement class implements the array interface so count() will work
395 * @param IResultWrapper|array|false $res
398 function numRows( $res ) {
399 // false does not implement Countable
400 $resource = ResultWrapper
::unwrap( $res );
402 return is_array( $resource ) ?
count( $resource ) : 0;
406 * @param IResultWrapper $res
409 function numFields( $res ) {
410 $resource = ResultWrapper
::unwrap( $res );
411 if ( is_array( $resource ) && count( $resource ) > 0 ) {
412 // The size of the result array is twice the number of fields. (T67578)
413 return count( $resource[0] ) / 2;
415 // If the result is empty return 0
421 * @param IResultWrapper $res
425 function fieldName( $res, $n ) {
426 $resource = ResultWrapper
::unwrap( $res );
427 if ( is_array( $resource ) ) {
428 $keys = array_keys( $resource[0] );
436 protected function doSelectDomain( DatabaseDomain
$domain ) {
437 if ( $domain->getSchema() !== null ) {
438 throw new DBExpectedError(
440 __CLASS__
. ": domain '{$domain->getId()}' has a schema component"
444 $database = $domain->getDatabase();
445 // A null database means "don't care" so leave it as is and update the table prefix
446 if ( $database === null ) {
447 $this->currentDomain
= new DatabaseDomain(
448 $this->currentDomain
->getDatabase(),
450 $domain->getTablePrefix()
456 if ( $database !== $this->getDBname() ) {
457 throw new DBExpectedError(
459 __CLASS__
. ": cannot change database (got '$database')"
467 * Use MySQL's naming (accounts for prefix etc) but remove surrounding backticks
469 * @param string $name
470 * @param string $format
473 function tableName( $name, $format = 'quoted' ) {
474 // table names starting with sqlite_ are reserved
475 if ( strpos( $name, 'sqlite_' ) === 0 ) {
479 return str_replace( '"', '', parent
::tableName( $name, $format ) );
483 * This must be called after nextSequenceVal
487 function insertId() {
488 // PDO::lastInsertId yields a string :(
489 return intval( $this->getBindingHandle()->lastInsertId() );
493 * @param IResultWrapper|array $res
496 function dataSeek( $res, $row ) {
497 $resource =& ResultWrapper
::unwrap( $res );
500 for ( $i = 0; $i < $row; $i++
) {
509 function lastError() {
510 if ( !is_object( $this->conn
) ) {
511 return "Cannot return last error, no db connection";
513 $e = $this->conn
->errorInfo();
521 function lastErrno() {
522 if ( !is_object( $this->conn
) ) {
523 return "Cannot return last error, no db connection";
525 $info = $this->conn
->errorInfo();
534 protected function fetchAffectedRowCount() {
535 return $this->lastAffectedRowCount
;
538 function tableExists( $table, $fname = __METHOD__
) {
539 $tableRaw = $this->tableName( $table, 'raw' );
540 if ( isset( $this->sessionTempTables
[$tableRaw] ) ) {
541 return true; // already known to exist
544 $encTable = $this->addQuotes( $tableRaw );
546 "SELECT 1 FROM sqlite_master WHERE type='table' AND name=$encTable",
548 self
::QUERY_IGNORE_DBO_TRX
551 return $res->numRows() ?
true : false;
555 * Returns information about an index
556 * Returns false if the index does not exist
557 * - if errors are explicitly ignored, returns NULL on failure
559 * @param string $table
560 * @param string $index
561 * @param string $fname
562 * @return array|false
564 function indexInfo( $table, $index, $fname = __METHOD__
) {
565 $sql = 'PRAGMA index_info(' . $this->addQuotes( $this->indexName( $index ) ) . ')';
566 $res = $this->query( $sql, $fname, self
::QUERY_IGNORE_DBO_TRX
);
567 if ( !$res ||
$res->numRows() == 0 ) {
571 foreach ( $res as $row ) {
572 $info[] = $row->name
;
579 * @param string $table
580 * @param string $index
581 * @param string $fname
584 function indexUnique( $table, $index, $fname = __METHOD__
) {
585 $row = $this->selectRow( 'sqlite_master', '*',
588 'name' => $this->indexName( $index ),
590 if ( !$row ||
!isset( $row->sql
) ) {
594 // $row->sql will be of the form CREATE [UNIQUE] INDEX ...
595 $indexPos = strpos( $row->sql
, 'INDEX' );
596 if ( $indexPos === false ) {
599 $firstPart = substr( $row->sql
, 0, $indexPos );
600 $options = explode( ' ', $firstPart );
602 return in_array( 'UNIQUE', $options );
606 * Filter the options used in SELECT statements
608 * @param array $options
611 function makeSelectOptions( $options ) {
612 foreach ( $options as $k => $v ) {
613 if ( is_numeric( $k ) && ( $v == 'FOR UPDATE' ||
$v == 'LOCK IN SHARE MODE' ) ) {
618 return parent
::makeSelectOptions( $options );
622 * @param array $options
625 protected function makeUpdateOptionsArray( $options ) {
626 $options = parent
::makeUpdateOptionsArray( $options );
627 $options = self
::fixIgnore( $options );
633 * @param array $options
636 static function fixIgnore( $options ) {
637 # SQLite uses OR IGNORE not just IGNORE
638 foreach ( $options as $k => $v ) {
639 if ( $v == 'IGNORE' ) {
640 $options[$k] = 'OR IGNORE';
648 * @param array $options
651 function makeInsertOptions( $options ) {
652 $options = self
::fixIgnore( $options );
654 return parent
::makeInsertOptions( $options );
658 * Based on generic method (parent) with some prior SQLite-sepcific adjustments
659 * @param string $table
661 * @param string $fname
662 * @param array $options
665 function insert( $table, $a, $fname = __METHOD__
, $options = [] ) {
666 if ( !count( $a ) ) {
670 # SQLite can't handle multi-row inserts, so divide up into multiple single-row inserts
671 if ( isset( $a[0] ) && is_array( $a[0] ) ) {
672 $affectedRowCount = 0;
674 $this->startAtomic( $fname, self
::ATOMIC_CANCELABLE
);
675 foreach ( $a as $v ) {
676 parent
::insert( $table, $v, "$fname/multi-row", $options );
677 $affectedRowCount +
= $this->affectedRows();
679 $this->endAtomic( $fname );
680 } catch ( Exception
$e ) {
681 $this->cancelAtomic( $fname );
684 $this->affectedRowCount
= $affectedRowCount;
686 parent
::insert( $table, $a, "$fname/single-row", $options );
693 * @param string $table
694 * @param array $uniqueIndexes Unused
695 * @param string|array $rows
696 * @param string $fname
698 function replace( $table, $uniqueIndexes, $rows, $fname = __METHOD__
) {
699 if ( !count( $rows ) ) {
703 # SQLite can't handle multi-row replaces, so divide up into multiple single-row queries
704 if ( isset( $rows[0] ) && is_array( $rows[0] ) ) {
705 $affectedRowCount = 0;
707 $this->startAtomic( $fname, self
::ATOMIC_CANCELABLE
);
708 foreach ( $rows as $v ) {
709 $this->nativeReplace( $table, $v, "$fname/multi-row" );
710 $affectedRowCount +
= $this->affectedRows();
712 $this->endAtomic( $fname );
713 } catch ( Exception
$e ) {
714 $this->cancelAtomic( $fname );
717 $this->affectedRowCount
= $affectedRowCount;
719 $this->nativeReplace( $table, $rows, "$fname/single-row" );
724 * Returns the size of a text field, or -1 for "unlimited"
725 * In SQLite this is SQLITE_MAX_LENGTH, by default 1GB. No way to query it though.
727 * @param string $table
728 * @param string $field
731 function textFieldSize( $table, $field ) {
738 function unionSupportsOrderAndLimit() {
743 * @param string[] $sqls
744 * @param bool $all Whether to "UNION ALL" or not
747 function unionQueries( $sqls, $all ) {
748 $glue = $all ?
' UNION ALL ' : ' UNION ';
750 return implode( $glue, $sqls );
756 function wasDeadlock() {
757 return $this->lastErrno() == 5; // SQLITE_BUSY
763 function wasReadOnlyError() {
764 return $this->lastErrno() == 8; // SQLITE_READONLY;
767 public function wasConnectionError( $errno ) {
768 return $errno == 17; // SQLITE_SCHEMA;
771 protected function wasKnownStatementRollbackError() {
772 // ON CONFLICT ROLLBACK clauses make it so that SQLITE_CONSTRAINT error is
773 // ambiguous with regard to whether it implies a ROLLBACK or an ABORT happened.
774 // https://sqlite.org/lang_createtable.html#uniqueconst
775 // https://sqlite.org/lang_conflict.html
779 public function serverIsReadOnly() {
780 $this->assertHasConnectionHandle();
782 $path = $this->getDbFilePath();
784 return ( !self
::isProcessMemoryPath( $path ) && !is_writable( $path ) );
788 * @return string Wikitext of a link to the server software's web site
790 public function getSoftwareLink() {
791 return "[{{int:version-db-sqlite-url}} SQLite]";
795 * @return string Version information from the database
797 function getServerVersion() {
798 $ver = $this->getBindingHandle()->getAttribute( PDO
::ATTR_SERVER_VERSION
);
804 * Get information about a given field
805 * Returns false if the field does not exist.
807 * @param string $table
808 * @param string $field
809 * @return SQLiteField|bool False on failure
811 function fieldInfo( $table, $field ) {
812 $tableName = $this->tableName( $table );
813 $sql = 'PRAGMA table_info(' . $this->addQuotes( $tableName ) . ')';
814 $res = $this->query( $sql, __METHOD__
, self
::QUERY_IGNORE_DBO_TRX
);
815 foreach ( $res as $row ) {
816 if ( $row->name
== $field ) {
817 return new SQLiteField( $row, $tableName );
824 protected function doBegin( $fname = '' ) {
825 if ( $this->trxMode
!= '' ) {
826 $this->query( "BEGIN {$this->trxMode}", $fname );
828 $this->query( 'BEGIN', $fname );
836 function strencode( $s ) {
837 return substr( $this->addQuotes( $s ), 1, -1 );
844 function encodeBlob( $b ) {
845 return new Blob( $b );
849 * @param Blob|string $b
852 function decodeBlob( $b ) {
853 if ( $b instanceof Blob
) {
861 * @param string|int|null|bool|Blob $s
864 function addQuotes( $s ) {
865 if ( $s instanceof Blob
) {
866 return "x'" . bin2hex( $s->fetch() ) . "'";
867 } elseif ( is_bool( $s ) ) {
869 } elseif ( strpos( (string)$s, "\0" ) !== false ) {
870 // SQLite doesn't support \0 in strings, so use the hex representation as a workaround.
871 // This is a known limitation of SQLite's mprintf function which PDO
872 // should work around, but doesn't. I have reported this to php.net as bug #63419:
873 // https://bugs.php.net/bug.php?id=63419
874 // There was already a similar report for SQLite3::escapeString, bug #62361:
875 // https://bugs.php.net/bug.php?id=62361
876 // There is an additional bug regarding sorting this data after insert
877 // on older versions of sqlite shipped with ubuntu 12.04
878 // https://phabricator.wikimedia.org/T74367
879 $this->queryLogger
->debug(
881 ': Quoting value containing null byte. ' .
882 'For consistency all binary data should have been ' .
883 'first processed with self::encodeBlob()'
885 return "x'" . bin2hex( (string)$s ) . "'";
887 return $this->getBindingHandle()->quote( (string)$s );
891 public function buildSubstring( $input, $startPosition, $length = null ) {
892 $this->assertBuildSubstringParams( $startPosition, $length );
893 $params = [ $input, $startPosition ];
894 if ( $length !== null ) {
897 return 'SUBSTR(' . implode( ',', $params ) . ')';
901 * @param string $field Field or column to cast
905 public function buildStringCast( $field ) {
906 return 'CAST ( ' . $field . ' AS TEXT )';
910 * No-op version of deadlockLoop
914 public function deadlockLoop( /*...*/ ) {
915 $args = func_get_args();
916 $function = array_shift( $args );
918 return $function( ...$args );
925 protected function replaceVars( $s ) {
926 $s = parent
::replaceVars( $s );
927 if ( preg_match( '/^\s*(CREATE|ALTER) TABLE/i', $s ) ) {
928 // CREATE TABLE hacks to allow schema file sharing with MySQL
930 // binary/varbinary column type -> blob
931 $s = preg_replace( '/\b(var)?binary(\(\d+\))/i', 'BLOB', $s );
932 // no such thing as unsigned
933 $s = preg_replace( '/\b(un)?signed\b/i', '', $s );
935 $s = preg_replace( '/\b(tiny|small|medium|big|)int(\s*\(\s*\d+\s*\)|\b)/i', 'INTEGER', $s );
936 // floating point types -> REAL
938 '/\b(float|double(\s+precision)?)(\s*\(\s*\d+\s*(,\s*\d+\s*)?\)|\b)/i',
943 $s = preg_replace( '/\b(var)?char\s*\(.*?\)/i', 'TEXT', $s );
944 // TEXT normalization
945 $s = preg_replace( '/\b(tiny|medium|long)text\b/i', 'TEXT', $s );
946 // BLOB normalization
947 $s = preg_replace( '/\b(tiny|small|medium|long|)blob\b/i', 'BLOB', $s );
949 $s = preg_replace( '/\bbool(ean)?\b/i', 'INTEGER', $s );
951 $s = preg_replace( '/\b(datetime|timestamp)\b/i', 'TEXT', $s );
953 $s = preg_replace( '/\benum\s*\([^)]*\)/i', 'TEXT', $s );
954 // binary collation type -> nothing
955 $s = preg_replace( '/\bbinary\b/i', '', $s );
956 // auto_increment -> autoincrement
957 $s = preg_replace( '/\bauto_increment\b/i', 'AUTOINCREMENT', $s );
958 // No explicit options
959 $s = preg_replace( '/\)[^);]*(;?)\s*$/', ')\1', $s );
960 // AUTOINCREMENT should immedidately follow PRIMARY KEY
961 $s = preg_replace( '/primary key (.*?) autoincrement/i', 'PRIMARY KEY AUTOINCREMENT $1', $s );
962 } elseif ( preg_match( '/^\s*CREATE (\s*(?:UNIQUE|FULLTEXT)\s+)?INDEX/i', $s ) ) {
963 // No truncated indexes
964 $s = preg_replace( '/\(\d+\)/', '', $s );
966 $s = preg_replace( '/\bfulltext\b/i', '', $s );
967 } elseif ( preg_match( '/^\s*DROP INDEX/i', $s ) ) {
968 // DROP INDEX is database-wide, not table-specific, so no ON <table> clause.
969 $s = preg_replace( '/\sON\s+[^\s]*/i', '', $s );
970 } elseif ( preg_match( '/^\s*INSERT IGNORE\b/i', $s ) ) {
971 // INSERT IGNORE --> INSERT OR IGNORE
972 $s = preg_replace( '/^\s*INSERT IGNORE\b/i', 'INSERT OR IGNORE', $s );
978 public function lock( $lockName, $method, $timeout = 5 ) {
979 $status = $this->lockMgr
->lock( [ $lockName ], LockManager
::LOCK_EX
, $timeout );
981 $this->lockMgr
instanceof FSLockManager
&&
982 $status->hasMessage( 'lockmanager-fail-openlock' )
984 throw new DBError( $this, "Cannot create directory \"{$this->getLockFileDirectory()}\"" );
987 return $status->isOK();
990 public function unlock( $lockName, $method ) {
991 return $this->lockMgr
->unlock( [ $lockName ], LockManager
::LOCK_EX
)->isGood();
995 * Build a concatenation list to feed into a SQL query
997 * @param string[] $stringList
1000 function buildConcat( $stringList ) {
1001 return '(' . implode( ') || (', $stringList ) . ')';
1004 public function buildGroupConcatField(
1005 $delim, $table, $field, $conds = '', $join_conds = []
1007 $fld = "group_concat($field," . $this->addQuotes( $delim ) . ')';
1009 return '(' . $this->selectSQLText( $table, $fld, $conds, null, [], $join_conds ) . ')';
1013 * @param string $oldName
1014 * @param string $newName
1015 * @param bool $temporary
1016 * @param string $fname
1017 * @return bool|IResultWrapper
1018 * @throws RuntimeException
1020 function duplicateTableStructure( $oldName, $newName, $temporary = false, $fname = __METHOD__
) {
1021 $res = $this->query( "SELECT sql FROM sqlite_master WHERE tbl_name=" .
1022 $this->addQuotes( $oldName ) . " AND type='table'", $fname );
1023 $obj = $this->fetchObject( $res );
1025 throw new RuntimeException( "Couldn't retrieve structure for table $oldName" );
1028 $sql = preg_replace(
1030 preg_quote( trim( $this->addIdentifierQuotes( $oldName ), '"' ), '/' ) .
1032 $this->addIdentifierQuotes( $newName ),
1037 if ( preg_match( '/^\\s*CREATE\\s+VIRTUAL\\s+TABLE\b/i', $sql ) ) {
1038 $this->queryLogger
->debug(
1039 "Table $oldName is virtual, can't create a temporary duplicate.\n" );
1041 $sql = str_replace( 'CREATE TABLE', 'CREATE TEMPORARY TABLE', $sql );
1045 $res = $this->query( $sql, $fname, self
::QUERY_PSEUDO_PERMANENT
);
1047 // Take over indexes
1048 $indexList = $this->query( 'PRAGMA INDEX_LIST(' . $this->addQuotes( $oldName ) . ')' );
1049 foreach ( $indexList as $index ) {
1050 if ( strpos( $index->name
, 'sqlite_autoindex' ) === 0 ) {
1054 if ( $index->unique
) {
1055 $sql = 'CREATE UNIQUE INDEX';
1057 $sql = 'CREATE INDEX';
1059 // Try to come up with a new index name, given indexes have database scope in SQLite
1060 $indexName = $newName . '_' . $index->name
;
1061 $sql .= ' ' . $indexName . ' ON ' . $newName;
1063 $indexInfo = $this->query( 'PRAGMA INDEX_INFO(' . $this->addQuotes( $index->name
) . ')' );
1065 foreach ( $indexInfo as $indexInfoRow ) {
1066 $fields[$indexInfoRow->seqno
] = $indexInfoRow->name
;
1069 $sql .= '(' . implode( ',', $fields ) . ')';
1071 $this->query( $sql );
1078 * List all tables on the database
1080 * @param string|null $prefix Only show tables with this prefix, e.g. mw_
1081 * @param string $fname Calling function name
1085 function listTables( $prefix = null, $fname = __METHOD__
) {
1086 $result = $this->select(
1094 foreach ( $result as $table ) {
1095 $vars = get_object_vars( $table );
1096 $table = array_pop( $vars );
1098 if ( !$prefix ||
strpos( $table, $prefix ) === 0 ) {
1099 if ( strpos( $table, 'sqlite_' ) !== 0 ) {
1100 $endArray[] = $table;
1109 * Override due to no CASCADE support
1111 * @param string $tableName
1112 * @param string $fName
1113 * @return bool|IResultWrapper
1114 * @throws DBReadOnlyError
1116 public function dropTable( $tableName, $fName = __METHOD__
) {
1117 if ( !$this->tableExists( $tableName, $fName ) ) {
1120 $sql = "DROP TABLE " . $this->tableName( $tableName );
1122 return $this->query( $sql, $fName, self
::QUERY_IGNORE_DBO_TRX
);
1125 public function setTableAliases( array $aliases ) {
1126 parent
::setTableAliases( $aliases );
1127 foreach ( $this->tableAliases
as $params ) {
1128 if ( isset( $this->alreadyAttached
[$params['dbname']] ) ) {
1131 $this->attachDatabase( $params['dbname'] );
1132 $this->alreadyAttached
[$params['dbname']] = true;
1136 public function resetSequenceForTable( $table, $fname = __METHOD__
) {
1137 $encTable = $this->addIdentifierQuotes( 'sqlite_sequence' );
1138 $encName = $this->addQuotes( $this->tableName( $table, 'raw' ) );
1140 "DELETE FROM $encTable WHERE name = $encName",
1142 self
::QUERY_IGNORE_DBO_TRX
1146 public function databasesAreIndependent() {
1153 protected function getBindingHandle() {
1154 return parent
::getBindingHandle();
1159 * @deprecated since 1.29
1161 class_alias( DatabaseSqlite
::class, 'DatabaseSqlite' );