Skip to content

Commit

Permalink
Fix issue with empty default value and apostrophes in comments (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
bennett-treptow authored Mar 17, 2024
1 parent b334164 commit eff9af0
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
10 changes: 6 additions & 4 deletions src/Tokenizers/BaseTokenizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,18 @@ abstract class BaseTokenizer

private const SPACE_REPLACER = '&!@';
private const SINGLE_QUOTE_REPLACER = '!*@';
private const EMPTY_STRING_REPLACER = '$$EMPTY_STRING';

public function __construct(string $value)
{
$this->value = $value;
$prune = false;
$pruneSingleQuotes = false;

if (preg_match("/(DEFAULT|COMMENT) ''/", $value, $matches)) {
$value = str_replace($matches[1] . ' \'\'', $matches[1] . ' ' . self::EMPTY_STRING_REPLACER, $value);
}

//first get rid of any single quoted stuff with '' around it
if (preg_match_all('/\'\'(.+?)\'\'/', $value, $matches)) {
foreach ($matches[0] as $key => $singleQuoted) {
Expand All @@ -25,9 +30,6 @@ public function __construct(string $value)
$pruneSingleQuotes = true;
}
}
if (preg_match('/\'\'/', $value)) {
$value = str_replace('\'\'', '$$EMPTY_STRING', $value);
}

if (preg_match_all("/'(.+?)'/", $value, $matches)) {
foreach ($matches[0] as $quoteWithSpace) {
Expand All @@ -38,7 +40,7 @@ public function __construct(string $value)
$prune = true;
}
}
$value = str_replace('$$EMPTY_STRING', '\'\'', $value);
$value = str_replace(self::EMPTY_STRING_REPLACER, '\'\'', $value);
$this->tokens = array_map(function ($item) {
return trim($item, ', ');
}, str_getcsv($value, ' ', "'"));
Expand Down
1 change: 0 additions & 1 deletion tests/Unit/GeneratorManagers/MySQLGeneratorManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ public function test_can_remove_database_prefix()
$mocked->addTableDefinition($definition);
$this->assertEquals('posts', $definition->getTableName());


config()->set('database.connections.' . $connection . '.prefix', '');

$definition = (new TableDefinition())->setTableName('wp_posts');
Expand Down
8 changes: 8 additions & 0 deletions tests/Unit/Tokenizers/MySQL/ColumnTokenizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,14 @@ public function test_it_tokenizes_varchar_with_default_empty_string_and_comment(
$this->assertEquals('this is the "comment"', $columnDefinition->getComment());
}

public function test_it_tokenizes_varchar_with_default_empty_string_and_comment_with_apostrophe()
{
$columnTokenizer = ColumnTokenizer::parse("`testing` varchar(255) DEFAULT '' COMMENT 'this is the \"comment\" ''inside single quote''");
$columnDefinition = $columnTokenizer->definition();
$this->assertEquals('', $columnDefinition->getDefaultValue());
$this->assertEquals('this is the "comment" \'inside single quote\'', $columnDefinition->getComment());
}

public function test_it_tokenizes_varchar_with_boolean_literal_default()
{
$columnTokenizer = ColumnTokenizer::parse("`testing` bit(2) DEFAULT b'10'");
Expand Down

0 comments on commit eff9af0

Please sign in to comment.