Skip to content

Commit 7d5e59b

Browse files
committed
Added Value, supported in table & column options
1 parent 95dbcfc commit 7d5e59b

File tree

9 files changed

+118
-21
lines changed

9 files changed

+118
-21
lines changed

readme.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,36 @@ $sql->getSqlQueries($driver); // returns string[]
104104
$sql->save($file, $driver); // saves SQL into file
105105
```
106106

107+
## Special Values
108+
109+
There are value objects for specific cases:
110+
111+
### TableName
112+
113+
Delimited table name.
114+
115+
```php
116+
use CzProject\SqlGenerator\TableName;
117+
118+
$table = $sql->createTable(TableName::create('schema.table'))
119+
$table->addForeignKey('fk_table_id', 'id', TableName::create('schema2.table2'), 'id');
120+
// and more ($sql->renameTable(),...)
121+
```
122+
123+
124+
### Value
125+
126+
Scalar/stringable/datetime value. It can be used in option values.
127+
128+
```php
129+
use CzProject\SqlGenerator\Value;
130+
131+
$table->setOption('AUTO_INCREMENT', Value::create(123)); // generates AUTO_INCREMENT=123
132+
$table->setOption('CHECKSUM', Value::create(FALSE)); // generates CHECKSUM=0
133+
$table->setOption('COMPRESSION', Value::create('NONE')); // generates COMPRESSION='NONE'
134+
```
135+
136+
107137
## Supported database
108138

109139
Currently is supported common SQL and MySQL.

src/Statements/AddColumn.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use CzProject\SqlGenerator\IDriver;
66
use CzProject\SqlGenerator\IStatement;
7+
use CzProject\SqlGenerator\Value;
78

89

910
class AddColumn implements IStatement
@@ -22,7 +23,7 @@ class AddColumn implements IStatement
2223
* @param string $name
2324
* @param string $type
2425
* @param array<int|float|string> $parameters
25-
* @param array<string, string|NULL> $options [name => value]
26+
* @param array<string, string|Value|NULL> $options [name => value]
2627
*/
2728
public function __construct($name, $type, array $parameters = NULL, array $options = [])
2829
{

src/Statements/AlterTable.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use CzProject\SqlGenerator\IDriver;
77
use CzProject\SqlGenerator\IStatement;
88
use CzProject\SqlGenerator\TableName;
9+
use CzProject\SqlGenerator\Value;
910

1011

1112
class AlterTable implements IStatement
@@ -19,7 +20,7 @@ class AlterTable implements IStatement
1920
/** @var string|NULL */
2021
private $comment;
2122

22-
/** @var array<string, string> [name => value] */
23+
/** @var array<string, string|Value> [name => value] */
2324
private $options = [];
2425

2526

@@ -36,7 +37,7 @@ public function __construct($tableName)
3637
* @param string $name
3738
* @param string $type
3839
* @param array<int|float|string> $parameters
39-
* @param array<string, string|NULL> $options [name => value]
40+
* @param array<string, string|Value|NULL> $options [name => value]
4041
* @return AddColumn
4142
*/
4243
public function addColumn($name, $type, array $parameters = NULL, array $options = [])
@@ -59,7 +60,7 @@ public function dropColumn($column)
5960
* @param string $name
6061
* @param string $type
6162
* @param array<int|float|string> $parameters
62-
* @param array<string, string|NULL> $options [name => value]
63+
* @param array<string, string|Value|NULL> $options [name => value]
6364
* @return ModifyColumn
6465
*/
6566
public function modifyColumn($name, $type, array $parameters = NULL, array $options = [])
@@ -125,7 +126,7 @@ public function setComment($comment)
125126

126127
/**
127128
* @param string $name
128-
* @param string $value
129+
* @param string|Value $value
129130
* @return static
130131
*/
131132
public function setOption($name, $value)
@@ -174,7 +175,7 @@ public function toSql(IDriver $driver)
174175
$output .= ",\n";
175176
}
176177

177-
$output .= $optionName . '=' . $optionValue;
178+
$output .= $optionName . '=' . ($optionValue instanceof Value ? $optionValue->toString($driver) : $optionValue);
178179
}
179180

180181
$output .= ';';

src/Statements/ColumnDefinition.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use CzProject\SqlGenerator\Helpers;
77
use CzProject\SqlGenerator\IDriver;
88
use CzProject\SqlGenerator\IStatement;
9+
use CzProject\SqlGenerator\Value;
910

1011

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

22-
/** @var array<string, string|NULL> [name => value] */
23+
/** @var array<string, string|Value|NULL> [name => value] */
2324
private $options = [];
2425

2526
/** @var bool */
@@ -39,7 +40,7 @@ class ColumnDefinition implements IStatement
3940
* @param string $name
4041
* @param string $type
4142
* @param array<int|float|string>|NULL $parameters
42-
* @param array<string, string|NULL> $options [name => value]
43+
* @param array<string, string|Value|NULL> $options [name => value]
4344
*/
4445
public function __construct($name, $type, array $parameters = NULL, array $options = [])
4546
{
@@ -118,13 +119,13 @@ public function toSql(IDriver $driver)
118119

119120
foreach ($specialOptions as $option) {
120121
if (isset($options[$option])) {
121-
$output .= ' ' . self::formatOption($option, $options[$option]);
122+
$output .= ' ' . self::formatOption($option, $options[$option], $driver);
122123
unset($options[$option]);
123124
}
124125
}
125126

126127
foreach ($options as $option => $value) {
127-
$output .= ' ' . self::formatOption($option, $value);
128+
$output .= ' ' . self::formatOption($option, $value, $driver);
128129
}
129130

130131
$output .= ' ' . ($this->nullable ? 'NULL' : 'NOT NULL');
@@ -147,11 +148,15 @@ public function toSql(IDriver $driver)
147148

148149
/**
149150
* @param string $name
150-
* @param string|NULL $value
151+
* @param string|Value|NULL $value
151152
* @return string
152153
*/
153-
private static function formatOption($name, $value)
154+
private static function formatOption($name, $value, IDriver $driver)
154155
{
156+
if ($value instanceof Value) {
157+
$value = $value->toString($driver);
158+
}
159+
155160
return $name . ($value !== NULL ? (' ' . $value) : '');
156161
}
157162
}

src/Statements/CreateTable.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use CzProject\SqlGenerator\IDriver;
88
use CzProject\SqlGenerator\IStatement;
99
use CzProject\SqlGenerator\TableName;
10+
use CzProject\SqlGenerator\Value;
1011

1112

1213
class CreateTable implements IStatement
@@ -26,7 +27,7 @@ class CreateTable implements IStatement
2627
/** @var string|NULL */
2728
private $comment;
2829

29-
/** @var array<string, string> [name => value] */
30+
/** @var array<string, string|Value> [name => value] */
3031
private $options = [];
3132

3233

@@ -43,7 +44,7 @@ public function __construct($tableName)
4344
* @param string $name
4445
* @param string $type
4546
* @param array<int|float|string>|NULL $parameters
46-
* @param array<string, string|NULL> $options
47+
* @param array<string, string|Value|NULL> $options
4748
* @return ColumnDefinition
4849
*/
4950
public function addColumn($name, $type, array $parameters = NULL, array $options = [])
@@ -101,7 +102,7 @@ public function setComment($comment)
101102

102103
/**
103104
* @param string $name
104-
* @param string $value
105+
* @param string|Value $value
105106
* @return static
106107
*/
107108
public function setOption($name, $value)
@@ -160,7 +161,7 @@ public function toSql(IDriver $driver)
160161

161162
foreach ($this->options as $optionName => $optionValue) {
162163
$output .= "\n";
163-
$output .= $optionName . '=' . $optionValue;
164+
$output .= $optionName . '=' . ($optionValue instanceof Value ? $optionValue->toString($driver) : $optionValue);
164165
}
165166

166167
$output .= ';';

src/Statements/ModifyColumn.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use CzProject\SqlGenerator\IDriver;
66
use CzProject\SqlGenerator\IStatement;
7+
use CzProject\SqlGenerator\Value;
78

89

910
class ModifyColumn implements IStatement
@@ -22,7 +23,7 @@ class ModifyColumn implements IStatement
2223
* @param string $name
2324
* @param string $type
2425
* @param array<int|float|string> $parameters
25-
* @param array<string, string|NULL> $options [name => value]
26+
* @param array<string, string|Value|NULL> $options [name => value]
2627
*/
2728
public function __construct($name, $type, array $parameters = NULL, array $options = [])
2829
{

src/Value.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace CzProject\SqlGenerator;
4+
5+
6+
class Value
7+
{
8+
/** @var scalar|\Stringable|\DateTimeInterface */
9+
private $value;
10+
11+
12+
/**
13+
* @param scalar|\Stringable|\DateTimeInterface $value
14+
*/
15+
public function __construct($value)
16+
{
17+
$this->value = $value;
18+
}
19+
20+
21+
/**
22+
* @return string
23+
*/
24+
public function toString(IDriver $driver)
25+
{
26+
return Helpers::formatValue($this->value, $driver);
27+
}
28+
29+
30+
/**
31+
* @param scalar|\Stringable|\DateTimeInterface $value
32+
* @return self
33+
*/
34+
public static function create($value)
35+
{
36+
return new self($value);
37+
}
38+
}

tests/SqlGenerator/SqlDocument.alterTable.phpt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use CzProject\SqlGenerator\Drivers;
44
use CzProject\SqlGenerator\SqlDocument;
55
use CzProject\SqlGenerator\Statements\IndexDefinition;
6+
use CzProject\SqlGenerator\Value;
67
use Tester\Assert;
78

89
require __DIR__ . '/../bootstrap.php';
@@ -14,10 +15,16 @@ test(function () {
1415
$driver = new Drivers\MysqlDriver;
1516

1617
$contactTable = $sql->alterTable('contact');
18+
$contactTable->setOption('AUTO_INCREMENT', Value::create(123));
19+
$contactTable->setOption('CHECKSUM', Value::create(FALSE));
20+
$contactTable->setOption('COMPRESSION', Value::create('NONE'));
1721
$contactTable->setOption('ENGINE', 'InnoDB');
1822

1923
// columns
20-
$contactTable->addColumn('active', 'TINYINT', [1], ['UNSIGNED' => NULL])
24+
$contactTable->addColumn('active', 'TINYINT', [1], [
25+
'UNSIGNED' => NULL,
26+
'MYOPTION' => Value::create('abc')
27+
])
2128
->setDefaultValue(TRUE)
2229
->setNullable()
2330
->setComment('Contact status')
@@ -56,7 +63,7 @@ test(function () {
5663

5764
Assert::same(implode("\n", [
5865
'ALTER TABLE `contact`',
59-
"ADD COLUMN `active` TINYINT(1) UNSIGNED NULL DEFAULT 1 COMMENT 'Contact status' AFTER `name`,",
66+
"ADD COLUMN `active` TINYINT(1) UNSIGNED MYOPTION 'abc' NULL DEFAULT 1 COMMENT 'Contact status' AFTER `name`,",
6067
"ADD COLUMN `id` INT NOT NULL AUTO_INCREMENT FIRST,",
6168
"MODIFY COLUMN `name` VARCHAR(200) NULL DEFAULT 'XYZ' COMMENT 'Name of contact' AFTER `id`,",
6269
"MODIFY COLUMN `id` INT NOT NULL AUTO_INCREMENT FIRST,",
@@ -66,6 +73,9 @@ test(function () {
6673
"DROP FOREIGN KEY `fk_creator`,",
6774
"ADD CONSTRAINT `fk_creator` FOREIGN KEY (`creator_id`) REFERENCES `user` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,",
6875
'COMMENT \'Table of contacts.\',',
76+
'AUTO_INCREMENT=123,',
77+
'CHECKSUM=0,',
78+
'COMPRESSION=\'NONE\',',
6979
'ENGINE=InnoDB;',
7080
'',
7181
]), $sql->toSql($driver));

tests/SqlGenerator/SqlDocument.createTable.phpt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use CzProject\SqlGenerator\Drivers;
44
use CzProject\SqlGenerator\SqlDocument;
55
use CzProject\SqlGenerator\Statements\IndexDefinition;
6+
use CzProject\SqlGenerator\Value;
67
use Tester\Assert;
78

89
require __DIR__ . '/../bootstrap.php';
@@ -15,13 +16,19 @@ test(function () {
1516

1617
$contactTable = $sql->createTable('contact')
1718
->setComment('Clients table.')
19+
->setOption('AUTO_INCREMENT', Value::create(123))
20+
->setOption('CHECKSUM', Value::create(FALSE))
21+
->setOption('COMPRESSION', Value::create('NONE'))
1822
->setOption('ENGINE', 'InnoDB');
1923
$contactTable->addColumn('id', 'INT', NULL, ['UNSIGNED' => NULL])
2024
->setAutoIncrement();
2125
$contactTable->addColumn('name', 'VARCHAR(100)')
2226
->setComment('Client name');
2327
$contactTable->addColumn('surname', 'VARCHAR(100)');
24-
$contactTable->addColumn('active', 'TINYINT', [1], ['UNSIGNED' => NULL])
28+
$contactTable->addColumn('active', 'TINYINT', [1], [
29+
'UNSIGNED' => NULL,
30+
'MYOPTION' => Value::create('abc'),
31+
])
2532
->setDefaultValue(TRUE);
2633
$contactTable->addColumn('status', 'ENUM', ['new', 'verified'])
2734
->setDefaultValue('new');
@@ -43,7 +50,7 @@ test(function () {
4350
"\t`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,",
4451
"\t`name` VARCHAR(100) NOT NULL COMMENT 'Client name',",
4552
"\t`surname` VARCHAR(100) NOT NULL,",
46-
"\t`active` TINYINT(1) UNSIGNED NOT NULL DEFAULT 1,",
53+
"\t`active` TINYINT(1) UNSIGNED MYOPTION 'abc' NOT NULL DEFAULT 1,",
4754
"\t`status` ENUM('new', 'verified') NOT NULL DEFAULT 'new',",
4855
"\t`created` DATETIME NOT NULL,",
4956
"\t`removed` DATETIME NULL,",
@@ -52,6 +59,9 @@ test(function () {
5259
"\tCONSTRAINT `fk_creator` FOREIGN KEY (`creator_id`) REFERENCES `user` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT",
5360
')',
5461
'COMMENT \'Clients table.\'',
62+
'AUTO_INCREMENT=123',
63+
'CHECKSUM=0',
64+
'COMPRESSION=\'NONE\'',
5565
'ENGINE=InnoDB;',
5666
'',
5767
]), $sql->toSql($driver));

0 commit comments

Comments
 (0)