Skip to content

Commit

Permalink
Added Value, supported in table & column options
Browse files Browse the repository at this point in the history
  • Loading branch information
janpecha committed Jul 27, 2023
1 parent 95dbcfc commit 7d5e59b
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 21 deletions.
30 changes: 30 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion src/Statements/AddColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use CzProject\SqlGenerator\IDriver;
use CzProject\SqlGenerator\IStatement;
use CzProject\SqlGenerator\Value;


class AddColumn implements IStatement
Expand All @@ -22,7 +23,7 @@ class AddColumn implements IStatement
* @param string $name
* @param string $type
* @param array<int|float|string> $parameters
* @param array<string, string|NULL> $options [name => value]
* @param array<string, string|Value|NULL> $options [name => value]
*/
public function __construct($name, $type, array $parameters = NULL, array $options = [])
{
Expand Down
11 changes: 6 additions & 5 deletions src/Statements/AlterTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use CzProject\SqlGenerator\IDriver;
use CzProject\SqlGenerator\IStatement;
use CzProject\SqlGenerator\TableName;
use CzProject\SqlGenerator\Value;


class AlterTable implements IStatement
Expand All @@ -19,7 +20,7 @@ class AlterTable implements IStatement
/** @var string|NULL */
private $comment;

/** @var array<string, string> [name => value] */
/** @var array<string, string|Value> [name => value] */
private $options = [];


Expand All @@ -36,7 +37,7 @@ public function __construct($tableName)
* @param string $name
* @param string $type
* @param array<int|float|string> $parameters
* @param array<string, string|NULL> $options [name => value]
* @param array<string, string|Value|NULL> $options [name => value]
* @return AddColumn
*/
public function addColumn($name, $type, array $parameters = NULL, array $options = [])
Expand All @@ -59,7 +60,7 @@ public function dropColumn($column)
* @param string $name
* @param string $type
* @param array<int|float|string> $parameters
* @param array<string, string|NULL> $options [name => value]
* @param array<string, string|Value|NULL> $options [name => value]
* @return ModifyColumn
*/
public function modifyColumn($name, $type, array $parameters = NULL, array $options = [])
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -174,7 +175,7 @@ public function toSql(IDriver $driver)
$output .= ",\n";
}

$output .= $optionName . '=' . $optionValue;
$output .= $optionName . '=' . ($optionValue instanceof Value ? $optionValue->toString($driver) : $optionValue);
}

$output .= ';';
Expand Down
17 changes: 11 additions & 6 deletions src/Statements/ColumnDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use CzProject\SqlGenerator\Helpers;
use CzProject\SqlGenerator\IDriver;
use CzProject\SqlGenerator\IStatement;
use CzProject\SqlGenerator\Value;


class ColumnDefinition implements IStatement
Expand All @@ -19,7 +20,7 @@ class ColumnDefinition implements IStatement
/** @var array<int|float|string> */
private $parameters = [];

/** @var array<string, string|NULL> [name => value] */
/** @var array<string, string|Value|NULL> [name => value] */
private $options = [];

/** @var bool */
Expand All @@ -39,7 +40,7 @@ class ColumnDefinition implements IStatement
* @param string $name
* @param string $type
* @param array<int|float|string>|NULL $parameters
* @param array<string, string|NULL> $options [name => value]
* @param array<string, string|Value|NULL> $options [name => value]
*/
public function __construct($name, $type, array $parameters = NULL, array $options = [])
{
Expand Down Expand Up @@ -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');
Expand All @@ -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) : '');
}
}
9 changes: 5 additions & 4 deletions src/Statements/CreateTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use CzProject\SqlGenerator\IDriver;
use CzProject\SqlGenerator\IStatement;
use CzProject\SqlGenerator\TableName;
use CzProject\SqlGenerator\Value;


class CreateTable implements IStatement
Expand All @@ -26,7 +27,7 @@ class CreateTable implements IStatement
/** @var string|NULL */
private $comment;

/** @var array<string, string> [name => value] */
/** @var array<string, string|Value> [name => value] */
private $options = [];


Expand All @@ -43,7 +44,7 @@ public function __construct($tableName)
* @param string $name
* @param string $type
* @param array<int|float|string>|NULL $parameters
* @param array<string, string|NULL> $options
* @param array<string, string|Value|NULL> $options
* @return ColumnDefinition
*/
public function addColumn($name, $type, array $parameters = NULL, array $options = [])
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 .= ';';
Expand Down
3 changes: 2 additions & 1 deletion src/Statements/ModifyColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use CzProject\SqlGenerator\IDriver;
use CzProject\SqlGenerator\IStatement;
use CzProject\SqlGenerator\Value;


class ModifyColumn implements IStatement
Expand All @@ -22,7 +23,7 @@ class ModifyColumn implements IStatement
* @param string $name
* @param string $type
* @param array<int|float|string> $parameters
* @param array<string, string|NULL> $options [name => value]
* @param array<string, string|Value|NULL> $options [name => value]
*/
public function __construct($name, $type, array $parameters = NULL, array $options = [])
{
Expand Down
38 changes: 38 additions & 0 deletions src/Value.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace CzProject\SqlGenerator;


class Value
{
/** @var scalar|\Stringable|\DateTimeInterface */
private $value;


/**
* @param scalar|\Stringable|\DateTimeInterface $value
*/
public function __construct($value)
{
$this->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);
}
}
14 changes: 12 additions & 2 deletions tests/SqlGenerator/SqlDocument.alterTable.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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')
Expand Down Expand Up @@ -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,",
Expand All @@ -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));
Expand Down
14 changes: 12 additions & 2 deletions tests/SqlGenerator/SqlDocument.createTable.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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');
Expand All @@ -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,",
Expand All @@ -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));
Expand Down

0 comments on commit 7d5e59b

Please sign in to comment.