4 * This class contains code common to different SQLite-related maintenance scripts
9 * Checks whether PHP has SQLite support
12 public static function isPresent() {
14 $compiled = wfDl( 'pdo_sqlite' );
20 * Checks given files for correctness of SQL syntax. MySQL DDL will be converted to
21 * SQLite-compatible during processing.
22 * Will throw exceptions on SQL errors
23 * @return mixed true if no error or error string in case of errors
25 public static function checkSqlSyntax( $files ) {
26 if ( !Sqlite::isPresent() ) {
27 throw new MWException( "Can't check SQL syntax: SQLite not found" );
29 if ( !is_array( $files ) ) {
30 $files = array( $files );
33 $allowedTypes = array_flip( array(
37 'blob', // NULL type is omitted intentionally
40 $db = new DatabaseSqliteStandalone( ':memory:' );
42 foreach ( $files as $file ) {
43 $err = $db->sourceFile( $file );
49 $tables = $db->query( "SELECT name FROM sqlite_master WHERE type='table'", __METHOD__ );
50 foreach ( $tables as $table ) {
51 if ( strpos( $table->name, 'sqlite_' ) === 0 ) continue;
53 $columns = $db->query( "PRAGMA table_info({$table->name})", __METHOD__ );
54 foreach ( $columns as $col ) {
55 if ( !isset( $allowedTypes[strtolower( $col->type )] ) ) {
57 return "Table {$table->name} has column {$col->name} with non-native type '{$col->type}'";
61 } catch ( DBError $e ) {
62 return $e->getMessage();