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