From c0a27ae1a3f0ba93d634ef81fbedd76281ecb454 Mon Sep 17 00:00:00 2001 From: Collin Lefeber Date: Fri, 3 Feb 2023 16:20:44 -0500 Subject: [PATCH] Fix field support for CHARACTER SET & COLLATE --- src/Processor/CreateProcessor.php | 4 ++-- src/Schema/Column/CharacterColumn.php | 5 ++--- tests/CreateTableParseTest.php | 24 ++++++++++++++++-------- tests/fixtures/create_table.sql | 9 ++++++++- 4 files changed, 28 insertions(+), 14 deletions(-) diff --git a/src/Processor/CreateProcessor.php b/src/Processor/CreateProcessor.php index cb1acf80..2a823d60 100644 --- a/src/Processor/CreateProcessor.php +++ b/src/Processor/CreateProcessor.php @@ -277,8 +277,8 @@ private static function getIntegerDefinitionColumn(Query\MysqlColumnType $stmt) */ private static function getTextDefinitionColumn(Query\MysqlColumnType $stmt) { - $collation = null; - $character_set = null; + $collation = $stmt->collation; + $character_set = $stmt->character_set; switch (strtoupper($stmt->type)) { case DataType::TEXT: diff --git a/src/Schema/Column/CharacterColumn.php b/src/Schema/Column/CharacterColumn.php index 7dd7bf44..6a5d2df9 100644 --- a/src/Schema/Column/CharacterColumn.php +++ b/src/Schema/Column/CharacterColumn.php @@ -69,9 +69,8 @@ public function getPhpCode() : string return '(new \\' . static::class . '(' . $this->max_string_length - . ($this->character_set !== null && $this->collation !== null - ? ', \'' . $this->character_set . '\'' . ', \'' . $this->collation . '\'' - : '') + . ($this->character_set !== null ? ', \'' . $this->character_set . '\'' : '') + . ($this->collation !== null ? ', \'' . $this->collation . '\'' : '') . '))' . $default . $this->getNullablePhp(); diff --git a/tests/CreateTableParseTest.php b/tests/CreateTableParseTest.php index 35bc0712..c7710445 100644 --- a/tests/CreateTableParseTest.php +++ b/tests/CreateTableParseTest.php @@ -1,10 +1,9 @@ parse($query); + $create_queries = (new CreateTableParser)->parse($query); - $this->assertCount(5, $create_queries); + $this->assertNotEmpty($create_queries); + + $table_defs = []; foreach ($create_queries as $create_query) { - $table = \Vimeo\MysqlEngine\Processor\CreateProcessor::makeTableDefinition( + $table = CreateProcessor::makeTableDefinition( $create_query, 'foo' ); + $table_defs[$table->name] = $table; $new_table_php_code = $table->getPhpCode(); $new_table = eval('return ' . $new_table_php_code . ';'); - $this->assertSame(\var_export($table, true), \var_export($new_table, true)); + $this->assertSame(\var_export($table, true), \var_export($new_table, true), + "The table definition for {$table->name} did not match the generated php version."); } + + // specific parsing checks + $this->assertInstanceOf(TableDefinition::class, $table_defs['tweets']); + $this->assertEquals('utf8mb4', $table_defs['tweets']->columns['text']->getCharacterSet()); + $this->assertEquals('utf8mb4_unicode_ci', $table_defs['tweets']->columns['text']->getCollation()); } } diff --git a/tests/fixtures/create_table.sql b/tests/fixtures/create_table.sql index f207482c..185c103f 100644 --- a/tests/fixtures/create_table.sql +++ b/tests/fixtures/create_table.sql @@ -60,4 +60,11 @@ CREATE TABLE `orders` `created_on` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `modified_on` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`id`) -)ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci; \ No newline at end of file +)ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci; + +CREATE TABLE `tweets` ( + `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, + `text` varchar(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci, + PRIMARY KEY (`id`) +) +ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;