15
15
namespace Cake \Database \Schema ;
16
16
17
17
use Cake \Database \Exception ;
18
+ use Cake \Database \Schema \TableSchema ;
18
19
19
20
/**
20
21
* Schema management/reflection features for Postgres.
@@ -90,56 +91,56 @@ protected function _convertColumn($column)
90
91
return ['type ' => $ col , 'length ' => null ];
91
92
}
92
93
if (strpos ($ col , 'timestamp ' ) !== false ) {
93
- return ['type ' => ' timestamp ' , 'length ' => null ];
94
+ return ['type ' => TableSchema:: TYPE_TIMESTAMP , 'length ' => null ];
94
95
}
95
96
if (strpos ($ col , 'time ' ) !== false ) {
96
- return ['type ' => ' time ' , 'length ' => null ];
97
+ return ['type ' => TableSchema:: TYPE_TIME , 'length ' => null ];
97
98
}
98
99
if ($ col === 'serial ' || $ col === 'integer ' ) {
99
- return ['type ' => ' integer ' , 'length ' => 10 ];
100
+ return ['type ' => TableSchema:: TYPE_INTEGER , 'length ' => 10 ];
100
101
}
101
102
if ($ col === 'bigserial ' || $ col === 'bigint ' ) {
102
- return ['type ' => ' biginteger ' , 'length ' => 20 ];
103
+ return ['type ' => TableSchema:: TYPE_BIGINTEGER , 'length ' => 20 ];
103
104
}
104
105
if ($ col === 'smallint ' ) {
105
- return ['type ' => ' smallinteger ' , 'length ' => 5 ];
106
+ return ['type ' => TableSchema:: TYPE_SMALLINTEGER , 'length ' => 5 ];
106
107
}
107
108
if ($ col === 'inet ' ) {
108
- return ['type ' => ' string ' , 'length ' => 39 ];
109
+ return ['type ' => TableSchema:: TYPE_STRING , 'length ' => 39 ];
109
110
}
110
111
if ($ col === 'uuid ' ) {
111
- return ['type ' => ' uuid ' , 'length ' => null ];
112
+ return ['type ' => TableSchema:: TYPE_UUID , 'length ' => null ];
112
113
}
113
114
if ($ col === 'char ' || $ col === 'character ' ) {
114
- return ['type ' => ' string ' , 'fixed ' => true , 'length ' => $ length ];
115
+ return ['type ' => TableSchema:: TYPE_STRING , 'fixed ' => true , 'length ' => $ length ];
115
116
}
116
117
// money is 'string' as it includes arbitrary text content
117
118
// before the number value.
118
119
if (strpos ($ col , 'char ' ) !== false ||
119
120
strpos ($ col , 'money ' ) !== false
120
121
) {
121
- return ['type ' => ' string ' , 'length ' => $ length ];
122
+ return ['type ' => TableSchema:: TYPE_STRING , 'length ' => $ length ];
122
123
}
123
124
if (strpos ($ col , 'text ' ) !== false ) {
124
- return ['type ' => ' text ' , 'length ' => null ];
125
+ return ['type ' => TableSchema:: TYPE_TEXT , 'length ' => null ];
125
126
}
126
127
if ($ col === 'bytea ' ) {
127
- return ['type ' => ' binary ' , 'length ' => null ];
128
+ return ['type ' => TableSchema:: TYPE_BINARY , 'length ' => null ];
128
129
}
129
130
if ($ col === 'real ' || strpos ($ col , 'double ' ) !== false ) {
130
- return ['type ' => ' float ' , 'length ' => null ];
131
+ return ['type ' => TableSchema:: TYPE_FLOAT , 'length ' => null ];
131
132
}
132
133
if (strpos ($ col , 'numeric ' ) !== false ||
133
134
strpos ($ col , 'decimal ' ) !== false
134
135
) {
135
- return ['type ' => ' decimal ' , 'length ' => null ];
136
+ return ['type ' => TableSchema:: TYPE_DECIMAL , 'length ' => null ];
136
137
}
137
138
138
139
if (strpos ($ col , 'json ' ) !== false ) {
139
- return ['type ' => ' json ' , 'length ' => null ];
140
+ return ['type ' => TableSchema:: TYPE_JSON , 'length ' => null ];
140
141
}
141
142
142
- return ['type ' => ' string ' , 'length ' => null ];
143
+ return ['type ' => TableSchema:: TYPE_STRING , 'length ' => null ];
143
144
}
144
145
145
146
/**
@@ -149,7 +150,7 @@ public function convertColumnDescription(TableSchema $schema, $row)
149
150
{
150
151
$ field = $ this ->_convertColumn ($ row ['type ' ]);
151
152
152
- if ($ field ['type ' ] === ' boolean ' ) {
153
+ if ($ field ['type ' ] === TableSchema:: TYPE_BOOLEAN ) {
153
154
if ($ row ['default ' ] === 'true ' ) {
154
155
$ row ['default ' ] = 1 ;
155
156
}
@@ -351,38 +352,40 @@ public function columnSql(TableSchema $schema, $name)
351
352
$ data = $ schema ->column ($ name );
352
353
$ out = $ this ->_driver ->quoteIdentifier ($ name );
353
354
$ typeMap = [
354
- ' tinyinteger ' => ' SMALLINT ' ,
355
- ' smallinteger ' => ' SMALLINT ' ,
356
- ' boolean ' => ' BOOLEAN ' ,
357
- ' binary ' => ' BYTEA ' ,
358
- ' float ' => ' FLOAT ' ,
359
- ' decimal ' => ' DECIMAL ' ,
360
- ' date ' => ' DATE ' ,
361
- ' time ' => ' TIME ' ,
362
- ' datetime ' => ' TIMESTAMP ' ,
363
- ' timestamp ' => ' TIMESTAMP ' ,
364
- ' uuid ' => ' UUID ' ,
365
- ' json ' => ' JSONB '
355
+ TableSchema:: TYPE_TINYINTEGER => ' SMALLINT ' ,
356
+ TableSchema:: TYPE_SMALLINTEGER => ' SMALLINT ' ,
357
+ TableSchema:: TYPE_BINARY => ' BYTEA ' ,
358
+ TableSchema:: TYPE_BOOLEAN => ' BOOLEAN ' ,
359
+ TableSchema:: TYPE_FLOAT => ' FLOAT ' ,
360
+ TableSchema:: TYPE_DECIMAL => ' DECIMAL ' ,
361
+ TableSchema:: TYPE_DATE => ' DATE ' ,
362
+ TableSchema:: TYPE_TIME => ' TIME ' ,
363
+ TableSchema:: TYPE_DATETIME => ' TIMESTAMP ' ,
364
+ TableSchema:: TYPE_TIMESTAMP => ' TIMESTAMP ' ,
365
+ TableSchema:: TYPE_UUID => ' UUID ' ,
366
+ TableSchema:: TYPE_JSON => ' JSONB '
366
367
];
367
368
368
369
if (isset ($ typeMap [$ data ['type ' ]])) {
369
370
$ out .= $ typeMap [$ data ['type ' ]];
370
371
}
371
372
372
- if ($ data ['type ' ] === ' integer ' || $ data ['type ' ] === ' biginteger ' ) {
373
- $ type = $ data ['type ' ] === ' integer ' ? ' INTEGER ' : ' BIGINT ' ;
373
+ if ($ data ['type ' ] === TableSchema:: TYPE_INTEGER || $ data ['type ' ] === TableSchema:: TYPE_BIGINTEGER ) {
374
+ $ type = $ data ['type ' ] === TableSchema:: TYPE_INTEGER ? ' INTEGER ' : ' BIGINT ' ;
374
375
if ([$ name ] === $ schema ->primaryKey () || $ data ['autoIncrement ' ] === true ) {
375
- $ type = $ data ['type ' ] === ' integer ' ? ' SERIAL ' : ' BIGSERIAL ' ;
376
+ $ type = $ data ['type ' ] === TableSchema:: TYPE_INTEGER ? ' SERIAL ' : ' BIGSERIAL ' ;
376
377
unset($ data ['null ' ], $ data ['default ' ]);
377
378
}
378
379
$ out .= $ type ;
379
380
}
380
381
381
- if ($ data ['type ' ] === ' text ' && $ data ['length ' ] !== TableSchema::LENGTH_TINY ) {
382
+ if ($ data ['type ' ] === TableSchema:: TYPE_TEXT && $ data ['length ' ] !== TableSchema::LENGTH_TINY ) {
382
383
$ out .= ' TEXT ' ;
383
384
}
384
385
385
- if ($ data ['type ' ] === 'string ' || ($ data ['type ' ] === 'text ' && $ data ['length ' ] === TableSchema::LENGTH_TINY )) {
386
+ if ($ data ['type ' ] === TableSchema::TYPE_STRING ||
387
+ ($ data ['type ' ] === TableSchema::TYPE_TEXT && $ data ['length ' ] === TableSchema::LENGTH_TINY )
388
+ ) {
386
389
$ isFixed = !empty ($ data ['fixed ' ]);
387
390
$ type = ' VARCHAR ' ;
388
391
if ($ isFixed ) {
@@ -394,16 +397,16 @@ public function columnSql(TableSchema $schema, $name)
394
397
}
395
398
}
396
399
397
- $ hasCollate = [' text ' , ' string ' ];
400
+ $ hasCollate = [TableSchema:: TYPE_TEXT , TableSchema:: TYPE_STRING ];
398
401
if (in_array ($ data ['type ' ], $ hasCollate , true ) && isset ($ data ['collate ' ]) && $ data ['collate ' ] !== '' ) {
399
402
$ out .= ' COLLATE " ' . $ data ['collate ' ] . '" ' ;
400
403
}
401
404
402
- if ($ data ['type ' ] === ' float ' && isset ($ data ['precision ' ])) {
405
+ if ($ data ['type ' ] === TableSchema:: TYPE_FLOAT && isset ($ data ['precision ' ])) {
403
406
$ out .= '( ' . (int )$ data ['precision ' ] . ') ' ;
404
407
}
405
408
406
- if ($ data ['type ' ] === ' decimal ' &&
409
+ if ($ data ['type ' ] === TableSchema:: TYPE_DECIMAL &&
407
410
(isset ($ data ['length ' ]) || isset ($ data ['precision ' ]))
408
411
) {
409
412
$ out .= '( ' . (int )$ data ['length ' ] . ', ' . (int )$ data ['precision ' ] . ') ' ;
@@ -414,8 +417,9 @@ public function columnSql(TableSchema $schema, $name)
414
417
}
415
418
416
419
if (isset ($ data ['default ' ]) &&
417
- in_array ($ data ['type ' ], ['timestamp ' , 'datetime ' ]) &&
418
- strtolower ($ data ['default ' ]) === 'current_timestamp ' ) {
420
+ in_array ($ data ['type ' ], [TableSchema::TYPE_TIMESTAMP , TableSchema::TYPE_DATETIME ]) &&
421
+ strtolower ($ data ['default ' ]) === 'current_timestamp '
422
+ ) {
419
423
$ out .= ' DEFAULT CURRENT_TIMESTAMP ' ;
420
424
} elseif (isset ($ data ['default ' ])) {
421
425
$ defaultValue = $ data ['default ' ];
0 commit comments