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