MediaWiki
REL1_21
|
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 00050 public static function checkSqlSyntax( $files ) { 00051 if ( !Sqlite::isPresent() ) { 00052 throw new MWException( "Can't check SQL syntax: SQLite not found" ); 00053 } 00054 if ( !is_array( $files ) ) { 00055 $files = array( $files ); 00056 } 00057 00058 $allowedTypes = array_flip( array( 00059 'integer', 00060 'real', 00061 'text', 00062 'blob', // NULL type is omitted intentionally 00063 ) ); 00064 00065 $db = new DatabaseSqliteStandalone( ':memory:' ); 00066 try { 00067 foreach ( $files as $file ) { 00068 $err = $db->sourceFile( $file ); 00069 if ( $err != true ) { 00070 return $err; 00071 } 00072 } 00073 00074 $tables = $db->query( "SELECT name FROM sqlite_master WHERE type='table'", __METHOD__ ); 00075 foreach ( $tables as $table ) { 00076 if ( strpos( $table->name, 'sqlite_' ) === 0 ) continue; 00077 00078 $columns = $db->query( "PRAGMA table_info({$table->name})", __METHOD__ ); 00079 foreach ( $columns as $col ) { 00080 if ( !isset( $allowedTypes[strtolower( $col->type )] ) ) { 00081 $db->close(); 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 return true; 00091 } 00092 };