More return documentation
[lhc/web/wiklou.git] / includes / db / 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 * @file
7 * @ingroup Database
8 */
9
10 /**
11 * @ingroup Database
12 */
13 class DatabaseSqlite extends DatabaseBase {
14
15 private static $fulltextEnabled = null;
16
17 var $mAffectedRows;
18 var $mLastResult;
19 var $mDatabaseFile;
20 var $mName;
21
22 /**
23 * @var PDO
24 */
25 protected $mConn;
26
27 /**
28 * Constructor.
29 * Parameters $server, $user and $password are not used.
30 * @param $server string
31 * @param $user string
32 * @param $password string
33 * @param $dbName string
34 * @param $flags int
35 */
36 function __construct( $server = false, $user = false, $password = false, $dbName = false, $flags = 0 ) {
37 $this->mName = $dbName;
38 parent::__construct( $server, $user, $password, $dbName, $flags );
39 // parent doesn't open when $user is false, but we can work with $dbName
40 if( $dbName ) {
41 global $wgSharedDB;
42 if( $this->open( $server, $user, $password, $dbName ) && $wgSharedDB ) {
43 $this->attachDatabase( $wgSharedDB );
44 }
45 }
46 }
47
48 /**
49 * @return string
50 */
51 function getType() {
52 return 'sqlite';
53 }
54
55 /**
56 * @todo: check if it should be true like parent class
57 *
58 * @return bool
59 */
60 function implicitGroupby() {
61 return false;
62 }
63
64 /** Open an SQLite database and return a resource handle to it
65 * NOTE: only $dbName is used, the other parameters are irrelevant for SQLite databases
66 *
67 * @param $server
68 * @param $user
69 * @param $pass
70 * @param $dbName
71 *
72 * @return PDO
73 */
74 function open( $server, $user, $pass, $dbName ) {
75 global $wgSQLiteDataDir;
76
77 $fileName = self::generateFileName( $wgSQLiteDataDir, $dbName );
78 if ( !is_readable( $fileName ) ) {
79 $this->mConn = false;
80 throw new DBConnectionError( $this, "SQLite database not accessible" );
81 }
82 $this->openFile( $fileName );
83 return $this->mConn;
84 }
85
86 /**
87 * Opens a database file
88 *
89 * @param $fileName string
90 *
91 * @return PDO|bool SQL connection or false if failed
92 */
93 function openFile( $fileName ) {
94 $this->mDatabaseFile = $fileName;
95 try {
96 if ( $this->mFlags & DBO_PERSISTENT ) {
97 $this->mConn = new PDO( "sqlite:$fileName", '', '',
98 array( PDO::ATTR_PERSISTENT => true ) );
99 } else {
100 $this->mConn = new PDO( "sqlite:$fileName", '', '' );
101 }
102 } catch ( PDOException $e ) {
103 $err = $e->getMessage();
104 }
105 if ( !$this->mConn ) {
106 wfDebug( "DB connection error: $err\n" );
107 throw new DBConnectionError( $this, $err );
108 }
109 $this->mOpened = !!$this->mConn;
110 # set error codes only, don't raise exceptions
111 if ( $this->mOpened ) {
112 $this->mConn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT );
113 return true;
114 }
115 }
116
117 /**
118 * Close an SQLite database
119 *
120 * @return bool
121 */
122 function close() {
123 $this->mOpened = false;
124 if ( is_object( $this->mConn ) ) {
125 if ( $this->trxLevel() ) $this->commit();
126 $this->mConn = null;
127 }
128 return true;
129 }
130
131 /**
132 * Generates a database file name. Explicitly public for installer.
133 * @param $dir String: Directory where database resides
134 * @param $dbName String: Database name
135 * @return String
136 */
137 public static function generateFileName( $dir, $dbName ) {
138 return "$dir/$dbName.sqlite";
139 }
140
141 /**
142 * Check if the searchindext table is FTS enabled.
143 * @return bool if not enabled.
144 */
145 function checkForEnabledSearch() {
146 if ( self::$fulltextEnabled === null ) {
147 self::$fulltextEnabled = false;
148 $table = $this->tableName( 'searchindex' );
149 $res = $this->query( "SELECT sql FROM sqlite_master WHERE tbl_name = '$table'", __METHOD__ );
150 if ( $res ) {
151 $row = $res->fetchRow();
152 self::$fulltextEnabled = stristr($row['sql'], 'fts' ) !== false;
153 }
154 }
155 return self::$fulltextEnabled;
156 }
157
158 /**
159 * Returns version of currently supported SQLite fulltext search module or false if none present.
160 * @return String
161 */
162 static function getFulltextSearchModule() {
163 static $cachedResult = null;
164 if ( $cachedResult !== null ) {
165 return $cachedResult;
166 }
167 $cachedResult = false;
168 $table = 'dummy_search_test';
169
170 $db = new DatabaseSqliteStandalone( ':memory:' );
171
172 if ( $db->query( "CREATE VIRTUAL TABLE $table USING FTS3(dummy_field)", __METHOD__, true ) ) {
173 $cachedResult = 'FTS3';
174 }
175 $db->close();
176 return $cachedResult;
177 }
178
179 /**
180 * Attaches external database to our connection, see http://sqlite.org/lang_attach.html
181 * for details.
182 *
183 * @param $name String: database name to be used in queries like SELECT foo FROM dbname.table
184 * @param $file String: database file name. If omitted, will be generated using $name and $wgSQLiteDataDir
185 * @param $fname String: calling function name
186 *
187 * @return ResultWrapper
188 */
189 function attachDatabase( $name, $file = false, $fname = 'DatabaseSqlite::attachDatabase' ) {
190 global $wgSQLiteDataDir;
191 if ( !$file ) {
192 $file = self::generateFileName( $wgSQLiteDataDir, $name );
193 }
194 $file = $this->addQuotes( $file );
195 return $this->query( "ATTACH DATABASE $file AS $name", $fname );
196 }
197
198 /**
199 * @see DatabaseBase::isWriteQuery()
200 *
201 * @param $sql string
202 *
203 * @return bool
204 */
205 function isWriteQuery( $sql ) {
206 return parent::isWriteQuery( $sql ) && !preg_match( '/^ATTACH\b/i', $sql );
207 }
208
209 /**
210 * SQLite doesn't allow buffered results or data seeking etc, so we'll use fetchAll as the result
211 *
212 * @param $sql string
213 *
214 * @return ResultWrapper
215 */
216 protected function doQuery( $sql ) {
217 $res = $this->mConn->query( $sql );
218 if ( $res === false ) {
219 return false;
220 } else {
221 $r = $res instanceof ResultWrapper ? $res->result : $res;
222 $this->mAffectedRows = $r->rowCount();
223 $res = new ResultWrapper( $this, $r->fetchAll() );
224 }
225 return $res;
226 }
227
228 /**
229 * @param $res ResultWrapper
230 */
231 function freeResult( $res ) {
232 if ( $res instanceof ResultWrapper ) {
233 $res->result = null;
234 } else {
235 $res = null;
236 }
237 }
238
239 /**
240 * @param $res ResultWrapper
241 * @return
242 */
243 function fetchObject( $res ) {
244 if ( $res instanceof ResultWrapper ) {
245 $r =& $res->result;
246 } else {
247 $r =& $res;
248 }
249
250 $cur = current( $r );
251 if ( is_array( $cur ) ) {
252 next( $r );
253 $obj = new stdClass;
254 foreach ( $cur as $k => $v ) {
255 if ( !is_numeric( $k ) ) {
256 $obj->$k = $v;
257 }
258 }
259
260 return $obj;
261 }
262 return false;
263 }
264
265 /**
266 * @param $res ResultWrapper
267 * @return bool|mixed
268 */
269 function fetchRow( $res ) {
270 if ( $res instanceof ResultWrapper ) {
271 $r =& $res->result;
272 } else {
273 $r =& $res;
274 }
275 $cur = current( $r );
276 if ( is_array( $cur ) ) {
277 next( $r );
278 return $cur;
279 }
280 return false;
281 }
282
283 /**
284 * The PDO::Statement class implements the array interface so count() will work
285 *
286 * @param $res ResultWrapper
287 *
288 * @return int
289 */
290 function numRows( $res ) {
291 $r = $res instanceof ResultWrapper ? $res->result : $res;
292 return count( $r );
293 }
294
295 /**
296 * @param $res ResultWrapper
297 * @return int
298 */
299 function numFields( $res ) {
300 $r = $res instanceof ResultWrapper ? $res->result : $res;
301 return is_array( $r ) ? count( $r[0] ) : 0;
302 }
303
304 /**
305 * @param $res ResultWrapper
306 * @param $n
307 * @return bool
308 */
309 function fieldName( $res, $n ) {
310 $r = $res instanceof ResultWrapper ? $res->result : $res;
311 if ( is_array( $r ) ) {
312 $keys = array_keys( $r[0] );
313 return $keys[$n];
314 }
315 return false;
316 }
317
318 /**
319 * Use MySQL's naming (accounts for prefix etc) but remove surrounding backticks
320 *
321 * @param $name
322 * @param $format String
323 * @return string
324 */
325 function tableName( $name, $format = 'quoted' ) {
326 // table names starting with sqlite_ are reserved
327 if ( strpos( $name, 'sqlite_' ) === 0 ) {
328 return $name;
329 }
330 return str_replace( '"', '', parent::tableName( $name, $format ) );
331 }
332
333 /**
334 * Index names have DB scope
335 *
336 * @param $index string
337 *
338 * @return string
339 */
340 function indexName( $index ) {
341 return $index;
342 }
343
344 /**
345 * This must be called after nextSequenceVal
346 *
347 * @return int
348 */
349 function insertId() {
350 return $this->mConn->lastInsertId();
351 }
352
353 /**
354 * @param $res ResultWrapper
355 * @param $row
356 */
357 function dataSeek( $res, $row ) {
358 if ( $res instanceof ResultWrapper ) {
359 $r =& $res->result;
360 } else {
361 $r =& $res;
362 }
363 reset( $r );
364 if ( $row > 0 ) {
365 for ( $i = 0; $i < $row; $i++ ) {
366 next( $r );
367 }
368 }
369 }
370
371 /**
372 * @return string
373 */
374 function lastError() {
375 if ( !is_object( $this->mConn ) ) {
376 return "Cannot return last error, no db connection";
377 }
378 $e = $this->mConn->errorInfo();
379 return isset( $e[2] ) ? $e[2] : '';
380 }
381
382 /**
383 * @return string
384 */
385 function lastErrno() {
386 if ( !is_object( $this->mConn ) ) {
387 return "Cannot return last error, no db connection";
388 } else {
389 $info = $this->mConn->errorInfo();
390 return $info[1];
391 }
392 }
393
394 /**
395 * @return int
396 */
397 function affectedRows() {
398 return $this->mAffectedRows;
399 }
400
401 /**
402 * Returns information about an index
403 * Returns false if the index does not exist
404 * - if errors are explicitly ignored, returns NULL on failure
405 *
406 * @return array
407 */
408 function indexInfo( $table, $index, $fname = 'DatabaseSqlite::indexExists' ) {
409 $sql = 'PRAGMA index_info(' . $this->addQuotes( $this->indexName( $index ) ) . ')';
410 $res = $this->query( $sql, $fname );
411 if ( !$res ) {
412 return null;
413 }
414 if ( $res->numRows() == 0 ) {
415 return false;
416 }
417 $info = array();
418 foreach ( $res as $row ) {
419 $info[] = $row->name;
420 }
421 return $info;
422 }
423
424 /**
425 * @param $table
426 * @param $index
427 * @param $fname string
428 * @return bool|null
429 */
430 function indexUnique( $table, $index, $fname = 'DatabaseSqlite::indexUnique' ) {
431 $row = $this->selectRow( 'sqlite_master', '*',
432 array(
433 'type' => 'index',
434 'name' => $this->indexName( $index ),
435 ), $fname );
436 if ( !$row || !isset( $row->sql ) ) {
437 return null;
438 }
439
440 // $row->sql will be of the form CREATE [UNIQUE] INDEX ...
441 $indexPos = strpos( $row->sql, 'INDEX' );
442 if ( $indexPos === false ) {
443 return null;
444 }
445 $firstPart = substr( $row->sql, 0, $indexPos );
446 $options = explode( ' ', $firstPart );
447 return in_array( 'UNIQUE', $options );
448 }
449
450 /**
451 * Filter the options used in SELECT statements
452 *
453 * @param $options array
454 *
455 * @return array
456 */
457 function makeSelectOptions( $options ) {
458 foreach ( $options as $k => $v ) {
459 if ( is_numeric( $k ) && $v == 'FOR UPDATE' ) {
460 $options[$k] = '';
461 }
462 }
463 return parent::makeSelectOptions( $options );
464 }
465
466 /**
467 * @param $options array
468 * @return string
469 */
470 function makeUpdateOptions( $options ) {
471 $options = self::fixIgnore( $options );
472 return parent::makeUpdateOptions( $options );
473 }
474
475 /**
476 * @param $options array
477 * @return array
478 */
479 static function fixIgnore( $options ) {
480 # SQLite uses OR IGNORE not just IGNORE
481 foreach ( $options as $k => $v ) {
482 if ( $v == 'IGNORE' ) {
483 $options[$k] = 'OR IGNORE';
484 }
485 }
486 return $options;
487 }
488
489 /**
490 * @param $options array
491 * @return string
492 */
493 function makeInsertOptions( $options ) {
494 $options = self::fixIgnore( $options );
495 return parent::makeInsertOptions( $options );
496 }
497
498 /**
499 * Based on generic method (parent) with some prior SQLite-sepcific adjustments
500 * @return bool
501 */
502 function insert( $table, $a, $fname = 'DatabaseSqlite::insert', $options = array() ) {
503 if ( !count( $a ) ) {
504 return true;
505 }
506
507 # SQLite can't handle multi-row inserts, so divide up into multiple single-row inserts
508 if ( isset( $a[0] ) && is_array( $a[0] ) ) {
509 $ret = true;
510 foreach ( $a as $v ) {
511 if ( !parent::insert( $table, $v, "$fname/multi-row", $options ) ) {
512 $ret = false;
513 }
514 }
515 } else {
516 $ret = parent::insert( $table, $a, "$fname/single-row", $options );
517 }
518
519 return $ret;
520 }
521
522 /**
523 * @param $table
524 * @param $uniqueIndexes
525 * @param $rows
526 * @param $fname string
527 * @return bool|ResultWrapper
528 */
529 function replace( $table, $uniqueIndexes, $rows, $fname = 'DatabaseSqlite::replace' ) {
530 if ( !count( $rows ) ) return true;
531
532 # SQLite can't handle multi-row replaces, so divide up into multiple single-row queries
533 if ( isset( $rows[0] ) && is_array( $rows[0] ) ) {
534 $ret = true;
535 foreach ( $rows as $v ) {
536 if ( !$this->nativeReplace( $table, $v, "$fname/multi-row" ) ) {
537 $ret = false;
538 }
539 }
540 } else {
541 $ret = $this->nativeReplace( $table, $rows, "$fname/single-row" );
542 }
543
544 return $ret;
545 }
546
547 /**
548 * Returns the size of a text field, or -1 for "unlimited"
549 * In SQLite this is SQLITE_MAX_LENGTH, by default 1GB. No way to query it though.
550 *
551 * @return int
552 */
553 function textFieldSize( $table, $field ) {
554 return -1;
555 }
556
557 /**
558 * @return bool
559 */
560 function unionSupportsOrderAndLimit() {
561 return false;
562 }
563
564 /**
565 * @param $sqls
566 * @param $all
567 * @return string
568 */
569 function unionQueries( $sqls, $all ) {
570 $glue = $all ? ' UNION ALL ' : ' UNION ';
571 return implode( $glue, $sqls );
572 }
573
574 /**
575 * @return bool
576 */
577 function wasDeadlock() {
578 return $this->lastErrno() == 5; // SQLITE_BUSY
579 }
580
581 /**
582 * @return bool
583 */
584 function wasErrorReissuable() {
585 return $this->lastErrno() == 17; // SQLITE_SCHEMA;
586 }
587
588 /**
589 * @return bool
590 */
591 function wasReadOnlyError() {
592 return $this->lastErrno() == 8; // SQLITE_READONLY;
593 }
594
595 /**
596 * @return string wikitext of a link to the server software's web site
597 */
598 public static function getSoftwareLink() {
599 return "[http://sqlite.org/ SQLite]";
600 }
601
602 /**
603 * @return string Version information from the database
604 */
605 function getServerVersion() {
606 $ver = $this->mConn->getAttribute( PDO::ATTR_SERVER_VERSION );
607 return $ver;
608 }
609
610 /**
611 * @return string User-friendly database information
612 */
613 public function getServerInfo() {
614 return wfMsg( self::getFulltextSearchModule() ? 'sqlite-has-fts' : 'sqlite-no-fts', $this->getServerVersion() );
615 }
616
617 /**
618 * Get information about a given field
619 * Returns false if the field does not exist.
620 *
621 * @return SQLiteField|bool
622 */
623 function fieldInfo( $table, $field ) {
624 $tableName = $this->tableName( $table );
625 $sql = 'PRAGMA table_info(' . $this->addQuotes( $tableName ) . ')';
626 $res = $this->query( $sql, __METHOD__ );
627 foreach ( $res as $row ) {
628 if ( $row->name == $field ) {
629 return new SQLiteField( $row, $tableName );
630 }
631 }
632 return false;
633 }
634
635 function begin( $fname = '' ) {
636 if ( $this->mTrxLevel == 1 ) {
637 $this->commit();
638 }
639 $this->mConn->beginTransaction();
640 $this->mTrxLevel = 1;
641 }
642
643 function commit( $fname = '' ) {
644 if ( $this->mTrxLevel == 0 ) {
645 return;
646 }
647 $this->mConn->commit();
648 $this->mTrxLevel = 0;
649 }
650
651 function rollback( $fname = '' ) {
652 if ( $this->mTrxLevel == 0 ) {
653 return;
654 }
655 $this->mConn->rollBack();
656 $this->mTrxLevel = 0;
657 }
658
659 /**
660 * @param $sql
661 * @param $num
662 * @return string
663 */
664 function limitResultForUpdate( $sql, $num ) {
665 return $this->limitResult( $sql, $num );
666 }
667
668 /**
669 * @param $s string
670 * @return string
671 */
672 function strencode( $s ) {
673 return substr( $this->addQuotes( $s ), 1, - 1 );
674 }
675
676 /**
677 * @param $b
678 * @return Blob
679 */
680 function encodeBlob( $b ) {
681 return new Blob( $b );
682 }
683
684 /**
685 * @param $b Blob|string
686 * @return string
687 */
688 function decodeBlob( $b ) {
689 if ( $b instanceof Blob ) {
690 $b = $b->fetch();
691 }
692 return $b;
693 }
694
695 /**
696 * @param $s Blob|string
697 * @return string
698 */
699 function addQuotes( $s ) {
700 if ( $s instanceof Blob ) {
701 return "x'" . bin2hex( $s->fetch() ) . "'";
702 } else {
703 return $this->mConn->quote( $s );
704 }
705 }
706
707 /**
708 * @return string
709 */
710 function buildLike() {
711 $params = func_get_args();
712 if ( count( $params ) > 0 && is_array( $params[0] ) ) {
713 $params = $params[0];
714 }
715 return parent::buildLike( $params ) . "ESCAPE '\' ";
716 }
717
718 /**
719 * @return string
720 */
721 public function getSearchEngine() {
722 return "SearchSqlite";
723 }
724
725 /**
726 * No-op version of deadlockLoop
727 * @return mixed
728 */
729 public function deadlockLoop( /*...*/ ) {
730 $args = func_get_args();
731 $function = array_shift( $args );
732 return call_user_func_array( $function, $args );
733 }
734
735 /**
736 * @param $s string
737 * @return string
738 */
739 protected function replaceVars( $s ) {
740 $s = parent::replaceVars( $s );
741 if ( preg_match( '/^\s*(CREATE|ALTER) TABLE/i', $s ) ) {
742 // CREATE TABLE hacks to allow schema file sharing with MySQL
743
744 // binary/varbinary column type -> blob
745 $s = preg_replace( '/\b(var)?binary(\(\d+\))/i', 'BLOB', $s );
746 // no such thing as unsigned
747 $s = preg_replace( '/\b(un)?signed\b/i', '', $s );
748 // INT -> INTEGER
749 $s = preg_replace( '/\b(tiny|small|medium|big|)int(\s*\(\s*\d+\s*\)|\b)/i', 'INTEGER', $s );
750 // floating point types -> REAL
751 $s = preg_replace( '/\b(float|double(\s+precision)?)(\s*\(\s*\d+\s*(,\s*\d+\s*)?\)|\b)/i', 'REAL', $s );
752 // varchar -> TEXT
753 $s = preg_replace( '/\b(var)?char\s*\(.*?\)/i', 'TEXT', $s );
754 // TEXT normalization
755 $s = preg_replace( '/\b(tiny|medium|long)text\b/i', 'TEXT', $s );
756 // BLOB normalization
757 $s = preg_replace( '/\b(tiny|small|medium|long|)blob\b/i', 'BLOB', $s );
758 // BOOL -> INTEGER
759 $s = preg_replace( '/\bbool(ean)?\b/i', 'INTEGER', $s );
760 // DATETIME -> TEXT
761 $s = preg_replace( '/\b(datetime|timestamp)\b/i', 'TEXT', $s );
762 // No ENUM type
763 $s = preg_replace( '/\benum\s*\([^)]*\)/i', 'TEXT', $s );
764 // binary collation type -> nothing
765 $s = preg_replace( '/\bbinary\b/i', '', $s );
766 // auto_increment -> autoincrement
767 $s = preg_replace( '/\bauto_increment\b/i', 'AUTOINCREMENT', $s );
768 // No explicit options
769 $s = preg_replace( '/\)[^);]*(;?)\s*$/', ')\1', $s );
770 // AUTOINCREMENT should immedidately follow PRIMARY KEY
771 $s = preg_replace( '/primary key (.*?) autoincrement/i', 'PRIMARY KEY AUTOINCREMENT $1', $s );
772 } elseif ( preg_match( '/^\s*CREATE (\s*(?:UNIQUE|FULLTEXT)\s+)?INDEX/i', $s ) ) {
773 // No truncated indexes
774 $s = preg_replace( '/\(\d+\)/', '', $s );
775 // No FULLTEXT
776 $s = preg_replace( '/\bfulltext\b/i', '', $s );
777 }
778 return $s;
779 }
780
781 /**
782 * Build a concatenation list to feed into a SQL query
783 *
784 * @param $stringList array
785 *
786 * @return string
787 */
788 function buildConcat( $stringList ) {
789 return '(' . implode( ') || (', $stringList ) . ')';
790 }
791
792 /**
793 * @throws MWException
794 * @param $oldName
795 * @param $newName
796 * @param $temporary bool
797 * @param $fname string
798 * @return bool|ResultWrapper
799 */
800 function duplicateTableStructure( $oldName, $newName, $temporary = false, $fname = 'DatabaseSqlite::duplicateTableStructure' ) {
801 $res = $this->query( "SELECT sql FROM sqlite_master WHERE tbl_name=" . $this->addQuotes( $oldName ) . " AND type='table'", $fname );
802 $obj = $this->fetchObject( $res );
803 if ( !$obj ) {
804 throw new MWException( "Couldn't retrieve structure for table $oldName" );
805 }
806 $sql = $obj->sql;
807 $sql = preg_replace( '/(?<=\W)"?' . preg_quote( trim( $this->addIdentifierQuotes( $oldName ), '"' ) ) . '"?(?=\W)/', $this->addIdentifierQuotes( $newName ), $sql, 1 );
808 if ( $temporary ) {
809 if ( preg_match( '/^\\s*CREATE\\s+VIRTUAL\\s+TABLE\b/i', $sql ) ) {
810 wfDebug( "Table $oldName is virtual, can't create a temporary duplicate.\n" );
811 } else {
812 $sql = str_replace( 'CREATE TABLE', 'CREATE TEMPORARY TABLE', $sql );
813 }
814 }
815 return $this->query( $sql, $fname );
816 }
817
818
819 /**
820 * List all tables on the database
821 *
822 * @param $prefix string Only show tables with this prefix, e.g. mw_
823 * @param $fname String: calling function name
824 *
825 * @return array
826 */
827 function listTables( $prefix = null, $fname = 'DatabaseSqlite::listTables' ) {
828 $result = $this->select(
829 'sqlite_master',
830 'name',
831 "type='table'"
832 );
833
834 $endArray = array();
835
836 foreach( $result as $table ) {
837 $vars = get_object_vars($table);
838 $table = array_pop( $vars );
839
840 if( !$prefix || strpos( $table, $prefix ) === 0 ) {
841 if ( strpos( $table, 'sqlite_' ) !== 0 ) {
842 $endArray[] = $table;
843 }
844
845 }
846 }
847
848 return $endArray;
849 }
850
851 } // end DatabaseSqlite class
852
853 /**
854 * This class allows simple acccess to a SQLite database independently from main database settings
855 * @ingroup Database
856 */
857 class DatabaseSqliteStandalone extends DatabaseSqlite {
858 public function __construct( $fileName, $flags = 0 ) {
859 $this->mFlags = $flags;
860 $this->tablePrefix( null );
861 $this->openFile( $fileName );
862 }
863 }
864
865 /**
866 * @ingroup Database
867 */
868 class SQLiteField implements Field {
869 private $info, $tableName;
870 function __construct( $info, $tableName ) {
871 $this->info = $info;
872 $this->tableName = $tableName;
873 }
874
875 function name() {
876 return $this->info->name;
877 }
878
879 function tableName() {
880 return $this->tableName;
881 }
882
883 function defaultValue() {
884 if ( is_string( $this->info->dflt_value ) ) {
885 // Typically quoted
886 if ( preg_match( '/^\'(.*)\'$', $this->info->dflt_value ) ) {
887 return str_replace( "''", "'", $this->info->dflt_value );
888 }
889 }
890 return $this->info->dflt_value;
891 }
892
893 /**
894 * @return bool
895 */
896 function isNullable() {
897 return !$this->info->notnull;
898 }
899
900 function type() {
901 return $this->info->type;
902 }
903
904 } // end SQLiteField