Skip to content

Commit 3c6ec7c

Browse files
authored
Merge pull request #39 from cfebs/fix-charset-collation
Fix field support for CHARACTER SET & COLLATE
2 parents 3425b60 + c0a27ae commit 3c6ec7c

File tree

4 files changed

+28
-14
lines changed

4 files changed

+28
-14
lines changed

src/Processor/CreateProcessor.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,8 @@ private static function getIntegerDefinitionColumn(Query\MysqlColumnType $stmt)
277277
*/
278278
private static function getTextDefinitionColumn(Query\MysqlColumnType $stmt)
279279
{
280-
$collation = null;
281-
$character_set = null;
280+
$collation = $stmt->collation;
281+
$character_set = $stmt->character_set;
282282

283283
switch (strtoupper($stmt->type)) {
284284
case DataType::TEXT:

src/Schema/Column/CharacterColumn.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,8 @@ public function getPhpCode() : string
6969

7070
return '(new \\' . static::class . '('
7171
. $this->max_string_length
72-
. ($this->character_set !== null && $this->collation !== null
73-
? ', \'' . $this->character_set . '\'' . ', \'' . $this->collation . '\''
74-
: '')
72+
. ($this->character_set !== null ? ', \'' . $this->character_set . '\'' : '')
73+
. ($this->collation !== null ? ', \'' . $this->collation . '\'' : '')
7574
. '))'
7675
. $default
7776
. $this->getNullablePhp();

tests/CreateTableParseTest.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,40 @@
11
<?php
22
namespace Vimeo\MysqlEngine\Tests;
33

4-
use Vimeo\MysqlEngine\Query\SelectQuery;
5-
use Vimeo\MysqlEngine\Query\Expression\ColumnExpression;
6-
use Vimeo\MysqlEngine\Query\Expression\CaseOperatorExpression;
7-
use Vimeo\MysqlEngine\Query\Expression\BinaryOperatorExpression;
4+
use Vimeo\MysqlEngine\Parser\CreateTableParser;
5+
use Vimeo\MysqlEngine\Processor\CreateProcessor;
6+
use Vimeo\MysqlEngine\Schema\TableDefinition;
87

98
class CreateTableParseTest extends \PHPUnit\Framework\TestCase
109
{
1110
public function testSimpleParse()
1211
{
1312
$query = file_get_contents(__DIR__ . '/fixtures/create_table.sql');
1413

15-
$create_queries = (new \Vimeo\MysqlEngine\Parser\CreateTableParser)->parse($query);
14+
$create_queries = (new CreateTableParser)->parse($query);
1615

17-
$this->assertCount(5, $create_queries);
16+
$this->assertNotEmpty($create_queries);
17+
18+
$table_defs = [];
1819

1920
foreach ($create_queries as $create_query) {
20-
$table = \Vimeo\MysqlEngine\Processor\CreateProcessor::makeTableDefinition(
21+
$table = CreateProcessor::makeTableDefinition(
2122
$create_query,
2223
'foo'
2324
);
25+
$table_defs[$table->name] = $table;
2426

2527
$new_table_php_code = $table->getPhpCode();
2628

2729
$new_table = eval('return ' . $new_table_php_code . ';');
2830

29-
$this->assertSame(\var_export($table, true), \var_export($new_table, true));
31+
$this->assertSame(\var_export($table, true), \var_export($new_table, true),
32+
"The table definition for {$table->name} did not match the generated php version.");
3033
}
34+
35+
// specific parsing checks
36+
$this->assertInstanceOf(TableDefinition::class, $table_defs['tweets']);
37+
$this->assertEquals('utf8mb4', $table_defs['tweets']->columns['text']->getCharacterSet());
38+
$this->assertEquals('utf8mb4_unicode_ci', $table_defs['tweets']->columns['text']->getCollation());
3139
}
3240
}

tests/fixtures/create_table.sql

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,11 @@ CREATE TABLE `orders`
6060
`created_on` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
6161
`modified_on` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
6262
PRIMARY KEY (`id`)
63-
)ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
63+
)ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
64+
65+
CREATE TABLE `tweets` (
66+
`id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
67+
`text` varchar(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
68+
PRIMARY KEY (`id`)
69+
)
70+
ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

0 commit comments

Comments
 (0)