Skip to content

Commit

Permalink
Merge pull request #20 from camcima/hotfix/backslash-strings
Browse files Browse the repository at this point in the history
Fixed issue when there were backslashes in strings.
  • Loading branch information
camcima authored Nov 2, 2017
2 parents 854f33e + c52b40d commit 6e1d219
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 6 deletions.
42 changes: 38 additions & 4 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function parseDatabase($sqlScript)
{
$database = new Database();

$tables = $this->parseTables($sqlScript);
$tables = $this->parseTables($this->convertStringsToBase64($sqlScript));

foreach ($tables as $table) {
$this->parseTableDefinition($table);
Expand All @@ -48,12 +48,12 @@ public function parseTables($sqlScript)
for ($i = 0; $i < $loopCounter; $i++) {
$name = $matches['tableName'][$i];
$ifNotExists = $matches['ifNotExists'][$i];
$definition = $matches['tableDefinition'][$i];
$creationScript = $matches['creationScript'][$i];
$definition = $this->convertStringsFromBase64($matches['tableDefinition'][$i]);
$creationScript = $this->convertStringsFromBase64($matches['creationScript'][$i]);
$engine = $matches['engine'][$i];
$autoIncrement = $matches['autoIncrement'][$i];
$defaultCharset = $matches['defaultCharset'][$i];
$comment = $matches['comment'][$i];
$comment = base64_decode($matches['comment'][$i]);
$rowFormat = $matches['rowFormat'][$i];
$keyBlockSize = $matches['keyBlockSize'][$i];

Expand Down Expand Up @@ -355,4 +355,38 @@ private function getColumnPrecision($decimalPrecision, $doublePrecision, $floatP

return;
}

public function convertStringsToBase64($sqlScript)
{
$sqlScript = preg_replace_callback('/DEFAULT\s*\'(?<defaultValue>[^\']+)\'/', function ($matches) {
return sprintf('DEFAULT \'%s\'', base64_encode($matches['defaultValue']));
}, $sqlScript);

$sqlScript = preg_replace_callback('/COMMENT\s*\'(?<comment>[^\']+)\'/', function ($matches) {
return sprintf('COMMENT \'%s\'', base64_encode($matches['comment']));
}, $sqlScript);

$sqlScript = preg_replace_callback('/COMMENT\s*=\s*\'(?<comment>([^\']|\'\')+)\'/', function ($matches) {
return sprintf('COMMENT=\'%s\'', base64_encode($matches['comment']));
}, $sqlScript);

return $sqlScript;
}

public function convertStringsFromBase64($sqlScript)
{
$sqlScript = preg_replace_callback('/DEFAULT\s*\'(?<defaultValue>[^\']+)\'/', function ($matches) {
return sprintf('DEFAULT \'%s\'', base64_decode($matches['defaultValue']));
}, $sqlScript);

$sqlScript = preg_replace_callback('/COMMENT\s*\'(?<comment>[^\']+)\'/', function ($matches) {
return sprintf('COMMENT \'%s\'', base64_decode($matches['comment']));
}, $sqlScript);

$sqlScript = preg_replace_callback('/COMMENT\s*=\s*\'(?<comment>([^\']|\'\')+)\'/', function ($matches) {
return sprintf('COMMENT=\'%s\'', base64_decode($matches['comment']));
}, $sqlScript);

return $sqlScript;
}
}
19 changes: 19 additions & 0 deletions tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -359,4 +359,23 @@ public function testIsParsingFractionalSeconds()
$this->assertEquals('`timestamp_column` TIMESTAMP(2) NOT NULL', $database->getTableByName('fractional_seconds')->getColumnByName('timestamp_column')->generateCreationScript());
$this->assertEquals('`time_column` TIME(3) NOT NULL', $database->getTableByName('fractional_seconds')->getColumnByName('time_column')->generateCreationScript());
}

public function testIsParsingColumnWithBackslashInDefaultValue()
{
$creationScript = $this->getDatabaseFixture('backslash.sql');

$parser = new Parser();

$database = $parser->parseDatabase($creationScript);

$this->assertInstanceOf(Database::class, $database);
$this->assertCount(1, $database->getTables());
$this->assertCount(1, $database->getTableByName('backslash')->getColumns());
$this->assertCount(0, $database->getTableByName('backslash')->getPrimaryKeys());
$this->assertCount(0, $database->getTableByName('backslash')->getIndexes());
$this->assertEquals('utf8', $database->getTableByName('backslash')->getDefaultCharset());
$this->assertEquals('InnoDB', $database->getTableByName('backslash')->getEngine());
$this->assertEquals('Table/Comment', $database->getTableByName('backslash')->getComment());
$this->assertEquals('`time_zone` varchar(255) NOT NULL DEFAULT \'America/Los_Angeles\' COMMENT \'Column/Comment\'', $database->getTableByName('backslash')->getColumnByName('time_zone')->generateCreationScript());
}
}
3 changes: 3 additions & 0 deletions tests/fixtures/backslash.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CREATE TABLE IF NOT EXISTS `backslash` (
`time_zone` varchar(255) NOT NULL DEFAULT 'America/Los_Angeles' COMMENT 'Column/Comment'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT = 'Table/Comment';
2 changes: 1 addition & 1 deletion tests/fixtures/comment_change1.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
CREATE TABLE `comments` (
`field1` varchar(50) NOT NULL COMMENT 'Old Comment',
`field1` varchar(50) NOT NULL COMMENT 'Old Comment'
) ENGINE=InnoDB;
2 changes: 1 addition & 1 deletion tests/fixtures/comment_change2.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
CREATE TABLE `comments` (
`field1` varchar(50) NOT NULL COMMENT 'New Comment',
`field1` varchar(50) NOT NULL COMMENT 'New Comment'
) ENGINE=InnoDB;

0 comments on commit 6e1d219

Please sign in to comment.