Fix use of GenderCache in ApiPageSet::processTitlesArray
[lhc/web/wiklou.git] / includes / libs / rdbms / database / DatabaseSqlite.php
1 <?php
2 /**
3 * This is the SQLite database abstraction layer.
4 * See maintenance/sqlite/README for development notes and other specific information
5 *
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.
10 *
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.
15 *
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
20 *
21 * @file
22 * @ingroup Database
23 */
24 namespace Wikimedia\Rdbms;
25
26 use NullLockManager;
27 use PDO;
28 use PDOException;
29 use Exception;
30 use LockManager;
31 use FSLockManager;
32 use RuntimeException;
33 use stdClass;
34
35 /**
36 * @ingroup Database
37 */
38 class DatabaseSqlite extends Database {
39 /** @var string|null Directory for SQLite database files listed under their DB name */
40 protected $dbDir;
41 /** @var string|null Explicit path for the SQLite database file */
42 protected $dbPath;
43 /** @var string Transaction mode */
44 protected $trxMode;
45
46 /** @var int The number of rows affected as an integer */
47 protected $lastAffectedRowCount;
48 /** @var resource */
49 protected $lastResultHandle;
50
51 /** @var PDO */
52 protected $conn;
53
54 /** @var FSLockManager (hopefully on the same server as the DB) */
55 protected $lockMgr;
56
57 /** @var array List of shared database already attached to this connection */
58 private $alreadyAttached = [];
59
60 /** @var string[] See https://www.sqlite.org/lang_transaction.html */
61 private static $VALID_TRX_MODES = [ '', 'DEFERRED', 'IMMEDIATE', 'EXCLUSIVE' ];
62
63 /**
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)
68 * @param array $p
69 */
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 );
75 }
76 } elseif ( isset( $p['dbDirectory'] ) ) {
77 $this->dbDir = $p['dbDirectory'];
78 }
79
80 parent::__construct( $p );
81
82 $this->trxMode = strtoupper( $p['trxMode'] ?? '' );
83
84 $lockDirectory = $this->getLockFileDirectory();
85 if ( $lockDirectory !== null ) {
86 $this->lockMgr = new FSLockManager( [
87 'domain' => $this->getDomainID(),
88 'lockDirectory' => $lockDirectory
89 ] );
90 } else {
91 $this->lockMgr = new NullLockManager( [ 'domain' => $this->getDomainID() ] );
92 }
93 }
94
95 protected static function getAttributes() {
96 return [ self::ATTR_DB_LEVEL_LOCKING => true ];
97 }
98
99 /**
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
106 * @since 1.25
107 */
108 public static function newStandaloneInstance( $filename, array $p = [] ) {
109 $p['dbFilePath'] = $filename;
110 $p['schema'] = null;
111 $p['tablePrefix'] = '';
112 /** @var DatabaseSqlite $db */
113 $db = Database::factory( 'sqlite', $p );
114
115 return $db;
116 }
117
118 /**
119 * @return string
120 */
121 public function getType() {
122 return 'sqlite';
123 }
124
125 protected function open( $server, $user, $pass, $dbName, $schema, $tablePrefix ) {
126 $this->close();
127
128 // Note that for SQLite, $server, $user, and $pass are ignored
129
130 if ( $schema !== null ) {
131 throw $this->newExceptionAfterConnectError( "Got schema '$schema'; not supported." );
132 }
133
134 if ( $this->dbPath !== null ) {
135 $path = $this->dbPath;
136 } elseif ( $this->dbDir !== null ) {
137 $path = self::generateFileName( $this->dbDir, $dbName );
138 } else {
139 throw $this->newExceptionAfterConnectError( "DB path or directory required" );
140 }
141
142 // Check if the database file already exists but is non-readable
143 if (
144 !self::isProcessMemoryPath( $path ) &&
145 file_exists( $path ) &&
146 !is_readable( $path )
147 ) {
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" );
151 }
152
153 $attributes = [];
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()
161 );
162 } else {
163 $attributes[PDO::ATTR_PERSISTENT] = true;
164 }
165 }
166
167 try {
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() );
172 }
173
174 $this->currentDomain = new DatabaseDomain( $dbName, null, $tablePrefix );
175
176 try {
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 );
184 }
185 } catch ( Exception $e ) {
186 throw $this->newExceptionAfterConnectError( $e->getMessage() );
187 }
188 }
189
190 /**
191 * @return string|null SQLite DB file path
192 * @throws DBUnexpectedError
193 * @since 1.25
194 */
195 public function getDbFilePath() {
196 return $this->dbPath ?? self::generateFileName( $this->dbDir, $this->getDBname() );
197 }
198
199 /**
200 * @return string|null Lock file directory
201 */
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';
207 }
208
209 return null;
210 }
211
212 /**
213 * Does not actually close the connection, just destroys the reference for GC to do its work
214 * @return bool
215 */
216 protected function closeConnection() {
217 $this->conn = null;
218
219 return true;
220 }
221
222 /**
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)
226 * @return string
227 * @throws DBUnexpectedError
228 */
229 public static function generateFileName( $dir, $dbName ) {
230 if ( $dir == '' ) {
231 throw new DBUnexpectedError( null, __CLASS__ . ": no DB directory specified" );
232 } elseif ( self::isProcessMemoryPath( $dir ) ) {
233 throw new DBUnexpectedError(
234 null,
235 __CLASS__ . ": cannot use process memory directory '$dir'"
236 );
237 } elseif ( !strlen( $dbName ) ) {
238 throw new DBUnexpectedError( null, __CLASS__ . ": no DB name specified" );
239 }
240
241 return "$dir/$dbName.sqlite";
242 }
243
244 /**
245 * @param string $path
246 * @return string
247 */
248 private static function generateDatabaseName( $path ) {
249 if ( preg_match( '/^(:memory:$|file::memory:)/', $path ) ) {
250 // E.g. "file::memory:?cache=shared" => ":memory":
251 return ':memory:';
252 } elseif ( preg_match( '/^file::([^?]+)\?mode=memory(&|$)/', $path, $m ) ) {
253 // E.g. "file:memdb1?mode=memory" => ":memdb1:"
254 return ":{$m[1]}:";
255 } else {
256 // E.g. "/home/.../some_db.sqlite3" => "some_db"
257 return preg_replace( '/\.sqlite\d?$/', '', basename( $path ) );
258 }
259 }
260
261 /**
262 * @param string $path
263 * @return bool
264 */
265 private static function isProcessMemoryPath( $path ) {
266 return preg_match( '/^(:memory:$|file:(:memory:|[^?]+\?mode=memory(&|$)))/', $path );
267 }
268
269 /**
270 * Returns version of currently supported SQLite fulltext search module or false if none present.
271 * @return string
272 */
273 static function getFulltextSearchModule() {
274 static $cachedResult = null;
275 if ( $cachedResult !== null ) {
276 return $cachedResult;
277 }
278 $cachedResult = false;
279 $table = 'dummy_search_test';
280
281 $db = self::newStandaloneInstance( ':memory:' );
282 if ( $db->query( "CREATE VIRTUAL TABLE $table USING FTS3(dummy_field)", __METHOD__, true ) ) {
283 $cachedResult = 'FTS3';
284 }
285 $db->close();
286
287 return $cachedResult;
288 }
289
290 /**
291 * Attaches external database to the connection handle
292 *
293 * @see https://sqlite.org/lang_attach.html
294 *
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
301 */
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 );
305
306 return $this->query(
307 "ATTACH DATABASE $encFile AS $name",
308 $fname,
309 self::QUERY_IGNORE_DBO_TRX
310 );
311 }
312
313 protected function isWriteQuery( $sql ) {
314 return parent::isWriteQuery( $sql ) && !preg_match( '/^(ATTACH|PRAGMA)\b/i', $sql );
315 }
316
317 protected function isTransactableQuery( $sql ) {
318 return parent::isTransactableQuery( $sql ) && !in_array(
319 $this->getQueryVerb( $sql ),
320 [ 'ATTACH', 'PRAGMA' ],
321 true
322 );
323 }
324
325 /**
326 * SQLite doesn't allow buffered results or data seeking etc, so we'll use fetchAll as the result
327 *
328 * @param string $sql
329 * @return bool|IResultWrapper
330 */
331 protected function doQuery( $sql ) {
332 $res = $this->getBindingHandle()->query( $sql );
333 if ( $res === false ) {
334 return false;
335 }
336
337 $resource = ResultWrapper::unwrap( $res );
338 $this->lastAffectedRowCount = $resource->rowCount();
339 $res = new ResultWrapper( $this, $resource->fetchAll() );
340
341 return $res;
342 }
343
344 /**
345 * @param IResultWrapper|mixed $res
346 */
347 function freeResult( $res ) {
348 if ( $res instanceof ResultWrapper ) {
349 $res->free();
350 }
351 }
352
353 /**
354 * @param IResultWrapper|array $res
355 * @return stdClass|bool
356 */
357 function fetchObject( $res ) {
358 $resource =& ResultWrapper::unwrap( $res );
359
360 $cur = current( $resource );
361 if ( is_array( $cur ) ) {
362 next( $resource );
363 $obj = new stdClass;
364 foreach ( $cur as $k => $v ) {
365 if ( !is_numeric( $k ) ) {
366 $obj->$k = $v;
367 }
368 }
369
370 return $obj;
371 }
372
373 return false;
374 }
375
376 /**
377 * @param IResultWrapper|mixed $res
378 * @return array|bool
379 */
380 function fetchRow( $res ) {
381 $resource =& ResultWrapper::unwrap( $res );
382 $cur = current( $resource );
383 if ( is_array( $cur ) ) {
384 next( $resource );
385
386 return $cur;
387 }
388
389 return false;
390 }
391
392 /**
393 * The PDO::Statement class implements the array interface so count() will work
394 *
395 * @param IResultWrapper|array|false $res
396 * @return int
397 */
398 function numRows( $res ) {
399 // false does not implement Countable
400 $resource = ResultWrapper::unwrap( $res );
401
402 return is_array( $resource ) ? count( $resource ) : 0;
403 }
404
405 /**
406 * @param IResultWrapper $res
407 * @return int
408 */
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;
414 } else {
415 // If the result is empty return 0
416 return 0;
417 }
418 }
419
420 /**
421 * @param IResultWrapper $res
422 * @param int $n
423 * @return bool
424 */
425 function fieldName( $res, $n ) {
426 $resource = ResultWrapper::unwrap( $res );
427 if ( is_array( $resource ) ) {
428 $keys = array_keys( $resource[0] );
429
430 return $keys[$n];
431 }
432
433 return false;
434 }
435
436 protected function doSelectDomain( DatabaseDomain $domain ) {
437 if ( $domain->getSchema() !== null ) {
438 throw new DBExpectedError(
439 $this,
440 __CLASS__ . ": domain '{$domain->getId()}' has a schema component"
441 );
442 }
443
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(),
449 null,
450 $domain->getTablePrefix()
451 );
452
453 return true;
454 }
455
456 if ( $database !== $this->getDBname() ) {
457 throw new DBExpectedError(
458 $this,
459 __CLASS__ . ": cannot change database (got '$database')"
460 );
461 }
462
463 return true;
464 }
465
466 /**
467 * Use MySQL's naming (accounts for prefix etc) but remove surrounding backticks
468 *
469 * @param string $name
470 * @param string $format
471 * @return string
472 */
473 function tableName( $name, $format = 'quoted' ) {
474 // table names starting with sqlite_ are reserved
475 if ( strpos( $name, 'sqlite_' ) === 0 ) {
476 return $name;
477 }
478
479 return str_replace( '"', '', parent::tableName( $name, $format ) );
480 }
481
482 /**
483 * This must be called after nextSequenceVal
484 *
485 * @return int
486 */
487 function insertId() {
488 // PDO::lastInsertId yields a string :(
489 return intval( $this->getBindingHandle()->lastInsertId() );
490 }
491
492 /**
493 * @param IResultWrapper|array $res
494 * @param int $row
495 */
496 function dataSeek( $res, $row ) {
497 $resource =& ResultWrapper::unwrap( $res );
498 reset( $resource );
499 if ( $row > 0 ) {
500 for ( $i = 0; $i < $row; $i++ ) {
501 next( $resource );
502 }
503 }
504 }
505
506 /**
507 * @return string
508 */
509 function lastError() {
510 if ( !is_object( $this->conn ) ) {
511 return "Cannot return last error, no db connection";
512 }
513 $e = $this->conn->errorInfo();
514
515 return $e[2] ?? '';
516 }
517
518 /**
519 * @return string
520 */
521 function lastErrno() {
522 if ( !is_object( $this->conn ) ) {
523 return "Cannot return last error, no db connection";
524 } else {
525 $info = $this->conn->errorInfo();
526
527 return $info[1];
528 }
529 }
530
531 /**
532 * @return int
533 */
534 protected function fetchAffectedRowCount() {
535 return $this->lastAffectedRowCount;
536 }
537
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
542 }
543
544 $encTable = $this->addQuotes( $tableRaw );
545 $res = $this->query(
546 "SELECT 1 FROM sqlite_master WHERE type='table' AND name=$encTable",
547 __METHOD__,
548 self::QUERY_IGNORE_DBO_TRX
549 );
550
551 return $res->numRows() ? true : false;
552 }
553
554 /**
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
558 *
559 * @param string $table
560 * @param string $index
561 * @param string $fname
562 * @return array|false
563 */
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 ) {
568 return false;
569 }
570 $info = [];
571 foreach ( $res as $row ) {
572 $info[] = $row->name;
573 }
574
575 return $info;
576 }
577
578 /**
579 * @param string $table
580 * @param string $index
581 * @param string $fname
582 * @return bool|null
583 */
584 function indexUnique( $table, $index, $fname = __METHOD__ ) {
585 $row = $this->selectRow( 'sqlite_master', '*',
586 [
587 'type' => 'index',
588 'name' => $this->indexName( $index ),
589 ], $fname );
590 if ( !$row || !isset( $row->sql ) ) {
591 return null;
592 }
593
594 // $row->sql will be of the form CREATE [UNIQUE] INDEX ...
595 $indexPos = strpos( $row->sql, 'INDEX' );
596 if ( $indexPos === false ) {
597 return null;
598 }
599 $firstPart = substr( $row->sql, 0, $indexPos );
600 $options = explode( ' ', $firstPart );
601
602 return in_array( 'UNIQUE', $options );
603 }
604
605 /**
606 * Filter the options used in SELECT statements
607 *
608 * @param array $options
609 * @return array
610 */
611 function makeSelectOptions( $options ) {
612 foreach ( $options as $k => $v ) {
613 if ( is_numeric( $k ) && ( $v == 'FOR UPDATE' || $v == 'LOCK IN SHARE MODE' ) ) {
614 $options[$k] = '';
615 }
616 }
617
618 return parent::makeSelectOptions( $options );
619 }
620
621 /**
622 * @param array $options
623 * @return array
624 */
625 protected function makeUpdateOptionsArray( $options ) {
626 $options = parent::makeUpdateOptionsArray( $options );
627 $options = self::fixIgnore( $options );
628
629 return $options;
630 }
631
632 /**
633 * @param array $options
634 * @return array
635 */
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';
641 }
642 }
643
644 return $options;
645 }
646
647 /**
648 * @param array $options
649 * @return string
650 */
651 function makeInsertOptions( $options ) {
652 $options = self::fixIgnore( $options );
653
654 return parent::makeInsertOptions( $options );
655 }
656
657 /**
658 * Based on generic method (parent) with some prior SQLite-sepcific adjustments
659 * @param string $table
660 * @param array $a
661 * @param string $fname
662 * @param array $options
663 * @return bool
664 */
665 function insert( $table, $a, $fname = __METHOD__, $options = [] ) {
666 if ( !count( $a ) ) {
667 return true;
668 }
669
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;
673 try {
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();
678 }
679 $this->endAtomic( $fname );
680 } catch ( Exception $e ) {
681 $this->cancelAtomic( $fname );
682 throw $e;
683 }
684 $this->affectedRowCount = $affectedRowCount;
685 } else {
686 parent::insert( $table, $a, "$fname/single-row", $options );
687 }
688
689 return true;
690 }
691
692 /**
693 * @param string $table
694 * @param array $uniqueIndexes Unused
695 * @param string|array $rows
696 * @param string $fname
697 */
698 function replace( $table, $uniqueIndexes, $rows, $fname = __METHOD__ ) {
699 if ( !count( $rows ) ) {
700 return;
701 }
702
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;
706 try {
707 $this->startAtomic( $fname, self::ATOMIC_CANCELABLE );
708 foreach ( $rows as $v ) {
709 $this->nativeReplace( $table, $v, "$fname/multi-row" );
710 $affectedRowCount += $this->affectedRows();
711 }
712 $this->endAtomic( $fname );
713 } catch ( Exception $e ) {
714 $this->cancelAtomic( $fname );
715 throw $e;
716 }
717 $this->affectedRowCount = $affectedRowCount;
718 } else {
719 $this->nativeReplace( $table, $rows, "$fname/single-row" );
720 }
721 }
722
723 /**
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.
726 *
727 * @param string $table
728 * @param string $field
729 * @return int
730 */
731 function textFieldSize( $table, $field ) {
732 return -1;
733 }
734
735 /**
736 * @return bool
737 */
738 function unionSupportsOrderAndLimit() {
739 return false;
740 }
741
742 /**
743 * @param string[] $sqls
744 * @param bool $all Whether to "UNION ALL" or not
745 * @return string
746 */
747 function unionQueries( $sqls, $all ) {
748 $glue = $all ? ' UNION ALL ' : ' UNION ';
749
750 return implode( $glue, $sqls );
751 }
752
753 /**
754 * @return bool
755 */
756 function wasDeadlock() {
757 return $this->lastErrno() == 5; // SQLITE_BUSY
758 }
759
760 /**
761 * @return bool
762 */
763 function wasReadOnlyError() {
764 return $this->lastErrno() == 8; // SQLITE_READONLY;
765 }
766
767 public function wasConnectionError( $errno ) {
768 return $errno == 17; // SQLITE_SCHEMA;
769 }
770
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
776 return false;
777 }
778
779 public function serverIsReadOnly() {
780 $this->assertHasConnectionHandle();
781
782 $path = $this->getDbFilePath();
783
784 return ( !self::isProcessMemoryPath( $path ) && !is_writable( $path ) );
785 }
786
787 /**
788 * @return string Wikitext of a link to the server software's web site
789 */
790 public function getSoftwareLink() {
791 return "[{{int:version-db-sqlite-url}} SQLite]";
792 }
793
794 /**
795 * @return string Version information from the database
796 */
797 function getServerVersion() {
798 $ver = $this->getBindingHandle()->getAttribute( PDO::ATTR_SERVER_VERSION );
799
800 return $ver;
801 }
802
803 /**
804 * Get information about a given field
805 * Returns false if the field does not exist.
806 *
807 * @param string $table
808 * @param string $field
809 * @return SQLiteField|bool False on failure
810 */
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 );
818 }
819 }
820
821 return false;
822 }
823
824 protected function doBegin( $fname = '' ) {
825 if ( $this->trxMode != '' ) {
826 $this->query( "BEGIN {$this->trxMode}", $fname );
827 } else {
828 $this->query( 'BEGIN', $fname );
829 }
830 }
831
832 /**
833 * @param string $s
834 * @return string
835 */
836 function strencode( $s ) {
837 return substr( $this->addQuotes( $s ), 1, -1 );
838 }
839
840 /**
841 * @param string $b
842 * @return Blob
843 */
844 function encodeBlob( $b ) {
845 return new Blob( $b );
846 }
847
848 /**
849 * @param Blob|string $b
850 * @return string
851 */
852 function decodeBlob( $b ) {
853 if ( $b instanceof Blob ) {
854 $b = $b->fetch();
855 }
856
857 return $b;
858 }
859
860 /**
861 * @param string|int|null|bool|Blob $s
862 * @return string|int
863 */
864 function addQuotes( $s ) {
865 if ( $s instanceof Blob ) {
866 return "x'" . bin2hex( $s->fetch() ) . "'";
867 } elseif ( is_bool( $s ) ) {
868 return (int)$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(
880 __FUNCTION__ .
881 ': Quoting value containing null byte. ' .
882 'For consistency all binary data should have been ' .
883 'first processed with self::encodeBlob()'
884 );
885 return "x'" . bin2hex( (string)$s ) . "'";
886 } else {
887 return $this->getBindingHandle()->quote( (string)$s );
888 }
889 }
890
891 public function buildSubstring( $input, $startPosition, $length = null ) {
892 $this->assertBuildSubstringParams( $startPosition, $length );
893 $params = [ $input, $startPosition ];
894 if ( $length !== null ) {
895 $params[] = $length;
896 }
897 return 'SUBSTR(' . implode( ',', $params ) . ')';
898 }
899
900 /**
901 * @param string $field Field or column to cast
902 * @return string
903 * @since 1.28
904 */
905 public function buildStringCast( $field ) {
906 return 'CAST ( ' . $field . ' AS TEXT )';
907 }
908
909 /**
910 * No-op version of deadlockLoop
911 *
912 * @return mixed
913 */
914 public function deadlockLoop( /*...*/ ) {
915 $args = func_get_args();
916 $function = array_shift( $args );
917
918 return $function( ...$args );
919 }
920
921 /**
922 * @param string $s
923 * @return string
924 */
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
929
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 );
934 // INT -> INTEGER
935 $s = preg_replace( '/\b(tiny|small|medium|big|)int(\s*\(\s*\d+\s*\)|\b)/i', 'INTEGER', $s );
936 // floating point types -> REAL
937 $s = preg_replace(
938 '/\b(float|double(\s+precision)?)(\s*\(\s*\d+\s*(,\s*\d+\s*)?\)|\b)/i',
939 'REAL',
940 $s
941 );
942 // varchar -> TEXT
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 );
948 // BOOL -> INTEGER
949 $s = preg_replace( '/\bbool(ean)?\b/i', 'INTEGER', $s );
950 // DATETIME -> TEXT
951 $s = preg_replace( '/\b(datetime|timestamp)\b/i', 'TEXT', $s );
952 // No ENUM type
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 );
965 // No FULLTEXT
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 );
973 }
974
975 return $s;
976 }
977
978 public function lock( $lockName, $method, $timeout = 5 ) {
979 $status = $this->lockMgr->lock( [ $lockName ], LockManager::LOCK_EX, $timeout );
980 if (
981 $this->lockMgr instanceof FSLockManager &&
982 $status->hasMessage( 'lockmanager-fail-openlock' )
983 ) {
984 throw new DBError( $this, "Cannot create directory \"{$this->getLockFileDirectory()}\"" );
985 }
986
987 return $status->isOK();
988 }
989
990 public function unlock( $lockName, $method ) {
991 return $this->lockMgr->unlock( [ $lockName ], LockManager::LOCK_EX )->isGood();
992 }
993
994 /**
995 * Build a concatenation list to feed into a SQL query
996 *
997 * @param string[] $stringList
998 * @return string
999 */
1000 function buildConcat( $stringList ) {
1001 return '(' . implode( ') || (', $stringList ) . ')';
1002 }
1003
1004 public function buildGroupConcatField(
1005 $delim, $table, $field, $conds = '', $join_conds = []
1006 ) {
1007 $fld = "group_concat($field," . $this->addQuotes( $delim ) . ')';
1008
1009 return '(' . $this->selectSQLText( $table, $fld, $conds, null, [], $join_conds ) . ')';
1010 }
1011
1012 /**
1013 * @param string $oldName
1014 * @param string $newName
1015 * @param bool $temporary
1016 * @param string $fname
1017 * @return bool|IResultWrapper
1018 * @throws RuntimeException
1019 */
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 );
1024 if ( !$obj ) {
1025 throw new RuntimeException( "Couldn't retrieve structure for table $oldName" );
1026 }
1027 $sql = $obj->sql;
1028 $sql = preg_replace(
1029 '/(?<=\W)"?' .
1030 preg_quote( trim( $this->addIdentifierQuotes( $oldName ), '"' ), '/' ) .
1031 '"?(?=\W)/',
1032 $this->addIdentifierQuotes( $newName ),
1033 $sql,
1034 1
1035 );
1036 if ( $temporary ) {
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" );
1040 } else {
1041 $sql = str_replace( 'CREATE TABLE', 'CREATE TEMPORARY TABLE', $sql );
1042 }
1043 }
1044
1045 $res = $this->query( $sql, $fname, self::QUERY_PSEUDO_PERMANENT );
1046
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 ) {
1051 continue;
1052 }
1053
1054 if ( $index->unique ) {
1055 $sql = 'CREATE UNIQUE INDEX';
1056 } else {
1057 $sql = 'CREATE INDEX';
1058 }
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;
1062
1063 $indexInfo = $this->query( 'PRAGMA INDEX_INFO(' . $this->addQuotes( $index->name ) . ')' );
1064 $fields = [];
1065 foreach ( $indexInfo as $indexInfoRow ) {
1066 $fields[$indexInfoRow->seqno] = $indexInfoRow->name;
1067 }
1068
1069 $sql .= '(' . implode( ',', $fields ) . ')';
1070
1071 $this->query( $sql );
1072 }
1073
1074 return $res;
1075 }
1076
1077 /**
1078 * List all tables on the database
1079 *
1080 * @param string|null $prefix Only show tables with this prefix, e.g. mw_
1081 * @param string $fname Calling function name
1082 *
1083 * @return array
1084 */
1085 function listTables( $prefix = null, $fname = __METHOD__ ) {
1086 $result = $this->select(
1087 'sqlite_master',
1088 'name',
1089 "type='table'"
1090 );
1091
1092 $endArray = [];
1093
1094 foreach ( $result as $table ) {
1095 $vars = get_object_vars( $table );
1096 $table = array_pop( $vars );
1097
1098 if ( !$prefix || strpos( $table, $prefix ) === 0 ) {
1099 if ( strpos( $table, 'sqlite_' ) !== 0 ) {
1100 $endArray[] = $table;
1101 }
1102 }
1103 }
1104
1105 return $endArray;
1106 }
1107
1108 /**
1109 * Override due to no CASCADE support
1110 *
1111 * @param string $tableName
1112 * @param string $fName
1113 * @return bool|IResultWrapper
1114 * @throws DBReadOnlyError
1115 */
1116 public function dropTable( $tableName, $fName = __METHOD__ ) {
1117 if ( !$this->tableExists( $tableName, $fName ) ) {
1118 return false;
1119 }
1120 $sql = "DROP TABLE " . $this->tableName( $tableName );
1121
1122 return $this->query( $sql, $fName, self::QUERY_IGNORE_DBO_TRX );
1123 }
1124
1125 public function setTableAliases( array $aliases ) {
1126 parent::setTableAliases( $aliases );
1127 foreach ( $this->tableAliases as $params ) {
1128 if ( isset( $this->alreadyAttached[$params['dbname']] ) ) {
1129 continue;
1130 }
1131 $this->attachDatabase( $params['dbname'] );
1132 $this->alreadyAttached[$params['dbname']] = true;
1133 }
1134 }
1135
1136 public function resetSequenceForTable( $table, $fname = __METHOD__ ) {
1137 $encTable = $this->addIdentifierQuotes( 'sqlite_sequence' );
1138 $encName = $this->addQuotes( $this->tableName( $table, 'raw' ) );
1139 $this->query(
1140 "DELETE FROM $encTable WHERE name = $encName",
1141 $fname,
1142 self::QUERY_IGNORE_DBO_TRX
1143 );
1144 }
1145
1146 public function databasesAreIndependent() {
1147 return true;
1148 }
1149
1150 /**
1151 * @return PDO
1152 */
1153 protected function getBindingHandle() {
1154 return parent::getBindingHandle();
1155 }
1156 }
1157
1158 /**
1159 * @deprecated since 1.29
1160 */
1161 class_alias( DatabaseSqlite::class, 'DatabaseSqlite' );