sqlite.inc 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * Helper class for sqlite-specific scripts
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. * @ingroup Maintenance
  22. */
  23. /**
  24. * This class contains code common to different SQLite-related maintenance scripts
  25. *
  26. * @ingroup Maintenance
  27. */
  28. class Sqlite {
  29. /**
  30. * Checks whether PHP has SQLite support
  31. * @return bool
  32. */
  33. public static function isPresent() {
  34. return extension_loaded( 'pdo_sqlite' );
  35. }
  36. /**
  37. * Checks given files for correctness of SQL syntax. MySQL DDL will be converted to
  38. * SQLite-compatible during processing.
  39. * Will throw exceptions on SQL errors
  40. * @param array|string $files
  41. * @throws MWException
  42. * @return bool True if no error or error string in case of errors
  43. */
  44. public static function checkSqlSyntax( $files ) {
  45. if ( !Sqlite::isPresent() ) {
  46. throw new MWException( "Can't check SQL syntax: SQLite not found" );
  47. }
  48. if ( !is_array( $files ) ) {
  49. $files = [ $files ];
  50. }
  51. $allowedTypes = array_flip( [
  52. 'integer',
  53. 'real',
  54. 'text',
  55. 'blob', // NULL type is omitted intentionally
  56. ] );
  57. $db = DatabaseSqlite::newStandaloneInstance( ':memory:' );
  58. try {
  59. foreach ( $files as $file ) {
  60. $err = $db->sourceFile( $file );
  61. if ( $err != true ) {
  62. return $err;
  63. }
  64. }
  65. $tables = $db->query( "SELECT name FROM sqlite_master WHERE type='table'", __METHOD__ );
  66. foreach ( $tables as $table ) {
  67. if ( strpos( $table->name, 'sqlite_' ) === 0 ) {
  68. continue;
  69. }
  70. $columns = $db->query( "PRAGMA table_info({$table->name})", __METHOD__ );
  71. foreach ( $columns as $col ) {
  72. if ( !isset( $allowedTypes[strtolower( $col->type )] ) ) {
  73. $db->close();
  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();
  82. return true;
  83. }
  84. }