1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\QueryBuilder; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Db\Exception\NotSupportedException; |
8
|
|
|
use Yiisoft\Db\Query\QueryInterface; |
9
|
|
|
use Yiisoft\Db\Schema\Builder\ColumnInterface; |
10
|
|
|
use Yiisoft\Db\Schema\QuoterInterface; |
11
|
|
|
use Yiisoft\Db\Schema\SchemaInterface; |
12
|
|
|
|
13
|
|
|
use function implode; |
14
|
|
|
use function is_string; |
15
|
|
|
use function preg_split; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* It's used to create and change the structure of database objects in a database. |
19
|
|
|
* |
20
|
|
|
* These database objects include views, schemas, tables, indexes, etc. |
21
|
|
|
* |
22
|
|
|
* @link https://en.wikipedia.org/wiki/Data_definition_language |
23
|
|
|
*/ |
24
|
|
|
abstract class AbstractDDLQueryBuilder implements DDLQueryBuilderInterface |
25
|
|
|
{ |
26
|
|
|
public function __construct( |
27
|
|
|
protected QueryBuilderInterface $queryBuilder, |
28
|
|
|
protected QuoterInterface $quoter, |
29
|
|
|
protected SchemaInterface $schema |
30
|
|
|
) { |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function addCheck(string $table, string $name, string $expression): string |
34
|
|
|
{ |
35
|
|
|
return 'ALTER TABLE ' |
36
|
|
|
. $this->quoter->quoteTableName($table) |
37
|
|
|
. ' ADD CONSTRAINT ' |
38
|
|
|
. $this->quoter->quoteColumnName($name) |
39
|
|
|
. ' CHECK (' . $this->quoter->quoteSql($expression) . ')'; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function addColumn(string $table, string $column, ColumnInterface|string $type): string |
43
|
|
|
{ |
44
|
|
|
return 'ALTER TABLE ' |
45
|
|
|
. $this->quoter->quoteTableName($table) |
46
|
|
|
. ' ADD ' |
47
|
|
|
. $this->quoter->quoteColumnName($column) |
48
|
|
|
. ' ' |
49
|
|
|
. $this->queryBuilder->getColumnType($type); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function addCommentOnColumn(string $table, string $column, string $comment): string |
53
|
|
|
{ |
54
|
|
|
return 'COMMENT ON COLUMN ' |
55
|
|
|
. $this->quoter->quoteTableName($table) |
56
|
|
|
. '.' |
57
|
|
|
. $this->quoter->quoteColumnName($column) |
58
|
|
|
. ' IS ' |
59
|
|
|
. (string) $this->quoter->quoteValue($comment); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function addCommentOnTable(string $table, string $comment): string |
63
|
|
|
{ |
64
|
|
|
return 'COMMENT ON TABLE ' |
65
|
|
|
. $this->quoter->quoteTableName($table) |
66
|
|
|
. ' IS ' |
67
|
|
|
. (string) $this->quoter->quoteValue($comment); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function addDefaultValue(string $table, string $name, string $column, mixed $value): string |
71
|
|
|
{ |
72
|
|
|
throw new NotSupportedException(__METHOD__ . ' is not supported by this DBMS.'); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function addForeignKey( |
76
|
|
|
string $table, |
77
|
|
|
string $name, |
78
|
|
|
array|string $columns, |
79
|
|
|
string $referenceTable, |
80
|
|
|
array|string $referenceColumns, |
81
|
|
|
string|null $delete = null, |
82
|
|
|
string|null $update = null |
83
|
|
|
): string { |
84
|
|
|
$sql = 'ALTER TABLE ' |
85
|
|
|
. $this->quoter->quoteTableName($table) |
86
|
|
|
. ' ADD CONSTRAINT ' . $this->quoter->quoteColumnName($name) |
87
|
|
|
. ' FOREIGN KEY (' . $this->queryBuilder->buildColumns($columns) . ')' |
88
|
|
|
. ' REFERENCES ' . $this->quoter->quoteTableName($referenceTable) |
89
|
|
|
. ' (' . $this->queryBuilder->buildColumns($referenceColumns) . ')'; |
90
|
|
|
|
91
|
|
|
if ($delete !== null) { |
92
|
|
|
$sql .= ' ON DELETE ' . $delete; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if ($update !== null) { |
96
|
|
|
$sql .= ' ON UPDATE ' . $update; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $sql; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function addPrimaryKey(string $table, string $name, array|string $columns): string |
103
|
|
|
{ |
104
|
|
|
if (is_string($columns)) { |
|
|
|
|
105
|
|
|
$columns = preg_split('/\s*,\s*/', $columns, -1, PREG_SPLIT_NO_EMPTY); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** @psalm-var string[] $columns */ |
109
|
|
|
foreach ($columns as $i => $col) { |
110
|
|
|
$columns[$i] = $this->quoter->quoteColumnName($col); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return 'ALTER TABLE ' |
114
|
|
|
. $this->quoter->quoteTableName($table) |
115
|
|
|
. ' ADD CONSTRAINT ' . $this->quoter->quoteColumnName($name) |
116
|
|
|
. ' PRIMARY KEY (' . implode(', ', $columns) . ')'; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function addUnique(string $table, string $name, array|string $columns): string |
120
|
|
|
{ |
121
|
|
|
if (is_string($columns)) { |
|
|
|
|
122
|
|
|
$columns = preg_split('/\s*,\s*/', $columns, -1, PREG_SPLIT_NO_EMPTY); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** @psalm-var string[] $columns */ |
126
|
|
|
foreach ($columns as $i => $col) { |
127
|
|
|
$columns[$i] = $this->quoter->quoteColumnName($col); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return 'ALTER TABLE ' |
131
|
|
|
. $this->quoter->quoteTableName($table) |
132
|
|
|
. ' ADD CONSTRAINT ' . $this->quoter->quoteColumnName($name) |
133
|
|
|
. ' UNIQUE (' . implode(', ', $columns) . ')'; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function alterColumn( |
137
|
|
|
string $table, |
138
|
|
|
string $column, |
139
|
|
|
ColumnInterface|string $type |
140
|
|
|
): string { |
141
|
|
|
return 'ALTER TABLE ' |
142
|
|
|
. $this->quoter->quoteTableName($table) |
143
|
|
|
. ' CHANGE ' |
144
|
|
|
. $this->quoter->quoteColumnName($column) |
145
|
|
|
. ' ' |
146
|
|
|
. $this->quoter->quoteColumnName($column) . ' ' |
147
|
|
|
. $this->queryBuilder->getColumnType($type); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function checkIntegrity(string $schema = '', string $table = '', bool $check = true): string |
151
|
|
|
{ |
152
|
|
|
throw new NotSupportedException(__METHOD__ . ' is not supported by this DBMS.'); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function createIndex( |
156
|
|
|
string $table, |
157
|
|
|
string $name, |
158
|
|
|
array|string $columns, |
159
|
|
|
string $indexType = null, |
160
|
|
|
string $indexMethod = null |
161
|
|
|
): string { |
162
|
|
|
return 'CREATE ' . (!empty($indexType) ? $indexType . ' ' : '') . 'INDEX ' |
163
|
|
|
. $this->quoter->quoteTableName($name) |
164
|
|
|
. ' ON ' . $this->quoter->quoteTableName($table) |
165
|
|
|
. ' (' . $this->queryBuilder->buildColumns($columns) . ')'; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function createTable(string $table, array $columns, string $options = null): string |
169
|
|
|
{ |
170
|
|
|
$cols = []; |
171
|
|
|
|
172
|
|
|
foreach ($columns as $name => $type) { |
173
|
|
|
if (is_string($name)) { |
174
|
|
|
$cols[] = "\t" |
175
|
|
|
. $this->quoter->quoteColumnName($name) |
176
|
|
|
. ' ' |
177
|
|
|
. $this->queryBuilder->getColumnType($type); |
178
|
|
|
} else { |
179
|
|
|
/** @psalm-var string $type */ |
180
|
|
|
$cols[] = "\t" . $type; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
$sql = 'CREATE TABLE ' |
185
|
|
|
. $this->quoter->quoteTableName($table) . " (\n" . implode(",\n", $cols) . "\n)"; |
186
|
|
|
|
187
|
|
|
return $options === null ? $sql : $sql . ' ' . $options; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
public function createView(string $viewName, QueryInterface|string $subQuery): string |
191
|
|
|
{ |
192
|
|
|
if ($subQuery instanceof QueryInterface) { |
193
|
|
|
[$rawQuery, $params] = $this->queryBuilder->build($subQuery); |
194
|
|
|
|
195
|
|
|
/** @psalm-var mixed $value */ |
196
|
|
|
foreach ($params as $key => $value) { |
197
|
|
|
/** @psalm-var mixed */ |
198
|
|
|
$params[$key] = $this->quoter->quoteValue($value); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
$subQuery = strtr($rawQuery, $params); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
return 'CREATE VIEW ' . $this->quoter->quoteTableName($viewName) . ' AS ' . $subQuery; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
public function dropCheck(string $table, string $name): string |
208
|
|
|
{ |
209
|
|
|
return 'ALTER TABLE ' |
210
|
|
|
. $this->quoter->quoteTableName($table) |
211
|
|
|
. ' DROP CONSTRAINT ' |
212
|
|
|
. $this->quoter->quoteColumnName($name); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
public function dropColumn(string $table, string $column): string |
216
|
|
|
{ |
217
|
|
|
return 'ALTER TABLE ' |
218
|
|
|
. $this->quoter->quoteTableName($table) |
219
|
|
|
. ' DROP COLUMN ' |
220
|
|
|
. $this->quoter->quoteColumnName($column); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
public function dropCommentFromColumn(string $table, string $column): string |
224
|
|
|
{ |
225
|
|
|
return 'COMMENT ON COLUMN ' |
226
|
|
|
. $this->quoter->quoteTableName($table) |
227
|
|
|
. '.' |
228
|
|
|
. $this->quoter->quoteColumnName($column) |
229
|
|
|
. ' IS NULL'; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
public function dropCommentFromTable(string $table): string |
233
|
|
|
{ |
234
|
|
|
return 'COMMENT ON TABLE ' |
235
|
|
|
. $this->quoter->quoteTableName($table) |
236
|
|
|
. ' IS NULL'; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
public function dropDefaultValue(string $table, string $name): string |
240
|
|
|
{ |
241
|
|
|
throw new NotSupportedException(__METHOD__ . ' is not supported by this DBMS.'); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
public function dropForeignKey(string $table, string $name): string |
245
|
|
|
{ |
246
|
|
|
return 'ALTER TABLE ' |
247
|
|
|
. $this->quoter->quoteTableName($table) |
248
|
|
|
. ' DROP CONSTRAINT ' |
249
|
|
|
. $this->quoter->quoteColumnName($name); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
public function dropIndex(string $table, string $name): string |
253
|
|
|
{ |
254
|
|
|
return 'DROP INDEX ' |
255
|
|
|
. $this->quoter->quoteTableName($name) |
256
|
|
|
. ' ON ' |
257
|
|
|
. $this->quoter->quoteTableName($table); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
public function dropPrimaryKey(string $table, string $name): string |
261
|
|
|
{ |
262
|
|
|
return 'ALTER TABLE ' |
263
|
|
|
. $this->quoter->quoteTableName($table) |
264
|
|
|
. ' DROP CONSTRAINT ' |
265
|
|
|
. $this->quoter->quoteColumnName($name); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
public function dropTable(string $table): string |
269
|
|
|
{ |
270
|
|
|
return 'DROP TABLE ' . $this->quoter->quoteTableName($table); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
public function dropUnique(string $table, string $name): string |
274
|
|
|
{ |
275
|
|
|
return 'ALTER TABLE ' |
276
|
|
|
. $this->quoter->quoteTableName($table) |
277
|
|
|
. ' DROP CONSTRAINT ' |
278
|
|
|
. $this->quoter->quoteColumnName($name); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
public function dropView(string $viewName): string |
282
|
|
|
{ |
283
|
|
|
return 'DROP VIEW ' . $this->quoter->quoteTableName($viewName); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
public function renameColumn(string $table, string $oldName, string $newName): string |
287
|
|
|
{ |
288
|
|
|
return 'ALTER TABLE ' |
289
|
|
|
. $this->quoter->quoteTableName($table) |
290
|
|
|
. ' RENAME COLUMN ' . $this->quoter->quoteColumnName($oldName) |
291
|
|
|
. ' TO ' . $this->quoter->quoteColumnName($newName); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
public function renameTable(string $oldName, string $newName): string |
295
|
|
|
{ |
296
|
|
|
return 'RENAME TABLE ' |
297
|
|
|
. $this->quoter->quoteTableName($oldName) |
298
|
|
|
. ' TO ' . $this->quoter->quoteTableName($newName); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
public function truncateTable(string $table): string |
302
|
|
|
{ |
303
|
|
|
return 'TRUNCATE TABLE ' . $this->quoter->quoteTableName($table); |
304
|
|
|
} |
305
|
|
|
} |
306
|
|
|
|