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