Skip to content

Commit 1c212be

Browse files
committed
ColumnDefinition: fixed order of CHARACTER SET & COLLATE for MySQL
1 parent 7710af1 commit 1c212be

2 files changed

Lines changed: 49 additions & 4 deletions

File tree

src/Statements/ColumnDefinition.php

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace CzProject\SqlGenerator\Statements;
44

55
use CzProject\SqlGenerator\DuplicateException;
6+
use CzProject\SqlGenerator\Drivers;
67
use CzProject\SqlGenerator\Helpers;
78
use CzProject\SqlGenerator\IDriver;
89
use CzProject\SqlGenerator\IStatement;
@@ -109,14 +110,27 @@ public function toSql(IDriver $driver)
109110
$output .= '(' . implode(', ', $parameters) . ')';
110111
}
111112

112-
foreach ($this->options as $option => $value) {
113-
$output .= ' ' . $option;
113+
$options = $this->options;
114+
$specialOptions = array();
114115

115-
if ($value !== NULL) {
116-
$output .= ' ' . $value;
116+
if ($driver instanceof Drivers\MysqlDriver) {
117+
$specialOptions = array(
118+
'CHARACTER SET',
119+
'COLLATE',
120+
);
121+
}
122+
123+
foreach ($specialOptions as $option) {
124+
if (isset($options[$option])) {
125+
$output .= ' ' . self::formatOption($option, $options[$option]);
126+
unset($options[$option]);
117127
}
118128
}
119129

130+
foreach ($options as $option => $value) {
131+
$output .= ' ' . self::formatOption($option, $value);
132+
}
133+
120134
$output .= ' ' . ($this->nullable ? 'NULL' : 'NOT NULL');
121135

122136
if (isset($this->defaultValue)) {
@@ -133,4 +147,15 @@ public function toSql(IDriver $driver)
133147

134148
return $output;
135149
}
150+
151+
152+
/**
153+
* @param string
154+
* @param string|NULL
155+
* @return string
156+
*/
157+
private static function formatOption($name, $value)
158+
{
159+
return $name . ($value !== NULL ? (' ' . $value) : '');
160+
}
136161
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
use CzProject\SqlGenerator\Drivers;
4+
use CzProject\SqlGenerator\Statements\ColumnDefinition;
5+
use Tester\Assert;
6+
7+
require __DIR__ . '/../bootstrap.php';
8+
require __DIR__ . '/../libs/DummyDriver.php';
9+
10+
11+
test(function () {
12+
13+
$column = new ColumnDefinition('name', 'VARCHAR', array(100), array(
14+
'OPTION' => 'option_value',
15+
'COLLATE' => 'latin2_general_ci',
16+
'CHARACTER SET' => 'latin2',
17+
));
18+
Assert::same('`name` VARCHAR(100) CHARACTER SET latin2 COLLATE latin2_general_ci OPTION option_value NOT NULL', $column->toSql(new Drivers\MysqlDriver));
19+
20+
});

0 commit comments

Comments
 (0)