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