-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCreateQuery.php
215 lines (194 loc) · 6.71 KB
/
CreateQuery.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
namespace Tivins\Database;
use UnitEnum;
use Tivins\Database\Enums\TextSize;
/**
* Most of the parameters of this object are intended to be used with named-parameters.
*/
class CreateQuery extends Query
{
private array $fields = [];
private array $indexes = [];
private string $engine = 'InnoDB';
public function getEngine(): string
{
return $this->engine;
}
public function setEngine(string $engine): CreateQuery
{
$this->engine = $engine;
return $this;
}
public function addAutoIncrement(string $name, bool $unsigned = true, $size = ''): self
{
$this->fields[] = [
'type' => $size . 'int',
'attr' => ($unsigned ? 'unsigned' : '') . ' auto_increment',
'name' => $name
];
$this->indexes[] = ['type' => 'primary key', 'columns' => [$name]];
return $this;
}
/**
* This function is indented to be called using named parameters.
*
* Example:
* ```php
* $query->addString('field', nullable: false, length: 64)
* ```
*
* @param string $name The column name.
* @param int $length The length of the string.
* @param bool $nullable Is it nullable or not ?
* @return $this The current object.
*/
public function addString(string $name, int $length = 255, bool $nullable = true): self
{
$this->fields[] = [
'type' => 'varchar(' . $length . ')',
'attr' => ($nullable ? '' : ' not null'),
'name' => $name,
];
return $this;
}
public function addBool(string $name, bool $default = false): self
{
$this->fields[] = [
'type' => 'tinyint(1)',
'attr' => 'not null default ' . (int)($default),
'name' => $name,
];
return $this;
}
public function addText(string $name, bool $nullable = true, TextSize $size = TextSize::Standard): self
{
$this->fields[] = [
'type' => $size->value . 'text',
'attr' => ($nullable ? '' : ' not null'),
'name' => $name,
];
return $this;
}
/**
* This function is indented to be called using named parameters.
*
* Examples:
* ```php
* $query->addInteger('field', -1);
* $query->addInteger('field', 0, nullable: false);
* $query->addInteger('field', null, nullable: true, unsigned: true);
* ```
*
* @param string $name The column name.
* @param int|null $default The default value. If `null` is given, the $nullable will automatically set to `true`.
* @param bool $unsigned Does the integer unsigned or not ?
* @param bool $nullable Can column contain null value or not ?
* @param string $size 'tiny','small','medium', '' or 'big'.
* @return $this The current object.
*/
public function addInteger(string $name, ?int $default, bool $unsigned = false, bool $nullable = false, string $size = ''): self
{
$this->fields[] = [
'type' => $size . 'int' . ($unsigned ? ' unsigned' : ''),
'attr' => trim(($nullable || is_null($default) ? '' : 'not null')
. ' default ' . (is_null($default) ? 'null' : $default)),
'name' => $name,
];
return $this;
}
public function addFloat(string $name, ?float $default, bool $nullable = false): self
{
$this->fields[] = [
'type' => 'float',
'attr' => trim(($nullable || is_null($default) ? '' : 'not null')
. ' default ' . (is_null($default) ? 'null' : $default)),
'name' => $name,
];
return $this;
}
/**
* Adds an unsigned integer field, with 0 as default value and is not nullable.
*
* @param string $name The field name.
* @return $this The current object.
*/
public function addPointer(string $name): self
{
return $this->addInteger($name, 0, unsigned: true);
}
public function addGeometry(string $name): self
{
$this->fields[] = ['type' => 'geometry', 'attr' => '', 'name' => $name];
return $this;
}
/**
* Add SQL enum from PHP enum (UnitEnum or BackedEnum).
*
* Enum Fruits { case Apple; case Banana; }
* $query->addEnum('fruits', Fruits::cases());
*
* @param string $name The field name.
* @param UnitEnum[] $cases
* Use the `value` property of the BackedEnum if available, or `name` property of the Enum otherwise.
*
* @param UnitEnum|null $default
* @return $this The current object.
*
* @see addStdEnum()
*/
public function addEnum(string $name, array $cases, ?UnitEnum $default = null): self
{
$values = array_map(fn(UnitEnum $enum) => json_encode($enum->value ?? $enum->name), $cases);
$this->fields[] = [
'name' => $name,
'type' => 'enum(' . implode(',', $values) . ')',
'attr' => '',
];
return $this;
}
/**
* @param string $name The field name.
* @param string[] $cases The enumeration as an array of strings.
* @param string|null $default The default value.
* @return $this The current object.
* @see addEnum()
*/
public function addStdEnum(string $name, array $cases, ?string $default = null): self
{
$values = array_map('json_encode', $cases);
$this->fields[] = ['type' => 'enum(' . implode(',', $values) . ')', 'attr' => '', 'name' => $name];
return $this;
}
public function addJSON(string $name, ?string $default = null, bool $nullable = true): self
{
$this->fields[] = [
'type' => 'json',
'attr' => trim(($nullable || is_null($default) ? '' : 'not null')
. ' default ' . (is_null($default) ? 'null' : $default)),
'name' => $name];
return $this;
}
public function addUniqueKey(array $columns): self
{
$this->indexes[] = ['type' => 'unique', 'columns' => $columns];
return $this;
}
public function addIndex(array $columns): self
{
$this->indexes[] = ['type' => 'index', 'columns' => $columns];
return $this;
}
public function build(): QueryData
{
$statements = [];
foreach ($this->fields as $field) {
$statements[] = trim("`$field[name]` $field[type] $field[attr]");
}
foreach ($this->indexes as $index) {
$statements[] = $index['type'] . ' (' . implode(',', $index['columns']) . ')';
}
$statements = implode(', ', $statements);
$sql = "create table if not exists `$this->tableName` ($statements) engine=$this->engine";
return new QueryData($sql);
}
}