Skip to content

Commit

Permalink
Make parser case insensitive.
Browse files Browse the repository at this point in the history
  • Loading branch information
Carlos Cima committed Jun 29, 2016
1 parent f50c412 commit 2ade504
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/RegExpPattern.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,19 @@ public static function tables()
$pattern = '/(?<creationScript>CREATE\s+TABLE\s+(?<ifNotExists>IF NOT EXISTS)?\s*`(?<tableName>\S+)`\s+';
$pattern .= '\((?<tableDefinition>[^\/]+)\)';
$pattern .= '(';
$pattern .= '(?:\s+ENGINE=(?<engine>[^;\s]+))?\s*';
$pattern .= '(?:\s+ENGINE\s*=\s*(?<engine>[^;\s]+))?\s*';
$pattern .= '|';
$pattern .= '(?:AUTO_INCREMENT=(?<autoIncrement>\d+))?\s*';
$pattern .= '(?:AUTO_INCREMENT\s*=\s*(?<autoIncrement>\d+))?\s*';
$pattern .= '|';
$pattern .= '(?:DEFAULT CHARSET=(?<defaultCharset>[^;\s]+))?\s*';
$pattern .= '(?:DEFAULT CHARSET\s*=\s*(?<defaultCharset>[^;\s]+))?\s*';
$pattern .= '|';
$pattern .= '(?:COLLATE=.+?)?\s*';
$pattern .= '(?:COLLATE\s*=\s*.+?)?\s*';
$pattern .= '|';
$pattern .= '(?:COMMENT=\'(?<comment>([^\']|\'\')+)\')?\s*';
$pattern .= '(?:COMMENT\s*=\s*\'(?<comment>([^\']|\'\')+)\')?\s*';
$pattern .= ')*';
$pattern .= ')(?:\/\*.+?\*\/)?\s*';
$pattern .= ';/';
$pattern .= 's'; // modifier
$pattern .= 'si'; // modifier

return $pattern;
}
Expand All @@ -67,6 +67,7 @@ public static function column()
$pattern .= '(?:ON UPDATE (?<onUpdateValue>\S+))?\s*';
$pattern .= '(?:COMMENT \'(?<comment>([^\']|\'\')+)\')?\s*';
$pattern .= '(?:,|$)/';
$pattern .= 'i'; // modifier

return $pattern;
}
Expand All @@ -76,15 +77,15 @@ public static function column()
*/
public static function dataType()
{
return '/(?<dataType>[^\(\s]+)\s*(?:\([^\)]+\))?\s*(?<unsigned>unsigned)?/';
return '/(?<dataType>[^\(\s]+)\s*(?:\([^\)]+\))?\s*(?<unsigned>unsigned)?/i';
}

/**
* @return string
*/
public static function primaryKey()
{
return '/PRIMARY KEY \((?<primaryKey>(?:`[^`]+`\s*(?:\(\d+\))?,?)+)\)/';
return '/PRIMARY KEY \((?<primaryKey>(?:`[^`]+`\s*(?:\(\d+\))?,?)+)\)/i';
}

/**
Expand All @@ -99,6 +100,7 @@ public static function foreignKey()
$pattern .= '(?<onDelete>ON DELETE .+?)?\s*';
$pattern .= '(?<onUpdate>ON UPDATE .+?)?\s*';
$pattern .= '(?:,|$)/';
$pattern .= 'i'; // modifier

return $pattern;
}
Expand All @@ -116,6 +118,7 @@ public static function index()
$pattern .= '\((?<columns>(?:`[^`]+`(?:\(\d+\))?,?)+)\)\s*';
$pattern .= '(?<options>[^,]+?)?\s*';
$pattern .= '(?:,|$)/';
$pattern .= 'i'; // modifier

return $pattern;
}
Expand All @@ -127,6 +130,7 @@ public static function indexColumn()
{
$pattern = '/^(?<columnName>[^\(]+)\s*';
$pattern .= '(?:\((?<firstCharacters>\d+)\))?$/';
$pattern .= 'i'; // modifier

return $pattern;
}
Expand Down
18 changes: 18 additions & 0 deletions tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
3 changes: 3 additions & 0 deletions tests/fixtures/jeff.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CREATE TABLE `contact` (
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT
) ENGINE = InnoDB;

0 comments on commit 2ade504

Please sign in to comment.