/
Table.php
713 lines (658 loc) · 19.4 KB
/
Table.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 3.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Database\Schema;
use Cake\Database\Exception;
use Cake\Database\Type;
use Cake\Datasource\ConnectionInterface;
/**
* Represents a single table in a database schema.
*
* Can either be populated using the reflection API's
* or by incrementally building an instance using
* methods.
*
* Once created Table instances can be added to
* Schema\Collection objects. They can also be converted into SQL using the
* createSql(), dropSql() and truncateSql() methods.
*/
class Table
{
/**
* The name of the table
*
* @var string
*/
protected $_table;
/**
* Columns in the table.
*
* @var array
*/
protected $_columns = [];
/**
* Indexes in the table.
*
* @var array
*/
protected $_indexes = [];
/**
* Constraints in the table.
*
* @var array
*/
protected $_constraints = [];
/**
* Options for the table.
*
* @var array
*/
protected $_options = [];
/**
* Whether or not the table is temporary
*
* @var bool
*/
protected $_temporary = false;
/**
* The valid keys that can be used in a column
* definition.
*
* @var array
*/
protected static $_columnKeys = [
'type' => null,
'baseType' => null,
'length' => null,
'precision' => null,
'null' => null,
'default' => null,
'comment' => null,
];
/**
* Additional type specific properties.
*
* @var array
*/
protected static $_columnExtras = [
'string' => [
'fixed' => null,
],
'integer' => [
'unsigned' => null,
'autoIncrement' => null,
],
'biginteger' => [
'unsigned' => null,
'autoIncrement' => null,
],
'decimal' => [
'unsigned' => null,
],
'float' => [
'unsigned' => null,
],
];
/**
* The valid keys that can be used in an index
* definition.
*
* @var array
*/
protected static $_indexKeys = [
'type' => null,
'columns' => [],
'length' => [],
'references' => [],
'update' => 'restrict',
'delete' => 'restrict',
];
/**
* Names of the valid index types.
*
* @var array
*/
protected static $_validIndexTypes = [
self::INDEX_INDEX,
self::INDEX_FULLTEXT,
];
/**
* Names of the valid constraint types.
*
* @var array
*/
protected static $_validConstraintTypes = [
self::CONSTRAINT_PRIMARY,
self::CONSTRAINT_UNIQUE,
self::CONSTRAINT_FOREIGN,
];
/**
* Names of the valid foreign key actions.
*
* @var array
*/
protected static $_validForeignKeyActions = [
self::ACTION_CASCADE,
self::ACTION_SET_NULL,
self::ACTION_SET_DEFAULT,
self::ACTION_NO_ACTION,
self::ACTION_RESTRICT,
];
/**
* Primary constraint type
*
* @var string
*/
const CONSTRAINT_PRIMARY = 'primary';
/**
* Unique constraint type
*
* @var string
*/
const CONSTRAINT_UNIQUE = 'unique';
/**
* Foreign constraint type
*
* @var string
*/
const CONSTRAINT_FOREIGN = 'foreign';
/**
* Index - index type
*
* @var string
*/
const INDEX_INDEX = 'index';
/**
* Fulltext index type
*
* @var string
*/
const INDEX_FULLTEXT = 'fulltext';
/**
* Foreign key cascade action
*
* @var string
*/
const ACTION_CASCADE = 'cascade';
/**
* Foreign key set null action
*
* @var string
*/
const ACTION_SET_NULL = 'setNull';
/**
* Foreign key no action
*
* @var string
*/
const ACTION_NO_ACTION = 'noAction';
/**
* Foreign key restrict action
*
* @var string
*/
const ACTION_RESTRICT = 'restrict';
/**
* Foreign key restrict default
*
* @var string
*/
const ACTION_SET_DEFAULT = 'setDefault';
/**
* Constructor.
*
* @param string $table The table name.
* @param array $columns The list of columns for the schema.
*/
public function __construct($table, array $columns = [])
{
$this->_table = $table;
foreach ($columns as $field => $definition) {
$this->addColumn($field, $definition);
}
}
/**
* Get the name of the table.
*
* @return string
*/
public function name()
{
return $this->_table;
}
/**
* Add a column to the table.
*
* ### Attributes
*
* Columns can have several attributes:
*
* - `type` The type of the column. This should be
* one of CakePHP's abstract types.
* - `length` The length of the column.
* - `precision` The number of decimal places to store
* for float and decimal types.
* - `default` The default value of the column.
* - `null` Whether or not the column can hold nulls.
* - `fixed` Whether or not the column is a fixed length column.
* This is only present/valid with string columns.
* - `unsigned` Whether or not the column is an unsigned column.
* This is only present/valid for integer, decimal, float columns.
*
* In addition to the above keys, the following keys are
* implemented in some database dialects, but not all:
*
* - `comment` The comment for the column.
*
* @param string $name The name of the column
* @param array $attrs The attributes for the column.
* @return $this
*/
public function addColumn($name, $attrs)
{
if (is_string($attrs)) {
$attrs = ['type' => $attrs];
}
$valid = static::$_columnKeys;
if (isset(static::$_columnExtras[$attrs['type']])) {
$valid += static::$_columnExtras[$attrs['type']];
}
$attrs = array_intersect_key($attrs, $valid);
$this->_columns[$name] = $attrs + $valid;
return $this;
}
/**
* Get the column names in the table.
*
* @return array
*/
public function columns()
{
return array_keys($this->_columns);
}
/**
* Get column data in the table.
*
* @param string $name The column name.
* @return array|null Column data or null.
*/
public function column($name)
{
if (!isset($this->_columns[$name])) {
return null;
}
$column = $this->_columns[$name];
unset($column['baseType']);
return $column;
}
/**
* Sets the type of a column, or returns its current type
* if none is passed.
*
* @param string $name The column to get the type of.
* @param string $type The type to set the column to.
* @return string|null Either the column type or null.
*/
public function columnType($name, $type = null)
{
if (!isset($this->_columns[$name])) {
return null;
}
if ($type !== null) {
$this->_columns[$name]['type'] = $type;
}
return $this->_columns[$name]['type'];
}
/**
* Returns the base type name for the provided column.
* This represent the database type a more complex class is
* based upon.
*
* @param string $column The column name to get the base type from
* @return string The base type name
*/
public function baseColumnType($column)
{
if (isset($this->_columns[$column]['baseType'])) {
return $this->_columns[$column]['baseType'];
}
$type = $this->columnType($column);
if ($type === null) {
return null;
}
if (Type::map($type)) {
$type = Type::build($type)->getBaseType();
}
return $this->_columns[$column]['baseType'] = $type;
}
/**
* Check whether or not a field is nullable
*
* Missing columns are nullable.
*
* @param string $name The column to get the type of.
* @return bool Whether or not the field is nullable.
*/
public function isNullable($name)
{
if (!isset($this->_columns[$name])) {
return true;
}
return ($this->_columns[$name]['null'] === true);
}
/**
* Get a hash of columns and their default values.
*
* @return array
*/
public function defaultValues()
{
$defaults = [];
foreach ($this->_columns as $name => $data) {
if (!array_key_exists('default', $data)) {
continue;
}
if ($data['default'] === null && $data['null'] !== true) {
continue;
}
$defaults[$name] = $data['default'];
}
return $defaults;
}
/**
* Add an index.
*
* Used to add indexes, and full text indexes in platforms that support
* them.
*
* ### Attributes
*
* - `type` The type of index being added.
* - `columns` The columns in the index.
*
* @param string $name The name of the index.
* @param array $attrs The attributes for the index.
* @return $this
* @throws \Cake\Database\Exception
*/
public function addIndex($name, $attrs)
{
if (is_string($attrs)) {
$attrs = ['type' => $attrs];
}
$attrs = array_intersect_key($attrs, static::$_indexKeys);
$attrs = $attrs + static::$_indexKeys;
unset($attrs['references'], $attrs['update'], $attrs['delete']);
if (!in_array($attrs['type'], static::$_validIndexTypes, true)) {
throw new Exception(sprintf('Invalid index type "%s" in index "%s" in table "%s".', $attrs['type'], $name, $this->_table));
}
if (empty($attrs['columns'])) {
throw new Exception(sprintf('Index "%s" in table "%s" must have at least one column.', $name, $this->_table));
}
$attrs['columns'] = (array)$attrs['columns'];
foreach ($attrs['columns'] as $field) {
if (empty($this->_columns[$field])) {
$msg = sprintf(
'Columns used in index "%s" in table "%s" must be added to the Table schema first. ' .
'The column "%s" was not found.',
$name,
$this->_table,
$field
);
throw new Exception($msg);
}
}
$this->_indexes[$name] = $attrs;
return $this;
}
/**
* Get the names of all the indexes in the table.
*
* @return array
*/
public function indexes()
{
return array_keys($this->_indexes);
}
/**
* Read information about an index based on name.
*
* @param string $name The name of the index.
* @return array|null Array of index data, or null
*/
public function index($name)
{
if (!isset($this->_indexes[$name])) {
return null;
}
return $this->_indexes[$name];
}
/**
* Get the column(s) used for the primary key.
*
* @return array Column name(s) for the primary key. An
* empty list will be returned when the table has no primary key.
*/
public function primaryKey()
{
foreach ($this->_constraints as $name => $data) {
if ($data['type'] === static::CONSTRAINT_PRIMARY) {
return $data['columns'];
}
}
return [];
}
/**
* Add a constraint.
*
* Used to add constraints to a table. For example primary keys, unique
* keys and foreign keys.
*
* ### Attributes
*
* - `type` The type of constraint being added.
* - `columns` The columns in the index.
* - `references` The table, column a foreign key references.
* - `update` The behavior on update. Options are 'restrict', 'setNull', 'cascade', 'noAction'.
* - `delete` The behavior on delete. Options are 'restrict', 'setNull', 'cascade', 'noAction'.
*
* The default for 'update' & 'delete' is 'cascade'.
*
* @param string $name The name of the constraint.
* @param array $attrs The attributes for the constraint.
* @return $this
* @throws \Cake\Database\Exception
*/
public function addConstraint($name, $attrs)
{
if (is_string($attrs)) {
$attrs = ['type' => $attrs];
}
$attrs = array_intersect_key($attrs, static::$_indexKeys);
$attrs = $attrs + static::$_indexKeys;
if (!in_array($attrs['type'], static::$_validConstraintTypes, true)) {
throw new Exception(sprintf('Invalid constraint type "%s" in table "%s".', $attrs['type'], $this->_table));
}
if (empty($attrs['columns'])) {
throw new Exception(sprintf('Constraints in table "%s" must have at least one column.', $this->_table));
}
$attrs['columns'] = (array)$attrs['columns'];
foreach ($attrs['columns'] as $field) {
if (empty($this->_columns[$field])) {
$msg = sprintf(
'Columns used in constraints must be added to the Table schema first. ' .
'The column "%s" was not found in table "%s".',
$field,
$this->_table
);
throw new Exception($msg);
}
}
if ($attrs['type'] === static::CONSTRAINT_FOREIGN) {
$attrs = $this->_checkForeignKey($attrs);
if (isset($this->_constraints[$name])) {
$this->_constraints[$name]['columns'] = array_merge(
$this->_constraints[$name]['columns'],
$attrs['columns']
);
$this->_constraints[$name]['references'][1] = array_merge(
(array)$this->_constraints[$name]['references'][1],
[$attrs['references'][1]]
);
return $this;
}
} else {
unset($attrs['references'], $attrs['update'], $attrs['delete']);
}
$this->_constraints[$name] = $attrs;
return $this;
}
/**
* Check whether or not a table has an autoIncrement column defined.
*
* @return bool
*/
public function hasAutoincrement()
{
foreach ($this->_columns as $column) {
if (isset($column['autoIncrement']) && $column['autoIncrement']) {
return true;
}
}
return false;
}
/**
* Helper method to check/validate foreign keys.
*
* @param array $attrs Attributes to set.
* @return array
* @throws \Cake\Database\Exception When foreign key definition is not valid.
*/
protected function _checkForeignKey($attrs)
{
if (count($attrs['references']) < 2) {
throw new Exception('References must contain a table and column.');
}
if (!in_array($attrs['update'], static::$_validForeignKeyActions)) {
throw new Exception(sprintf('Update action is invalid. Must be one of %s', implode(',', static::$_validForeignKeyActions)));
}
if (!in_array($attrs['delete'], static::$_validForeignKeyActions)) {
throw new Exception(sprintf('Delete action is invalid. Must be one of %s', implode(',', static::$_validForeignKeyActions)));
}
return $attrs;
}
/**
* Get the names of all the constraints in the table.
*
* @return array
*/
public function constraints()
{
return array_keys($this->_constraints);
}
/**
* Read information about a constraint based on name.
*
* @param string $name The name of the constraint.
* @return array|null Array of constraint data, or null
*/
public function constraint($name)
{
if (!isset($this->_constraints[$name])) {
return null;
}
return $this->_constraints[$name];
}
/**
* Get/set the options for a table.
*
* Table options allow you to set platform specific table level options.
* For example the engine type in MySQL.
*
* @param array|null $options The options to set, or null to read options.
* @return $this|array Either the table instance, or an array of options when reading.
*/
public function options($options = null)
{
if ($options === null) {
return $this->_options;
}
$this->_options = array_merge($this->_options, $options);
return $this;
}
/**
* Get/Set whether the table is temporary in the database
*
* @param bool|null $set whether or not the table is to be temporary
* @return $this|bool Either the table instance, the current temporary setting
*/
public function temporary($set = null)
{
if ($set === null) {
return $this->_temporary;
}
$this->_temporary = (bool)$set;
return $this;
}
/**
* Generate the SQL to create the Table.
*
* Uses the connection to access the schema dialect
* to generate platform specific SQL.
*
* @param \Cake\Datasource\ConnectionInterface $connection The connection to generate SQL for
* @return array List of SQL statements to create the table and the
* required indexes.
*/
public function createSql(ConnectionInterface $connection)
{
$dialect = $connection->driver()->schemaDialect();
$columns = $constraints = $indexes = [];
foreach (array_keys($this->_columns) as $name) {
$columns[] = $dialect->columnSql($this, $name);
}
foreach (array_keys($this->_constraints) as $name) {
$constraints[] = $dialect->constraintSql($this, $name);
}
foreach (array_keys($this->_indexes) as $name) {
$indexes[] = $dialect->indexSql($this, $name);
}
return $dialect->createTableSql($this, $columns, $constraints, $indexes);
}
/**
* Generate the SQL to drop a table.
*
* Uses the connection to access the schema dialect to generate platform
* specific SQL.
*
* @param \Cake\Datasource\ConnectionInterface $connection The connection to generate SQL for.
* @return array SQL to drop a table.
*/
public function dropSql(ConnectionInterface $connection)
{
$dialect = $connection->driver()->schemaDialect();
return $dialect->dropTableSql($this);
}
/**
* Generate the SQL statements to truncate a table
*
* @param \Cake\Datasource\ConnectionInterface $connection The connection to generate SQL for.
* @return array SQL to drop a table.
*/
public function truncateSql(ConnectionInterface $connection)
{
$dialect = $connection->driver()->schemaDialect();
return $dialect->truncateTableSql($this);
}
}