diff --git a/sources/Sql/Ddl/Table/AlterTableCommand.php b/sources/Sql/Ddl/Table/AlterTableCommand.php index f5e95fe2..e34c8b8f 100644 --- a/sources/Sql/Ddl/Table/AlterTableCommand.php +++ b/sources/Sql/Ddl/Table/AlterTableCommand.php @@ -61,6 +61,9 @@ public function __construct( AlterTableOption::checkValue($option); } } + if ($tableOptions === []) { + $tableOptions = null; + } $this->name = $name; $this->actions = is_array($actions) ? new AlterActionsList($actions) : $actions; @@ -131,15 +134,16 @@ public function serialize(Formatter $formatter): string $result .= $this->actions->serialize($formatter); - if ($this->tableOptions !== null && !$this->actions->isEmpty()) { - $result .= ','; - } - - if ($this->tableOptions !== null && !$this->tableOptions->isEmpty()) { + if ($this->tableOptions !== null) { + if (!$this->actions->isEmpty()) { + $result .= ','; + } $result .= "\n" . $formatter->indent . $this->tableOptions->serialize($formatter, ",\n", ' '); } - $result = rtrim($result, ','); + if (($this->tableOptions !== null || !$this->actions->isEmpty()) && $this->alterOptions !== []) { + $result .= ','; + } foreach ($this->alterOptions as $option => $value) { if ($option === AlterTableOption::ONLINE) { @@ -154,6 +158,8 @@ public function serialize(Formatter $formatter): string } } + $result = rtrim($result, ','); + if ($this->partitioning !== null) { $result .= "\n" . $this->partitioning->serialize($formatter); } diff --git a/tests/Parser/Ddl/TableCommandsParser.other.phpt b/tests/Parser/Ddl/TableCommandsParser.other.phpt index 6c9224a8..65137ca0 100644 --- a/tests/Parser/Ddl/TableCommandsParser.other.phpt +++ b/tests/Parser/Ddl/TableCommandsParser.other.phpt @@ -7,6 +7,16 @@ use SqlFtw\Tests\Assert; require __DIR__ . '/../../bootstrap.php'; +// combination of actions, table options and alter options +Assert::parseSerialize("ALTER TABLE tbl1 ADD COLUMN col1 INT"); +Assert::parseSerialize("ALTER TABLE tbl1 ADD COLUMN col1 INT, ALGORITHM INSTANT"); +Assert::parseSerialize("ALTER TABLE tbl1 ADD COLUMN col1 INT, ENGINE InnoDB"); +Assert::parseSerialize("ALTER TABLE tbl1 ADD COLUMN col1 INT, ENGINE InnoDB, ALGORITHM INSTANT"); +Assert::parseSerialize("ALTER TABLE tbl1 ENGINE InnoDB"); +Assert::parseSerialize("ALTER TABLE tbl1 ENGINE InnoDB, ALGORITHM INSTANT"); +Assert::parseSerialize("ALTER TABLE tbl1 ALGORITHM INSTANT"); + + // DROP [TEMPORARY] TABLE [IF EXISTS] tbl_name [, tbl_name] ... [RESTRICT | CASCADE] Assert::parseSerialize("DROP TABLE tbl1"); Assert::parseSerialize("DROP TEMPORARY TABLE tbl1");