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