|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Doctrine\DBAL\Platforms; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Schema\Column; |
|
6
|
|
|
use Doctrine\DBAL\Schema\ColumnDiff; |
|
7
|
|
|
use Doctrine\DBAL\Schema\ForeignKeyConstraint; |
|
8
|
|
|
use Doctrine\DBAL\Schema\Identifier; |
|
9
|
|
|
use Doctrine\DBAL\Schema\Index; |
|
10
|
|
|
use Doctrine\DBAL\Schema\Sequence; |
|
11
|
|
|
use Doctrine\DBAL\Schema\TableDiff; |
|
12
|
|
|
use Doctrine\DBAL\Types\BigIntType; |
|
13
|
|
|
use Doctrine\DBAL\Types\BinaryType; |
|
14
|
|
|
use Doctrine\DBAL\Types\BlobType; |
|
15
|
|
|
use Doctrine\DBAL\Types\IntegerType; |
|
16
|
|
|
use Doctrine\DBAL\Types\Type; |
|
17
|
|
|
use UnexpectedValueException; |
|
18
|
|
|
use function array_diff; |
|
19
|
|
|
use function array_merge; |
|
20
|
|
|
use function array_unique; |
|
21
|
|
|
use function array_values; |
|
22
|
|
|
use function count; |
|
23
|
|
|
use function explode; |
|
24
|
|
|
use function implode; |
|
25
|
|
|
use function in_array; |
|
26
|
|
|
use function is_array; |
|
27
|
|
|
use function is_bool; |
|
28
|
|
|
use function is_numeric; |
|
29
|
|
|
use function is_string; |
|
30
|
|
|
use function sprintf; |
|
31
|
|
|
use function strpos; |
|
32
|
|
|
use function strtolower; |
|
33
|
|
|
use function trim; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* PostgreSqlPlatform. |
|
37
|
|
|
* |
|
38
|
|
|
* @todo Rename: PostgreSQLPlatform |
|
39
|
|
|
*/ |
|
40
|
|
|
class PostgreSqlPlatform extends AbstractPlatform |
|
41
|
|
|
{ |
|
42
|
|
|
/** @var bool */ |
|
43
|
|
|
private $useBooleanTrueFalseStrings = true; |
|
44
|
|
|
|
|
45
|
|
|
/** @var string[][] PostgreSQL booleans literals */ |
|
46
|
|
|
private $booleanLiterals = [ |
|
47
|
|
|
'true' => [ |
|
48
|
|
|
't', |
|
49
|
|
|
'true', |
|
50
|
|
|
'y', |
|
51
|
|
|
'yes', |
|
52
|
|
|
'on', |
|
53
|
|
|
'1', |
|
54
|
|
|
], |
|
55
|
|
|
'false' => [ |
|
56
|
|
|
'f', |
|
57
|
|
|
'false', |
|
58
|
|
|
'n', |
|
59
|
|
|
'no', |
|
60
|
|
|
'off', |
|
61
|
|
|
'0', |
|
62
|
|
|
], |
|
63
|
|
|
]; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* PostgreSQL has different behavior with some drivers |
|
67
|
|
|
* with regard to how booleans have to be handled. |
|
68
|
|
|
* |
|
69
|
|
|
* Enables use of 'true'/'false' or otherwise 1 and 0 instead. |
|
70
|
|
|
* |
|
71
|
|
|
* @param bool $flag |
|
72
|
|
|
*/ |
|
73
|
17624 |
|
public function setUseBooleanTrueFalseStrings($flag) |
|
74
|
|
|
{ |
|
75
|
17624 |
|
$this->useBooleanTrueFalseStrings = (bool) $flag; |
|
76
|
17624 |
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* {@inheritDoc} |
|
80
|
|
|
*/ |
|
81
|
19660 |
|
public function getSubstringExpression($value, $from, $length = null) |
|
82
|
|
|
{ |
|
83
|
19660 |
|
if ($length === null) { |
|
84
|
19660 |
|
return 'SUBSTRING(' . $value . ' FROM ' . $from . ')'; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
18316 |
|
return 'SUBSTRING(' . $value . ' FROM ' . $from . ' FOR ' . $length . ')'; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* {@inheritDoc} |
|
92
|
|
|
*/ |
|
93
|
|
|
public function getNowExpression() |
|
94
|
|
|
{ |
|
95
|
|
|
return 'LOCALTIMESTAMP(0)'; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* {@inheritDoc} |
|
100
|
|
|
*/ |
|
101
|
18316 |
|
public function getRegexpExpression() |
|
102
|
|
|
{ |
|
103
|
18316 |
|
return 'SIMILAR TO'; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* {@inheritDoc} |
|
108
|
|
|
*/ |
|
109
|
6090 |
|
public function getLocateExpression($str, $substr, $startPos = false) |
|
110
|
|
|
{ |
|
111
|
6090 |
|
if ($startPos !== false) { |
|
112
|
6090 |
|
$str = $this->getSubstringExpression($str, $startPos); |
|
113
|
|
|
|
|
114
|
6090 |
|
return 'CASE WHEN (POSITION(' . $substr . ' IN ' . $str . ') = 0) THEN 0 ELSE (POSITION(' . $substr . ' IN ' . $str . ') + ' . ($startPos-1) . ') END'; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
6090 |
|
return 'POSITION(' . $substr . ' IN ' . $str . ')'; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* {@inheritdoc} |
|
122
|
|
|
*/ |
|
123
|
6097 |
|
protected function getDateArithmeticIntervalExpression($date, $operator, $interval, $unit) |
|
124
|
|
|
{ |
|
125
|
6097 |
|
if ($unit === DateIntervalUnit::QUARTER) { |
|
126
|
6097 |
|
$interval *= 3; |
|
127
|
6097 |
|
$unit = DateIntervalUnit::MONTH; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
6097 |
|
return '(' . $date . ' ' . $operator . ' (' . $interval . " || ' " . $unit . "')::interval)"; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* {@inheritDoc} |
|
135
|
|
|
*/ |
|
136
|
5887 |
|
public function getDateDiffExpression($date1, $date2) |
|
137
|
|
|
{ |
|
138
|
5887 |
|
return '(DATE(' . $date1 . ')-DATE(' . $date2 . '))'; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* {@inheritDoc} |
|
143
|
|
|
*/ |
|
144
|
18859 |
|
public function supportsSequences() |
|
145
|
|
|
{ |
|
146
|
18859 |
|
return true; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* {@inheritDoc} |
|
151
|
|
|
*/ |
|
152
|
5548 |
|
public function supportsSchemas() |
|
153
|
|
|
{ |
|
154
|
5548 |
|
return true; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* {@inheritdoc} |
|
159
|
|
|
*/ |
|
160
|
5373 |
|
public function getDefaultSchemaName() |
|
161
|
|
|
{ |
|
162
|
5373 |
|
return 'public'; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* {@inheritDoc} |
|
167
|
|
|
*/ |
|
168
|
18760 |
|
public function supportsIdentityColumns() |
|
169
|
|
|
{ |
|
170
|
18760 |
|
return true; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* {@inheritdoc} |
|
175
|
|
|
*/ |
|
176
|
17833 |
|
public function supportsPartialIndexes() |
|
177
|
|
|
{ |
|
178
|
17833 |
|
return true; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* {@inheritdoc} |
|
183
|
|
|
*/ |
|
184
|
17318 |
|
public function usesSequenceEmulatedIdentityColumns() |
|
185
|
|
|
{ |
|
186
|
17318 |
|
return true; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* {@inheritdoc} |
|
191
|
|
|
*/ |
|
192
|
18229 |
|
public function getIdentitySequenceName($tableName, $columnName) |
|
193
|
|
|
{ |
|
194
|
18229 |
|
return $tableName . '_' . $columnName . '_seq'; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* {@inheritDoc} |
|
199
|
|
|
*/ |
|
200
|
19812 |
|
public function supportsCommentOnStatement() |
|
201
|
|
|
{ |
|
202
|
19812 |
|
return true; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* {@inheritDoc} |
|
207
|
|
|
*/ |
|
208
|
17965 |
|
public function prefersSequences() |
|
209
|
|
|
{ |
|
210
|
17965 |
|
return true; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* {@inheritDoc} |
|
215
|
|
|
*/ |
|
216
|
20162 |
|
public function hasNativeGuidType() |
|
217
|
|
|
{ |
|
218
|
20162 |
|
return true; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* {@inheritDoc} |
|
223
|
|
|
*/ |
|
224
|
5576 |
|
public function getListDatabasesSQL() |
|
225
|
|
|
{ |
|
226
|
5576 |
|
return 'SELECT datname FROM pg_database'; |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* {@inheritDoc} |
|
231
|
|
|
*/ |
|
232
|
5548 |
|
public function getListNamespacesSQL() |
|
233
|
|
|
{ |
|
234
|
5548 |
|
return "SELECT schema_name AS nspname |
|
235
|
|
|
FROM information_schema.schemata |
|
236
|
|
|
WHERE schema_name NOT LIKE 'pg\_%' |
|
237
|
|
|
AND schema_name != 'information_schema'"; |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* {@inheritDoc} |
|
242
|
|
|
*/ |
|
243
|
3977 |
|
public function getListSequencesSQL($database) |
|
244
|
|
|
{ |
|
245
|
3977 |
|
return "SELECT sequence_name AS relname, |
|
246
|
|
|
sequence_schema AS schemaname |
|
247
|
|
|
FROM information_schema.sequences |
|
248
|
|
|
WHERE sequence_schema NOT LIKE 'pg\_%' |
|
249
|
|
|
AND sequence_schema != 'information_schema'"; |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* {@inheritDoc} |
|
254
|
|
|
*/ |
|
255
|
5943 |
|
public function getListTablesSQL() |
|
256
|
|
|
{ |
|
257
|
5943 |
|
return "SELECT quote_ident(table_name) AS table_name, |
|
258
|
|
|
table_schema AS schema_name |
|
259
|
|
|
FROM information_schema.tables |
|
260
|
|
|
WHERE table_schema NOT LIKE 'pg\_%' |
|
261
|
|
|
AND table_schema != 'information_schema' |
|
262
|
|
|
AND table_name != 'geometry_columns' |
|
263
|
|
|
AND table_name != 'spatial_ref_sys' |
|
264
|
|
|
AND table_type != 'VIEW'"; |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
/** |
|
268
|
|
|
* {@inheritDoc} |
|
269
|
|
|
*/ |
|
270
|
5457 |
|
public function getListViewsSQL($database) |
|
271
|
|
|
{ |
|
272
|
5457 |
|
return 'SELECT quote_ident(table_name) AS viewname, |
|
273
|
|
|
table_schema AS schemaname, |
|
274
|
|
|
view_definition AS definition |
|
275
|
|
|
FROM information_schema.views |
|
276
|
|
|
WHERE view_definition IS NOT NULL'; |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
/** |
|
280
|
|
|
* {@inheritDoc} |
|
281
|
|
|
*/ |
|
282
|
18081 |
|
public function getListTableForeignKeysSQL($table, $database = null) |
|
|
|
|
|
|
283
|
|
|
{ |
|
284
|
|
|
return 'SELECT quote_ident(r.conname) as conname, pg_catalog.pg_get_constraintdef(r.oid, true) as condef |
|
285
|
|
|
FROM pg_catalog.pg_constraint r |
|
286
|
|
|
WHERE r.conrelid = |
|
287
|
|
|
( |
|
288
|
|
|
SELECT c.oid |
|
289
|
|
|
FROM pg_catalog.pg_class c, pg_catalog.pg_namespace n |
|
290
|
18081 |
|
WHERE ' . $this->getTableWhereClause($table) . " AND n.oid = c.relnamespace |
|
291
|
|
|
) |
|
292
|
|
|
AND r.contype = 'f'"; |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
|
|
/** |
|
296
|
|
|
* {@inheritDoc} |
|
297
|
|
|
*/ |
|
298
|
5649 |
|
public function getCreateViewSQL($name, $sql) |
|
299
|
|
|
{ |
|
300
|
5649 |
|
return 'CREATE VIEW ' . $name . ' AS ' . $sql; |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
/** |
|
304
|
|
|
* {@inheritDoc} |
|
305
|
|
|
*/ |
|
306
|
5649 |
|
public function getDropViewSQL($name) |
|
307
|
|
|
{ |
|
308
|
5649 |
|
return 'DROP VIEW ' . $name; |
|
309
|
|
|
} |
|
310
|
|
|
|
|
311
|
|
|
/** |
|
312
|
|
|
* {@inheritDoc} |
|
313
|
|
|
*/ |
|
314
|
16561 |
|
public function getListTableConstraintsSQL($table) |
|
315
|
|
|
{ |
|
316
|
16561 |
|
$table = new Identifier($table); |
|
317
|
16561 |
|
$table = $this->quoteStringLiteral($table->getName()); |
|
318
|
|
|
|
|
319
|
16561 |
|
return sprintf( |
|
320
|
|
|
<<<'SQL' |
|
321
|
10 |
|
SELECT |
|
322
|
|
|
quote_ident(relname) as relname |
|
323
|
|
|
FROM |
|
324
|
|
|
pg_class |
|
325
|
|
|
WHERE oid IN ( |
|
326
|
|
|
SELECT indexrelid |
|
327
|
|
|
FROM pg_index, pg_class |
|
328
|
|
|
WHERE pg_class.relname = %s |
|
329
|
|
|
AND pg_class.oid = pg_index.indrelid |
|
330
|
|
|
AND (indisunique = 't' OR indisprimary = 't') |
|
331
|
|
|
) |
|
332
|
|
|
SQL |
|
333
|
|
|
, |
|
334
|
16561 |
|
$table |
|
335
|
|
|
); |
|
336
|
|
|
} |
|
337
|
|
|
|
|
338
|
|
|
/** |
|
339
|
|
|
* {@inheritDoc} |
|
340
|
|
|
* |
|
341
|
|
|
* @link http://ezcomponents.org/docs/api/trunk/DatabaseSchema/ezcDbSchemaPgsqlReader.html |
|
342
|
|
|
*/ |
|
343
|
18021 |
|
public function getListTableIndexesSQL($table, $currentDatabase = null) |
|
344
|
|
|
{ |
|
345
|
|
|
return 'SELECT quote_ident(relname) as relname, pg_index.indisunique, pg_index.indisprimary, |
|
346
|
|
|
pg_index.indkey, pg_index.indrelid, |
|
347
|
|
|
pg_get_expr(indpred, indrelid) AS where |
|
348
|
|
|
FROM pg_class, pg_index |
|
349
|
|
|
WHERE oid IN ( |
|
350
|
|
|
SELECT indexrelid |
|
351
|
|
|
FROM pg_index si, pg_class sc, pg_namespace sn |
|
352
|
18021 |
|
WHERE ' . $this->getTableWhereClause($table, 'sc', 'sn') . ' AND sc.oid=si.indrelid AND sc.relnamespace = sn.oid |
|
353
|
|
|
) AND pg_index.indexrelid = oid'; |
|
354
|
|
|
} |
|
355
|
|
|
|
|
356
|
|
|
/** |
|
357
|
|
|
* @param string $table |
|
358
|
|
|
* @param string $classAlias |
|
359
|
|
|
* @param string $namespaceAlias |
|
360
|
|
|
* |
|
361
|
|
|
* @return string |
|
362
|
|
|
*/ |
|
363
|
18121 |
|
private function getTableWhereClause($table, $classAlias = 'c', $namespaceAlias = 'n') |
|
364
|
|
|
{ |
|
365
|
18121 |
|
$whereClause = $namespaceAlias . ".nspname NOT IN ('pg_catalog', 'information_schema', 'pg_toast') AND "; |
|
366
|
18121 |
|
if (strpos($table, '.') !== false) { |
|
367
|
18015 |
|
[$schema, $table] = explode('.', $table); |
|
368
|
18015 |
|
$schema = $this->quoteStringLiteral($schema); |
|
369
|
|
|
} else { |
|
370
|
18091 |
|
$schema = "ANY(string_to_array((select replace(replace(setting,'\"\$user\"',user),' ','') from pg_catalog.pg_settings where name = 'search_path'),','))"; |
|
371
|
|
|
} |
|
372
|
|
|
|
|
373
|
18121 |
|
$table = new Identifier($table); |
|
374
|
18121 |
|
$table = $this->quoteStringLiteral($table->getName()); |
|
375
|
|
|
|
|
376
|
18121 |
|
return $whereClause . sprintf( |
|
377
|
60 |
|
'%s.relname = %s AND %s.nspname = %s', |
|
378
|
18121 |
|
$classAlias, |
|
379
|
18121 |
|
$table, |
|
380
|
18121 |
|
$namespaceAlias, |
|
381
|
18121 |
|
$schema |
|
382
|
|
|
); |
|
383
|
|
|
} |
|
384
|
|
|
|
|
385
|
|
|
/** |
|
386
|
|
|
* {@inheritDoc} |
|
387
|
|
|
*/ |
|
388
|
17981 |
|
public function getListTableColumnsSQL($table, $database = null) |
|
389
|
|
|
{ |
|
390
|
|
|
return "SELECT |
|
391
|
|
|
a.attnum, |
|
392
|
|
|
quote_ident(a.attname) AS field, |
|
393
|
|
|
t.typname AS type, |
|
394
|
|
|
format_type(a.atttypid, a.atttypmod) AS complete_type, |
|
395
|
|
|
(SELECT t1.typname FROM pg_catalog.pg_type t1 WHERE t1.oid = t.typbasetype) AS domain_type, |
|
396
|
|
|
(SELECT format_type(t2.typbasetype, t2.typtypmod) FROM |
|
397
|
|
|
pg_catalog.pg_type t2 WHERE t2.typtype = 'd' AND t2.oid = a.atttypid) AS domain_complete_type, |
|
398
|
|
|
a.attnotnull AS isnotnull, |
|
399
|
|
|
(SELECT 't' |
|
400
|
|
|
FROM pg_index |
|
401
|
|
|
WHERE c.oid = pg_index.indrelid |
|
402
|
|
|
AND pg_index.indkey[0] = a.attnum |
|
403
|
|
|
AND pg_index.indisprimary = 't' |
|
404
|
|
|
) AS pri, |
|
405
|
|
|
(SELECT pg_get_expr(adbin, adrelid) |
|
406
|
|
|
FROM pg_attrdef |
|
407
|
|
|
WHERE c.oid = pg_attrdef.adrelid |
|
408
|
|
|
AND pg_attrdef.adnum=a.attnum |
|
409
|
|
|
) AS default, |
|
410
|
|
|
(SELECT pg_description.description |
|
411
|
|
|
FROM pg_description WHERE pg_description.objoid = c.oid AND a.attnum = pg_description.objsubid |
|
412
|
|
|
) AS comment |
|
413
|
|
|
FROM pg_attribute a, pg_class c, pg_type t, pg_namespace n |
|
414
|
17981 |
|
WHERE " . $this->getTableWhereClause($table, 'c', 'n') . ' |
|
415
|
|
|
AND a.attnum > 0 |
|
416
|
|
|
AND a.attrelid = c.oid |
|
417
|
|
|
AND a.atttypid = t.oid |
|
418
|
|
|
AND n.oid = c.relnamespace |
|
419
|
|
|
ORDER BY a.attnum'; |
|
420
|
|
|
} |
|
421
|
|
|
|
|
422
|
|
|
/** |
|
423
|
|
|
* {@inheritDoc} |
|
424
|
|
|
*/ |
|
425
|
19690 |
|
public function getCreateDatabaseSQL($name) |
|
426
|
|
|
{ |
|
427
|
19690 |
|
return 'CREATE DATABASE ' . $name; |
|
428
|
|
|
} |
|
429
|
|
|
|
|
430
|
|
|
/** |
|
431
|
|
|
* Returns the SQL statement for disallowing new connections on the given database. |
|
432
|
|
|
* |
|
433
|
|
|
* This is useful to force DROP DATABASE operations which could fail because of active connections. |
|
434
|
|
|
* |
|
435
|
|
|
* @param string $database The name of the database to disallow new connections for. |
|
436
|
|
|
* |
|
437
|
|
|
* @return string |
|
438
|
|
|
*/ |
|
439
|
17906 |
|
public function getDisallowDatabaseConnectionsSQL($database) |
|
440
|
|
|
{ |
|
441
|
17906 |
|
return "UPDATE pg_database SET datallowconn = 'false' WHERE datname = " . $this->quoteStringLiteral($database); |
|
442
|
|
|
} |
|
443
|
|
|
|
|
444
|
|
|
/** |
|
445
|
|
|
* Returns the SQL statement for closing currently active connections on the given database. |
|
446
|
|
|
* |
|
447
|
|
|
* This is useful to force DROP DATABASE operations which could fail because of active connections. |
|
448
|
|
|
* |
|
449
|
|
|
* @param string $database The name of the database to close currently active connections for. |
|
450
|
|
|
* |
|
451
|
|
|
* @return string |
|
452
|
|
|
*/ |
|
453
|
12968 |
|
public function getCloseActiveDatabaseConnectionsSQL($database) |
|
454
|
|
|
{ |
|
455
|
|
|
return 'SELECT pg_terminate_backend(procpid) FROM pg_stat_activity WHERE datname = ' |
|
456
|
12968 |
|
. $this->quoteStringLiteral($database); |
|
457
|
|
|
} |
|
458
|
|
|
|
|
459
|
|
|
/** |
|
460
|
|
|
* {@inheritDoc} |
|
461
|
|
|
*/ |
|
462
|
19699 |
|
public function getAdvancedForeignKeyOptionsSQL(ForeignKeyConstraint $foreignKey) |
|
463
|
|
|
{ |
|
464
|
19699 |
|
$query = ''; |
|
465
|
|
|
|
|
466
|
19699 |
|
if ($foreignKey->hasOption('match')) { |
|
467
|
18343 |
|
$query .= ' MATCH ' . $foreignKey->getOption('match'); |
|
468
|
|
|
} |
|
469
|
|
|
|
|
470
|
19699 |
|
$query .= parent::getAdvancedForeignKeyOptionsSQL($foreignKey); |
|
471
|
|
|
|
|
472
|
19699 |
|
if ($foreignKey->hasOption('deferrable') && $foreignKey->getOption('deferrable') !== false) { |
|
473
|
18343 |
|
$query .= ' DEFERRABLE'; |
|
474
|
|
|
} else { |
|
475
|
19699 |
|
$query .= ' NOT DEFERRABLE'; |
|
476
|
|
|
} |
|
477
|
|
|
|
|
478
|
19699 |
|
if (($foreignKey->hasOption('feferred') && $foreignKey->getOption('feferred') !== false) |
|
479
|
19699 |
|
|| ($foreignKey->hasOption('deferred') && $foreignKey->getOption('deferred') !== false) |
|
480
|
|
|
) { |
|
481
|
18343 |
|
$query .= ' INITIALLY DEFERRED'; |
|
482
|
|
|
} else { |
|
483
|
19699 |
|
$query .= ' INITIALLY IMMEDIATE'; |
|
484
|
|
|
} |
|
485
|
|
|
|
|
486
|
19699 |
|
return $query; |
|
487
|
|
|
} |
|
488
|
|
|
|
|
489
|
|
|
/** |
|
490
|
|
|
* {@inheritDoc} |
|
491
|
|
|
*/ |
|
492
|
18469 |
|
public function getAlterTableSQL(TableDiff $diff) |
|
493
|
|
|
{ |
|
494
|
18469 |
|
$sql = []; |
|
495
|
18469 |
|
$commentsSQL = []; |
|
496
|
18469 |
|
$columnSql = []; |
|
497
|
|
|
|
|
498
|
18469 |
|
foreach ($diff->addedColumns as $column) { |
|
499
|
17091 |
|
if ($this->onSchemaAlterTableAddColumn($column, $diff, $columnSql)) { |
|
500
|
|
|
continue; |
|
501
|
|
|
} |
|
502
|
|
|
|
|
503
|
17091 |
|
$query = 'ADD ' . $this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray()); |
|
504
|
17091 |
|
$sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this) . ' ' . $query; |
|
505
|
|
|
|
|
506
|
17091 |
|
$comment = $this->getColumnComment($column); |
|
507
|
|
|
|
|
508
|
17091 |
|
if ($comment === null || $comment === '') { |
|
509
|
17081 |
|
continue; |
|
510
|
|
|
} |
|
511
|
|
|
|
|
512
|
15535 |
|
$commentsSQL[] = $this->getCommentOnColumnSQL( |
|
513
|
15535 |
|
$diff->getName($this)->getQuotedName($this), |
|
514
|
15535 |
|
$column->getQuotedName($this), |
|
515
|
10 |
|
$comment |
|
516
|
|
|
); |
|
517
|
|
|
} |
|
518
|
|
|
|
|
519
|
18469 |
|
foreach ($diff->removedColumns as $column) { |
|
520
|
18051 |
|
if ($this->onSchemaAlterTableRemoveColumn($column, $diff, $columnSql)) { |
|
521
|
|
|
continue; |
|
522
|
|
|
} |
|
523
|
|
|
|
|
524
|
18051 |
|
$query = 'DROP ' . $column->getQuotedName($this); |
|
525
|
18051 |
|
$sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this) . ' ' . $query; |
|
526
|
|
|
} |
|
527
|
|
|
|
|
528
|
18469 |
|
foreach ($diff->changedColumns as $columnDiff) { |
|
529
|
|
|
/** @var $columnDiff \Doctrine\DBAL\Schema\ColumnDiff */ |
|
530
|
18389 |
|
if ($this->onSchemaAlterTableChangeColumn($columnDiff, $diff, $columnSql)) { |
|
531
|
|
|
continue; |
|
532
|
|
|
} |
|
533
|
|
|
|
|
534
|
18389 |
|
if ($this->isUnchangedBinaryColumn($columnDiff)) { |
|
535
|
16804 |
|
continue; |
|
536
|
|
|
} |
|
537
|
|
|
|
|
538
|
18379 |
|
$oldColumnName = $columnDiff->getOldColumnName()->getQuotedName($this); |
|
539
|
18379 |
|
$column = $columnDiff->column; |
|
540
|
|
|
|
|
541
|
18379 |
|
if ($columnDiff->hasChanged('type') || $columnDiff->hasChanged('precision') || $columnDiff->hasChanged('scale') || $columnDiff->hasChanged('fixed')) { |
|
542
|
18200 |
|
$type = $column->getType(); |
|
543
|
|
|
|
|
544
|
|
|
// SERIAL/BIGSERIAL are not "real" types and we can't alter a column to that type |
|
545
|
18200 |
|
$columnDefinition = $column->toArray(); |
|
546
|
18200 |
|
$columnDefinition['autoincrement'] = false; |
|
547
|
|
|
|
|
548
|
|
|
// here was a server version check before, but DBAL API does not support this anymore. |
|
549
|
18200 |
|
$query = 'ALTER ' . $oldColumnName . ' TYPE ' . $type->getSQLDeclaration($columnDefinition, $this); |
|
550
|
18200 |
|
$sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this) . ' ' . $query; |
|
551
|
|
|
} |
|
552
|
|
|
|
|
553
|
18379 |
|
if ($columnDiff->hasChanged('default') || $this->typeChangeBreaksDefaultValue($columnDiff)) { |
|
554
|
17791 |
|
$defaultClause = $column->getDefault() === null |
|
555
|
17760 |
|
? ' DROP DEFAULT' |
|
556
|
17791 |
|
: ' SET' . $this->getDefaultValueDeclarationSQL($column->toArray()); |
|
557
|
17791 |
|
$query = 'ALTER ' . $oldColumnName . $defaultClause; |
|
558
|
17791 |
|
$sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this) . ' ' . $query; |
|
559
|
|
|
} |
|
560
|
|
|
|
|
561
|
18379 |
|
if ($columnDiff->hasChanged('notnull')) { |
|
562
|
15653 |
|
$query = 'ALTER ' . $oldColumnName . ' ' . ($column->getNotnull() ? 'SET' : 'DROP') . ' NOT NULL'; |
|
563
|
15653 |
|
$sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this) . ' ' . $query; |
|
564
|
|
|
} |
|
565
|
|
|
|
|
566
|
18379 |
|
if ($columnDiff->hasChanged('autoincrement')) { |
|
567
|
5719 |
|
if ($column->getAutoincrement()) { |
|
568
|
|
|
// add autoincrement |
|
569
|
5719 |
|
$seqName = $this->getIdentitySequenceName($diff->name, $oldColumnName); |
|
570
|
|
|
|
|
571
|
5719 |
|
$sql[] = 'CREATE SEQUENCE ' . $seqName; |
|
572
|
5719 |
|
$sql[] = "SELECT setval('" . $seqName . "', (SELECT MAX(" . $oldColumnName . ') FROM ' . $diff->getName($this)->getQuotedName($this) . '))'; |
|
573
|
5719 |
|
$query = 'ALTER ' . $oldColumnName . " SET DEFAULT nextval('" . $seqName . "')"; |
|
574
|
5719 |
|
$sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this) . ' ' . $query; |
|
575
|
|
|
} else { |
|
576
|
|
|
// Drop autoincrement, but do NOT drop the sequence. It might be re-used by other tables or have |
|
577
|
5712 |
|
$query = 'ALTER ' . $oldColumnName . ' DROP DEFAULT'; |
|
578
|
5712 |
|
$sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this) . ' ' . $query; |
|
579
|
|
|
} |
|
580
|
|
|
} |
|
581
|
|
|
|
|
582
|
18379 |
|
$newComment = $this->getColumnComment($column); |
|
583
|
18379 |
|
$oldComment = $this->getOldColumnComment($columnDiff); |
|
584
|
|
|
|
|
585
|
18379 |
|
if ($columnDiff->hasChanged('comment') || ($columnDiff->fromColumn !== null && $oldComment !== $newComment)) { |
|
586
|
17832 |
|
$commentsSQL[] = $this->getCommentOnColumnSQL( |
|
587
|
17832 |
|
$diff->getName($this)->getQuotedName($this), |
|
588
|
17832 |
|
$column->getQuotedName($this), |
|
589
|
30 |
|
$newComment |
|
590
|
|
|
); |
|
591
|
|
|
} |
|
592
|
|
|
|
|
593
|
18379 |
|
if (! $columnDiff->hasChanged('length')) { |
|
594
|
18369 |
|
continue; |
|
595
|
|
|
} |
|
596
|
|
|
|
|
597
|
15022 |
|
$query = 'ALTER ' . $oldColumnName . ' TYPE ' . $column->getType()->getSQLDeclaration($column->toArray(), $this); |
|
598
|
15022 |
|
$sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this) . ' ' . $query; |
|
599
|
|
|
} |
|
600
|
|
|
|
|
601
|
18469 |
|
foreach ($diff->renamedColumns as $oldColumnName => $column) { |
|
602
|
17016 |
|
if ($this->onSchemaAlterTableRenameColumn($oldColumnName, $column, $diff, $columnSql)) { |
|
603
|
|
|
continue; |
|
604
|
|
|
} |
|
605
|
|
|
|
|
606
|
17016 |
|
$oldColumnName = new Identifier($oldColumnName); |
|
607
|
|
|
|
|
608
|
17016 |
|
$sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this) . |
|
609
|
17016 |
|
' RENAME COLUMN ' . $oldColumnName->getQuotedName($this) . ' TO ' . $column->getQuotedName($this); |
|
610
|
|
|
} |
|
611
|
|
|
|
|
612
|
18469 |
|
$tableSql = []; |
|
613
|
|
|
|
|
614
|
18469 |
|
if (! $this->onSchemaAlterTable($diff, $tableSql)) { |
|
615
|
18469 |
|
$sql = array_merge($sql, $commentsSQL); |
|
616
|
|
|
|
|
617
|
18469 |
|
$newName = $diff->getNewName(); |
|
618
|
|
|
|
|
619
|
18469 |
|
if ($newName !== false) { |
|
620
|
15653 |
|
$sql[] = sprintf( |
|
621
|
20 |
|
'ALTER TABLE %s RENAME TO %s', |
|
622
|
15653 |
|
$diff->getName($this)->getQuotedName($this), |
|
623
|
15653 |
|
$newName->getQuotedName($this) |
|
624
|
|
|
); |
|
625
|
|
|
} |
|
626
|
|
|
|
|
627
|
18469 |
|
$sql = array_merge( |
|
628
|
18469 |
|
$this->getPreAlterTableIndexForeignKeySQL($diff), |
|
629
|
18469 |
|
$sql, |
|
630
|
18469 |
|
$this->getPostAlterTableIndexForeignKeySQL($diff) |
|
631
|
|
|
); |
|
632
|
|
|
} |
|
633
|
|
|
|
|
634
|
18469 |
|
return array_merge($sql, $tableSql, $columnSql); |
|
635
|
|
|
} |
|
636
|
|
|
|
|
637
|
|
|
/** |
|
638
|
|
|
* Checks whether a given column diff is a logically unchanged binary type column. |
|
639
|
|
|
* |
|
640
|
|
|
* Used to determine whether a column alteration for a binary type column can be skipped. |
|
641
|
|
|
* Doctrine's {@link \Doctrine\DBAL\Types\BinaryType} and {@link \Doctrine\DBAL\Types\BlobType} |
|
642
|
|
|
* are mapped to the same database column type on this platform as this platform |
|
643
|
|
|
* does not have a native VARBINARY/BINARY column type. Therefore the {@link \Doctrine\DBAL\Schema\Comparator} |
|
644
|
|
|
* might detect differences for binary type columns which do not have to be propagated |
|
645
|
|
|
* to database as there actually is no difference at database level. |
|
646
|
|
|
* |
|
647
|
|
|
* @param ColumnDiff $columnDiff The column diff to check against. |
|
648
|
|
|
* |
|
649
|
|
|
* @return bool True if the given column diff is an unchanged binary type column, false otherwise. |
|
650
|
|
|
*/ |
|
651
|
18389 |
|
private function isUnchangedBinaryColumn(ColumnDiff $columnDiff) |
|
652
|
|
|
{ |
|
653
|
18389 |
|
$columnType = $columnDiff->column->getType(); |
|
654
|
|
|
|
|
655
|
18389 |
|
if (! $columnType instanceof BinaryType && ! $columnType instanceof BlobType) { |
|
656
|
18379 |
|
return false; |
|
657
|
|
|
} |
|
658
|
|
|
|
|
659
|
16804 |
|
$fromColumn = $columnDiff->fromColumn instanceof Column ? $columnDiff->fromColumn : null; |
|
660
|
|
|
|
|
661
|
16804 |
|
if ($fromColumn) { |
|
662
|
16804 |
|
$fromColumnType = $fromColumn->getType(); |
|
663
|
|
|
|
|
664
|
16804 |
|
if (! $fromColumnType instanceof BinaryType && ! $fromColumnType instanceof BlobType) { |
|
665
|
|
|
return false; |
|
666
|
|
|
} |
|
667
|
|
|
|
|
668
|
16804 |
|
return count(array_diff($columnDiff->changedProperties, ['type', 'length', 'fixed'])) === 0; |
|
669
|
|
|
} |
|
670
|
|
|
|
|
671
|
|
|
if ($columnDiff->hasChanged('type')) { |
|
672
|
|
|
return false; |
|
673
|
|
|
} |
|
674
|
|
|
|
|
675
|
|
|
return count(array_diff($columnDiff->changedProperties, ['length', 'fixed'])) === 0; |
|
676
|
|
|
} |
|
677
|
|
|
|
|
678
|
|
|
/** |
|
679
|
|
|
* {@inheritdoc} |
|
680
|
|
|
*/ |
|
681
|
16701 |
|
protected function getRenameIndexSQL($oldIndexName, Index $index, $tableName) |
|
682
|
|
|
{ |
|
683
|
16701 |
|
if (strpos($tableName, '.') !== false) { |
|
684
|
15005 |
|
[$schema] = explode('.', $tableName); |
|
685
|
15005 |
|
$oldIndexName = $schema . '.' . $oldIndexName; |
|
686
|
|
|
} |
|
687
|
|
|
|
|
688
|
16701 |
|
return ['ALTER INDEX ' . $oldIndexName . ' RENAME TO ' . $index->getQuotedName($this)]; |
|
689
|
|
|
} |
|
690
|
|
|
|
|
691
|
|
|
/** |
|
692
|
|
|
* {@inheritdoc} |
|
693
|
|
|
*/ |
|
694
|
18082 |
|
public function getCommentOnColumnSQL($tableName, $columnName, $comment) |
|
695
|
|
|
{ |
|
696
|
18082 |
|
$tableName = new Identifier($tableName); |
|
697
|
18082 |
|
$columnName = new Identifier($columnName); |
|
698
|
18082 |
|
$comment = $comment === null ? 'NULL' : $this->quoteStringLiteral($comment); |
|
699
|
|
|
|
|
700
|
18082 |
|
return sprintf( |
|
701
|
90 |
|
'COMMENT ON COLUMN %s.%s IS %s', |
|
702
|
18082 |
|
$tableName->getQuotedName($this), |
|
703
|
18082 |
|
$columnName->getQuotedName($this), |
|
704
|
18082 |
|
$comment |
|
705
|
|
|
); |
|
706
|
|
|
} |
|
707
|
|
|
|
|
708
|
|
|
/** |
|
709
|
|
|
* {@inheritDoc} |
|
710
|
|
|
*/ |
|
711
|
18909 |
|
public function getCreateSequenceSQL(Sequence $sequence) |
|
712
|
|
|
{ |
|
713
|
18909 |
|
return 'CREATE SEQUENCE ' . $sequence->getQuotedName($this) . |
|
714
|
18909 |
|
' INCREMENT BY ' . $sequence->getAllocationSize() . |
|
715
|
18909 |
|
' MINVALUE ' . $sequence->getInitialValue() . |
|
716
|
18909 |
|
' START ' . $sequence->getInitialValue() . |
|
717
|
18909 |
|
$this->getSequenceCacheSQL($sequence); |
|
718
|
|
|
} |
|
719
|
|
|
|
|
720
|
|
|
/** |
|
721
|
|
|
* {@inheritDoc} |
|
722
|
|
|
*/ |
|
723
|
|
|
public function getAlterSequenceSQL(Sequence $sequence) |
|
724
|
|
|
{ |
|
725
|
|
|
return 'ALTER SEQUENCE ' . $sequence->getQuotedName($this) . |
|
726
|
|
|
' INCREMENT BY ' . $sequence->getAllocationSize() . |
|
727
|
|
|
$this->getSequenceCacheSQL($sequence); |
|
728
|
|
|
} |
|
729
|
|
|
|
|
730
|
|
|
/** |
|
731
|
|
|
* Cache definition for sequences |
|
732
|
|
|
* |
|
733
|
|
|
* @return string |
|
734
|
|
|
*/ |
|
735
|
18909 |
|
private function getSequenceCacheSQL(Sequence $sequence) |
|
736
|
|
|
{ |
|
737
|
18909 |
|
if ($sequence->getCache() > 1) { |
|
738
|
16858 |
|
return ' CACHE ' . $sequence->getCache(); |
|
739
|
|
|
} |
|
740
|
|
|
|
|
741
|
18899 |
|
return ''; |
|
742
|
|
|
} |
|
743
|
|
|
|
|
744
|
|
|
/** |
|
745
|
|
|
* {@inheritDoc} |
|
746
|
|
|
*/ |
|
747
|
18899 |
|
public function getDropSequenceSQL($sequence) |
|
748
|
|
|
{ |
|
749
|
18899 |
|
if ($sequence instanceof Sequence) { |
|
750
|
|
|
$sequence = $sequence->getQuotedName($this); |
|
751
|
|
|
} |
|
752
|
|
|
|
|
753
|
18899 |
|
return 'DROP SEQUENCE ' . $sequence . ' CASCADE'; |
|
754
|
|
|
} |
|
755
|
|
|
|
|
756
|
|
|
/** |
|
757
|
|
|
* {@inheritDoc} |
|
758
|
|
|
*/ |
|
759
|
18138 |
|
public function getCreateSchemaSQL($schemaName) |
|
760
|
|
|
{ |
|
761
|
18138 |
|
return 'CREATE SCHEMA ' . $schemaName; |
|
762
|
|
|
} |
|
763
|
|
|
|
|
764
|
|
|
/** |
|
765
|
|
|
* {@inheritDoc} |
|
766
|
|
|
*/ |
|
767
|
18006 |
|
public function getDropForeignKeySQL($foreignKey, $table) |
|
768
|
|
|
{ |
|
769
|
18006 |
|
return $this->getDropConstraintSQL($foreignKey, $table); |
|
770
|
|
|
} |
|
771
|
|
|
|
|
772
|
|
|
/** |
|
773
|
|
|
* {@inheritDoc} |
|
774
|
|
|
*/ |
|
775
|
19802 |
|
protected function _getCreateTableSQL($tableName, array $columns, array $options = []) |
|
776
|
|
|
{ |
|
777
|
19802 |
|
$queryFields = $this->getColumnDeclarationListSQL($columns); |
|
778
|
|
|
|
|
779
|
19802 |
|
if (isset($options['primary']) && ! empty($options['primary'])) { |
|
780
|
17900 |
|
$keyColumns = array_unique(array_values($options['primary'])); |
|
781
|
17900 |
|
$queryFields .= ', PRIMARY KEY(' . implode(', ', $keyColumns) . ')'; |
|
782
|
|
|
} |
|
783
|
|
|
|
|
784
|
19802 |
|
$query = 'CREATE TABLE ' . $tableName . ' (' . $queryFields . ')'; |
|
785
|
|
|
|
|
786
|
19802 |
|
$sql = [$query]; |
|
787
|
|
|
|
|
788
|
19802 |
|
if (isset($options['indexes']) && ! empty($options['indexes'])) { |
|
789
|
17779 |
|
foreach ($options['indexes'] as $index) { |
|
790
|
17779 |
|
$sql[] = $this->getCreateIndexSQL($index, $tableName); |
|
791
|
|
|
} |
|
792
|
|
|
} |
|
793
|
|
|
|
|
794
|
19802 |
|
if (isset($options['foreignKeys'])) { |
|
795
|
17490 |
|
foreach ((array) $options['foreignKeys'] as $definition) { |
|
796
|
17399 |
|
$sql[] = $this->getCreateForeignKeySQL($definition, $tableName); |
|
797
|
|
|
} |
|
798
|
|
|
} |
|
799
|
|
|
|
|
800
|
19802 |
|
return $sql; |
|
801
|
|
|
} |
|
802
|
|
|
|
|
803
|
|
|
/** |
|
804
|
|
|
* Converts a single boolean value. |
|
805
|
|
|
* |
|
806
|
|
|
* First converts the value to its native PHP boolean type |
|
807
|
|
|
* and passes it to the given callback function to be reconverted |
|
808
|
|
|
* into any custom representation. |
|
809
|
|
|
* |
|
810
|
|
|
* @param mixed $value The value to convert. |
|
811
|
|
|
* @param callable $callback The callback function to use for converting the real boolean value. |
|
812
|
|
|
* |
|
813
|
|
|
* @return mixed |
|
814
|
|
|
* |
|
815
|
|
|
* @throws UnexpectedValueException |
|
816
|
|
|
*/ |
|
817
|
19334 |
|
private function convertSingleBooleanValue($value, $callback) |
|
818
|
|
|
{ |
|
819
|
19334 |
|
if ($value === null) { |
|
820
|
18099 |
|
return $callback(null); |
|
821
|
|
|
} |
|
822
|
|
|
|
|
823
|
19314 |
|
if (is_bool($value) || is_numeric($value)) { |
|
824
|
19204 |
|
return $callback((bool) $value); |
|
825
|
|
|
} |
|
826
|
|
|
|
|
827
|
18383 |
|
if (! is_string($value)) { |
|
828
|
|
|
return $callback(true); |
|
829
|
|
|
} |
|
830
|
|
|
|
|
831
|
|
|
/** |
|
832
|
|
|
* Better safe than sorry: http://php.net/in_array#106319 |
|
833
|
|
|
*/ |
|
834
|
18383 |
|
if (in_array(strtolower(trim($value)), $this->booleanLiterals['false'], true)) { |
|
835
|
18223 |
|
return $callback(false); |
|
836
|
|
|
} |
|
837
|
|
|
|
|
838
|
17934 |
|
if (in_array(strtolower(trim($value)), $this->booleanLiterals['true'], true)) { |
|
839
|
17924 |
|
return $callback(true); |
|
840
|
|
|
} |
|
841
|
|
|
|
|
842
|
17020 |
|
throw new UnexpectedValueException("Unrecognized boolean literal '${value}'"); |
|
843
|
|
|
} |
|
844
|
|
|
|
|
845
|
|
|
/** |
|
846
|
|
|
* Converts one or multiple boolean values. |
|
847
|
|
|
* |
|
848
|
|
|
* First converts the value(s) to their native PHP boolean type |
|
849
|
|
|
* and passes them to the given callback function to be reconverted |
|
850
|
|
|
* into any custom representation. |
|
851
|
|
|
* |
|
852
|
|
|
* @param mixed $item The value(s) to convert. |
|
853
|
|
|
* @param callable $callback The callback function to use for converting the real boolean value(s). |
|
854
|
|
|
* |
|
855
|
|
|
* @return mixed |
|
856
|
|
|
*/ |
|
857
|
19334 |
|
private function doConvertBooleans($item, $callback) |
|
858
|
|
|
{ |
|
859
|
19334 |
|
if (is_array($item)) { |
|
860
|
|
|
foreach ($item as $key => $value) { |
|
861
|
|
|
$item[$key] = $this->convertSingleBooleanValue($value, $callback); |
|
862
|
|
|
} |
|
863
|
|
|
|
|
864
|
|
|
return $item; |
|
865
|
|
|
} |
|
866
|
|
|
|
|
867
|
19334 |
|
return $this->convertSingleBooleanValue($item, $callback); |
|
868
|
|
|
} |
|
869
|
|
|
|
|
870
|
|
|
/** |
|
871
|
|
|
* {@inheritDoc} |
|
872
|
|
|
* |
|
873
|
|
|
* Postgres wants boolean values converted to the strings 'true'/'false'. |
|
874
|
|
|
*/ |
|
875
|
19194 |
|
public function convertBooleans($item) |
|
876
|
|
|
{ |
|
877
|
19194 |
|
if (! $this->useBooleanTrueFalseStrings) { |
|
878
|
17624 |
|
return parent::convertBooleans($item); |
|
879
|
|
|
} |
|
880
|
|
|
|
|
881
|
19174 |
|
return $this->doConvertBooleans( |
|
882
|
19174 |
|
$item, |
|
883
|
|
|
static function ($boolean) { |
|
884
|
19174 |
|
if ($boolean === null) { |
|
885
|
17641 |
|
return 'NULL'; |
|
886
|
|
|
} |
|
887
|
|
|
|
|
888
|
19164 |
|
return $boolean === true ? 'true' : 'false'; |
|
889
|
19174 |
|
} |
|
890
|
|
|
); |
|
891
|
|
|
} |
|
892
|
|
|
|
|
893
|
|
|
/** |
|
894
|
|
|
* {@inheritDoc} |
|
895
|
|
|
*/ |
|
896
|
18223 |
|
public function convertBooleansToDatabaseValue($item) |
|
897
|
|
|
{ |
|
898
|
18223 |
|
if (! $this->useBooleanTrueFalseStrings) { |
|
899
|
17317 |
|
return parent::convertBooleansToDatabaseValue($item); |
|
900
|
|
|
} |
|
901
|
|
|
|
|
902
|
18213 |
|
return $this->doConvertBooleans( |
|
903
|
18213 |
|
$item, |
|
904
|
|
|
static function ($boolean) { |
|
905
|
18203 |
|
return $boolean === null ? null : (int) $boolean; |
|
906
|
18213 |
|
} |
|
907
|
|
|
); |
|
908
|
|
|
} |
|
909
|
|
|
|
|
910
|
|
|
/** |
|
911
|
|
|
* {@inheritDoc} |
|
912
|
|
|
*/ |
|
913
|
17948 |
|
public function convertFromBoolean($item) |
|
914
|
|
|
{ |
|
915
|
17948 |
|
if (in_array(strtolower($item), $this->booleanLiterals['false'], true)) { |
|
916
|
17205 |
|
return false; |
|
917
|
|
|
} |
|
918
|
|
|
|
|
919
|
17888 |
|
return parent::convertFromBoolean($item); |
|
920
|
|
|
} |
|
921
|
|
|
|
|
922
|
|
|
/** |
|
923
|
|
|
* {@inheritDoc} |
|
924
|
|
|
*/ |
|
925
|
18146 |
|
public function getSequenceNextValSQL($sequenceName) |
|
926
|
|
|
{ |
|
927
|
18146 |
|
return "SELECT NEXTVAL('" . $sequenceName . "')"; |
|
928
|
|
|
} |
|
929
|
|
|
|
|
930
|
|
|
/** |
|
931
|
|
|
* {@inheritDoc} |
|
932
|
|
|
*/ |
|
933
|
18289 |
|
public function getSetTransactionIsolationSQL($level) |
|
934
|
|
|
{ |
|
935
|
|
|
return 'SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL ' |
|
936
|
18289 |
|
. $this->_getTransactionIsolationLevelSQL($level); |
|
937
|
|
|
} |
|
938
|
|
|
|
|
939
|
|
|
/** |
|
940
|
|
|
* {@inheritDoc} |
|
941
|
|
|
*/ |
|
942
|
17344 |
|
public function getBooleanTypeDeclarationSQL(array $field) |
|
943
|
|
|
{ |
|
944
|
17344 |
|
return 'BOOLEAN'; |
|
945
|
|
|
} |
|
946
|
|
|
|
|
947
|
|
|
/** |
|
948
|
|
|
* {@inheritDoc} |
|
949
|
|
|
*/ |
|
950
|
19770 |
|
public function getIntegerTypeDeclarationSQL(array $field) |
|
951
|
|
|
{ |
|
952
|
19770 |
|
if (! empty($field['autoincrement'])) { |
|
953
|
19542 |
|
return 'SERIAL'; |
|
954
|
|
|
} |
|
955
|
|
|
|
|
956
|
19590 |
|
return 'INT'; |
|
957
|
|
|
} |
|
958
|
|
|
|
|
959
|
|
|
/** |
|
960
|
|
|
* {@inheritDoc} |
|
961
|
|
|
*/ |
|
962
|
19105 |
|
public function getBigIntTypeDeclarationSQL(array $field) |
|
963
|
|
|
{ |
|
964
|
19105 |
|
if (! empty($field['autoincrement'])) { |
|
965
|
19091 |
|
return 'BIGSERIAL'; |
|
966
|
|
|
} |
|
967
|
|
|
|
|
968
|
5625 |
|
return 'BIGINT'; |
|
969
|
|
|
} |
|
970
|
|
|
|
|
971
|
|
|
/** |
|
972
|
|
|
* {@inheritDoc} |
|
973
|
|
|
*/ |
|
974
|
19231 |
|
public function getSmallIntTypeDeclarationSQL(array $field) |
|
975
|
|
|
{ |
|
976
|
19231 |
|
return 'SMALLINT'; |
|
977
|
|
|
} |
|
978
|
|
|
|
|
979
|
|
|
/** |
|
980
|
|
|
* {@inheritDoc} |
|
981
|
|
|
*/ |
|
982
|
16750 |
|
public function getGuidTypeDeclarationSQL(array $field) |
|
983
|
|
|
{ |
|
984
|
16750 |
|
return 'UUID'; |
|
985
|
|
|
} |
|
986
|
|
|
|
|
987
|
|
|
/** |
|
988
|
|
|
* {@inheritDoc} |
|
989
|
|
|
*/ |
|
990
|
18474 |
|
public function getDateTimeTypeDeclarationSQL(array $fieldDeclaration) |
|
991
|
|
|
{ |
|
992
|
18474 |
|
return 'TIMESTAMP(0) WITHOUT TIME ZONE'; |
|
993
|
|
|
} |
|
994
|
|
|
|
|
995
|
|
|
/** |
|
996
|
|
|
* {@inheritDoc} |
|
997
|
|
|
*/ |
|
998
|
4998 |
|
public function getDateTimeTzTypeDeclarationSQL(array $fieldDeclaration) |
|
999
|
|
|
{ |
|
1000
|
4998 |
|
return 'TIMESTAMP(0) WITH TIME ZONE'; |
|
1001
|
|
|
} |
|
1002
|
|
|
|
|
1003
|
|
|
/** |
|
1004
|
|
|
* {@inheritDoc} |
|
1005
|
|
|
*/ |
|
1006
|
5534 |
|
public function getDateTypeDeclarationSQL(array $fieldDeclaration) |
|
1007
|
|
|
{ |
|
1008
|
5534 |
|
return 'DATE'; |
|
1009
|
|
|
} |
|
1010
|
|
|
|
|
1011
|
|
|
/** |
|
1012
|
|
|
* {@inheritDoc} |
|
1013
|
|
|
*/ |
|
1014
|
5534 |
|
public function getTimeTypeDeclarationSQL(array $fieldDeclaration) |
|
1015
|
|
|
{ |
|
1016
|
5534 |
|
return 'TIME(0) WITHOUT TIME ZONE'; |
|
1017
|
|
|
} |
|
1018
|
|
|
|
|
1019
|
|
|
/** |
|
1020
|
|
|
* {@inheritDoc} |
|
1021
|
|
|
* |
|
1022
|
|
|
* @deprecated Use application-generated UUIDs instead |
|
1023
|
|
|
*/ |
|
1024
|
|
|
public function getGuidExpression() |
|
1025
|
|
|
{ |
|
1026
|
|
|
return 'UUID_GENERATE_V4()'; |
|
1027
|
|
|
} |
|
1028
|
|
|
|
|
1029
|
|
|
/** |
|
1030
|
|
|
* {@inheritDoc} |
|
1031
|
|
|
*/ |
|
1032
|
|
|
protected function _getCommonIntegerTypeDeclarationSQL(array $columnDef) |
|
1033
|
|
|
{ |
|
1034
|
|
|
return ''; |
|
1035
|
|
|
} |
|
1036
|
|
|
|
|
1037
|
|
|
/** |
|
1038
|
|
|
* {@inheritDoc} |
|
1039
|
|
|
*/ |
|
1040
|
19566 |
|
protected function getVarcharTypeDeclarationSQLSnippet($length, $fixed) |
|
1041
|
|
|
{ |
|
1042
|
19566 |
|
return $fixed ? ($length ? 'CHAR(' . $length . ')' : 'CHAR(255)') |
|
1043
|
19566 |
|
: ($length ? 'VARCHAR(' . $length . ')' : 'VARCHAR(255)'); |
|
1044
|
|
|
} |
|
1045
|
|
|
|
|
1046
|
|
|
/** |
|
1047
|
|
|
* {@inheritdoc} |
|
1048
|
|
|
*/ |
|
1049
|
18133 |
|
protected function getBinaryTypeDeclarationSQLSnippet($length, $fixed) |
|
1050
|
|
|
{ |
|
1051
|
18133 |
|
return 'BYTEA'; |
|
1052
|
|
|
} |
|
1053
|
|
|
|
|
1054
|
|
|
/** |
|
1055
|
|
|
* {@inheritDoc} |
|
1056
|
|
|
*/ |
|
1057
|
17654 |
|
public function getClobTypeDeclarationSQL(array $field) |
|
1058
|
|
|
{ |
|
1059
|
17654 |
|
return 'TEXT'; |
|
1060
|
|
|
} |
|
1061
|
|
|
|
|
1062
|
|
|
/** |
|
1063
|
|
|
* {@inheritDoc} |
|
1064
|
|
|
*/ |
|
1065
|
18298 |
|
public function getName() |
|
1066
|
|
|
{ |
|
1067
|
18298 |
|
return 'postgresql'; |
|
1068
|
|
|
} |
|
1069
|
|
|
|
|
1070
|
|
|
/** |
|
1071
|
|
|
* {@inheritDoc} |
|
1072
|
|
|
* |
|
1073
|
|
|
* PostgreSQL returns all column names in SQL result sets in lowercase. |
|
1074
|
|
|
*/ |
|
1075
|
|
|
public function getSQLResultCasing($column) |
|
1076
|
|
|
{ |
|
1077
|
|
|
return strtolower($column); |
|
1078
|
|
|
} |
|
1079
|
|
|
|
|
1080
|
|
|
/** |
|
1081
|
|
|
* {@inheritDoc} |
|
1082
|
|
|
*/ |
|
1083
|
4914 |
|
public function getDateTimeTzFormatString() |
|
1084
|
|
|
{ |
|
1085
|
4914 |
|
return 'Y-m-d H:i:sO'; |
|
1086
|
|
|
} |
|
1087
|
|
|
|
|
1088
|
|
|
/** |
|
1089
|
|
|
* {@inheritDoc} |
|
1090
|
|
|
*/ |
|
1091
|
4788 |
|
public function getEmptyIdentityInsertSQL($quotedTableName, $quotedIdentifierColumnName) |
|
1092
|
|
|
{ |
|
1093
|
4788 |
|
return 'INSERT INTO ' . $quotedTableName . ' (' . $quotedIdentifierColumnName . ') VALUES (DEFAULT)'; |
|
1094
|
|
|
} |
|
1095
|
|
|
|
|
1096
|
|
|
/** |
|
1097
|
|
|
* {@inheritDoc} |
|
1098
|
|
|
*/ |
|
1099
|
17338 |
|
public function getTruncateTableSQL($tableName, $cascade = false) |
|
1100
|
|
|
{ |
|
1101
|
17338 |
|
$tableIdentifier = new Identifier($tableName); |
|
1102
|
17338 |
|
$sql = 'TRUNCATE ' . $tableIdentifier->getQuotedName($this); |
|
1103
|
|
|
|
|
1104
|
17338 |
|
if ($cascade) { |
|
1105
|
|
|
$sql .= ' CASCADE'; |
|
1106
|
|
|
} |
|
1107
|
|
|
|
|
1108
|
17338 |
|
return $sql; |
|
1109
|
|
|
} |
|
1110
|
|
|
|
|
1111
|
|
|
/** |
|
1112
|
|
|
* {@inheritDoc} |
|
1113
|
|
|
*/ |
|
1114
|
|
|
public function getReadLockSQL() |
|
1115
|
|
|
{ |
|
1116
|
|
|
return 'FOR SHARE'; |
|
1117
|
|
|
} |
|
1118
|
|
|
|
|
1119
|
|
|
/** |
|
1120
|
|
|
* {@inheritDoc} |
|
1121
|
|
|
*/ |
|
1122
|
19437 |
|
protected function initializeDoctrineTypeMappings() |
|
1123
|
|
|
{ |
|
1124
|
19437 |
|
$this->doctrineTypeMapping = [ |
|
1125
|
|
|
'smallint' => 'smallint', |
|
1126
|
|
|
'int2' => 'smallint', |
|
1127
|
|
|
'serial' => 'integer', |
|
1128
|
|
|
'serial4' => 'integer', |
|
1129
|
|
|
'int' => 'integer', |
|
1130
|
|
|
'int4' => 'integer', |
|
1131
|
|
|
'integer' => 'integer', |
|
1132
|
|
|
'bigserial' => 'bigint', |
|
1133
|
|
|
'serial8' => 'bigint', |
|
1134
|
|
|
'bigint' => 'bigint', |
|
1135
|
|
|
'int8' => 'bigint', |
|
1136
|
|
|
'bool' => 'boolean', |
|
1137
|
|
|
'boolean' => 'boolean', |
|
1138
|
|
|
'text' => 'text', |
|
1139
|
|
|
'tsvector' => 'text', |
|
1140
|
|
|
'varchar' => 'string', |
|
1141
|
|
|
'interval' => 'string', |
|
1142
|
|
|
'_varchar' => 'string', |
|
1143
|
|
|
'char' => 'string', |
|
1144
|
|
|
'bpchar' => 'string', |
|
1145
|
|
|
'inet' => 'string', |
|
1146
|
|
|
'date' => 'date', |
|
1147
|
|
|
'datetime' => 'datetime', |
|
1148
|
|
|
'timestamp' => 'datetime', |
|
1149
|
|
|
'timestamptz' => 'datetimetz', |
|
1150
|
|
|
'time' => 'time', |
|
1151
|
|
|
'timetz' => 'time', |
|
1152
|
|
|
'float' => 'float', |
|
1153
|
|
|
'float4' => 'float', |
|
1154
|
|
|
'float8' => 'float', |
|
1155
|
|
|
'double' => 'float', |
|
1156
|
|
|
'double precision' => 'float', |
|
1157
|
|
|
'real' => 'float', |
|
1158
|
|
|
'decimal' => 'decimal', |
|
1159
|
|
|
'money' => 'decimal', |
|
1160
|
|
|
'numeric' => 'decimal', |
|
1161
|
|
|
'year' => 'date', |
|
1162
|
|
|
'uuid' => 'guid', |
|
1163
|
|
|
'bytea' => 'blob', |
|
1164
|
|
|
]; |
|
1165
|
19437 |
|
} |
|
1166
|
|
|
|
|
1167
|
|
|
/** |
|
1168
|
|
|
* {@inheritDoc} |
|
1169
|
|
|
*/ |
|
1170
|
19566 |
|
public function getVarcharMaxLength() |
|
1171
|
|
|
{ |
|
1172
|
19566 |
|
return 65535; |
|
1173
|
|
|
} |
|
1174
|
|
|
|
|
1175
|
|
|
/** |
|
1176
|
|
|
* {@inheritdoc} |
|
1177
|
|
|
*/ |
|
1178
|
18143 |
|
public function getBinaryMaxLength() |
|
1179
|
|
|
{ |
|
1180
|
18143 |
|
return 0; |
|
1181
|
|
|
} |
|
1182
|
|
|
|
|
1183
|
|
|
/** |
|
1184
|
|
|
* {@inheritdoc} |
|
1185
|
|
|
*/ |
|
1186
|
18143 |
|
public function getBinaryDefaultLength() |
|
1187
|
|
|
{ |
|
1188
|
18143 |
|
return 0; |
|
1189
|
|
|
} |
|
1190
|
|
|
|
|
1191
|
|
|
/** |
|
1192
|
|
|
* {@inheritDoc} |
|
1193
|
|
|
*/ |
|
1194
|
3830 |
|
protected function getReservedKeywordsClass() |
|
1195
|
|
|
{ |
|
1196
|
3830 |
|
return Keywords\PostgreSQLKeywords::class; |
|
1197
|
|
|
} |
|
1198
|
|
|
|
|
1199
|
|
|
/** |
|
1200
|
|
|
* {@inheritDoc} |
|
1201
|
|
|
*/ |
|
1202
|
18630 |
|
public function getBlobTypeDeclarationSQL(array $field) |
|
1203
|
|
|
{ |
|
1204
|
18630 |
|
return 'BYTEA'; |
|
1205
|
|
|
} |
|
1206
|
|
|
|
|
1207
|
|
|
/** |
|
1208
|
|
|
* {@inheritdoc} |
|
1209
|
|
|
*/ |
|
1210
|
19902 |
|
public function getDefaultValueDeclarationSQL($field) |
|
1211
|
|
|
{ |
|
1212
|
19902 |
|
if ($this->isSerialField($field)) { |
|
1213
|
19572 |
|
return ''; |
|
1214
|
|
|
} |
|
1215
|
|
|
|
|
1216
|
18032 |
|
return parent::getDefaultValueDeclarationSQL($field); |
|
1217
|
|
|
} |
|
1218
|
|
|
|
|
1219
|
|
|
/** |
|
1220
|
|
|
* @param mixed[] $field |
|
1221
|
|
|
*/ |
|
1222
|
19902 |
|
private function isSerialField(array $field) : bool |
|
1223
|
|
|
{ |
|
1224
|
19902 |
|
return isset($field['type'], $field['autoincrement']) |
|
1225
|
19902 |
|
&& $field['autoincrement'] === true |
|
1226
|
19902 |
|
&& $this->isNumericType($field['type']); |
|
1227
|
|
|
} |
|
1228
|
|
|
|
|
1229
|
|
|
/** |
|
1230
|
|
|
* Check whether the type of a column is changed in a way that invalidates the default value for the column |
|
1231
|
|
|
*/ |
|
1232
|
18369 |
|
private function typeChangeBreaksDefaultValue(ColumnDiff $columnDiff) : bool |
|
1233
|
|
|
{ |
|
1234
|
18369 |
|
if (! $columnDiff->fromColumn) { |
|
1235
|
17006 |
|
return $columnDiff->hasChanged('type'); |
|
1236
|
|
|
} |
|
1237
|
|
|
|
|
1238
|
18139 |
|
$oldTypeIsNumeric = $this->isNumericType($columnDiff->fromColumn->getType()); |
|
1239
|
18139 |
|
$newTypeIsNumeric = $this->isNumericType($columnDiff->column->getType()); |
|
1240
|
|
|
|
|
1241
|
|
|
// default should not be changed when switching between numeric types and the default comes from a sequence |
|
1242
|
18139 |
|
return $columnDiff->hasChanged('type') |
|
1243
|
18139 |
|
&& ! ($oldTypeIsNumeric && $newTypeIsNumeric && $columnDiff->column->getAutoincrement()); |
|
1244
|
|
|
} |
|
1245
|
|
|
|
|
1246
|
19612 |
|
private function isNumericType(Type $type) : bool |
|
1247
|
|
|
{ |
|
1248
|
19612 |
|
return $type instanceof IntegerType || $type instanceof BigIntType; |
|
1249
|
|
|
} |
|
1250
|
|
|
|
|
1251
|
18379 |
|
private function getOldColumnComment(ColumnDiff $columnDiff) : ?string |
|
1252
|
|
|
{ |
|
1253
|
18379 |
|
return $columnDiff->fromColumn ? $this->getColumnComment($columnDiff->fromColumn) : null; |
|
1254
|
|
|
} |
|
1255
|
|
|
|
|
1256
|
|
|
public function getListTableMetadataSQL(string $table, ?string $schema = null) : string |
|
1257
|
|
|
{ |
|
1258
|
|
|
if ($schema !== null) { |
|
1259
|
|
|
$table = $schema . '.' . $table; |
|
1260
|
|
|
} |
|
1261
|
|
|
|
|
1262
|
|
|
return sprintf( |
|
1263
|
|
|
<<<'SQL' |
|
1264
|
|
|
SELECT obj_description(%s::regclass) AS table_comment; |
|
1265
|
|
|
SQL |
|
1266
|
|
|
, |
|
1267
|
|
|
$this->quoteStringLiteral($table) |
|
1268
|
|
|
); |
|
1269
|
|
|
} |
|
1270
|
|
|
} |
|
1271
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.