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\Abstracts; |
15
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------ |
17
|
|
|
|
18
|
|
|
use O2System\Spl\Exceptions\RuntimeException; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class AbstractForge |
22
|
|
|
* @package O2System\Database\Sql\Abstracts |
23
|
|
|
*/ |
24
|
|
|
abstract class AbstractForge |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* AbstractForge::$conn |
28
|
|
|
* |
29
|
|
|
* Query Builder database connection instance. |
30
|
|
|
* |
31
|
|
|
* @var AbstractConnection |
32
|
|
|
*/ |
33
|
|
|
protected $conn; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* AbstractForge::$unsignedSupportColumnTypes |
37
|
|
|
* |
38
|
|
|
* UNSIGNED support |
39
|
|
|
* |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
protected $unsignedSupportColumnTypes = []; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* AbstractForge::$quotedTableOptions |
46
|
|
|
* |
47
|
|
|
* Table Options list which required to be quoted |
48
|
|
|
* |
49
|
|
|
* @var array |
50
|
|
|
*/ |
51
|
|
|
protected $quotedTableOptions = []; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* AbstractForge::$nullStatement |
55
|
|
|
* |
56
|
|
|
* NULL value representation in CREATE/ALTER TABLE statements |
57
|
|
|
* |
58
|
|
|
* @var string |
59
|
|
|
*/ |
60
|
|
|
protected $nullStatement = 'NULL'; |
61
|
|
|
|
62
|
|
|
//-------------------------------------------------------------------- |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* AbstractQueryBuilder::__construct |
66
|
|
|
* |
67
|
|
|
* @param \O2System\Database\Sql\Abstracts\AbstractConnection $conn |
68
|
|
|
*/ |
69
|
|
|
public function __construct(AbstractConnection &$conn) |
70
|
|
|
{ |
71
|
|
|
$this->conn =& $conn; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
// ------------------------------------------------------------------------ |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* AbstractForge::createDatabase |
78
|
|
|
* |
79
|
|
|
* @param string $database |
80
|
|
|
* |
81
|
|
|
* @return bool |
82
|
|
|
* @throws \O2System\Spl\Exceptions\RuntimeException |
83
|
|
|
* @throws \Psr\Cache\InvalidArgumentException |
84
|
|
|
*/ |
85
|
|
|
public function createDatabase($database) |
86
|
|
|
{ |
87
|
|
|
if (false !== ($statement = $this->platformCreateDatabaseStatement($database))) { |
|
|
|
|
88
|
|
|
if ( ! $this->conn->query($statement) and $this->conn->debugEnable) { |
89
|
|
|
throw new RuntimeException('Unable to create the specified database.'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return true; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if ($this->conn->debugEnable) { |
96
|
|
|
throw new RuntimeException('This feature is not available for the database you are using.'); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return false; |
100
|
|
|
} |
101
|
|
|
//-------------------------------------------------------------------- |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* AbstractForge::platformCreateDatabaseStatement |
105
|
|
|
* |
106
|
|
|
* @param string $database |
107
|
|
|
* |
108
|
|
|
* @return string |
109
|
|
|
*/ |
110
|
|
|
abstract public function platformCreateDatabaseStatement($database); |
111
|
|
|
|
112
|
|
|
//-------------------------------------------------------------------- |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* AbstractForge::dropDatabase |
116
|
|
|
* |
117
|
|
|
* @param string $database |
118
|
|
|
* |
119
|
|
|
* @return bool |
120
|
|
|
* @throws \O2System\Spl\Exceptions\RuntimeException |
121
|
|
|
* @throws \Psr\Cache\InvalidArgumentException |
122
|
|
|
*/ |
123
|
|
|
public function dropDatabase($database) |
124
|
|
|
{ |
125
|
|
|
if (false !== ($statement = $this->platformDropDatabaseStatement($database))) { |
|
|
|
|
126
|
|
|
if ( ! $this->conn->query($statement) and $this->conn->debugEnable) { |
127
|
|
|
throw new RuntimeException('Unable to drop the specified database.'); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return true; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
if ($this->conn->debugEnable) { |
134
|
|
|
throw new RuntimeException('This feature is not available for the database you are using.'); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return false; |
138
|
|
|
} |
139
|
|
|
//-------------------------------------------------------------------- |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* AbstractForge::platformDropDatabaseStatement |
143
|
|
|
* |
144
|
|
|
* @param string $database |
145
|
|
|
* |
146
|
|
|
* @return string |
147
|
|
|
*/ |
148
|
|
|
abstract public function platformDropDatabaseStatement($database); |
149
|
|
|
|
150
|
|
|
//-------------------------------------------------------------------- |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* AbstractForge::backupDatabase |
154
|
|
|
* |
155
|
|
|
* @param string $database |
156
|
|
|
* @param string $backupFilePath |
157
|
|
|
* |
158
|
|
|
* @return bool |
159
|
|
|
* @throws \O2System\Spl\Exceptions\RuntimeException |
160
|
|
|
* @throws \Psr\Cache\InvalidArgumentException |
161
|
|
|
*/ |
162
|
|
|
public function backupDatabase($database, $backupFilePath) |
163
|
|
|
{ |
164
|
|
|
if (false !== ($statement = $this->platformBackupDatabaseStatement($database, $backupFilePath))) { |
|
|
|
|
165
|
|
|
if ( ! $this->conn->query($statement) and $this->conn->debugEnable) { |
166
|
|
|
throw new RuntimeException('Unable to backup the specified database.'); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return true; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
if ($this->conn->debugEnable) { |
173
|
|
|
throw new RuntimeException('This feature is not available for the database you are using.'); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
return false; |
177
|
|
|
} |
178
|
|
|
//-------------------------------------------------------------------- |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* AbstractForge::platformBackupDatabaseStatement |
182
|
|
|
* |
183
|
|
|
* @param string $database |
184
|
|
|
* @param string $backupFilePath |
185
|
|
|
* |
186
|
|
|
* @return string |
187
|
|
|
*/ |
188
|
|
|
abstract public function platformBackupDatabaseStatement($database, $backupFilePath); |
189
|
|
|
|
190
|
|
|
//-------------------------------------------------------------------- |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* AbstractForge::prepareTableNameStatement |
194
|
|
|
* |
195
|
|
|
* @param string $table |
196
|
|
|
* |
197
|
|
|
* @return string |
198
|
|
|
*/ |
199
|
|
|
public function prepareTableNameStatement($table) |
200
|
|
|
{ |
201
|
|
|
$prefix = $this->conn->getConfig('tablePrefix'); |
202
|
|
|
$table = str_replace($prefix, '', $table); |
|
|
|
|
203
|
|
|
|
204
|
|
|
return $prefix . $table; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
//-------------------------------------------------------------------- |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* AbstractForge::runTableStatementQuery |
211
|
|
|
* |
212
|
|
|
* @param string $statement |
213
|
|
|
* @param string $errorMessage |
214
|
|
|
* |
215
|
|
|
* @return bool |
216
|
|
|
* @throws \O2System\Spl\Exceptions\RuntimeException |
217
|
|
|
* @throws \Psr\Cache\InvalidArgumentException |
218
|
|
|
*/ |
219
|
|
|
protected function runTableStatementQuery($statement, $errorMessage) |
220
|
|
|
{ |
221
|
|
|
if ($statement !== false) { |
|
|
|
|
222
|
|
|
if ($this->conn->query($statement)) { |
223
|
|
|
return true; |
224
|
|
|
} elseif ($this->conn->debugEnable) { |
225
|
|
|
throw new RuntimeException($errorMessage); |
226
|
|
|
} |
227
|
|
|
} elseif ($this->conn->debugEnable) { |
228
|
|
|
throw new RuntimeException('This feature is not available for the database you are using.'); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
return false; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
//-------------------------------------------------------------------- |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* AbstractForge::createTable |
238
|
|
|
* |
239
|
|
|
* @param string $table |
240
|
|
|
* @param array $columns |
241
|
|
|
* @param bool $force |
242
|
|
|
* @param array $attributes |
243
|
|
|
* |
244
|
|
|
* @return bool |
245
|
|
|
* @throws \O2System\Spl\Exceptions\RuntimeException |
246
|
|
|
* @throws \Psr\Cache\InvalidArgumentException |
247
|
|
|
*/ |
248
|
|
|
public function createTable($table, array $columns = [], $force = false, array $attributes = []) |
249
|
|
|
{ |
250
|
|
|
return $this->runTableStatementQuery( |
251
|
|
|
$this->platformCreateTableStatement( |
252
|
|
|
$this->prepareTableNameStatement($table), |
253
|
|
|
$columns, |
254
|
|
|
$force, |
255
|
|
|
$attributes |
256
|
|
|
), |
257
|
|
|
'Unable to create table on the specified database' |
258
|
|
|
); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
//-------------------------------------------------------------------- |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* AbstractForge::platformCreateTableStatement |
265
|
|
|
* |
266
|
|
|
* @param string $table |
267
|
|
|
* @param array $columns |
268
|
|
|
* @param bool $force |
269
|
|
|
* @param array $attributes |
270
|
|
|
* |
271
|
|
|
* @return mixed |
272
|
|
|
*/ |
273
|
|
|
abstract public function platformCreateTableStatement( |
274
|
|
|
$table, |
275
|
|
|
array $columns = [], |
276
|
|
|
$force = false, |
277
|
|
|
array $attributes = [] |
278
|
|
|
); |
279
|
|
|
|
280
|
|
|
//-------------------------------------------------------------------- |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* AbstractForge::dropTable |
284
|
|
|
* |
285
|
|
|
* @param string $table |
286
|
|
|
* |
287
|
|
|
* @return bool |
288
|
|
|
* @throws \O2System\Spl\Exceptions\RuntimeException |
289
|
|
|
* @throws \Psr\Cache\InvalidArgumentException |
290
|
|
|
*/ |
291
|
|
|
public function dropTable($table) |
292
|
|
|
{ |
293
|
|
|
return $this->runTableStatementQuery( |
294
|
|
|
$this->platformDropDatabaseStatement( |
295
|
|
|
$this->prepareTableNameStatement($table) |
296
|
|
|
), |
297
|
|
|
'Unable to drop the specified database table.' |
298
|
|
|
); |
299
|
|
|
} |
300
|
|
|
//-------------------------------------------------------------------- |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* AbstractForge::platformDropTableStatement |
304
|
|
|
* |
305
|
|
|
* @param string $table |
306
|
|
|
* |
307
|
|
|
* @return string |
308
|
|
|
*/ |
309
|
|
|
abstract public function platformDropTableStatement($table); |
310
|
|
|
|
311
|
|
|
//-------------------------------------------------------------------- |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* AbstractForge::truncateTable |
315
|
|
|
* |
316
|
|
|
* @param string $table |
317
|
|
|
* |
318
|
|
|
* @return bool |
319
|
|
|
* @throws \O2System\Spl\Exceptions\RuntimeException |
320
|
|
|
* @throws \Psr\Cache\InvalidArgumentException |
321
|
|
|
*/ |
322
|
|
|
public function truncateTable($table) |
323
|
|
|
{ |
324
|
|
|
return $this->runTableStatementQuery( |
325
|
|
|
$this->platformTruncateTableStatement( |
326
|
|
|
$this->prepareTableNameStatement($table) |
327
|
|
|
), |
328
|
|
|
'Unable to truncate the specified database table.' |
329
|
|
|
); |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
//-------------------------------------------------------------------- |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* AbstractForge::platformTruncateTableStatement |
336
|
|
|
* |
337
|
|
|
* @param string $table |
338
|
|
|
* |
339
|
|
|
* @return mixed |
340
|
|
|
*/ |
341
|
|
|
abstract protected function platformTruncateTableStatement($table); |
342
|
|
|
|
343
|
|
|
//-------------------------------------------------------------------- |
344
|
|
|
|
345
|
|
|
/** |
346
|
|
|
* AbstractForge::addTableColumn |
347
|
|
|
* |
348
|
|
|
* @param string $table |
349
|
|
|
* @param string $column |
350
|
|
|
* @param array $attributes |
351
|
|
|
* |
352
|
|
|
* @return bool|\O2System\Database\DataObjects\Result |
353
|
|
|
* @throws \O2System\Spl\Exceptions\RuntimeException |
354
|
|
|
* @throws \Psr\Cache\InvalidArgumentException |
355
|
|
|
*/ |
356
|
|
|
public function addTableColumn($table, $column, array $attributes) |
357
|
|
|
{ |
358
|
|
|
return $this->runTableStatementQuery( |
359
|
|
|
$this->platformAlterTableStatement( |
360
|
|
|
$this->prepareTableNameStatement($table), |
361
|
|
|
$column, |
362
|
|
|
$attributes, |
363
|
|
|
'ADD' |
364
|
|
|
), |
365
|
|
|
'Unable to add column to specified database table' |
366
|
|
|
); |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
//-------------------------------------------------------------------- |
370
|
|
|
|
371
|
|
|
/** |
372
|
|
|
* AbstractForge::modifyTableColumn |
373
|
|
|
* |
374
|
|
|
* @param string $table |
375
|
|
|
* @param string $column |
376
|
|
|
* @param array $attributes |
377
|
|
|
* |
378
|
|
|
* @return bool|\O2System\Database\DataObjects\Result |
379
|
|
|
* @throws \O2System\Spl\Exceptions\RuntimeException |
380
|
|
|
* @throws \Psr\Cache\InvalidArgumentException |
381
|
|
|
*/ |
382
|
|
|
public function modifyTableColumn($table, $column, array $attributes) |
383
|
|
|
{ |
384
|
|
|
$table = $this->conn->getConfig('tablePrefix') . $table; |
385
|
|
|
|
386
|
|
|
return $this->runTableStatementQuery( |
387
|
|
|
$this->platformAlterTableStatement( |
388
|
|
|
$this->prepareTableNameStatement($table), |
389
|
|
|
$column, |
390
|
|
|
$attributes, |
391
|
|
|
'MODIFY' |
392
|
|
|
), |
393
|
|
|
'Unable to modify column on specified database table' |
394
|
|
|
); |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
//-------------------------------------------------------------------- |
398
|
|
|
|
399
|
|
|
/** |
400
|
|
|
* AbstractForge::platformAlterTableStatement |
401
|
|
|
* |
402
|
|
|
* @param string $table |
403
|
|
|
* @param string $column |
404
|
|
|
* @param array $attributes |
405
|
|
|
* @param string $action |
406
|
|
|
* |
407
|
|
|
* @return string |
408
|
|
|
*/ |
409
|
|
|
abstract public function platformAlterTableStatement($table, $column, array $attributes, $action); |
410
|
|
|
|
411
|
|
|
//-------------------------------------------------------------------- |
412
|
|
|
|
413
|
|
|
/** |
414
|
|
|
* AbstractForge::addDropColumn |
415
|
|
|
* |
416
|
|
|
* @param string $table |
417
|
|
|
* @param string $column |
418
|
|
|
* |
419
|
|
|
* @return bool |
420
|
|
|
* @throws \O2System\Spl\Exceptions\RuntimeException |
421
|
|
|
* @throws \Psr\Cache\InvalidArgumentException |
422
|
|
|
*/ |
423
|
|
|
public function dropTableColumn($table, $column) |
424
|
|
|
{ |
425
|
|
|
if (false === ($sqlStatement = $this->platformAlterTableDropColumnStatement($table, $column))) { |
|
|
|
|
426
|
|
|
if ($this->conn->debugEnable) { |
427
|
|
|
throw new RuntimeException('This feature is not available for the database you are using.'); |
428
|
|
|
} |
429
|
|
|
|
430
|
|
|
return false; |
431
|
|
|
} |
432
|
|
|
return $this->runTableStatementQuery( |
433
|
|
|
$this->platformAlterTableDropColumnStatement( |
434
|
|
|
$this->prepareTableNameStatement($table), |
435
|
|
|
$column |
436
|
|
|
), |
437
|
|
|
'Unable to drop column on specified database table' |
438
|
|
|
); |
439
|
|
|
} |
440
|
|
|
|
441
|
|
|
//-------------------------------------------------------------------- |
442
|
|
|
|
443
|
|
|
/** |
444
|
|
|
* AbstractForge::platformAlterTableDropColumnStatement |
445
|
|
|
* |
446
|
|
|
* @param string $table |
447
|
|
|
* @param string $column |
448
|
|
|
* |
449
|
|
|
* @return string |
450
|
|
|
*/ |
451
|
|
|
abstract protected function platformAlterTableDropColumnStatement($table, $column); |
452
|
|
|
|
453
|
|
|
//-------------------------------------------------------------------- |
454
|
|
|
|
455
|
|
|
/** |
456
|
|
|
* AbstractForge::addTablePrimaryKey |
457
|
|
|
* |
458
|
|
|
* @param string $table |
459
|
|
|
* @param string $column |
460
|
|
|
* |
461
|
|
|
* @return bool |
462
|
|
|
* @throws \O2System\Spl\Exceptions\RuntimeException |
463
|
|
|
* @throws \Psr\Cache\InvalidArgumentException |
464
|
|
|
*/ |
465
|
|
|
public function addTablePrimaryKey($table, $column) |
466
|
|
|
{ |
467
|
|
|
return $this->addTablePrimaryKeys($table, [$column]); |
468
|
|
|
} |
469
|
|
|
|
470
|
|
|
//-------------------------------------------------------------------- |
471
|
|
|
|
472
|
|
|
/** |
473
|
|
|
* AbstractForge::addTablePrimaryKeys |
474
|
|
|
* |
475
|
|
|
* @param string $table |
476
|
|
|
* @param array $columns |
477
|
|
|
* |
478
|
|
|
* @return bool |
479
|
|
|
* @throws \O2System\Spl\Exceptions\RuntimeException |
480
|
|
|
* @throws \Psr\Cache\InvalidArgumentException |
481
|
|
|
*/ |
482
|
|
|
public function addTablePrimaryKeys($table, array $columns) |
483
|
|
|
{ |
484
|
|
|
return $this->runTableStatementQuery( |
485
|
|
|
$this->platformAlterTablePrimaryKeysStatement( |
486
|
|
|
$this->prepareTableNameStatement($table), |
487
|
|
|
$columns |
488
|
|
|
), |
489
|
|
|
'Unable to add unique constraint to specified database table' |
490
|
|
|
); |
491
|
|
|
} |
492
|
|
|
|
493
|
|
|
//-------------------------------------------------------------------- |
494
|
|
|
|
495
|
|
|
/** |
496
|
|
|
* AbstractForge::platformAlterTablePrimaryKeysStatement |
497
|
|
|
* |
498
|
|
|
* @param string $table |
499
|
|
|
* @param array $columns |
500
|
|
|
* |
501
|
|
|
* @return mixed |
502
|
|
|
*/ |
503
|
|
|
abstract protected function platformAlterTablePrimaryKeysStatement($table, array $columns); |
504
|
|
|
|
505
|
|
|
//-------------------------------------------------------------------- |
506
|
|
|
|
507
|
|
|
/** |
508
|
|
|
* AbstractForge::addTableForeignKey |
509
|
|
|
* |
510
|
|
|
* @param string $table |
511
|
|
|
* @param string $column |
512
|
|
|
* @param string $references |
513
|
|
|
* |
514
|
|
|
* @return bool |
515
|
|
|
* @throws \O2System\Spl\Exceptions\RuntimeException |
516
|
|
|
* @throws \Psr\Cache\InvalidArgumentException |
517
|
|
|
*/ |
518
|
|
|
public function addTableForeignKey($table, $column, $references) |
519
|
|
|
{ |
520
|
|
|
return $this->runTableStatementQuery( |
521
|
|
|
$this->platformAlterTableForeignKeyStatement( |
522
|
|
|
$this->prepareTableNameStatement($table), |
523
|
|
|
$column, |
524
|
|
|
$references |
525
|
|
|
), |
526
|
|
|
'Unable to add foreign key constraint to specified database table' |
527
|
|
|
); |
528
|
|
|
} |
529
|
|
|
|
530
|
|
|
//-------------------------------------------------------------------- |
531
|
|
|
|
532
|
|
|
/** |
533
|
|
|
* AbstractForge::platformAlterTableForeignKeyStatement |
534
|
|
|
* |
535
|
|
|
* @param string $table |
536
|
|
|
* @param string $column |
537
|
|
|
* @param string $references |
538
|
|
|
* |
539
|
|
|
* @return string |
540
|
|
|
*/ |
541
|
|
|
abstract protected function platformAlterTableForeignKeyStatement($table, $column, $references); |
542
|
|
|
|
543
|
|
|
//-------------------------------------------------------------------- |
544
|
|
|
|
545
|
|
|
/** |
546
|
|
|
* AbstractForge::dropTableForeignKey |
547
|
|
|
* |
548
|
|
|
* @param string $table |
549
|
|
|
* @param string $column |
550
|
|
|
* |
551
|
|
|
* @return bool|\O2System\Database\DataObjects\Result |
552
|
|
|
* @throws \O2System\Spl\Exceptions\RuntimeException |
553
|
|
|
* @throws \Psr\Cache\InvalidArgumentException |
554
|
|
|
*/ |
555
|
|
|
public function dropTableForeignKey($table, $column) |
556
|
|
|
{ |
557
|
|
|
return $this->runTableStatementQuery( |
558
|
|
|
$this->platformDropTableForeignKeyStatement( |
559
|
|
|
$this->prepareTableNameStatement($table), |
560
|
|
|
$column |
561
|
|
|
), |
562
|
|
|
'Unable to drop foreign key on specified database table' |
563
|
|
|
); |
564
|
|
|
} |
565
|
|
|
|
566
|
|
|
//-------------------------------------------------------------------- |
567
|
|
|
|
568
|
|
|
/** |
569
|
|
|
* AbstractForge::platformDropTableForeignKeyStatement |
570
|
|
|
* |
571
|
|
|
* @param string $table |
572
|
|
|
* @param string $column |
573
|
|
|
* |
574
|
|
|
* @return string |
575
|
|
|
*/ |
576
|
|
|
abstract protected function platformDropTableForeignKeyStatement($table, $column); |
577
|
|
|
|
578
|
|
|
//-------------------------------------------------------------------- |
579
|
|
|
|
580
|
|
|
/** |
581
|
|
|
* AbstractForge::addTableUniques |
582
|
|
|
* |
583
|
|
|
* @param string $table |
584
|
|
|
* @param string $column |
585
|
|
|
* |
586
|
|
|
* @return bool |
587
|
|
|
* @throws \O2System\Spl\Exceptions\RuntimeException |
588
|
|
|
* @throws \Psr\Cache\InvalidArgumentException |
589
|
|
|
*/ |
590
|
|
|
public function addTableUnique($table, $column) |
591
|
|
|
{ |
592
|
|
|
return $this->addTableUniques($table, [$column]); |
593
|
|
|
} |
594
|
|
|
|
595
|
|
|
//-------------------------------------------------------------------- |
596
|
|
|
|
597
|
|
|
/** |
598
|
|
|
* AbstractForge::addTableUniques |
599
|
|
|
* |
600
|
|
|
* @param string $table |
601
|
|
|
* @param array $columns |
602
|
|
|
* |
603
|
|
|
* @return bool |
604
|
|
|
* @throws \O2System\Spl\Exceptions\RuntimeException |
605
|
|
|
* @throws \Psr\Cache\InvalidArgumentException |
606
|
|
|
*/ |
607
|
|
|
public function addTableUniques($table, array $columns) |
608
|
|
|
{ |
609
|
|
|
return $this->runTableStatementQuery( |
610
|
|
|
$this->platformAlterTableUniquesStatement( |
611
|
|
|
$this->prepareTableNameStatement($table), |
612
|
|
|
$columns |
613
|
|
|
), |
614
|
|
|
'Unable to add unique constraint to specified database table' |
615
|
|
|
); |
616
|
|
|
} |
617
|
|
|
|
618
|
|
|
//-------------------------------------------------------------------- |
619
|
|
|
|
620
|
|
|
/** |
621
|
|
|
* AbstractForge::platformAlterTableUniquesStatement |
622
|
|
|
* |
623
|
|
|
* @param string $table |
624
|
|
|
* @param array $columns |
625
|
|
|
* |
626
|
|
|
* @return mixed |
627
|
|
|
*/ |
628
|
|
|
abstract protected function platformAlterTableUniquesStatement($table, array $columns); |
629
|
|
|
|
630
|
|
|
//-------------------------------------------------------------------- |
631
|
|
|
|
632
|
|
|
/** |
633
|
|
|
* AbstractForge::createTableIndex |
634
|
|
|
* |
635
|
|
|
* @param string $table |
636
|
|
|
* @param string $column |
637
|
|
|
* @param bool $unique |
638
|
|
|
* |
639
|
|
|
* @return bool |
640
|
|
|
* @throws \O2System\Spl\Exceptions\RuntimeException |
641
|
|
|
* @throws \Psr\Cache\InvalidArgumentException |
642
|
|
|
*/ |
643
|
|
|
public function createTableIndex($table, $column, $unique = false) |
644
|
|
|
{ |
645
|
|
|
return $this->createTableIndexes($table, [$column], $unique); |
646
|
|
|
} |
647
|
|
|
|
648
|
|
|
//-------------------------------------------------------------------- |
649
|
|
|
|
650
|
|
|
/** |
651
|
|
|
* AbstractForge::createTableIndexes |
652
|
|
|
* |
653
|
|
|
* @param string $table |
654
|
|
|
* @param array $columns |
655
|
|
|
* @param bool $unique |
656
|
|
|
* |
657
|
|
|
* @return bool |
658
|
|
|
* @throws \O2System\Spl\Exceptions\RuntimeException |
659
|
|
|
* @throws \Psr\Cache\InvalidArgumentException |
660
|
|
|
*/ |
661
|
|
|
public function createTableIndexes($table, array $columns, $unique = false) |
662
|
|
|
{ |
663
|
|
|
return $this->runTableStatementQuery( |
664
|
|
|
$this->platformCreateTableIndexesStatement( |
665
|
|
|
$this->prepareTableNameStatement($table), |
666
|
|
|
$columns, |
667
|
|
|
$unique |
668
|
|
|
), |
669
|
|
|
'Unable to add indexes to specified database table' |
670
|
|
|
); |
671
|
|
|
} |
672
|
|
|
|
673
|
|
|
//-------------------------------------------------------------------- |
674
|
|
|
|
675
|
|
|
/** |
676
|
|
|
* AbstractForge::platformCreateTableIndexesStatement |
677
|
|
|
* |
678
|
|
|
* @param string $table |
679
|
|
|
* @param array $columns |
680
|
|
|
* @param bool $unique |
681
|
|
|
* |
682
|
|
|
* @return string |
683
|
|
|
*/ |
684
|
|
|
abstract protected function platformCreateTableIndexesStatement($table, array $columns, $unique = false); |
685
|
|
|
|
686
|
|
|
//-------------------------------------------------------------------- |
687
|
|
|
|
688
|
|
|
/** |
689
|
|
|
* AbstractForge::dropTableIndex |
690
|
|
|
* |
691
|
|
|
* @param string $table |
692
|
|
|
* @param string $column |
693
|
|
|
* |
694
|
|
|
* @return bool|\O2System\Database\DataObjects\Result |
695
|
|
|
* @throws \O2System\Spl\Exceptions\RuntimeException |
696
|
|
|
* @throws \Psr\Cache\InvalidArgumentException |
697
|
|
|
*/ |
698
|
|
|
public function dropTableIndex($table, $column) |
699
|
|
|
{ |
700
|
|
|
return $this->dropTableIndexes($table, [$column]); |
701
|
|
|
} |
702
|
|
|
|
703
|
|
|
//-------------------------------------------------------------------- |
704
|
|
|
|
705
|
|
|
/** |
706
|
|
|
* AbstractForge::dropTableIndexes |
707
|
|
|
* |
708
|
|
|
* @param string $table |
709
|
|
|
* @param array $columns |
710
|
|
|
* |
711
|
|
|
* @return bool|\O2System\Database\DataObjects\Result |
712
|
|
|
* @throws \O2System\Spl\Exceptions\RuntimeException |
713
|
|
|
* @throws \Psr\Cache\InvalidArgumentException |
714
|
|
|
*/ |
715
|
|
|
public function dropTableIndexes($table, array $columns) |
716
|
|
|
{ |
717
|
|
|
return $this->runTableStatementQuery( |
718
|
|
|
$this->platformDropTableIndexesStatement( |
719
|
|
|
$this->prepareTableNameStatement($table), |
720
|
|
|
$columns |
721
|
|
|
), |
722
|
|
|
'Unable to drop indexes on specified database table' |
723
|
|
|
); |
724
|
|
|
} |
725
|
|
|
|
726
|
|
|
//-------------------------------------------------------------------- |
727
|
|
|
|
728
|
|
|
/** |
729
|
|
|
* AbstractForge::platformDropTableIndexesStatement |
730
|
|
|
* |
731
|
|
|
* @param string $table |
732
|
|
|
* @param array $columns |
733
|
|
|
* |
734
|
|
|
* @return string |
735
|
|
|
*/ |
736
|
|
|
abstract protected function platformDropTableIndexesStatement($table, array $columns); |
737
|
|
|
|
738
|
|
|
//-------------------------------------------------------------------- |
739
|
|
|
|
740
|
|
|
/** |
741
|
|
|
* AbstractForge::setTableColumnDefaultValue |
742
|
|
|
* |
743
|
|
|
* @param string $table |
744
|
|
|
* @param string $column |
745
|
|
|
* @param mixed $value |
746
|
|
|
* |
747
|
|
|
* @return bool |
748
|
|
|
* @throws \O2System\Spl\Exceptions\RuntimeException |
749
|
|
|
* @throws \Psr\Cache\InvalidArgumentException |
750
|
|
|
*/ |
751
|
|
|
public function setTableColumnDefaultValue($table, $column, $value) |
752
|
|
|
{ |
753
|
|
|
return $this->runTableStatementQuery( |
754
|
|
|
$this->platformAlterTableSetColumnDefaultValueStatement( |
755
|
|
|
$this->prepareTableNameStatement($table), |
756
|
|
|
$column, |
757
|
|
|
$value |
758
|
|
|
), |
759
|
|
|
'Unable to set default value to specified database table' |
760
|
|
|
); |
761
|
|
|
} |
762
|
|
|
|
763
|
|
|
//-------------------------------------------------------------------- |
764
|
|
|
|
765
|
|
|
/** |
766
|
|
|
* AbstractForge::platformAlterTableSetColumnDefaultValueStatement |
767
|
|
|
* |
768
|
|
|
* @param string $table |
769
|
|
|
* @param string $column |
770
|
|
|
* @param mixed $value |
771
|
|
|
* |
772
|
|
|
* @return string |
773
|
|
|
*/ |
774
|
|
|
abstract protected function platformAlterTableSetColumnDefaultValueStatement($table, $column, $value); |
775
|
|
|
|
776
|
|
|
//-------------------------------------------------------------------- |
777
|
|
|
|
778
|
|
|
/** |
779
|
|
|
* AbstractForge::dropTableColumnDefaultValue |
780
|
|
|
* |
781
|
|
|
* @param string $table |
782
|
|
|
* @param string $column |
783
|
|
|
* |
784
|
|
|
* @return bool |
785
|
|
|
* @throws \O2System\Spl\Exceptions\RuntimeException |
786
|
|
|
* @throws \Psr\Cache\InvalidArgumentException |
787
|
|
|
*/ |
788
|
|
|
public function dropTableColumnDefaultValue($table, $column) |
789
|
|
|
{ |
790
|
|
|
return $this->runTableStatementQuery( |
791
|
|
|
$this->platformAlterTableDropColumnDefaultValueStatement( |
792
|
|
|
$this->prepareTableNameStatement($table), |
793
|
|
|
$column |
794
|
|
|
), |
795
|
|
|
'Unable to drop default value on specified database table' |
796
|
|
|
); |
797
|
|
|
} |
798
|
|
|
|
799
|
|
|
//-------------------------------------------------------------------- |
800
|
|
|
|
801
|
|
|
/** |
802
|
|
|
* AbstractForge::platformAlterTableDropColumnDefaultValueStatement |
803
|
|
|
* |
804
|
|
|
* @param string $table |
805
|
|
|
* @param string $column |
806
|
|
|
* |
807
|
|
|
* @return string |
808
|
|
|
*/ |
809
|
|
|
abstract protected function platformAlterTableDropColumnDefaultValueStatement($table, $column); |
810
|
|
|
|
811
|
|
|
//-------------------------------------------------------------------- |
812
|
|
|
|
813
|
|
|
/** |
814
|
|
|
* AbstractForge::addTableColumnCheck |
815
|
|
|
* |
816
|
|
|
* @param string $table |
817
|
|
|
* @param array $conditions |
818
|
|
|
* |
819
|
|
|
* @return bool|\O2System\Database\DataObjects\Result |
820
|
|
|
* @throws \O2System\Spl\Exceptions\RuntimeException |
821
|
|
|
* @throws \Psr\Cache\InvalidArgumentException |
822
|
|
|
*/ |
823
|
|
|
public function addTableColumnCheck($table, array $conditions) |
824
|
|
|
{ |
825
|
|
|
return $this->runTableStatementQuery( |
826
|
|
|
$this->platformAlterTableCheckStatement( |
827
|
|
|
$this->prepareTableNameStatement($table), |
828
|
|
|
$conditions |
829
|
|
|
), |
830
|
|
|
'Unable to add value check constraint to specified database table' |
831
|
|
|
); |
832
|
|
|
} |
833
|
|
|
|
834
|
|
|
//-------------------------------------------------------------------- |
835
|
|
|
|
836
|
|
|
/** |
837
|
|
|
* AbstractForge::platformAlterTableCheckStatement |
838
|
|
|
* |
839
|
|
|
* @param string $table |
840
|
|
|
* @param array $conditions |
841
|
|
|
* |
842
|
|
|
* @return string |
843
|
|
|
*/ |
844
|
|
|
abstract protected function platformAlterTableCheckStatement($table, array $conditions); |
845
|
|
|
|
846
|
|
|
//-------------------------------------------------------------------- |
847
|
|
|
|
848
|
|
|
/** |
849
|
|
|
* AbstractForge::dropTableColumnCheck |
850
|
|
|
* |
851
|
|
|
* @param string $table |
852
|
|
|
* @param array $columns |
853
|
|
|
* |
854
|
|
|
* @return bool |
855
|
|
|
* @throws \O2System\Spl\Exceptions\RuntimeException |
856
|
|
|
* @throws \Psr\Cache\InvalidArgumentException |
857
|
|
|
*/ |
858
|
|
|
public function dropTableColumnCheck($table, array $columns) |
859
|
|
|
{ |
860
|
|
|
return $this->runTableStatementQuery( |
861
|
|
|
$this->platformAlterTableDropCheckStatement( |
862
|
|
|
$this->prepareTableNameStatement($table), |
863
|
|
|
$columns |
864
|
|
|
), |
865
|
|
|
'Unable to drop value check constraint on specified database table' |
866
|
|
|
); |
867
|
|
|
} |
868
|
|
|
|
869
|
|
|
//-------------------------------------------------------------------- |
870
|
|
|
|
871
|
|
|
/** |
872
|
|
|
* AbstractForge::platformAlterTableDropCheckStatement |
873
|
|
|
* |
874
|
|
|
* @param string $table |
875
|
|
|
* @param array $columns |
876
|
|
|
* |
877
|
|
|
* @return mixed |
878
|
|
|
*/ |
879
|
|
|
abstract protected function platformAlterTableDropCheckStatement($table, array $columns); |
880
|
|
|
|
881
|
|
|
//-------------------------------------------------------------------- |
882
|
|
|
|
883
|
|
|
/** |
884
|
|
|
* AbstractForge::createView |
885
|
|
|
* |
886
|
|
|
* @param string $name |
887
|
|
|
* @param string $query |
888
|
|
|
* @param bool $force |
889
|
|
|
* |
890
|
|
|
* @return false |
891
|
|
|
* @throws \O2System\Spl\Exceptions\RuntimeException |
892
|
|
|
* @throws \Psr\Cache\InvalidArgumentException |
893
|
|
|
*/ |
894
|
|
|
public function createView($name, $query, $force = false) |
895
|
|
|
{ |
896
|
|
|
if (false !== ($statement = $this->platformCreateViewStatement($name, $query, $force))) { |
|
|
|
|
897
|
|
|
if (false !== ($result = $this->conn->query($statement))) { |
898
|
|
|
return $result; |
|
|
|
|
899
|
|
|
} |
900
|
|
|
|
901
|
|
|
if ($this->conn->debugEnable) { |
902
|
|
|
throw new RuntimeException('Unable to create view specified database'); |
903
|
|
|
} |
904
|
|
|
} |
905
|
|
|
|
906
|
|
|
if ($this->conn->debugEnable) { |
907
|
|
|
throw new RuntimeException('This feature is not available for the database you are using.'); |
908
|
|
|
} |
909
|
|
|
|
910
|
|
|
return false; |
911
|
|
|
} |
912
|
|
|
|
913
|
|
|
//-------------------------------------------------------------------- |
914
|
|
|
|
915
|
|
|
/** |
916
|
|
|
* AbstractForge::platformCreateViewStatement |
917
|
|
|
* |
918
|
|
|
* @param string $name |
919
|
|
|
* @param string $query |
920
|
|
|
* @param bool $force |
921
|
|
|
* |
922
|
|
|
* @return string |
923
|
|
|
*/ |
924
|
|
|
abstract protected function platformCreateViewStatement($name, $query, $force = false); |
925
|
|
|
|
926
|
|
|
//-------------------------------------------------------------------- |
927
|
|
|
|
928
|
|
|
/** |
929
|
|
|
* AbstractForge::dropView |
930
|
|
|
* |
931
|
|
|
* @param string $name |
932
|
|
|
* |
933
|
|
|
* @return bool |
934
|
|
|
* @throws \O2System\Spl\Exceptions\RuntimeException |
935
|
|
|
* @throws \Psr\Cache\InvalidArgumentException |
936
|
|
|
*/ |
937
|
|
|
public function dropView($name) |
938
|
|
|
{ |
939
|
|
|
if (false !== ($statement = $this->platformDropViewStatement($name))) { |
|
|
|
|
940
|
|
|
if (false !== ($result = $this->conn->query($statement))) { |
941
|
|
|
return $result; |
|
|
|
|
942
|
|
|
} |
943
|
|
|
|
944
|
|
|
if ($this->conn->debugEnable) { |
945
|
|
|
throw new RuntimeException('Unable to drop view on specified database'); |
946
|
|
|
} |
947
|
|
|
} |
948
|
|
|
|
949
|
|
|
if ($this->conn->debugEnable) { |
950
|
|
|
throw new RuntimeException('This feature is not available for the database you are using.'); |
951
|
|
|
} |
952
|
|
|
|
953
|
|
|
return false; |
954
|
|
|
} |
955
|
|
|
|
956
|
|
|
//-------------------------------------------------------------------- |
957
|
|
|
|
958
|
|
|
/** |
959
|
|
|
* AbstractForge::platformDropViewStatement |
960
|
|
|
* |
961
|
|
|
* @param string $name |
962
|
|
|
* |
963
|
|
|
* @return string |
964
|
|
|
*/ |
965
|
|
|
abstract protected function platformDropViewStatement($name); |
966
|
|
|
} |