Merge "RCFilters UI: Add 'direction' property to the wrapper"
[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 use Wikimedia\Rdbms\Blob;
25 use Wikimedia\Rdbms\SQLiteField;
26
27 /**
28 * @ingroup Database
29 */
30 class DatabaseSqlite extends Database {
31 /** @var bool Whether full text is enabled */
32 private static $fulltextEnabled = null;
33
34 /** @var string Directory */
35 protected $dbDir;
36 /** @var string File name for SQLite database file */
37 protected $dbPath;
38 /** @var string Transaction mode */
39 protected $trxMode;
40
41 /** @var int The number of rows affected as an integer */
42 protected $mAffectedRows;
43 /** @var resource */
44 protected $mLastResult;
45
46 /** @var PDO */
47 protected $mConn;
48
49 /** @var FSLockManager (hopefully on the same server as the DB) */
50 protected $lockMgr;
51
52 /**
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)
58 * @param array $p
59 */
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." );
70 } else {
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'] ) ) {
79 $done = [];
80 foreach ( $this->tableAliases as $params ) {
81 if ( isset( $done[$params['dbname']] ) ) {
82 continue;
83 }
84 $this->attachDatabase( $params['dbname'] );
85 $done[$params['dbname']] = 1;
86 }
87 }
88 }
89 }
90
91 $this->trxMode = isset( $p['trxMode'] ) ? strtoupper( $p['trxMode'] ) : null;
92 if ( $this->trxMode &&
93 !in_array( $this->trxMode, [ 'DEFERRED', 'IMMEDIATE', 'EXCLUSIVE' ] )
94 ) {
95 $this->trxMode = null;
96 $this->queryLogger->warning( "Invalid SQLite transaction mode provided." );
97 }
98
99 $this->lockMgr = new FSLockManager( [
100 'domain' => $lockDomain,
101 'lockDirectory' => "{$this->dbDir}/locks"
102 ] );
103 }
104
105 /**
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
112 * @since 1.25
113 */
114 public static function newStandaloneInstance( $filename, array $p = [] ) {
115 $p['dbFilePath'] = $filename;
116 $p['schema'] = false;
117 $p['tablePrefix'] = '';
118
119 return Database::factory( 'sqlite', $p );
120 }
121
122 /**
123 * @return string
124 */
125 function getType() {
126 return 'sqlite';
127 }
128
129 /**
130 * @todo Check if it should be true like parent class
131 *
132 * @return bool
133 */
134 function implicitGroupby() {
135 return false;
136 }
137
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
140 *
141 * @param string $server
142 * @param string $user
143 * @param string $pass
144 * @param string $dbName
145 *
146 * @throws DBConnectionError
147 * @return bool
148 */
149 function open( $server, $user, $pass, $dbName ) {
150 $this->close();
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" );
155 }
156 $this->openFile( $fileName );
157
158 return (bool)$this->mConn;
159 }
160
161 /**
162 * Opens a database file
163 *
164 * @param string $fileName
165 * @throws DBConnectionError
166 * @return PDO|bool SQL connection or false if failed
167 */
168 protected function openFile( $fileName ) {
169 $err = false;
170
171 $this->dbPath = $fileName;
172 try {
173 if ( $this->mFlags & self::DBO_PERSISTENT ) {
174 $this->mConn = new PDO( "sqlite:$fileName", '', '',
175 [ PDO::ATTR_PERSISTENT => true ] );
176 } else {
177 $this->mConn = new PDO( "sqlite:$fileName", '', '' );
178 }
179 } catch ( PDOException $e ) {
180 $err = $e->getMessage();
181 }
182
183 if ( !$this->mConn ) {
184 $this->queryLogger->debug( "DB connection error: $err\n" );
185 throw new DBConnectionError( $this, $err );
186 }
187
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' );
194
195 return $this->mConn;
196 }
197
198 return false;
199 }
200
201 public function selectDB( $db ) {
202 return false; // doesn't make sense
203 }
204
205 /**
206 * @return string SQLite DB file path
207 * @since 1.25
208 */
209 public function getDbFilePath() {
210 return $this->dbPath;
211 }
212
213 /**
214 * Does not actually close the connection, just destroys the reference for GC to do its work
215 * @return bool
216 */
217 protected function closeConnection() {
218 $this->mConn = null;
219
220 return true;
221 }
222
223 /**
224 * Generates a database file name. Explicitly public for installer.
225 * @param string $dir Directory where database resides
226 * @param string $dbName Database name
227 * @return string
228 */
229 public static function generateFileName( $dir, $dbName ) {
230 return "$dir/$dbName.sqlite";
231 }
232
233 /**
234 * Check if the searchindext table is FTS enabled.
235 * @return bool False if not enabled.
236 */
237 function checkForEnabledSearch() {
238 if ( self::$fulltextEnabled === null ) {
239 self::$fulltextEnabled = false;
240 $table = $this->tableName( 'searchindex' );
241 $res = $this->query( "SELECT sql FROM sqlite_master WHERE tbl_name = '$table'", __METHOD__ );
242 if ( $res ) {
243 $row = $res->fetchRow();
244 self::$fulltextEnabled = stristr( $row['sql'], 'fts' ) !== false;
245 }
246 }
247
248 return self::$fulltextEnabled;
249 }
250
251 /**
252 * Returns version of currently supported SQLite fulltext search module or false if none present.
253 * @return string
254 */
255 static function getFulltextSearchModule() {
256 static $cachedResult = null;
257 if ( $cachedResult !== null ) {
258 return $cachedResult;
259 }
260 $cachedResult = false;
261 $table = 'dummy_search_test';
262
263 $db = self::newStandaloneInstance( ':memory:' );
264 if ( $db->query( "CREATE VIRTUAL TABLE $table USING FTS3(dummy_field)", __METHOD__, true ) ) {
265 $cachedResult = 'FTS3';
266 }
267 $db->close();
268
269 return $cachedResult;
270 }
271
272 /**
273 * Attaches external database to our connection, see https://sqlite.org/lang_attach.html
274 * for details.
275 *
276 * @param string $name Database name to be used in queries like
277 * SELECT foo FROM dbname.table
278 * @param bool|string $file Database file name. If omitted, will be generated
279 * using $name and configured data directory
280 * @param string $fname Calling function name
281 * @return ResultWrapper
282 */
283 function attachDatabase( $name, $file = false, $fname = __METHOD__ ) {
284 if ( !$file ) {
285 $file = self::generateFileName( $this->dbDir, $name );
286 }
287 $file = $this->addQuotes( $file );
288
289 return $this->query( "ATTACH DATABASE $file AS $name", $fname );
290 }
291
292 function isWriteQuery( $sql ) {
293 return parent::isWriteQuery( $sql ) && !preg_match( '/^(ATTACH|PRAGMA)\b/i', $sql );
294 }
295
296 /**
297 * SQLite doesn't allow buffered results or data seeking etc, so we'll use fetchAll as the result
298 *
299 * @param string $sql
300 * @return bool|ResultWrapper
301 */
302 protected function doQuery( $sql ) {
303 $res = $this->mConn->query( $sql );
304 if ( $res === false ) {
305 return false;
306 }
307
308 $r = $res instanceof ResultWrapper ? $res->result : $res;
309 $this->mAffectedRows = $r->rowCount();
310 $res = new ResultWrapper( $this, $r->fetchAll() );
311
312 return $res;
313 }
314
315 /**
316 * @param ResultWrapper|mixed $res
317 */
318 function freeResult( $res ) {
319 if ( $res instanceof ResultWrapper ) {
320 $res->result = null;
321 } else {
322 $res = null;
323 }
324 }
325
326 /**
327 * @param ResultWrapper|array $res
328 * @return stdClass|bool
329 */
330 function fetchObject( $res ) {
331 if ( $res instanceof ResultWrapper ) {
332 $r =& $res->result;
333 } else {
334 $r =& $res;
335 }
336
337 $cur = current( $r );
338 if ( is_array( $cur ) ) {
339 next( $r );
340 $obj = new stdClass;
341 foreach ( $cur as $k => $v ) {
342 if ( !is_numeric( $k ) ) {
343 $obj->$k = $v;
344 }
345 }
346
347 return $obj;
348 }
349
350 return false;
351 }
352
353 /**
354 * @param ResultWrapper|mixed $res
355 * @return array|bool
356 */
357 function fetchRow( $res ) {
358 if ( $res instanceof ResultWrapper ) {
359 $r =& $res->result;
360 } else {
361 $r =& $res;
362 }
363 $cur = current( $r );
364 if ( is_array( $cur ) ) {
365 next( $r );
366
367 return $cur;
368 }
369
370 return false;
371 }
372
373 /**
374 * The PDO::Statement class implements the array interface so count() will work
375 *
376 * @param ResultWrapper|array $res
377 * @return int
378 */
379 function numRows( $res ) {
380 $r = $res instanceof ResultWrapper ? $res->result : $res;
381
382 return count( $r );
383 }
384
385 /**
386 * @param ResultWrapper $res
387 * @return int
388 */
389 function numFields( $res ) {
390 $r = $res instanceof ResultWrapper ? $res->result : $res;
391 if ( is_array( $r ) && count( $r ) > 0 ) {
392 // The size of the result array is twice the number of fields. (Bug: 65578)
393 return count( $r[0] ) / 2;
394 } else {
395 // If the result is empty return 0
396 return 0;
397 }
398 }
399
400 /**
401 * @param ResultWrapper $res
402 * @param int $n
403 * @return bool
404 */
405 function fieldName( $res, $n ) {
406 $r = $res instanceof ResultWrapper ? $res->result : $res;
407 if ( is_array( $r ) ) {
408 $keys = array_keys( $r[0] );
409
410 return $keys[$n];
411 }
412
413 return false;
414 }
415
416 /**
417 * Use MySQL's naming (accounts for prefix etc) but remove surrounding backticks
418 *
419 * @param string $name
420 * @param string $format
421 * @return string
422 */
423 function tableName( $name, $format = 'quoted' ) {
424 // table names starting with sqlite_ are reserved
425 if ( strpos( $name, 'sqlite_' ) === 0 ) {
426 return $name;
427 }
428
429 return str_replace( '"', '', parent::tableName( $name, $format ) );
430 }
431
432 /**
433 * This must be called after nextSequenceVal
434 *
435 * @return int
436 */
437 function insertId() {
438 // PDO::lastInsertId yields a string :(
439 return intval( $this->mConn->lastInsertId() );
440 }
441
442 /**
443 * @param ResultWrapper|array $res
444 * @param int $row
445 */
446 function dataSeek( $res, $row ) {
447 if ( $res instanceof ResultWrapper ) {
448 $r =& $res->result;
449 } else {
450 $r =& $res;
451 }
452 reset( $r );
453 if ( $row > 0 ) {
454 for ( $i = 0; $i < $row; $i++ ) {
455 next( $r );
456 }
457 }
458 }
459
460 /**
461 * @return string
462 */
463 function lastError() {
464 if ( !is_object( $this->mConn ) ) {
465 return "Cannot return last error, no db connection";
466 }
467 $e = $this->mConn->errorInfo();
468
469 return isset( $e[2] ) ? $e[2] : '';
470 }
471
472 /**
473 * @return string
474 */
475 function lastErrno() {
476 if ( !is_object( $this->mConn ) ) {
477 return "Cannot return last error, no db connection";
478 } else {
479 $info = $this->mConn->errorInfo();
480
481 return $info[1];
482 }
483 }
484
485 /**
486 * @return int
487 */
488 function affectedRows() {
489 return $this->mAffectedRows;
490 }
491
492 /**
493 * Returns information about an index
494 * Returns false if the index does not exist
495 * - if errors are explicitly ignored, returns NULL on failure
496 *
497 * @param string $table
498 * @param string $index
499 * @param string $fname
500 * @return array|false
501 */
502 function indexInfo( $table, $index, $fname = __METHOD__ ) {
503 $sql = 'PRAGMA index_info(' . $this->addQuotes( $this->indexName( $index ) ) . ')';
504 $res = $this->query( $sql, $fname );
505 if ( !$res || $res->numRows() == 0 ) {
506 return false;
507 }
508 $info = [];
509 foreach ( $res as $row ) {
510 $info[] = $row->name;
511 }
512
513 return $info;
514 }
515
516 /**
517 * @param string $table
518 * @param string $index
519 * @param string $fname
520 * @return bool|null
521 */
522 function indexUnique( $table, $index, $fname = __METHOD__ ) {
523 $row = $this->selectRow( 'sqlite_master', '*',
524 [
525 'type' => 'index',
526 'name' => $this->indexName( $index ),
527 ], $fname );
528 if ( !$row || !isset( $row->sql ) ) {
529 return null;
530 }
531
532 // $row->sql will be of the form CREATE [UNIQUE] INDEX ...
533 $indexPos = strpos( $row->sql, 'INDEX' );
534 if ( $indexPos === false ) {
535 return null;
536 }
537 $firstPart = substr( $row->sql, 0, $indexPos );
538 $options = explode( ' ', $firstPart );
539
540 return in_array( 'UNIQUE', $options );
541 }
542
543 /**
544 * Filter the options used in SELECT statements
545 *
546 * @param array $options
547 * @return array
548 */
549 function makeSelectOptions( $options ) {
550 foreach ( $options as $k => $v ) {
551 if ( is_numeric( $k ) && ( $v == 'FOR UPDATE' || $v == 'LOCK IN SHARE MODE' ) ) {
552 $options[$k] = '';
553 }
554 }
555
556 return parent::makeSelectOptions( $options );
557 }
558
559 /**
560 * @param array $options
561 * @return string
562 */
563 protected function makeUpdateOptionsArray( $options ) {
564 $options = parent::makeUpdateOptionsArray( $options );
565 $options = self::fixIgnore( $options );
566
567 return $options;
568 }
569
570 /**
571 * @param array $options
572 * @return array
573 */
574 static function fixIgnore( $options ) {
575 # SQLite uses OR IGNORE not just IGNORE
576 foreach ( $options as $k => $v ) {
577 if ( $v == 'IGNORE' ) {
578 $options[$k] = 'OR IGNORE';
579 }
580 }
581
582 return $options;
583 }
584
585 /**
586 * @param array $options
587 * @return string
588 */
589 function makeInsertOptions( $options ) {
590 $options = self::fixIgnore( $options );
591
592 return parent::makeInsertOptions( $options );
593 }
594
595 /**
596 * Based on generic method (parent) with some prior SQLite-sepcific adjustments
597 * @param string $table
598 * @param array $a
599 * @param string $fname
600 * @param array $options
601 * @return bool
602 */
603 function insert( $table, $a, $fname = __METHOD__, $options = [] ) {
604 if ( !count( $a ) ) {
605 return true;
606 }
607
608 # SQLite can't handle multi-row inserts, so divide up into multiple single-row inserts
609 if ( isset( $a[0] ) && is_array( $a[0] ) ) {
610 $ret = true;
611 foreach ( $a as $v ) {
612 if ( !parent::insert( $table, $v, "$fname/multi-row", $options ) ) {
613 $ret = false;
614 }
615 }
616 } else {
617 $ret = parent::insert( $table, $a, "$fname/single-row", $options );
618 }
619
620 return $ret;
621 }
622
623 /**
624 * @param string $table
625 * @param array $uniqueIndexes Unused
626 * @param string|array $rows
627 * @param string $fname
628 * @return bool|ResultWrapper
629 */
630 function replace( $table, $uniqueIndexes, $rows, $fname = __METHOD__ ) {
631 if ( !count( $rows ) ) {
632 return true;
633 }
634
635 # SQLite can't handle multi-row replaces, so divide up into multiple single-row queries
636 if ( isset( $rows[0] ) && is_array( $rows[0] ) ) {
637 $ret = true;
638 foreach ( $rows as $v ) {
639 if ( !$this->nativeReplace( $table, $v, "$fname/multi-row" ) ) {
640 $ret = false;
641 }
642 }
643 } else {
644 $ret = $this->nativeReplace( $table, $rows, "$fname/single-row" );
645 }
646
647 return $ret;
648 }
649
650 /**
651 * Returns the size of a text field, or -1 for "unlimited"
652 * In SQLite this is SQLITE_MAX_LENGTH, by default 1GB. No way to query it though.
653 *
654 * @param string $table
655 * @param string $field
656 * @return int
657 */
658 function textFieldSize( $table, $field ) {
659 return -1;
660 }
661
662 /**
663 * @return bool
664 */
665 function unionSupportsOrderAndLimit() {
666 return false;
667 }
668
669 /**
670 * @param string[] $sqls
671 * @param bool $all Whether to "UNION ALL" or not
672 * @return string
673 */
674 function unionQueries( $sqls, $all ) {
675 $glue = $all ? ' UNION ALL ' : ' UNION ';
676
677 return implode( $glue, $sqls );
678 }
679
680 /**
681 * @return bool
682 */
683 function wasDeadlock() {
684 return $this->lastErrno() == 5; // SQLITE_BUSY
685 }
686
687 /**
688 * @return bool
689 */
690 function wasErrorReissuable() {
691 return $this->lastErrno() == 17; // SQLITE_SCHEMA;
692 }
693
694 /**
695 * @return bool
696 */
697 function wasReadOnlyError() {
698 return $this->lastErrno() == 8; // SQLITE_READONLY;
699 }
700
701 /**
702 * @return string Wikitext of a link to the server software's web site
703 */
704 public function getSoftwareLink() {
705 return "[{{int:version-db-sqlite-url}} SQLite]";
706 }
707
708 /**
709 * @return string Version information from the database
710 */
711 function getServerVersion() {
712 $ver = $this->mConn->getAttribute( PDO::ATTR_SERVER_VERSION );
713
714 return $ver;
715 }
716
717 /**
718 * Get information about a given field
719 * Returns false if the field does not exist.
720 *
721 * @param string $table
722 * @param string $field
723 * @return SQLiteField|bool False on failure
724 */
725 function fieldInfo( $table, $field ) {
726 $tableName = $this->tableName( $table );
727 $sql = 'PRAGMA table_info(' . $this->addQuotes( $tableName ) . ')';
728 $res = $this->query( $sql, __METHOD__ );
729 foreach ( $res as $row ) {
730 if ( $row->name == $field ) {
731 return new SQLiteField( $row, $tableName );
732 }
733 }
734
735 return false;
736 }
737
738 protected function doBegin( $fname = '' ) {
739 if ( $this->trxMode ) {
740 $this->query( "BEGIN {$this->trxMode}", $fname );
741 } else {
742 $this->query( 'BEGIN', $fname );
743 }
744 $this->mTrxLevel = 1;
745 }
746
747 /**
748 * @param string $s
749 * @return string
750 */
751 function strencode( $s ) {
752 return substr( $this->addQuotes( $s ), 1, -1 );
753 }
754
755 /**
756 * @param string $b
757 * @return Blob
758 */
759 function encodeBlob( $b ) {
760 return new Blob( $b );
761 }
762
763 /**
764 * @param Blob|string $b
765 * @return string
766 */
767 function decodeBlob( $b ) {
768 if ( $b instanceof Blob ) {
769 $b = $b->fetch();
770 }
771
772 return $b;
773 }
774
775 /**
776 * @param string|int|null|bool|Blob $s
777 * @return string|int
778 */
779 function addQuotes( $s ) {
780 if ( $s instanceof Blob ) {
781 return "x'" . bin2hex( $s->fetch() ) . "'";
782 } elseif ( is_bool( $s ) ) {
783 return (int)$s;
784 } elseif ( strpos( $s, "\0" ) !== false ) {
785 // SQLite doesn't support \0 in strings, so use the hex representation as a workaround.
786 // This is a known limitation of SQLite's mprintf function which PDO
787 // should work around, but doesn't. I have reported this to php.net as bug #63419:
788 // https://bugs.php.net/bug.php?id=63419
789 // There was already a similar report for SQLite3::escapeString, bug #62361:
790 // https://bugs.php.net/bug.php?id=62361
791 // There is an additional bug regarding sorting this data after insert
792 // on older versions of sqlite shipped with ubuntu 12.04
793 // https://phabricator.wikimedia.org/T74367
794 $this->queryLogger->debug(
795 __FUNCTION__ .
796 ': Quoting value containing null byte. ' .
797 'For consistency all binary data should have been ' .
798 'first processed with self::encodeBlob()'
799 );
800 return "x'" . bin2hex( $s ) . "'";
801 } else {
802 return $this->mConn->quote( $s );
803 }
804 }
805
806 /**
807 * @return string
808 */
809 function buildLike() {
810 $params = func_get_args();
811 if ( count( $params ) > 0 && is_array( $params[0] ) ) {
812 $params = $params[0];
813 }
814
815 return parent::buildLike( $params ) . "ESCAPE '\' ";
816 }
817
818 /**
819 * @param string $field Field or column to cast
820 * @return string
821 * @since 1.28
822 */
823 public function buildStringCast( $field ) {
824 return 'CAST ( ' . $field . ' AS TEXT )';
825 }
826
827 /**
828 * No-op version of deadlockLoop
829 *
830 * @return mixed
831 */
832 public function deadlockLoop( /*...*/ ) {
833 $args = func_get_args();
834 $function = array_shift( $args );
835
836 return call_user_func_array( $function, $args );
837 }
838
839 /**
840 * @param string $s
841 * @return string
842 */
843 protected function replaceVars( $s ) {
844 $s = parent::replaceVars( $s );
845 if ( preg_match( '/^\s*(CREATE|ALTER) TABLE/i', $s ) ) {
846 // CREATE TABLE hacks to allow schema file sharing with MySQL
847
848 // binary/varbinary column type -> blob
849 $s = preg_replace( '/\b(var)?binary(\(\d+\))/i', 'BLOB', $s );
850 // no such thing as unsigned
851 $s = preg_replace( '/\b(un)?signed\b/i', '', $s );
852 // INT -> INTEGER
853 $s = preg_replace( '/\b(tiny|small|medium|big|)int(\s*\(\s*\d+\s*\)|\b)/i', 'INTEGER', $s );
854 // floating point types -> REAL
855 $s = preg_replace(
856 '/\b(float|double(\s+precision)?)(\s*\(\s*\d+\s*(,\s*\d+\s*)?\)|\b)/i',
857 'REAL',
858 $s
859 );
860 // varchar -> TEXT
861 $s = preg_replace( '/\b(var)?char\s*\(.*?\)/i', 'TEXT', $s );
862 // TEXT normalization
863 $s = preg_replace( '/\b(tiny|medium|long)text\b/i', 'TEXT', $s );
864 // BLOB normalization
865 $s = preg_replace( '/\b(tiny|small|medium|long|)blob\b/i', 'BLOB', $s );
866 // BOOL -> INTEGER
867 $s = preg_replace( '/\bbool(ean)?\b/i', 'INTEGER', $s );
868 // DATETIME -> TEXT
869 $s = preg_replace( '/\b(datetime|timestamp)\b/i', 'TEXT', $s );
870 // No ENUM type
871 $s = preg_replace( '/\benum\s*\([^)]*\)/i', 'TEXT', $s );
872 // binary collation type -> nothing
873 $s = preg_replace( '/\bbinary\b/i', '', $s );
874 // auto_increment -> autoincrement
875 $s = preg_replace( '/\bauto_increment\b/i', 'AUTOINCREMENT', $s );
876 // No explicit options
877 $s = preg_replace( '/\)[^);]*(;?)\s*$/', ')\1', $s );
878 // AUTOINCREMENT should immedidately follow PRIMARY KEY
879 $s = preg_replace( '/primary key (.*?) autoincrement/i', 'PRIMARY KEY AUTOINCREMENT $1', $s );
880 } elseif ( preg_match( '/^\s*CREATE (\s*(?:UNIQUE|FULLTEXT)\s+)?INDEX/i', $s ) ) {
881 // No truncated indexes
882 $s = preg_replace( '/\(\d+\)/', '', $s );
883 // No FULLTEXT
884 $s = preg_replace( '/\bfulltext\b/i', '', $s );
885 } elseif ( preg_match( '/^\s*DROP INDEX/i', $s ) ) {
886 // DROP INDEX is database-wide, not table-specific, so no ON <table> clause.
887 $s = preg_replace( '/\sON\s+[^\s]*/i', '', $s );
888 } elseif ( preg_match( '/^\s*INSERT IGNORE\b/i', $s ) ) {
889 // INSERT IGNORE --> INSERT OR IGNORE
890 $s = preg_replace( '/^\s*INSERT IGNORE\b/i', 'INSERT OR IGNORE', $s );
891 }
892
893 return $s;
894 }
895
896 public function lock( $lockName, $method, $timeout = 5 ) {
897 if ( !is_dir( "{$this->dbDir}/locks" ) ) { // create dir as needed
898 if ( !is_writable( $this->dbDir ) || !mkdir( "{$this->dbDir}/locks" ) ) {
899 throw new DBError( $this, "Cannot create directory \"{$this->dbDir}/locks\"." );
900 }
901 }
902
903 return $this->lockMgr->lock( [ $lockName ], LockManager::LOCK_EX, $timeout )->isOK();
904 }
905
906 public function unlock( $lockName, $method ) {
907 return $this->lockMgr->unlock( [ $lockName ], LockManager::LOCK_EX )->isOK();
908 }
909
910 /**
911 * Build a concatenation list to feed into a SQL query
912 *
913 * @param string[] $stringList
914 * @return string
915 */
916 function buildConcat( $stringList ) {
917 return '(' . implode( ') || (', $stringList ) . ')';
918 }
919
920 public function buildGroupConcatField(
921 $delim, $table, $field, $conds = '', $join_conds = []
922 ) {
923 $fld = "group_concat($field," . $this->addQuotes( $delim ) . ')';
924
925 return '(' . $this->selectSQLText( $table, $fld, $conds, null, [], $join_conds ) . ')';
926 }
927
928 /**
929 * @param string $oldName
930 * @param string $newName
931 * @param bool $temporary
932 * @param string $fname
933 * @return bool|ResultWrapper
934 * @throws RuntimeException
935 */
936 function duplicateTableStructure( $oldName, $newName, $temporary = false, $fname = __METHOD__ ) {
937 $res = $this->query( "SELECT sql FROM sqlite_master WHERE tbl_name=" .
938 $this->addQuotes( $oldName ) . " AND type='table'", $fname );
939 $obj = $this->fetchObject( $res );
940 if ( !$obj ) {
941 throw new RuntimeException( "Couldn't retrieve structure for table $oldName" );
942 }
943 $sql = $obj->sql;
944 $sql = preg_replace(
945 '/(?<=\W)"?' . preg_quote( trim( $this->addIdentifierQuotes( $oldName ), '"' ) ) . '"?(?=\W)/',
946 $this->addIdentifierQuotes( $newName ),
947 $sql,
948 1
949 );
950 if ( $temporary ) {
951 if ( preg_match( '/^\\s*CREATE\\s+VIRTUAL\\s+TABLE\b/i', $sql ) ) {
952 $this->queryLogger->debug(
953 "Table $oldName is virtual, can't create a temporary duplicate.\n" );
954 } else {
955 $sql = str_replace( 'CREATE TABLE', 'CREATE TEMPORARY TABLE', $sql );
956 }
957 }
958
959 $res = $this->query( $sql, $fname );
960
961 // Take over indexes
962 $indexList = $this->query( 'PRAGMA INDEX_LIST(' . $this->addQuotes( $oldName ) . ')' );
963 foreach ( $indexList as $index ) {
964 if ( strpos( $index->name, 'sqlite_autoindex' ) === 0 ) {
965 continue;
966 }
967
968 if ( $index->unique ) {
969 $sql = 'CREATE UNIQUE INDEX';
970 } else {
971 $sql = 'CREATE INDEX';
972 }
973 // Try to come up with a new index name, given indexes have database scope in SQLite
974 $indexName = $newName . '_' . $index->name;
975 $sql .= ' ' . $indexName . ' ON ' . $newName;
976
977 $indexInfo = $this->query( 'PRAGMA INDEX_INFO(' . $this->addQuotes( $index->name ) . ')' );
978 $fields = [];
979 foreach ( $indexInfo as $indexInfoRow ) {
980 $fields[$indexInfoRow->seqno] = $indexInfoRow->name;
981 }
982
983 $sql .= '(' . implode( ',', $fields ) . ')';
984
985 $this->query( $sql );
986 }
987
988 return $res;
989 }
990
991 /**
992 * List all tables on the database
993 *
994 * @param string $prefix Only show tables with this prefix, e.g. mw_
995 * @param string $fname Calling function name
996 *
997 * @return array
998 */
999 function listTables( $prefix = null, $fname = __METHOD__ ) {
1000 $result = $this->select(
1001 'sqlite_master',
1002 'name',
1003 "type='table'"
1004 );
1005
1006 $endArray = [];
1007
1008 foreach ( $result as $table ) {
1009 $vars = get_object_vars( $table );
1010 $table = array_pop( $vars );
1011
1012 if ( !$prefix || strpos( $table, $prefix ) === 0 ) {
1013 if ( strpos( $table, 'sqlite_' ) !== 0 ) {
1014 $endArray[] = $table;
1015 }
1016 }
1017 }
1018
1019 return $endArray;
1020 }
1021
1022 /**
1023 * Override due to no CASCADE support
1024 *
1025 * @param string $tableName
1026 * @param string $fName
1027 * @return bool|ResultWrapper
1028 * @throws DBReadOnlyError
1029 */
1030 public function dropTable( $tableName, $fName = __METHOD__ ) {
1031 if ( !$this->tableExists( $tableName, $fName ) ) {
1032 return false;
1033 }
1034 $sql = "DROP TABLE " . $this->tableName( $tableName );
1035
1036 return $this->query( $sql, $fName );
1037 }
1038
1039 protected function requiresDatabaseUser() {
1040 return false; // just a file
1041 }
1042
1043 /**
1044 * @return string
1045 */
1046 public function __toString() {
1047 return 'SQLite ' . (string)$this->mConn->getAttribute( PDO::ATTR_SERVER_VERSION );
1048 }
1049 }