// CREATE TABLE hacks to allow schema file sharing with MySQL
// binary/varbinary column type -> blob
- $s = preg_replace( '/\b(var)?binary(\(\d+\))/i', 'blob\1', $s );
+ $s = preg_replace( '/\b(var)?binary(\(\d+\))/i', 'BLOB', $s );
// no such thing as unsigned
- $s = preg_replace( '/\bunsigned\b/i', '', $s );
- // INT -> INTEGER for primary keys
- $s = preg_replacE( '/\bint\b/i', 'integer', $s );
+ $s = preg_replace( '/\b(un)?signed\b/i', '', $s );
+ // INT -> INTEGER
+ $s = preg_replace( '/\b(tiny|small|medium|big|)int\b/i', 'INTEGER', $s );
+ // varchar -> TEXT
+ $s = preg_replace( '/\bvarchar\(\d+\)/i', 'TEXT', $s );
+ // TEXT normalization
+ $s = preg_replace( '/\b(tiny|medium|long)text\b/i', 'TEXT', $s );
+ // BLOB normalization
+ $s = preg_replace( '/\b(tiny|small|medium|long|)blob\b/i', 'BLOB', $s );
+ // BOOL -> INTEGER
+ $s = preg_replace( '/\bbool(ean)?\b/i', 'INTEGER', $s );
+ // DATETIME -> TEXT
+ $s = preg_replace( '/\b(datetime|timestamp)\b/i', 'TEXT', $s );
// No ENUM type
- $s = preg_replace( '/enum\([^)]*\)/i', 'blob', $s );
+ $s = preg_replace( '/enum\([^)]*\)/i', 'BLOB', $s );
// binary collation type -> nothing
$s = preg_replace( '/\bbinary\b/i', '', $s );
// auto_increment -> autoincrement
- $s = preg_replace( '/\bauto_increment\b/i', 'autoincrement', $s );
+ $s = preg_replace( '/\bauto_increment\b/i', 'AUTOINCREMENT', $s );
// No explicit options
- $s = preg_replace( '/\)[^)]*$/', ')', $s );
+ $s = preg_replace( '/\)[^);]*(;?)\s*$/', ')\1', $s );
} elseif ( preg_match( '/^\s*CREATE (\s*(?:UNIQUE|FULLTEXT)\s+)?INDEX/i', $s ) ) {
// No truncated indexes
$s = preg_replace( '/\(\d+\)/', '', $s );
function duplicateTableStructure( $oldName, $newName, $temporary = false, $fname = 'DatabaseSqlite::duplicateTableStructure' ) {
$res = $this->query( "SELECT sql FROM sqlite_master WHERE tbl_name='$oldName' AND type='table'", $fname );
- $sql = $this->fetchObject( $res )->sql;
+ $obj = $this->fetchObject( $res );
+ if ( !$obj ) {
+ throw new MWException( "Couldn't retrieve structure for table $oldName" );
+ }
+ $sql = $obj->sql;
$sql = preg_replace( '/\b' . preg_quote( $oldName ) . '\b/', $newName, $sql, 1 );
return $this->query( $sql, $fname );
}
--- /dev/null
+<?php
+
+class MockDatabaseSqlite extends DatabaseSqliteStandalone {
+ var $lastQuery;
+
+ function __construct( ) {
+ parent::__construct( '' );
+ }
+
+ function query( $sql, $fname = '', $tempIgnore = false ) {
+ $this->lastQuery = $sql;
+ return true;
+ }
+
+ function replaceVars( $s ) {
+ return parent::replaceVars( $s );
+ }
+}
+
+class DatabaseSqliteTest extends PHPUnit_Framework_TestCase {
+ var $db;
+
+ function setup() {
+ if ( !extension_loaded( 'pdo_sqlite' ) ) {
+ $this->markTestIncomplete( 'No SQLite support detected' );
+ }
+ $this->db = new MockDatabaseSqlite();
+ }
+
+ function replaceVars( $sql ) {
+ // normalize spacing to hide implementation details
+ return preg_replace( '/\s+/', ' ', $this->db->replaceVars( $sql ) );
+ }
+
+ function testReplaceVars() {
+ $this->assertEquals( 'foo', $this->replaceVars( 'foo' ), "Don't break anything accidentally" );
+
+ $this->assertEquals( "CREATE TABLE /**/foo (foo_key INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "
+ . "foo_name TEXT NOT NULL DEFAULT '');",
+ $this->replaceVars( "CREATE TABLE /**/foo (foo_key int unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT,
+ foo_name varchar(255) binary NOT NULL DEFAULT '') ENGINE=MyISAM;" )
+ );
+
+ $this->assertEquals( "CREATE TABLE foo ( foo_binary1 BLOB, foo_binary2 BLOB );",
+ $this->replaceVars( "CREATE TABLE foo ( foo_binary1 binary(16), foo_binary2 varbinary(32) );" )
+ );
+
+ $this->assertEquals( "CREATE TABLE text ( text_foo TEXT );",
+ $this->replaceVars( "CREATE TABLE text ( text_foo tinytext );" ),
+ 'Table name changed'
+ );
+ }
+}
\ No newline at end of file