|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the O2System Framework package. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
* |
|
8
|
|
|
* @author Steeve Andrian Salim |
|
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
// ------------------------------------------------------------------------ |
|
13
|
|
|
|
|
14
|
|
|
namespace O2System\Database\Sql\Drivers\MySql; |
|
15
|
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------ |
|
17
|
|
|
|
|
18
|
|
|
use O2System\Database\Sql\Abstracts\AbstractForge; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class Forge |
|
22
|
|
|
* @package O2System\Database\Sql\Drivers\MySql |
|
23
|
|
|
*/ |
|
24
|
|
|
class Forge extends AbstractForge |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* Forge::platformCreateDatabaseStatement |
|
28
|
|
|
* |
|
29
|
|
|
* @param string $database |
|
30
|
|
|
* |
|
31
|
|
|
* @return string |
|
32
|
|
|
*/ |
|
33
|
|
|
public function platformCreateDatabaseStatement($database) |
|
34
|
|
|
{ |
|
35
|
|
|
return 'CREATE DATABASE ' . $database; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
// ------------------------------------------------------------------------ |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Forge::platformDropDatabaseStatement |
|
42
|
|
|
* |
|
43
|
|
|
* @param string $database |
|
44
|
|
|
* |
|
45
|
|
|
* @return string |
|
46
|
|
|
*/ |
|
47
|
|
|
public function platformDropDatabaseStatement($database) |
|
48
|
|
|
{ |
|
49
|
|
|
return 'CREATE DATABASE ' . $this->conn->escapeIdentifiers($database); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
// ------------------------------------------------------------------------ |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Forge::platformBackupDatabaseStatement |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $database |
|
58
|
|
|
* @param string $backupFilePath |
|
59
|
|
|
* |
|
60
|
|
|
* @return string |
|
61
|
|
|
*/ |
|
62
|
|
|
public function platformBackupDatabaseStatement($database, $backupFilePath) |
|
63
|
|
|
{ |
|
64
|
|
|
return 'BACKUP DATABASE ' . $this->conn->escapeIdentifiers($database) . ' TO DISK=' . $this->conn->escape($backupFilePath); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
// ------------------------------------------------------------------------ |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Forge::platformCreateTableStatement |
|
71
|
|
|
* |
|
72
|
|
|
* @param string $table |
|
73
|
|
|
* @param array $columns |
|
74
|
|
|
* @param bool $force |
|
75
|
|
|
* @param array $attributes |
|
76
|
|
|
* |
|
77
|
|
|
* @return mixed |
|
78
|
|
|
*/ |
|
79
|
|
|
public function platformCreateTableStatement($table, array $columns, $force = false, array $attributes = []) |
|
80
|
|
|
{ |
|
81
|
|
|
$primaryKeys = $foreignKeys = $uniqueKeys = $indexesKeys = []; |
|
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
// Open Statement |
|
84
|
|
|
$statementLines[] = 'CREATE TABLE' . ($force === true ? ' IF NOT EXISTS ' : '') . $this->conn->escapeIdentifiers($table) . ' ('; |
|
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
// Columns Statement |
|
87
|
|
|
$columnStatements = []; |
|
88
|
|
|
foreach ($columns as $columnName => $columnAttributes) { |
|
89
|
|
|
if (isset($columnAttributes[ 'type' ])) { |
|
90
|
|
|
if (isset($columnAttributes[ 'foreign_key' ])) { |
|
91
|
|
|
$foreignKeys[ $columnName ] = $columnAttributes[ 'foreign_key' ]; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$columnStatementLine = []; |
|
95
|
|
|
$columnName = $this->conn->escapeIdentifiers($columnName); |
|
96
|
|
|
$columnAttributes[ 'type' ] = strtoupper($columnAttributes[ 'type' ]); |
|
97
|
|
|
|
|
98
|
|
|
if (isset($columnAttributes[ 'primary_key' ])) { |
|
99
|
|
|
if ($columnAttributes[ 'primary_key' ] === true) { |
|
100
|
|
|
$primaryKeys[] = $columnName; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
if (isset($columnAttributes[ 'unique' ])) { |
|
105
|
|
|
if ($columnAttributes[ 'unique' ] === true) { |
|
106
|
|
|
$uniqueKeys[] = $columnName; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
if ($columnAttributes[ 'type' ] === 'ENUM') { |
|
111
|
|
|
if (empty($columnAttributes[ 'value' ])) { |
|
112
|
|
|
continue; |
|
113
|
|
|
} else { |
|
114
|
|
|
if (is_string($columnAttributes[ 'value' ])) { |
|
115
|
|
|
$columnAttributes[ 'value' ] = explode(',', $columnAttributes[ 'value' ]); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
$columnAttributes[ 'value' ] = array_map(function ($value) { |
|
119
|
|
|
return $this->conn->escape(str_replace('\'', '', trim($value))); |
|
120
|
|
|
}, $columnAttributes[ 'value' ]); |
|
121
|
|
|
|
|
122
|
|
|
$columnStatementLine[] = $columnName . |
|
|
|
|
|
|
123
|
|
|
' ' . $columnAttributes[ 'type' ] . '(' . |
|
124
|
|
|
implode(',', $columnAttributes[ 'value' ]) |
|
125
|
|
|
. ')'; |
|
126
|
|
|
} |
|
127
|
|
|
} elseif (isset($columnAttributes[ 'length' ])) { |
|
128
|
|
|
$columnStatementLine[] = $columnName . ' ' . $columnAttributes[ 'type' ] . '(' . $columnAttributes[ 'length' ] . ')'; |
|
129
|
|
|
} else { |
|
130
|
|
|
$columnStatementLine[] = $columnName . ' ' . $columnAttributes[ 'type' ]; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
if (isset($columnAttributes[ 'unsigned' ])) { |
|
134
|
|
|
if ($columnAttributes[ 'unsigned' ] === true) { |
|
135
|
|
|
if (in_array($columnAttributes[ 'type' ], |
|
136
|
|
|
['INT', 'BIGINT', 'SMALLINT', 'TINYINT', 'FLOAT', 'DECIMAL', 'REAL'])) { |
|
137
|
|
|
$columnStatementLine[] = 'UNSIGNED'; |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
if (isset($columnAttributes[ 'collate' ])) { |
|
143
|
|
|
$columnStatementLine[] = 'COLLATE ' . $columnAttributes[ 'collate' ]; |
|
144
|
|
|
} elseif (in_array($columnAttributes[ 'type' ], |
|
145
|
|
|
['CHAR', 'VARCHAR', 'TEXT', 'LONGTEXT', 'TINYTEXT', 'ENUM'])) { |
|
146
|
|
|
$columnStatementLine[] = 'COLLATE ' . $this->conn->getConfig('collate'); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
if (isset($columnAttributes[ 'not_null' ])) { |
|
150
|
|
|
if ($columnAttributes[ 'not_null' ] === true) { |
|
151
|
|
|
$columnStatementLine[] = 'NOT NULL'; |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
if (isset($columnAttributes[ 'default' ])) { |
|
156
|
|
|
$columnStatementLine[] = 'DEFAULT ' . $this->conn->escape($columnAttributes[ 'default' ]); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
if (isset($columnAttributes[ 'auto_increment' ])) { |
|
160
|
|
|
if ($columnAttributes[ 'auto_increment' ] === true) { |
|
161
|
|
|
$columnStatementLine[] = 'AUTO_INCREMENT '; |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
if (isset($columnAttributes[ 'comment' ])) { |
|
166
|
|
|
$columnStatementLine[] = 'COMMENT ' . $columnAttributes[ 'comment' ]; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
$columnStatements[] = "\t" . implode(' ', $columnStatementLine); |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
// Keys Statement |
|
174
|
|
|
$keyStatements = []; |
|
175
|
|
|
$constraintStatements = []; |
|
176
|
|
|
|
|
177
|
|
|
// Primary Key Statement |
|
178
|
|
|
if (count($primaryKeys)) { |
|
179
|
|
|
$keyStatements[] = 'PRIMARY KEY (' . implode(', ', |
|
180
|
|
|
$primaryKeys) . ')' . (count($primaryKeys) >= 2 ? ' USING BTREE' : ''); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
// Unique Key Statement |
|
184
|
|
|
if (count($uniqueKeys)) { |
|
185
|
|
|
if (count($uniqueKeys) == 1) { |
|
186
|
|
|
$constraintStatements[] = 'UNIQUE (' . implode(',', $uniqueKeys) . ')'; |
|
187
|
|
|
} else { |
|
188
|
|
|
$uniqueName = 'idx_' . implode('_', $uniqueKeys); |
|
189
|
|
|
$constraintStatements[] = 'CONSTRAINT ' . $uniqueName . ' UNIQUE (' . implode(',', $uniqueKeys) . ')'; |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
// Foreign Keys Statement |
|
194
|
|
|
if (count($foreignKeys)) { |
|
195
|
|
|
foreach ($foreignKeys as $foreignKeyColumnName => $foreignKeyAttributes) { |
|
196
|
|
|
if (empty($foreignKeyAttributes[ 'name' ])) { |
|
197
|
|
|
$foreignKeyAttributes[ 'name' ] = 'fk_' . $foreignKeyColumnName; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
if (isset($foreignKeyAttributes[ 'references' ])) { |
|
201
|
|
|
$keyStatements[] = 'KEY ' . |
|
202
|
|
|
$this->conn->escapeIdentifiers($foreignKeyAttributes[ 'name' ]) . |
|
|
|
|
|
|
203
|
|
|
' (' . $this->conn->escapeIdentifiers($foreignKeyColumnName) . ')'; |
|
|
|
|
|
|
204
|
|
|
|
|
205
|
|
|
$referenceParts = array_map('trim', explode('.', $foreignKeyAttributes[ 'references' ])); |
|
206
|
|
|
list($referenceTable, $referenceColumn) = $referenceParts; |
|
207
|
|
|
|
|
208
|
|
|
$referenceOnDelete = 'NO ACTION'; |
|
209
|
|
|
$referenceOnUpdate = 'NO ACTION'; |
|
210
|
|
|
|
|
211
|
|
|
$validReferenceActions = [ |
|
212
|
|
|
'NO ACTION', |
|
213
|
|
|
'CASCADE', |
|
214
|
|
|
'RESTRICT', |
|
215
|
|
|
'SET NULL', |
|
216
|
|
|
]; |
|
217
|
|
|
|
|
218
|
|
|
if (isset($foreignKeyAttributes[ 'on_delete' ])) { |
|
219
|
|
|
if (in_array($foreignKeyAttributes[ 'on_delete' ], $validReferenceActions)) { |
|
220
|
|
|
$referenceOnDelete = $foreignKeyAttributes[ 'on_delete' ]; |
|
221
|
|
|
} |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
if (isset($foreignKeyAttributes[ 'on_update' ])) { |
|
225
|
|
|
if (in_array($foreignKeyAttributes[ 'on_update' ], $validReferenceActions)) { |
|
226
|
|
|
$referenceOnUpdate = $foreignKeyAttributes[ 'on_update' ]; |
|
227
|
|
|
} |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
$constraintStatements[] = 'CONSTRAINT ' . |
|
231
|
|
|
$this->conn->escapeIdentifiers($foreignKeyAttributes[ 'name' ]) . |
|
232
|
|
|
' FOREIGN KEY (' . $this->conn->escapeIdentifiers($foreignKeyColumnName) . ') REFERENCES ' . |
|
233
|
|
|
$this->conn->escapeIdentifiers($referenceTable) . ' (' . $this->conn->escapeIdentifiers($referenceColumn) . |
|
|
|
|
|
|
234
|
|
|
') ON DELETE ' . $referenceOnDelete . ' ON UPDATE ' . $referenceOnUpdate; |
|
235
|
|
|
} |
|
236
|
|
|
} |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
$statementLines[] = implode(',' . PHP_EOL, |
|
240
|
|
|
array_merge($columnStatements, $keyStatements, $constraintStatements)); |
|
241
|
|
|
|
|
242
|
|
|
if (empty($attributes)) { |
|
243
|
|
|
$attributes[ 'engine' ] = 'InnoDB'; |
|
244
|
|
|
$attributes[ 'default' ] = 'DEFAULT'; |
|
245
|
|
|
$attributes[ 'charset' ] = $this->conn->getConfig('charset'); |
|
246
|
|
|
$attributes[ 'collate' ] = $this->conn->getConfig('collate'); |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
$attributeStatements = []; |
|
250
|
|
|
foreach ($attributes as $key => $value) { |
|
251
|
|
|
$key = strtoupper(dash($key)); |
|
252
|
|
|
|
|
253
|
|
|
if ($key === 'DEFAULT') { |
|
254
|
|
|
$attributeStatements[] = $key; |
|
255
|
|
|
} else { |
|
256
|
|
|
$attributeStatements[] = $key . '=' . $value; |
|
257
|
|
|
} |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
$statementLines[] = ') ' . implode(' ', $attributeStatements); |
|
261
|
|
|
|
|
262
|
|
|
return implode(PHP_EOL, $statementLines); |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
// ------------------------------------------------------------------------ |
|
266
|
|
|
|
|
267
|
|
|
/** |
|
268
|
|
|
* Forge::platformDropTableStatement |
|
269
|
|
|
* |
|
270
|
|
|
* @param string $table |
|
271
|
|
|
* |
|
272
|
|
|
* @return string |
|
273
|
|
|
*/ |
|
274
|
|
|
public function platformDropTableStatement($table) |
|
275
|
|
|
{ |
|
276
|
|
|
return 'DROP TABLE ' . $this->conn->escapeIdentifiers($table); |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
// ------------------------------------------------------------------------ |
|
280
|
|
|
|
|
281
|
|
|
/** |
|
282
|
|
|
* AbstractForge::platformTruncateTableStatement |
|
283
|
|
|
* |
|
284
|
|
|
* @param string $table |
|
285
|
|
|
* |
|
286
|
|
|
* @return mixed |
|
287
|
|
|
*/ |
|
288
|
|
|
protected function platformTruncateTableStatement($table) |
|
289
|
|
|
{ |
|
290
|
|
|
return 'TRUNCATE TABLE ' . $this->conn->escapeIdentifiers($table); |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
// ------------------------------------------------------------------------ |
|
294
|
|
|
|
|
295
|
|
|
/** |
|
296
|
|
|
* Forge::platformAlterTableStatement |
|
297
|
|
|
* |
|
298
|
|
|
* @param string $table |
|
299
|
|
|
* @param string $column |
|
300
|
|
|
* @param array $attributes |
|
301
|
|
|
* @param string $action |
|
302
|
|
|
* |
|
303
|
|
|
* @return string |
|
304
|
|
|
*/ |
|
305
|
|
|
public function platformAlterTableStatement($table, $column, array $attributes, $action) |
|
306
|
|
|
{ |
|
307
|
|
|
$statementLines[] = 'ALTER TABLE ' . $this->conn->escapeIdentifiers($table); |
|
|
|
|
|
|
308
|
|
|
|
|
309
|
|
|
$alterTableStatementStrings[] = strtoupper($action); |
|
|
|
|
|
|
310
|
|
|
$alterTableStatementStrings[] = $this->conn->escapeIdentifiers($column); |
|
311
|
|
|
|
|
312
|
|
|
if (isset($attributes[ 'type' ])) { |
|
313
|
|
|
if ($attributes[ 'type' ] === 'ENUM') { |
|
314
|
|
|
if (empty($attributes[ 'value' ])) { |
|
315
|
|
|
return false; |
|
|
|
|
|
|
316
|
|
|
} else { |
|
317
|
|
|
if (is_string($attributes[ 'value' ])) { |
|
318
|
|
|
$attributes[ 'value' ] = explode(',', $attributes[ 'value' ]); |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
$attributes[ 'value' ] = array_map(function ($value) { |
|
322
|
|
|
return $this->conn->escape(str_replace('\'', '', trim($value))); |
|
323
|
|
|
}, $attributes[ 'value' ]); |
|
324
|
|
|
|
|
325
|
|
|
$alterTableStatementStrings[] = $attributes[ 'type' ] . '(' . |
|
326
|
|
|
implode(',', $attributes[ 'value' ]) |
|
327
|
|
|
. ')'; |
|
328
|
|
|
} |
|
329
|
|
|
} elseif (isset($attributes[ 'length' ])) { |
|
330
|
|
|
$alterTableStatementStrings[] = $attributes[ 'type' ] . '(' . $attributes[ 'length' ] . ')'; |
|
331
|
|
|
} else { |
|
332
|
|
|
$alterTableStatementStrings[] = $attributes[ 'type' ]; |
|
333
|
|
|
} |
|
334
|
|
|
|
|
335
|
|
|
if (isset($attributes[ 'unsigned' ])) { |
|
336
|
|
|
if ($attributes[ 'unsigned' ] === true) { |
|
337
|
|
|
if (in_array($attributes[ 'type' ], |
|
338
|
|
|
['INT', 'BIGINT', 'SMALLINT', 'TINYINT', 'FLOAT', 'DECIMAL', 'REAL'])) { |
|
339
|
|
|
$alterTableStatementStrings[] = 'UNSIGNED'; |
|
340
|
|
|
} |
|
341
|
|
|
} |
|
342
|
|
|
} |
|
343
|
|
|
|
|
344
|
|
|
if (isset($attributes[ 'collate' ])) { |
|
345
|
|
|
$alterTableStatementStrings[] = 'COLLATE ' . $attributes[ 'collate' ]; |
|
346
|
|
|
} elseif (in_array($attributes[ 'type' ], |
|
347
|
|
|
['CHAR', 'VARCHAR', 'TEXT', 'LONGTEXT', 'TINYTEXT', 'ENUM'])) { |
|
348
|
|
|
$alterTableStatementStrings[] = 'COLLATE ' . $this->conn->getConfig('collate'); |
|
349
|
|
|
} |
|
350
|
|
|
|
|
351
|
|
|
if (isset($attributes[ 'not_null' ])) { |
|
352
|
|
|
if ($attributes[ 'not_null' ] === true) { |
|
353
|
|
|
$alterTableStatementStrings[] = 'NOT NULL'; |
|
354
|
|
|
} |
|
355
|
|
|
} |
|
356
|
|
|
|
|
357
|
|
|
if (isset($attributes[ 'default' ])) { |
|
358
|
|
|
$alterTableStatementStrings[] = 'DEFAULT ' . $this->conn->escape($attributes[ 'default' ]); |
|
359
|
|
|
} |
|
360
|
|
|
|
|
361
|
|
|
if (isset($attributes[ 'auto_increment' ])) { |
|
362
|
|
|
if ($attributes[ 'auto_increment' ] === true) { |
|
363
|
|
|
$alterTableStatementStrings[] = 'AUTO_INCREMENT '; |
|
364
|
|
|
} |
|
365
|
|
|
} |
|
366
|
|
|
|
|
367
|
|
|
if (isset($attributes[ 'comment' ])) { |
|
368
|
|
|
$alterTableStatementStrings[] = 'COMMENT ' . $attributes[ 'comment' ]; |
|
369
|
|
|
} |
|
370
|
|
|
|
|
371
|
|
|
$statementLines[] = implode(' ', $alterTableStatementStrings) . ';'; |
|
372
|
|
|
} |
|
373
|
|
|
|
|
374
|
|
|
return implode(PHP_EOL, $statementLines); |
|
375
|
|
|
} |
|
376
|
|
|
|
|
377
|
|
|
// ------------------------------------------------------------------------ |
|
378
|
|
|
|
|
379
|
|
|
/** |
|
380
|
|
|
* Forge::platformAlterTableDropColumnStatement |
|
381
|
|
|
* |
|
382
|
|
|
* @param string $table |
|
383
|
|
|
* @param string $column |
|
384
|
|
|
* |
|
385
|
|
|
* @return string |
|
386
|
|
|
*/ |
|
387
|
|
|
protected function platformAlterTableDropColumnStatement($table, $column) |
|
388
|
|
|
{ |
|
389
|
|
|
$statementLines[] = 'ALTER TABLE ' . $this->conn->escapeIdentifiers($table); |
|
|
|
|
|
|
390
|
|
|
$statementLines[] = 'DROP COLUMN ' . $this->conn->escapeIdentifiers($column); |
|
391
|
|
|
|
|
392
|
|
|
return implode(PHP_EOL, $statementLines); |
|
393
|
|
|
} |
|
394
|
|
|
|
|
395
|
|
|
// ------------------------------------------------------------------------ |
|
396
|
|
|
|
|
397
|
|
|
/** |
|
398
|
|
|
* Forge::platformAlterTablePrimaryKeysStatement |
|
399
|
|
|
* |
|
400
|
|
|
* @param string $table |
|
401
|
|
|
* @param array $columns |
|
402
|
|
|
* |
|
403
|
|
|
* @return mixed |
|
404
|
|
|
*/ |
|
405
|
|
|
protected function platformAlterTablePrimaryKeysStatement($table, array $columns) |
|
406
|
|
|
{ |
|
407
|
|
|
$statementLines[] = 'ALTER TABLE ' . $this->conn->escapeIdentifiers($table); |
|
|
|
|
|
|
408
|
|
|
|
|
409
|
|
|
$keys = array_map(function ($column) { |
|
410
|
|
|
return $this->conn->escapeIdentifiers($column); |
|
411
|
|
|
}, $columns); |
|
412
|
|
|
|
|
413
|
|
|
if (count($columns) == 1) { |
|
414
|
|
|
$statementLines[] = 'ADD PRIMARY KEY (' . implode(',', $keys) . ');'; |
|
415
|
|
|
} else { |
|
416
|
|
|
$statementLines[] = 'ADD CONSTRAINT ' . |
|
417
|
|
|
$this->conn->escapeIdentifiers('pk_' . implode('_', $columns)) . |
|
418
|
|
|
' PRIMARY KEY (' . implode(',', $keys) . ');'; |
|
419
|
|
|
} |
|
420
|
|
|
|
|
421
|
|
|
return implode(PHP_EOL, $statementLines); |
|
422
|
|
|
} |
|
423
|
|
|
|
|
424
|
|
|
// ------------------------------------------------------------------------ |
|
425
|
|
|
|
|
426
|
|
|
/** |
|
427
|
|
|
* Forge::platformAlterTableForeignKeyStatement |
|
428
|
|
|
* |
|
429
|
|
|
* @param string $table |
|
430
|
|
|
* @param string $column |
|
431
|
|
|
* @param string $references |
|
432
|
|
|
* |
|
433
|
|
|
* @return string |
|
434
|
|
|
*/ |
|
435
|
|
|
protected function platformAlterTableForeignKeyStatement($table, $column, $references) |
|
436
|
|
|
{ |
|
437
|
|
|
$statementLines[] = 'ALTER TABLE ' . $this->conn->escapeIdentifiers($table); |
|
|
|
|
|
|
438
|
|
|
$statementLines[] = 'ADD CONSTRAINT ' . $this->conn->escapeIdentifiers('fk_' . $column); |
|
439
|
|
|
|
|
440
|
|
|
$referenceParts = array_map('trim', explode('.', $references)); |
|
441
|
|
|
list($referenceTable, $referenceColumn) = $referenceParts; |
|
442
|
|
|
|
|
443
|
|
|
$statementLines[] = 'FOREIGN KEY (' . $this->conn->escapeIdentifiers($column) . ') REFERENCES ' . |
|
444
|
|
|
$this->conn->escapeIdentifiers($referenceTable) . '(' . |
|
|
|
|
|
|
445
|
|
|
$this->conn->escapeIdentifiers($referenceColumn) . ')'; |
|
|
|
|
|
|
446
|
|
|
|
|
447
|
|
|
return implode(PHP_EOL, $statementLines); |
|
448
|
|
|
} |
|
449
|
|
|
|
|
450
|
|
|
// ------------------------------------------------------------------------ |
|
451
|
|
|
|
|
452
|
|
|
/** |
|
453
|
|
|
* Forge::platformDropTableForeignKeyStatement |
|
454
|
|
|
* |
|
455
|
|
|
* @param string $table |
|
456
|
|
|
* @param string $column |
|
457
|
|
|
* |
|
458
|
|
|
* @return string |
|
459
|
|
|
*/ |
|
460
|
|
|
protected function platformDropTableForeignKeyStatement($table, $column) |
|
461
|
|
|
{ |
|
462
|
|
|
$statementLines[] = 'ALTER TABLE ' . $this->conn->escapeIdentifiers($table); |
|
|
|
|
|
|
463
|
|
|
$statementLines[] = 'DROP FOREIGN KEY ' . $this->conn->escapeIdentifiers('fk_' . $column); |
|
464
|
|
|
|
|
465
|
|
|
return implode(PHP_EOL, $statementLines); |
|
466
|
|
|
} |
|
467
|
|
|
|
|
468
|
|
|
// ------------------------------------------------------------------------ |
|
469
|
|
|
|
|
470
|
|
|
/** |
|
471
|
|
|
* Forge::platformAlterTableUniquesStatement |
|
472
|
|
|
* |
|
473
|
|
|
* @param string $table |
|
474
|
|
|
* @param array $columns |
|
475
|
|
|
* |
|
476
|
|
|
* @return string |
|
477
|
|
|
*/ |
|
478
|
|
|
protected function platformAlterTableUniquesStatement($table, array $columns) |
|
479
|
|
|
{ |
|
480
|
|
|
$statementLines[] = 'ALTER TABLE ' . $this->conn->escapeIdentifiers($table); |
|
|
|
|
|
|
481
|
|
|
|
|
482
|
|
|
$keys = array_map(function ($column) { |
|
483
|
|
|
return $this->conn->escapeIdentifiers($column); |
|
484
|
|
|
}, $columns); |
|
485
|
|
|
|
|
486
|
|
|
if (count($columns) == 1) { |
|
487
|
|
|
$statementLines[] = 'ADD UNIQUE (' . implode(',', $keys) . ');'; |
|
488
|
|
|
} else { |
|
489
|
|
|
$statementLines[] = 'ADD CONSTRAINT ' . |
|
490
|
|
|
$this->conn->escapeIdentifiers('idx_' . implode('_', $columns)) . |
|
491
|
|
|
' UNIQUE (' . implode(',', $keys) . ');'; |
|
492
|
|
|
} |
|
493
|
|
|
|
|
494
|
|
|
return implode(PHP_EOL, $statementLines); |
|
495
|
|
|
} |
|
496
|
|
|
|
|
497
|
|
|
// ------------------------------------------------------------------------ |
|
498
|
|
|
|
|
499
|
|
|
/** |
|
500
|
|
|
* Forge::platformCreateTableIndexesStatement |
|
501
|
|
|
* |
|
502
|
|
|
* @param string $table |
|
503
|
|
|
* @param array $columns |
|
504
|
|
|
* @param bool $unique |
|
505
|
|
|
* |
|
506
|
|
|
* @return string |
|
507
|
|
|
*/ |
|
508
|
|
|
protected function platformCreateTableIndexesStatement($table, array $columns, $unique = false) |
|
509
|
|
|
{ |
|
510
|
|
|
$keys = array_map(function ($column) { |
|
511
|
|
|
return $this->conn->escapeIdentifiers($column); |
|
512
|
|
|
}, $columns); |
|
513
|
|
|
|
|
514
|
|
|
$statementLines[] = 'CREATE' . ($unique === true ? ' UNIQUE ' : ' ') . |
|
|
|
|
|
|
515
|
|
|
'INDEX ' . $this->conn->escapeIdentifiers('idx_' . implode('_', $columns)); |
|
516
|
|
|
$statementLines[] = 'ON ' . $this->conn->escapeIdentifiers($table) . ' (' . implode(', ', $keys) . ');'; |
|
517
|
|
|
|
|
518
|
|
|
return implode(PHP_EOL, $statementLines); |
|
519
|
|
|
} |
|
520
|
|
|
|
|
521
|
|
|
// ------------------------------------------------------------------------ |
|
522
|
|
|
|
|
523
|
|
|
/** |
|
524
|
|
|
* Forge::platformDropTableIndexesStatement |
|
525
|
|
|
* |
|
526
|
|
|
* @param string $table |
|
527
|
|
|
* @param array $columns |
|
528
|
|
|
* |
|
529
|
|
|
* @return string |
|
530
|
|
|
*/ |
|
531
|
|
|
protected function platformDropTableIndexesStatement($table, $columns) |
|
532
|
|
|
{ |
|
533
|
|
|
$statementLines[] = 'ALTER TABLE ' . $this->conn->escapeIdentifiers($table); |
|
|
|
|
|
|
534
|
|
|
$statementLines[] = 'DROP INDEX ' . $this->conn->escapeIdentifiers('idx_' . implode('_', $columns)); |
|
535
|
|
|
|
|
536
|
|
|
return implode(PHP_EOL, $statementLines); |
|
537
|
|
|
} |
|
538
|
|
|
|
|
539
|
|
|
// ------------------------------------------------------------------------ |
|
540
|
|
|
|
|
541
|
|
|
/** |
|
542
|
|
|
* Forge::platformAlterTableSetColumnDefaultValueStatement |
|
543
|
|
|
* |
|
544
|
|
|
* @param string $table |
|
545
|
|
|
* @param string $column |
|
546
|
|
|
* @param mixed $value |
|
547
|
|
|
* |
|
548
|
|
|
* @return string |
|
549
|
|
|
*/ |
|
550
|
|
|
protected function platformAlterTableSetColumnDefaultValueStatement($table, $column, $value) |
|
551
|
|
|
{ |
|
552
|
|
|
$statementLines[] = 'ALTER TABLE ' . $this->conn->escapeIdentifiers($table); |
|
|
|
|
|
|
553
|
|
|
$statementLines[] = 'ALTER ' . $this->conn->escapeIdentifiers($column) . ' SET DEFAULT ' . $this->conn->escape($value); |
|
554
|
|
|
|
|
555
|
|
|
return implode(PHP_EOL, $statementLines); |
|
556
|
|
|
} |
|
557
|
|
|
|
|
558
|
|
|
// ------------------------------------------------------------------------ |
|
559
|
|
|
|
|
560
|
|
|
/** |
|
561
|
|
|
* Forge::platformAlterTableDropColumnDefaultValue |
|
562
|
|
|
* |
|
563
|
|
|
* @param string $table |
|
564
|
|
|
* @param string $column |
|
565
|
|
|
* |
|
566
|
|
|
* @return string |
|
567
|
|
|
*/ |
|
568
|
|
|
protected function platformAlterTableDropColumnDefaultValueStatement($table, $column) |
|
569
|
|
|
{ |
|
570
|
|
|
$statementLines[] = 'ALTER TABLE ' . $this->conn->escapeIdentifiers($table); |
|
|
|
|
|
|
571
|
|
|
$statementLines[] = 'ALTER ' . $this->conn->escapeIdentifiers($column) . ' DROP DEFAULT;'; |
|
572
|
|
|
|
|
573
|
|
|
return implode(PHP_EOL, $statementLines); |
|
574
|
|
|
} |
|
575
|
|
|
|
|
576
|
|
|
// ------------------------------------------------------------------------ |
|
577
|
|
|
|
|
578
|
|
|
/** |
|
579
|
|
|
* Forge::platformAlterTableCheckStatement |
|
580
|
|
|
* |
|
581
|
|
|
* @param string $table |
|
582
|
|
|
* @param array $conditions |
|
583
|
|
|
* |
|
584
|
|
|
* @return string |
|
585
|
|
|
*/ |
|
586
|
|
|
protected function platformAlterTableCheckStatement($table, array $conditions) |
|
587
|
|
|
{ |
|
588
|
|
|
$statementLines[] = 'ALTER TABLE ' . $this->conn->escapeIdentifiers($table); |
|
|
|
|
|
|
589
|
|
|
|
|
590
|
|
|
$columns = array_keys($conditions); |
|
591
|
|
|
|
|
592
|
|
|
if (count($conditions) == 1) { |
|
593
|
|
|
$statementLines[] = 'ADD CHECK (' . $this->conn->escapeIdentifiers($columns[ 0 ]) . ')'; |
|
|
|
|
|
|
594
|
|
|
} else { |
|
595
|
|
|
$conditionStatementStrings = []; |
|
596
|
|
|
|
|
597
|
|
|
foreach ($conditions as $column => $condition) { |
|
598
|
|
|
if (preg_match('/\s*(?:<|>|!)?=\s*|\s*<>?\s*|\s*>\s*/i', $column, $match, PREG_OFFSET_CAPTURE)) { |
|
599
|
|
|
$operator = trim($match[ 0 ]); |
|
600
|
|
|
$column = trim(str_replace($operator, '', $column)); |
|
601
|
|
|
|
|
602
|
|
|
$conditionStatementStrings[] = $this->conn->escapeIdentifiers($column) . $operator . $this->conn->escape($condition); |
|
603
|
|
|
} else { |
|
604
|
|
|
$conditionStatementStrings[] = $this->conn->escapeIdentifiers($column) . '=' . $this->conn->escape($condition); |
|
|
|
|
|
|
605
|
|
|
} |
|
606
|
|
|
} |
|
607
|
|
|
|
|
608
|
|
|
$statementLines[] = 'ADD CONSTRAINT ' . |
|
609
|
|
|
$this->conn->escapeIdentifiers('chk_' . implode('_', $columns)) . |
|
610
|
|
|
' (' . implode(' AND ', $conditionStatementStrings) . ');'; |
|
611
|
|
|
} |
|
612
|
|
|
|
|
613
|
|
|
return implode(PHP_EOL, $statementLines); |
|
614
|
|
|
} |
|
615
|
|
|
|
|
616
|
|
|
// ------------------------------------------------------------------------ |
|
617
|
|
|
|
|
618
|
|
|
/** |
|
619
|
|
|
* Forge::platformAlterTableDropCheckStatement |
|
620
|
|
|
* |
|
621
|
|
|
* @param string $table |
|
622
|
|
|
* @param array $columns |
|
623
|
|
|
* |
|
624
|
|
|
* @return mixed |
|
625
|
|
|
*/ |
|
626
|
|
|
protected function platformAlterTableDropCheckStatement($table, array $columns) |
|
627
|
|
|
{ |
|
628
|
|
|
$statementLines[] = 'ALTER TABLE ' . $this->conn->escapeIdentifiers($table); |
|
|
|
|
|
|
629
|
|
|
$statementLines[] = 'DROP CHECK ' . $this->conn->escapeIdentifiers('chk_' . implode('_', $columns)) . ';'; |
|
630
|
|
|
|
|
631
|
|
|
return implode(PHP_EOL, $statementLines); |
|
632
|
|
|
} |
|
633
|
|
|
|
|
634
|
|
|
// ------------------------------------------------------------------------ |
|
635
|
|
|
|
|
636
|
|
|
/** |
|
637
|
|
|
* Forge::platformCreateViewStatement |
|
638
|
|
|
* |
|
639
|
|
|
* @param string $name |
|
640
|
|
|
* @param string $query |
|
641
|
|
|
* @param bool $force |
|
642
|
|
|
* |
|
643
|
|
|
* @return string |
|
644
|
|
|
*/ |
|
645
|
|
|
protected function platformCreateViewStatement($name, $query, $force = false) |
|
646
|
|
|
{ |
|
647
|
|
|
if ($force === true) { |
|
648
|
|
|
$statementLines[] = 'CREATE VIEW OR REPLACE VIEW ' . $this->conn->escapeIdentifiers('view_' . $name) . ' AS'; |
|
|
|
|
|
|
649
|
|
|
} else { |
|
650
|
|
|
$statementLines[] = 'CREATE VIEW ' . $this->conn->escapeIdentifiers('view_' . $name) . ' AS'; |
|
651
|
|
|
} |
|
652
|
|
|
|
|
653
|
|
|
$statementLines[] = $query; |
|
654
|
|
|
|
|
655
|
|
|
return implode(PHP_EOL, $statementLines); |
|
656
|
|
|
} |
|
657
|
|
|
|
|
658
|
|
|
// ------------------------------------------------------------------------ |
|
659
|
|
|
|
|
660
|
|
|
/** |
|
661
|
|
|
* Forge::platformDropViewStatement |
|
662
|
|
|
* |
|
663
|
|
|
* @param string $name |
|
664
|
|
|
* |
|
665
|
|
|
* @return string |
|
666
|
|
|
*/ |
|
667
|
|
|
protected function platformDropViewStatement($name) |
|
668
|
|
|
{ |
|
669
|
|
|
return 'DROP VIEW ' . $this->conn->escapeIdentifiers('view_' . $name) . ';'; |
|
670
|
|
|
} |
|
671
|
|
|
} |