MediaWiki  REL1_24
sqlite.inc
Go to the documentation of this file.
00001 <?php
00029 class Sqlite {
00030 
00035     public static function isPresent() {
00036         return extension_loaded( 'pdo_sqlite' );
00037     }
00038 
00047     public static function checkSqlSyntax( $files ) {
00048         if ( !Sqlite::isPresent() ) {
00049             throw new MWException( "Can't check SQL syntax: SQLite not found" );
00050         }
00051         if ( !is_array( $files ) ) {
00052             $files = array( $files );
00053         }
00054 
00055         $allowedTypes = array_flip( array(
00056             'integer',
00057             'real',
00058             'text',
00059             'blob', // NULL type is omitted intentionally
00060         ) );
00061 
00062         $db = new DatabaseSqliteStandalone( ':memory:' );
00063         try {
00064             foreach ( $files as $file ) {
00065                 $err = $db->sourceFile( $file );
00066                 if ( $err != true ) {
00067                     return $err;
00068                 }
00069             }
00070 
00071             $tables = $db->query( "SELECT name FROM sqlite_master WHERE type='table'", __METHOD__ );
00072             foreach ( $tables as $table ) {
00073                 if ( strpos( $table->name, 'sqlite_' ) === 0 ) {
00074                     continue;
00075                 }
00076 
00077                 $columns = $db->query( "PRAGMA table_info({$table->name})", __METHOD__ );
00078                 foreach ( $columns as $col ) {
00079                     if ( !isset( $allowedTypes[strtolower( $col->type )] ) ) {
00080                         $db->close();
00081 
00082                         return "Table {$table->name} has column {$col->name} with non-native type '{$col->type}'";
00083                     }
00084                 }
00085             }
00086         } catch ( DBError $e ) {
00087             return $e->getMessage();
00088         }
00089         $db->close();
00090 
00091         return true;
00092     }
00093 }