1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\DBAL\Schema; |
6
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Exception\DriverException; |
8
|
|
|
use Doctrine\DBAL\FetchMode; |
9
|
|
|
use Doctrine\DBAL\Platforms\PostgreSqlPlatform; |
10
|
|
|
use Doctrine\DBAL\Types\Type; |
11
|
|
|
use Doctrine\DBAL\Types\Types; |
12
|
|
|
use const CASE_LOWER; |
13
|
|
|
use function array_change_key_case; |
14
|
|
|
use function array_filter; |
15
|
|
|
use function array_keys; |
16
|
|
|
use function array_map; |
17
|
|
|
use function array_shift; |
18
|
|
|
use function assert; |
19
|
|
|
use function explode; |
20
|
|
|
use function implode; |
21
|
|
|
use function in_array; |
22
|
|
|
use function preg_match; |
23
|
|
|
use function preg_replace; |
24
|
|
|
use function sprintf; |
25
|
|
|
use function str_replace; |
26
|
|
|
use function stripos; |
27
|
|
|
use function strlen; |
28
|
|
|
use function strpos; |
29
|
|
|
use function strtolower; |
30
|
|
|
use function trim; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* PostgreSQL Schema Manager. |
34
|
|
|
*/ |
35
|
|
|
class PostgreSqlSchemaManager extends AbstractSchemaManager |
36
|
|
|
{ |
37
|
|
|
/** @var string[] */ |
38
|
|
|
private $existingSchemaPaths; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Gets all the existing schema names. |
42
|
|
|
* |
43
|
|
|
* @return string[] |
44
|
|
|
*/ |
45
|
472 |
|
public function getSchemaNames() |
46
|
|
|
{ |
47
|
472 |
|
$statement = $this->_conn->executeQuery("SELECT nspname FROM pg_namespace WHERE nspname !~ '^pg_.*' AND nspname != 'information_schema'"); |
48
|
|
|
|
49
|
472 |
|
return $statement->fetchAll(FetchMode::COLUMN); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Returns an array of schema search paths. |
54
|
|
|
* |
55
|
|
|
* This is a PostgreSQL only function. |
56
|
|
|
* |
57
|
|
|
* @return string[] |
58
|
|
|
*/ |
59
|
472 |
|
public function getSchemaSearchPaths() |
60
|
|
|
{ |
61
|
472 |
|
$params = $this->_conn->getParams(); |
62
|
472 |
|
$schema = explode(',', $this->_conn->fetchColumn('SHOW search_path')); |
|
|
|
|
63
|
|
|
|
64
|
472 |
|
if (isset($params['user'])) { |
65
|
472 |
|
$schema = str_replace('"$user"', $params['user'], $schema); |
66
|
|
|
} |
67
|
|
|
|
68
|
472 |
|
return array_map('trim', $schema); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Gets names of all existing schemas in the current users search path. |
73
|
|
|
* |
74
|
|
|
* This is a PostgreSQL only function. |
75
|
|
|
* |
76
|
|
|
* @return string[] |
77
|
|
|
*/ |
78
|
472 |
|
public function getExistingSchemaSearchPaths() |
79
|
|
|
{ |
80
|
472 |
|
if ($this->existingSchemaPaths === null) { |
81
|
472 |
|
$this->determineExistingSchemaSearchPaths(); |
82
|
|
|
} |
83
|
|
|
|
84
|
472 |
|
return $this->existingSchemaPaths; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Sets or resets the order of the existing schemas in the current search path of the user. |
89
|
|
|
* |
90
|
|
|
* This is a PostgreSQL only function. |
91
|
|
|
* |
92
|
|
|
* @return void |
93
|
|
|
*/ |
94
|
472 |
|
public function determineExistingSchemaSearchPaths() |
95
|
|
|
{ |
96
|
472 |
|
$names = $this->getSchemaNames(); |
97
|
472 |
|
$paths = $this->getSchemaSearchPaths(); |
98
|
|
|
|
99
|
|
|
$this->existingSchemaPaths = array_filter($paths, static function ($v) use ($names) { |
100
|
472 |
|
return in_array($v, $names); |
101
|
472 |
|
}); |
102
|
472 |
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* {@inheritdoc} |
106
|
|
|
*/ |
107
|
472 |
|
public function dropDatabase($database) |
108
|
|
|
{ |
109
|
|
|
try { |
110
|
472 |
|
parent::dropDatabase($database); |
111
|
472 |
|
} catch (DriverException $exception) { |
112
|
|
|
// If we have a SQLSTATE 55006, the drop database operation failed |
113
|
|
|
// because of active connections on the database. |
114
|
|
|
// To force dropping the database, we first have to close all active connections |
115
|
|
|
// on that database and issue the drop database operation again. |
116
|
472 |
|
if ($exception->getSQLState() !== '55006') { |
117
|
472 |
|
throw $exception; |
118
|
|
|
} |
119
|
|
|
|
120
|
275 |
|
assert($this->_platform instanceof PostgreSqlPlatform); |
121
|
|
|
|
122
|
275 |
|
$this->_execSql( |
123
|
|
|
[ |
124
|
275 |
|
$this->_platform->getDisallowDatabaseConnectionsSQL($database), |
125
|
275 |
|
$this->_platform->getCloseActiveDatabaseConnectionsSQL($database), |
126
|
|
|
] |
127
|
|
|
); |
128
|
|
|
|
129
|
275 |
|
parent::dropDatabase($database); |
130
|
|
|
} |
131
|
275 |
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* {@inheritdoc} |
135
|
|
|
*/ |
136
|
382 |
|
protected function _getPortableTableForeignKeyDefinition($tableForeignKey) |
137
|
|
|
{ |
138
|
382 |
|
$onUpdate = null; |
139
|
382 |
|
$onDelete = null; |
140
|
382 |
|
$localColumns = []; |
141
|
382 |
|
$foreignColumns = []; |
142
|
382 |
|
$foreignTable = null; |
143
|
|
|
|
144
|
382 |
|
if (preg_match('(ON UPDATE ([a-zA-Z0-9]+( (NULL|ACTION|DEFAULT))?))', $tableForeignKey['condef'], $match)) { |
145
|
376 |
|
$onUpdate = $match[1]; |
146
|
|
|
} |
147
|
382 |
|
if (preg_match('(ON DELETE ([a-zA-Z0-9]+( (NULL|ACTION|DEFAULT))?))', $tableForeignKey['condef'], $match)) { |
148
|
364 |
|
$onDelete = $match[1]; |
149
|
|
|
} |
150
|
|
|
|
151
|
382 |
|
if (preg_match('/FOREIGN KEY \((.+)\) REFERENCES (.+)\((.+)\)/', $tableForeignKey['condef'], $values)) { |
152
|
|
|
// PostgreSQL returns identifiers that are keywords with quotes, we need them later, don't get |
153
|
|
|
// the idea to trim them here. |
154
|
382 |
|
$localColumns = array_map('trim', explode(',', $values[1])); |
155
|
382 |
|
$foreignColumns = array_map('trim', explode(',', $values[3])); |
156
|
382 |
|
$foreignTable = $values[2]; |
157
|
|
|
} |
158
|
|
|
|
159
|
382 |
|
return new ForeignKeyConstraint( |
160
|
382 |
|
$localColumns, |
161
|
|
|
$foreignTable, |
162
|
|
|
$foreignColumns, |
163
|
382 |
|
$tableForeignKey['conname'], |
164
|
382 |
|
['onUpdate' => $onUpdate, 'onDelete' => $onDelete] |
165
|
|
|
); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* {@inheritdoc} |
170
|
|
|
*/ |
171
|
|
|
protected function _getPortableTriggerDefinition($trigger) |
172
|
|
|
{ |
173
|
|
|
return $trigger['trigger_name']; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* {@inheritdoc} |
178
|
|
|
*/ |
179
|
173 |
|
protected function _getPortableViewDefinition($view) |
180
|
|
|
{ |
181
|
173 |
|
return new View($view['schemaname'] . '.' . $view['viewname'], $view['definition']); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* {@inheritdoc} |
186
|
|
|
*/ |
187
|
|
|
protected function _getPortableUserDefinition($user) |
188
|
|
|
{ |
189
|
|
|
return [ |
190
|
|
|
'user' => $user['usename'], |
191
|
|
|
'password' => $user['passwd'], |
192
|
|
|
]; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* {@inheritdoc} |
197
|
|
|
*/ |
198
|
472 |
|
protected function _getPortableTableDefinition($table) |
199
|
|
|
{ |
200
|
472 |
|
$schemas = $this->getExistingSchemaSearchPaths(); |
201
|
472 |
|
$firstSchema = array_shift($schemas); |
202
|
|
|
|
203
|
472 |
|
if ($table['schema_name'] === $firstSchema) { |
204
|
472 |
|
return $table['table_name']; |
205
|
|
|
} |
206
|
|
|
|
207
|
382 |
|
return $table['schema_name'] . '.' . $table['table_name']; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* {@inheritdoc} |
212
|
|
|
* |
213
|
|
|
* @link http://ezcomponents.org/docs/api/trunk/DatabaseSchema/ezcDbSchemaPgsqlReader.html |
214
|
|
|
*/ |
215
|
430 |
|
protected function _getPortableTableIndexesList(array $tableIndexRows, string $tableName) : array |
216
|
|
|
{ |
217
|
430 |
|
$buffer = []; |
218
|
430 |
|
foreach ($tableIndexRows as $row) { |
219
|
406 |
|
$colNumbers = array_map('intval', explode(' ', $row['indkey'])); |
220
|
406 |
|
$columnNameSql = sprintf( |
221
|
|
|
'SELECT attnum, attname FROM pg_attribute WHERE attrelid=%d AND attnum IN (%s) ORDER BY attnum ASC', |
222
|
406 |
|
$row['indrelid'], |
223
|
406 |
|
implode(' ,', $colNumbers) |
224
|
|
|
); |
225
|
|
|
|
226
|
406 |
|
$stmt = $this->_conn->executeQuery($columnNameSql); |
227
|
406 |
|
$indexColumns = $stmt->fetchAll(); |
228
|
|
|
|
229
|
|
|
// required for getting the order of the columns right. |
230
|
406 |
|
foreach ($colNumbers as $colNum) { |
231
|
406 |
|
foreach ($indexColumns as $colRow) { |
232
|
406 |
|
if ($colNum !== $colRow['attnum']) { |
233
|
328 |
|
continue; |
234
|
|
|
} |
235
|
|
|
|
236
|
406 |
|
$buffer[] = [ |
237
|
406 |
|
'key_name' => $row['relname'], |
238
|
406 |
|
'column_name' => trim($colRow['attname']), |
239
|
406 |
|
'non_unique' => ! $row['indisunique'], |
240
|
406 |
|
'primary' => $row['indisprimary'], |
241
|
406 |
|
'where' => $row['where'], |
242
|
|
|
]; |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
|
247
|
430 |
|
return parent::_getPortableTableIndexesList($buffer, $tableName); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* {@inheritdoc} |
252
|
|
|
*/ |
253
|
275 |
|
protected function _getPortableDatabaseDefinition($database) |
254
|
|
|
{ |
255
|
275 |
|
return $database['datname']; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* {@inheritdoc} |
260
|
|
|
*/ |
261
|
269 |
|
protected function _getPortableSequencesList($sequences) |
262
|
|
|
{ |
263
|
269 |
|
$sequenceDefinitions = []; |
264
|
|
|
|
265
|
269 |
|
foreach ($sequences as $sequence) { |
266
|
269 |
|
if ($sequence['schemaname'] !== 'public') { |
267
|
269 |
|
$sequenceName = $sequence['schemaname'] . '.' . $sequence['relname']; |
268
|
|
|
} else { |
269
|
269 |
|
$sequenceName = $sequence['relname']; |
270
|
|
|
} |
271
|
|
|
|
272
|
269 |
|
$sequenceDefinitions[$sequenceName] = $sequence; |
273
|
|
|
} |
274
|
|
|
|
275
|
269 |
|
$list = []; |
276
|
|
|
|
277
|
269 |
|
foreach ($this->filterAssetNames(array_keys($sequenceDefinitions)) as $sequenceName) { |
278
|
269 |
|
$list[] = $this->_getPortableSequenceDefinition($sequenceDefinitions[$sequenceName]); |
279
|
|
|
} |
280
|
|
|
|
281
|
269 |
|
return $list; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* {@inheritdoc} |
286
|
|
|
*/ |
287
|
251 |
|
protected function getPortableNamespaceDefinition(array $namespace) |
288
|
|
|
{ |
289
|
251 |
|
return $namespace['nspname']; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* {@inheritdoc} |
294
|
|
|
*/ |
295
|
269 |
|
protected function _getPortableSequenceDefinition($sequence) |
296
|
|
|
{ |
297
|
269 |
|
if ($sequence['schemaname'] !== 'public') { |
298
|
269 |
|
$sequenceName = $sequence['schemaname'] . '.' . $sequence['relname']; |
299
|
|
|
} else { |
300
|
269 |
|
$sequenceName = $sequence['relname']; |
301
|
|
|
} |
302
|
|
|
|
303
|
269 |
|
if (! isset($sequence['increment_by'], $sequence['min_value'])) { |
304
|
|
|
/** @var string[] $data */ |
305
|
179 |
|
$data = $this->_conn->fetchAssoc('SELECT min_value, increment_by FROM ' . $this->_platform->quoteIdentifier($sequenceName)); |
306
|
|
|
|
307
|
179 |
|
$sequence += $data; |
308
|
|
|
} |
309
|
|
|
|
310
|
269 |
|
return new Sequence($sequenceName, (int) $sequence['increment_by'], (int) $sequence['min_value']); |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* {@inheritdoc} |
315
|
|
|
*/ |
316
|
430 |
|
protected function _getPortableTableColumnDefinition($tableColumn) |
317
|
|
|
{ |
318
|
430 |
|
$tableColumn = array_change_key_case($tableColumn, CASE_LOWER); |
319
|
|
|
|
320
|
430 |
|
if (strtolower($tableColumn['type']) === 'varchar' || strtolower($tableColumn['type']) === 'bpchar') { |
321
|
|
|
// get length from varchar definition |
322
|
358 |
|
$length = preg_replace('~.*\(([0-9]*)\).*~', '$1', $tableColumn['complete_type']); |
323
|
358 |
|
$tableColumn['length'] = $length; |
324
|
|
|
} |
325
|
|
|
|
326
|
430 |
|
$matches = []; |
327
|
|
|
|
328
|
430 |
|
$autoincrement = false; |
329
|
430 |
|
if ($tableColumn['default'] !== null && preg_match("/^nextval\('(.*)'(::.*)?\)$/", $tableColumn['default'], $matches)) { |
330
|
400 |
|
$tableColumn['sequence'] = $matches[1]; |
331
|
400 |
|
$tableColumn['default'] = null; |
332
|
400 |
|
$autoincrement = true; |
333
|
|
|
} |
334
|
|
|
|
335
|
430 |
|
if ($tableColumn['default'] !== null && preg_match("/^['(](.*)[')]::.*$/", $tableColumn['default'], $matches)) { |
336
|
358 |
|
$tableColumn['default'] = $matches[1]; |
337
|
|
|
} |
338
|
|
|
|
339
|
430 |
|
if ($tableColumn['default'] !== null && stripos($tableColumn['default'], 'NULL') === 0) { |
340
|
334 |
|
$tableColumn['default'] = null; |
341
|
|
|
} |
342
|
|
|
|
343
|
430 |
|
$length = $tableColumn['length'] ?? null; |
344
|
430 |
|
if ($length === '-1' && isset($tableColumn['atttypmod'])) { |
345
|
|
|
$length = $tableColumn['atttypmod'] - 4; |
346
|
|
|
} |
347
|
430 |
|
if ((int) $length <= 0) { |
348
|
430 |
|
$length = null; |
349
|
|
|
} |
350
|
430 |
|
$fixed = null; |
351
|
|
|
|
352
|
430 |
|
if (! isset($tableColumn['name'])) { |
353
|
430 |
|
$tableColumn['name'] = ''; |
354
|
|
|
} |
355
|
|
|
|
356
|
430 |
|
$precision = null; |
357
|
430 |
|
$scale = null; |
358
|
430 |
|
$jsonb = null; |
359
|
|
|
|
360
|
430 |
|
$dbType = strtolower($tableColumn['type']); |
361
|
430 |
|
if ($tableColumn['domain_type'] !== null |
362
|
430 |
|
&& strlen($tableColumn['domain_type']) |
363
|
430 |
|
&& ! $this->_platform->hasDoctrineTypeMappingFor($tableColumn['type']) |
364
|
|
|
) { |
365
|
406 |
|
$dbType = strtolower($tableColumn['domain_type']); |
366
|
406 |
|
$tableColumn['complete_type'] = $tableColumn['domain_complete_type']; |
367
|
|
|
} |
368
|
|
|
|
369
|
430 |
|
$type = $this->extractDoctrineTypeFromComment($tableColumn['comment']) |
370
|
430 |
|
?? $this->_platform->getDoctrineTypeMapping($dbType); |
371
|
|
|
|
372
|
430 |
|
switch ($dbType) { |
373
|
|
|
case 'smallint': |
374
|
|
|
case 'int2': |
375
|
317 |
|
$tableColumn['default'] = $this->fixVersion94NegativeNumericDefaultValue($tableColumn['default']); |
376
|
317 |
|
$length = null; |
377
|
317 |
|
break; |
378
|
|
|
case 'int': |
379
|
|
|
case 'int4': |
380
|
|
|
case 'integer': |
381
|
430 |
|
$tableColumn['default'] = $this->fixVersion94NegativeNumericDefaultValue($tableColumn['default']); |
382
|
430 |
|
$length = null; |
383
|
430 |
|
break; |
384
|
|
|
case 'bigint': |
385
|
|
|
case 'int8': |
386
|
317 |
|
$tableColumn['default'] = $this->fixVersion94NegativeNumericDefaultValue($tableColumn['default']); |
387
|
317 |
|
$length = null; |
388
|
317 |
|
break; |
389
|
|
|
case 'bool': |
390
|
|
|
case 'boolean': |
391
|
424 |
|
if ($tableColumn['default'] === 'true') { |
392
|
|
|
$tableColumn['default'] = true; |
393
|
|
|
} |
394
|
|
|
|
395
|
424 |
|
if ($tableColumn['default'] === 'false') { |
396
|
424 |
|
$tableColumn['default'] = false; |
397
|
|
|
} |
398
|
|
|
|
399
|
424 |
|
$length = null; |
400
|
424 |
|
break; |
401
|
|
|
case 'text': |
402
|
334 |
|
$fixed = false; |
403
|
334 |
|
break; |
404
|
|
|
case 'varchar': |
405
|
|
|
case 'interval': |
406
|
|
|
case '_varchar': |
407
|
358 |
|
$fixed = false; |
408
|
358 |
|
break; |
409
|
|
|
case 'char': |
410
|
|
|
case 'bpchar': |
411
|
334 |
|
$fixed = true; |
412
|
334 |
|
break; |
413
|
|
|
case 'float': |
414
|
|
|
case 'float4': |
415
|
|
|
case 'float8': |
416
|
|
|
case 'double': |
417
|
|
|
case 'double precision': |
418
|
|
|
case 'real': |
419
|
|
|
case 'decimal': |
420
|
|
|
case 'money': |
421
|
|
|
case 'numeric': |
422
|
406 |
|
$tableColumn['default'] = $this->fixVersion94NegativeNumericDefaultValue($tableColumn['default']); |
423
|
|
|
|
424
|
406 |
|
if (preg_match('([A-Za-z]+\(([0-9]+)\,([0-9]+)\))', $tableColumn['complete_type'], $match)) { |
425
|
406 |
|
$precision = $match[1]; |
426
|
406 |
|
$scale = $match[2]; |
427
|
406 |
|
$length = null; |
428
|
|
|
} |
429
|
406 |
|
break; |
430
|
|
|
case 'year': |
431
|
|
|
$length = null; |
432
|
|
|
break; |
433
|
|
|
|
434
|
|
|
// PostgreSQL 9.4+ only |
435
|
|
|
case 'jsonb': |
436
|
269 |
|
$jsonb = true; |
437
|
269 |
|
break; |
438
|
|
|
} |
439
|
|
|
|
440
|
430 |
|
if ($tableColumn['default'] && preg_match("('([^']+)'::)", $tableColumn['default'], $match)) { |
441
|
|
|
$tableColumn['default'] = $match[1]; |
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
$options = [ |
445
|
430 |
|
'length' => $length, |
446
|
430 |
|
'notnull' => (bool) $tableColumn['isnotnull'], |
447
|
430 |
|
'default' => $tableColumn['default'], |
448
|
430 |
|
'precision' => $precision, |
449
|
430 |
|
'scale' => $scale, |
450
|
430 |
|
'fixed' => $fixed, |
451
|
|
|
'unsigned' => false, |
452
|
430 |
|
'autoincrement' => $autoincrement, |
453
|
430 |
|
'comment' => isset($tableColumn['comment']) && $tableColumn['comment'] !== '' |
454
|
143 |
|
? $tableColumn['comment'] |
455
|
|
|
: null, |
456
|
|
|
]; |
457
|
|
|
|
458
|
430 |
|
$column = new Column($tableColumn['field'], Type::getType($type), $options); |
459
|
|
|
|
460
|
430 |
|
if (isset($tableColumn['collation']) && ! empty($tableColumn['collation'])) { |
461
|
|
|
$column->setPlatformOption('collation', $tableColumn['collation']); |
462
|
|
|
} |
463
|
|
|
|
464
|
430 |
|
if ($column->getType()->getName() === Types::JSON) { |
465
|
275 |
|
$column->setPlatformOption('jsonb', $jsonb); |
466
|
|
|
} |
467
|
|
|
|
468
|
430 |
|
return $column; |
469
|
|
|
} |
470
|
|
|
|
471
|
|
|
/** |
472
|
|
|
* PostgreSQL 9.4 puts parentheses around negative numeric default values that need to be stripped eventually. |
473
|
|
|
* |
474
|
|
|
* @param mixed $defaultValue |
475
|
|
|
* |
476
|
|
|
* @return mixed |
477
|
|
|
*/ |
478
|
430 |
|
private function fixVersion94NegativeNumericDefaultValue($defaultValue) |
479
|
|
|
{ |
480
|
430 |
|
if ($defaultValue !== null && strpos($defaultValue, '(') === 0) { |
481
|
106 |
|
return trim($defaultValue, '()'); |
482
|
|
|
} |
483
|
|
|
|
484
|
430 |
|
return $defaultValue; |
485
|
|
|
} |
486
|
|
|
} |
487
|
|
|
|