diff --git a/src/RegExpPattern.php b/src/RegExpPattern.php index 89dac48..6d82891 100644 --- a/src/RegExpPattern.php +++ b/src/RegExpPattern.php @@ -34,19 +34,19 @@ public static function tables() $pattern = '/(?CREATE\s+TABLE\s+(?IF NOT EXISTS)?\s*`(?\S+)`\s+'; $pattern .= '\((?[^\/]+)\)'; $pattern .= '('; - $pattern .= '(?:\s+ENGINE=(?[^;\s]+))?\s*'; + $pattern .= '(?:\s+ENGINE\s*=\s*(?[^;\s]+))?\s*'; $pattern .= '|'; - $pattern .= '(?:AUTO_INCREMENT=(?\d+))?\s*'; + $pattern .= '(?:AUTO_INCREMENT\s*=\s*(?\d+))?\s*'; $pattern .= '|'; - $pattern .= '(?:DEFAULT CHARSET=(?[^;\s]+))?\s*'; + $pattern .= '(?:DEFAULT CHARSET\s*=\s*(?[^;\s]+))?\s*'; $pattern .= '|'; - $pattern .= '(?:COLLATE=.+?)?\s*'; + $pattern .= '(?:COLLATE\s*=\s*.+?)?\s*'; $pattern .= '|'; - $pattern .= '(?:COMMENT=\'(?([^\']|\'\')+)\')?\s*'; + $pattern .= '(?:COMMENT\s*=\s*\'(?([^\']|\'\')+)\')?\s*'; $pattern .= ')*'; $pattern .= ')(?:\/\*.+?\*\/)?\s*'; $pattern .= ';/'; - $pattern .= 's'; // modifier + $pattern .= 'si'; // modifier return $pattern; } @@ -67,6 +67,7 @@ public static function column() $pattern .= '(?:ON UPDATE (?\S+))?\s*'; $pattern .= '(?:COMMENT \'(?([^\']|\'\')+)\')?\s*'; $pattern .= '(?:,|$)/'; + $pattern .= 'i'; // modifier return $pattern; } @@ -76,7 +77,7 @@ public static function column() */ public static function dataType() { - return '/(?[^\(\s]+)\s*(?:\([^\)]+\))?\s*(?unsigned)?/'; + return '/(?[^\(\s]+)\s*(?:\([^\)]+\))?\s*(?unsigned)?/i'; } /** @@ -84,7 +85,7 @@ public static function dataType() */ public static function primaryKey() { - return '/PRIMARY KEY \((?(?:`[^`]+`\s*(?:\(\d+\))?,?)+)\)/'; + return '/PRIMARY KEY \((?(?:`[^`]+`\s*(?:\(\d+\))?,?)+)\)/i'; } /** @@ -99,6 +100,7 @@ public static function foreignKey() $pattern .= '(?ON DELETE .+?)?\s*'; $pattern .= '(?ON UPDATE .+?)?\s*'; $pattern .= '(?:,|$)/'; + $pattern .= 'i'; // modifier return $pattern; } @@ -116,6 +118,7 @@ public static function index() $pattern .= '\((?(?:`[^`]+`(?:\(\d+\))?,?)+)\)\s*'; $pattern .= '(?[^,]+?)?\s*'; $pattern .= '(?:,|$)/'; + $pattern .= 'i'; // modifier return $pattern; } @@ -127,6 +130,7 @@ public static function indexColumn() { $pattern = '/^(?[^\(]+)\s*'; $pattern .= '(?:\((?\d+)\))?$/'; + $pattern .= 'i'; // modifier return $pattern; } diff --git a/tests/ParserTest.php b/tests/ParserTest.php index 35ea9bc..e0c3a96 100644 --- a/tests/ParserTest.php +++ b/tests/ParserTest.php @@ -333,4 +333,22 @@ public function testIsParsingCommentsWithSpecialCharacters() $this->assertEquals('`sha1_hash` varchar(50) NOT NULL DEFAULT \'\' COMMENT \'SHA1 hash of the version\'\'s data column.\'', $database->getTableByName('jos_ucm_history')->getColumnByName('sha1_hash')->generateCreationScript()); $this->assertEquals('0=auto delete; 1=keep', $database->getTableByName('jos_ucm_history')->getColumnByName('keep_forever')->getComment()); } + + public function testIsParsingCaseInsensitiveAndSpaces() + { + $creationScript = $this->getDatabaseFixture('jeff.sql'); + + $parser = new Parser(); + + $database = $parser->parseDatabase($creationScript); + + $this->assertInstanceOf(Database::class, $database); + $this->assertCount(1, $database->getTables()); + $this->assertCount(1, $database->getTableByName('contact')->getColumns()); + $this->assertCount(0, $database->getTableByName('contact')->getPrimaryKeys()); + $this->assertCount(0, $database->getTableByName('contact')->getIndexes()); + $this->assertNull($database->getTableByName('contact')->getDefaultCharset()); + $this->assertEquals('InnoDB', $database->getTableByName('contact')->getEngine()); + $this->assertEquals('`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT', $database->getTableByName('contact')->getColumnByName('id')->generateCreationScript()); + } } diff --git a/tests/fixtures/jeff.sql b/tests/fixtures/jeff.sql new file mode 100644 index 0000000..4a19f53 --- /dev/null +++ b/tests/fixtures/jeff.sql @@ -0,0 +1,3 @@ +CREATE TABLE `contact` ( + `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT + ) ENGINE = InnoDB; \ No newline at end of file