From 7d5e59bedded80a165affbd53bd31d9053fa9679 Mon Sep 17 00:00:00 2001 From: Jan Pecha Date: Thu, 27 Jul 2023 09:30:55 +0200 Subject: [PATCH] Added Value, supported in table & column options --- readme.md | 30 +++++++++++++++ src/Statements/AddColumn.php | 3 +- src/Statements/AlterTable.php | 11 +++--- src/Statements/ColumnDefinition.php | 17 ++++++--- src/Statements/CreateTable.php | 9 +++-- src/Statements/ModifyColumn.php | 3 +- src/Value.php | 38 +++++++++++++++++++ .../SqlGenerator/SqlDocument.alterTable.phpt | 14 ++++++- .../SqlGenerator/SqlDocument.createTable.phpt | 14 ++++++- 9 files changed, 118 insertions(+), 21 deletions(-) create mode 100644 src/Value.php diff --git a/readme.md b/readme.md index 6eaa47a..c2a726b 100644 --- a/readme.md +++ b/readme.md @@ -104,6 +104,36 @@ $sql->getSqlQueries($driver); // returns string[] $sql->save($file, $driver); // saves SQL into file ``` +## Special Values + +There are value objects for specific cases: + +### TableName + +Delimited table name. + +```php +use CzProject\SqlGenerator\TableName; + +$table = $sql->createTable(TableName::create('schema.table')) +$table->addForeignKey('fk_table_id', 'id', TableName::create('schema2.table2'), 'id'); +// and more ($sql->renameTable(),...) +``` + + +### Value + +Scalar/stringable/datetime value. It can be used in option values. + +```php +use CzProject\SqlGenerator\Value; + +$table->setOption('AUTO_INCREMENT', Value::create(123)); // generates AUTO_INCREMENT=123 +$table->setOption('CHECKSUM', Value::create(FALSE)); // generates CHECKSUM=0 +$table->setOption('COMPRESSION', Value::create('NONE')); // generates COMPRESSION='NONE' +``` + + ## Supported database Currently is supported common SQL and MySQL. diff --git a/src/Statements/AddColumn.php b/src/Statements/AddColumn.php index 21df7b5..5c25159 100644 --- a/src/Statements/AddColumn.php +++ b/src/Statements/AddColumn.php @@ -4,6 +4,7 @@ use CzProject\SqlGenerator\IDriver; use CzProject\SqlGenerator\IStatement; + use CzProject\SqlGenerator\Value; class AddColumn implements IStatement @@ -22,7 +23,7 @@ class AddColumn implements IStatement * @param string $name * @param string $type * @param array $parameters - * @param array $options [name => value] + * @param array $options [name => value] */ public function __construct($name, $type, array $parameters = NULL, array $options = []) { diff --git a/src/Statements/AlterTable.php b/src/Statements/AlterTable.php index 18ca682..43bf0bc 100644 --- a/src/Statements/AlterTable.php +++ b/src/Statements/AlterTable.php @@ -6,6 +6,7 @@ use CzProject\SqlGenerator\IDriver; use CzProject\SqlGenerator\IStatement; use CzProject\SqlGenerator\TableName; + use CzProject\SqlGenerator\Value; class AlterTable implements IStatement @@ -19,7 +20,7 @@ class AlterTable implements IStatement /** @var string|NULL */ private $comment; - /** @var array [name => value] */ + /** @var array [name => value] */ private $options = []; @@ -36,7 +37,7 @@ public function __construct($tableName) * @param string $name * @param string $type * @param array $parameters - * @param array $options [name => value] + * @param array $options [name => value] * @return AddColumn */ public function addColumn($name, $type, array $parameters = NULL, array $options = []) @@ -59,7 +60,7 @@ public function dropColumn($column) * @param string $name * @param string $type * @param array $parameters - * @param array $options [name => value] + * @param array $options [name => value] * @return ModifyColumn */ public function modifyColumn($name, $type, array $parameters = NULL, array $options = []) @@ -125,7 +126,7 @@ public function setComment($comment) /** * @param string $name - * @param string $value + * @param string|Value $value * @return static */ public function setOption($name, $value) @@ -174,7 +175,7 @@ public function toSql(IDriver $driver) $output .= ",\n"; } - $output .= $optionName . '=' . $optionValue; + $output .= $optionName . '=' . ($optionValue instanceof Value ? $optionValue->toString($driver) : $optionValue); } $output .= ';'; diff --git a/src/Statements/ColumnDefinition.php b/src/Statements/ColumnDefinition.php index 5d05eb0..e31e40e 100644 --- a/src/Statements/ColumnDefinition.php +++ b/src/Statements/ColumnDefinition.php @@ -6,6 +6,7 @@ use CzProject\SqlGenerator\Helpers; use CzProject\SqlGenerator\IDriver; use CzProject\SqlGenerator\IStatement; + use CzProject\SqlGenerator\Value; class ColumnDefinition implements IStatement @@ -19,7 +20,7 @@ class ColumnDefinition implements IStatement /** @var array */ private $parameters = []; - /** @var array [name => value] */ + /** @var array [name => value] */ private $options = []; /** @var bool */ @@ -39,7 +40,7 @@ class ColumnDefinition implements IStatement * @param string $name * @param string $type * @param array|NULL $parameters - * @param array $options [name => value] + * @param array $options [name => value] */ public function __construct($name, $type, array $parameters = NULL, array $options = []) { @@ -118,13 +119,13 @@ public function toSql(IDriver $driver) foreach ($specialOptions as $option) { if (isset($options[$option])) { - $output .= ' ' . self::formatOption($option, $options[$option]); + $output .= ' ' . self::formatOption($option, $options[$option], $driver); unset($options[$option]); } } foreach ($options as $option => $value) { - $output .= ' ' . self::formatOption($option, $value); + $output .= ' ' . self::formatOption($option, $value, $driver); } $output .= ' ' . ($this->nullable ? 'NULL' : 'NOT NULL'); @@ -147,11 +148,15 @@ public function toSql(IDriver $driver) /** * @param string $name - * @param string|NULL $value + * @param string|Value|NULL $value * @return string */ - private static function formatOption($name, $value) + private static function formatOption($name, $value, IDriver $driver) { + if ($value instanceof Value) { + $value = $value->toString($driver); + } + return $name . ($value !== NULL ? (' ' . $value) : ''); } } diff --git a/src/Statements/CreateTable.php b/src/Statements/CreateTable.php index a60c0db..246f6b9 100644 --- a/src/Statements/CreateTable.php +++ b/src/Statements/CreateTable.php @@ -7,6 +7,7 @@ use CzProject\SqlGenerator\IDriver; use CzProject\SqlGenerator\IStatement; use CzProject\SqlGenerator\TableName; + use CzProject\SqlGenerator\Value; class CreateTable implements IStatement @@ -26,7 +27,7 @@ class CreateTable implements IStatement /** @var string|NULL */ private $comment; - /** @var array [name => value] */ + /** @var array [name => value] */ private $options = []; @@ -43,7 +44,7 @@ public function __construct($tableName) * @param string $name * @param string $type * @param array|NULL $parameters - * @param array $options + * @param array $options * @return ColumnDefinition */ public function addColumn($name, $type, array $parameters = NULL, array $options = []) @@ -101,7 +102,7 @@ public function setComment($comment) /** * @param string $name - * @param string $value + * @param string|Value $value * @return static */ public function setOption($name, $value) @@ -160,7 +161,7 @@ public function toSql(IDriver $driver) foreach ($this->options as $optionName => $optionValue) { $output .= "\n"; - $output .= $optionName . '=' . $optionValue; + $output .= $optionName . '=' . ($optionValue instanceof Value ? $optionValue->toString($driver) : $optionValue); } $output .= ';'; diff --git a/src/Statements/ModifyColumn.php b/src/Statements/ModifyColumn.php index 079a252..de132cb 100644 --- a/src/Statements/ModifyColumn.php +++ b/src/Statements/ModifyColumn.php @@ -4,6 +4,7 @@ use CzProject\SqlGenerator\IDriver; use CzProject\SqlGenerator\IStatement; + use CzProject\SqlGenerator\Value; class ModifyColumn implements IStatement @@ -22,7 +23,7 @@ class ModifyColumn implements IStatement * @param string $name * @param string $type * @param array $parameters - * @param array $options [name => value] + * @param array $options [name => value] */ public function __construct($name, $type, array $parameters = NULL, array $options = []) { diff --git a/src/Value.php b/src/Value.php new file mode 100644 index 0000000..8d6dfd2 --- /dev/null +++ b/src/Value.php @@ -0,0 +1,38 @@ +value = $value; + } + + + /** + * @return string + */ + public function toString(IDriver $driver) + { + return Helpers::formatValue($this->value, $driver); + } + + + /** + * @param scalar|\Stringable|\DateTimeInterface $value + * @return self + */ + public static function create($value) + { + return new self($value); + } + } diff --git a/tests/SqlGenerator/SqlDocument.alterTable.phpt b/tests/SqlGenerator/SqlDocument.alterTable.phpt index d1ba62a..1678def 100644 --- a/tests/SqlGenerator/SqlDocument.alterTable.phpt +++ b/tests/SqlGenerator/SqlDocument.alterTable.phpt @@ -3,6 +3,7 @@ use CzProject\SqlGenerator\Drivers; use CzProject\SqlGenerator\SqlDocument; use CzProject\SqlGenerator\Statements\IndexDefinition; +use CzProject\SqlGenerator\Value; use Tester\Assert; require __DIR__ . '/../bootstrap.php'; @@ -14,10 +15,16 @@ test(function () { $driver = new Drivers\MysqlDriver; $contactTable = $sql->alterTable('contact'); + $contactTable->setOption('AUTO_INCREMENT', Value::create(123)); + $contactTable->setOption('CHECKSUM', Value::create(FALSE)); + $contactTable->setOption('COMPRESSION', Value::create('NONE')); $contactTable->setOption('ENGINE', 'InnoDB'); // columns - $contactTable->addColumn('active', 'TINYINT', [1], ['UNSIGNED' => NULL]) + $contactTable->addColumn('active', 'TINYINT', [1], [ + 'UNSIGNED' => NULL, + 'MYOPTION' => Value::create('abc') + ]) ->setDefaultValue(TRUE) ->setNullable() ->setComment('Contact status') @@ -56,7 +63,7 @@ test(function () { Assert::same(implode("\n", [ 'ALTER TABLE `contact`', - "ADD COLUMN `active` TINYINT(1) UNSIGNED NULL DEFAULT 1 COMMENT 'Contact status' AFTER `name`,", + "ADD COLUMN `active` TINYINT(1) UNSIGNED MYOPTION 'abc' NULL DEFAULT 1 COMMENT 'Contact status' AFTER `name`,", "ADD COLUMN `id` INT NOT NULL AUTO_INCREMENT FIRST,", "MODIFY COLUMN `name` VARCHAR(200) NULL DEFAULT 'XYZ' COMMENT 'Name of contact' AFTER `id`,", "MODIFY COLUMN `id` INT NOT NULL AUTO_INCREMENT FIRST,", @@ -66,6 +73,9 @@ test(function () { "DROP FOREIGN KEY `fk_creator`,", "ADD CONSTRAINT `fk_creator` FOREIGN KEY (`creator_id`) REFERENCES `user` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,", 'COMMENT \'Table of contacts.\',', + 'AUTO_INCREMENT=123,', + 'CHECKSUM=0,', + 'COMPRESSION=\'NONE\',', 'ENGINE=InnoDB;', '', ]), $sql->toSql($driver)); diff --git a/tests/SqlGenerator/SqlDocument.createTable.phpt b/tests/SqlGenerator/SqlDocument.createTable.phpt index 4d0b3cc..3b9c2d9 100644 --- a/tests/SqlGenerator/SqlDocument.createTable.phpt +++ b/tests/SqlGenerator/SqlDocument.createTable.phpt @@ -3,6 +3,7 @@ use CzProject\SqlGenerator\Drivers; use CzProject\SqlGenerator\SqlDocument; use CzProject\SqlGenerator\Statements\IndexDefinition; +use CzProject\SqlGenerator\Value; use Tester\Assert; require __DIR__ . '/../bootstrap.php'; @@ -15,13 +16,19 @@ test(function () { $contactTable = $sql->createTable('contact') ->setComment('Clients table.') + ->setOption('AUTO_INCREMENT', Value::create(123)) + ->setOption('CHECKSUM', Value::create(FALSE)) + ->setOption('COMPRESSION', Value::create('NONE')) ->setOption('ENGINE', 'InnoDB'); $contactTable->addColumn('id', 'INT', NULL, ['UNSIGNED' => NULL]) ->setAutoIncrement(); $contactTable->addColumn('name', 'VARCHAR(100)') ->setComment('Client name'); $contactTable->addColumn('surname', 'VARCHAR(100)'); - $contactTable->addColumn('active', 'TINYINT', [1], ['UNSIGNED' => NULL]) + $contactTable->addColumn('active', 'TINYINT', [1], [ + 'UNSIGNED' => NULL, + 'MYOPTION' => Value::create('abc'), + ]) ->setDefaultValue(TRUE); $contactTable->addColumn('status', 'ENUM', ['new', 'verified']) ->setDefaultValue('new'); @@ -43,7 +50,7 @@ test(function () { "\t`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,", "\t`name` VARCHAR(100) NOT NULL COMMENT 'Client name',", "\t`surname` VARCHAR(100) NOT NULL,", - "\t`active` TINYINT(1) UNSIGNED NOT NULL DEFAULT 1,", + "\t`active` TINYINT(1) UNSIGNED MYOPTION 'abc' NOT NULL DEFAULT 1,", "\t`status` ENUM('new', 'verified') NOT NULL DEFAULT 'new',", "\t`created` DATETIME NOT NULL,", "\t`removed` DATETIME NULL,", @@ -52,6 +59,9 @@ test(function () { "\tCONSTRAINT `fk_creator` FOREIGN KEY (`creator_id`) REFERENCES `user` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT", ')', 'COMMENT \'Clients table.\'', + 'AUTO_INCREMENT=123', + 'CHECKSUM=0', + 'COMPRESSION=\'NONE\'', 'ENGINE=InnoDB;', '', ]), $sql->toSql($driver));