MediaWiki master
Sqlite.php
Go to the documentation of this file.
1<?php
12
18class Sqlite {
19
24 public static function isPresent() {
25 return extension_loaded( 'pdo_sqlite' );
26 }
27
35 public static function checkSqlSyntax( $files ) {
36 if ( !self::isPresent() ) {
37 throw new RuntimeException( "Can't check SQL syntax: SQLite not found" );
38 }
39 if ( !is_array( $files ) ) {
40 $files = [ $files ];
41 }
42
43 $allowedTypes = array_fill_keys( [
44 'integer',
45 'real',
46 'text',
47 'blob',
48 // NULL type is omitted intentionally
49 ], true );
50
51 $db = DatabaseSqlite::newStandaloneInstance( ':memory:' );
52 try {
53 foreach ( $files as $file ) {
54 $err = $db->sourceFile( $file );
55 if ( $err ) {
56 return $err;
57 }
58 }
59
60 $tables = $db->query( "SELECT name FROM sqlite_master WHERE type='table'", __METHOD__ );
61 foreach ( $tables as $table ) {
62 if ( str_starts_with( $table->name, 'sqlite_' ) ) {
63 continue;
64 }
65
66 $columns = $db->query(
67 'PRAGMA table_info(' . $db->addIdentifierQuotes( $table->name ) . ')',
68 __METHOD__
69 );
70 foreach ( $columns as $col ) {
71 if ( !isset( $allowedTypes[strtolower( $col->type )] ) ) {
72 $db->close( __METHOD__ );
73
74 return "Table {$table->name} has column {$col->name} with non-native type '{$col->type}'";
75 }
76 }
77 }
78 } catch ( DBError $e ) {
79 return $e->getMessage();
80 }
81 $db->close( __METHOD__ );
82
83 return true;
84 }
85}
This class contains code common to different SQLite-related maintenance scripts.
Definition Sqlite.php:18
static isPresent()
Checks whether PHP has SQLite support.
Definition Sqlite.php:24
static checkSqlSyntax( $files)
Checks given files for correctness of SQL syntax.
Definition Sqlite.php:35
Database error base class.
Definition DBError.php:22
This is the SQLite database abstraction layer.