|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Pgsql; |
|
6
|
|
|
|
|
7
|
|
|
use Throwable; |
|
8
|
|
|
use Yiisoft\Db\Exception\Exception; |
|
9
|
|
|
use Yiisoft\Db\Exception\InvalidArgumentException; |
|
10
|
|
|
use Yiisoft\Db\Exception\NotSupportedException; |
|
11
|
|
|
use Yiisoft\Db\QueryBuilder\DDLQueryBuilder as AbstractDDLQueryBuilder; |
|
12
|
|
|
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface; |
|
13
|
|
|
use Yiisoft\Db\Schema\ColumnSchemaBuilder; |
|
14
|
|
|
use Yiisoft\Db\Schema\QuoterInterface; |
|
15
|
|
|
use Yiisoft\Db\Schema\SchemaInterface; |
|
16
|
|
|
|
|
17
|
|
|
use function array_diff; |
|
18
|
|
|
use function array_unshift; |
|
19
|
|
|
use function explode; |
|
20
|
|
|
use function implode; |
|
21
|
|
|
use function preg_match; |
|
22
|
|
|
use function preg_replace; |
|
23
|
|
|
use function str_contains; |
|
24
|
|
|
|
|
25
|
|
|
final class DDLQueryBuilder extends AbstractDDLQueryBuilder |
|
26
|
|
|
{ |
|
27
|
450 |
|
public function __construct( |
|
28
|
|
|
private QueryBuilderInterface $queryBuilder, |
|
29
|
|
|
private QuoterInterface $quoter, |
|
30
|
|
|
private SchemaInterface $schema |
|
31
|
|
|
) { |
|
32
|
450 |
|
parent::__construct($queryBuilder, $quoter, $schema); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @throws NotSupportedException |
|
37
|
|
|
*/ |
|
38
|
1 |
|
public function addDefaultValue(string $name, string $table, string $column, mixed $value): string |
|
39
|
|
|
{ |
|
40
|
1 |
|
throw new NotSupportedException(__METHOD__ . '()' . ' is not supported by PostgreSQL.'); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
1 |
|
public function alterColumn(string $table, string $column, ColumnSchemaBuilder|string $type): string |
|
44
|
|
|
{ |
|
45
|
1 |
|
$columnName = $this->quoter->quoteColumnName($column); |
|
46
|
1 |
|
$tableName = $this->quoter->quoteTableName($table); |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* {@see https://github.com/yiisoft/yii2/issues/4492} |
|
50
|
|
|
* {@see http://www.postgresql.org/docs/9.1/static/sql-altertable.html} |
|
51
|
|
|
*/ |
|
52
|
1 |
|
if (preg_match('/^(DROP|SET|RESET|USING)\s+/i', (string) $type)) { |
|
53
|
1 |
|
return "ALTER TABLE $tableName ALTER COLUMN $columnName $type"; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
1 |
|
$type = 'TYPE ' . $this->queryBuilder->getColumnType($type); |
|
57
|
1 |
|
$multiAlterStatement = []; |
|
58
|
1 |
|
$constraintPrefix = preg_replace('/[^a-z0-9_]/i', '', $table . '_' . $column); |
|
59
|
|
|
|
|
60
|
1 |
|
if (preg_match('/\s+DEFAULT\s+(["\']?\w*["\']?)/i', $type, $matches)) { |
|
61
|
1 |
|
$type = preg_replace('/\s+DEFAULT\s+(["\']?\w*["\']?)/i', '', $type); |
|
62
|
1 |
|
$multiAlterStatement[] = "ALTER COLUMN $columnName SET DEFAULT $matches[1]"; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
1 |
|
$type = preg_replace('/\s+NOT\s+NULL/i', '', $type, -1, $count); |
|
66
|
|
|
|
|
67
|
1 |
|
if ($count) { |
|
68
|
1 |
|
$multiAlterStatement[] = "ALTER COLUMN $columnName SET NOT NULL"; |
|
69
|
|
|
} else { |
|
70
|
|
|
/** remove additional null if any */ |
|
71
|
1 |
|
$type = preg_replace('/\s+NULL/i', '', $type, -1, $count); |
|
72
|
1 |
|
if ($count) { |
|
73
|
1 |
|
$multiAlterStatement[] = "ALTER COLUMN $columnName DROP NOT NULL"; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
1 |
|
if (preg_match('/\s+CHECK\s+\((.+)\)/i', $type, $matches)) { |
|
78
|
1 |
|
$type = preg_replace('/\s+CHECK\s+\((.+)\)/i', '', $type); |
|
79
|
1 |
|
$multiAlterStatement[] = "ADD CONSTRAINT {$constraintPrefix}_check CHECK ($matches[1])"; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
1 |
|
$type = preg_replace('/\s+UNIQUE/i', '', $type, -1, $count); |
|
83
|
|
|
|
|
84
|
1 |
|
if ($count) { |
|
85
|
1 |
|
$multiAlterStatement[] = "ADD UNIQUE ($columnName)"; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** add what's left at the beginning */ |
|
89
|
1 |
|
array_unshift($multiAlterStatement, "ALTER COLUMN $columnName $type"); |
|
90
|
|
|
|
|
91
|
1 |
|
return 'ALTER TABLE ' . $tableName . ' ' . implode(', ', $multiAlterStatement); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @throws Exception|NotSupportedException|Throwable |
|
96
|
|
|
*/ |
|
97
|
2 |
|
public function checkIntegrity(string $schema = '', string $table = '', bool $check = true): string |
|
98
|
|
|
{ |
|
99
|
|
|
/** @var Schema $schemaInstance */ |
|
100
|
2 |
|
$schemaInstance = $this->schema; |
|
101
|
2 |
|
$enable = $check ? 'ENABLE' : 'DISABLE'; |
|
102
|
2 |
|
$schema = $schema ?: $schemaInstance->getDefaultSchema(); |
|
103
|
2 |
|
$tableNames = []; |
|
104
|
2 |
|
$viewNames = []; |
|
105
|
|
|
|
|
106
|
2 |
|
if ($schema !== null) { |
|
107
|
2 |
|
$tableNames = $table ? [$table] : $schemaInstance->getTableNames($schema); |
|
108
|
2 |
|
$viewNames = $schemaInstance->getViewNames($schema); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
2 |
|
$tableNames = array_diff($tableNames, $viewNames); |
|
112
|
2 |
|
$command = ''; |
|
113
|
|
|
|
|
114
|
|
|
/** @psalm-var string[] $tableNames */ |
|
115
|
2 |
|
foreach ($tableNames as $tableName) { |
|
116
|
2 |
|
$tableName = $this->quoter->quoteTableName("$schema.$tableName"); |
|
117
|
2 |
|
$command .= "ALTER TABLE $tableName $enable TRIGGER ALL; "; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** enable to have ability to alter several tables */ |
|
121
|
|
|
//$pdo = $db->getSlavePdo(); |
|
122
|
|
|
//$pdo?->setAttribute(PDO::ATTR_EMULATE_PREPARES, true); |
|
123
|
|
|
|
|
124
|
2 |
|
return $command; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @throws Exception|InvalidArgumentException |
|
129
|
|
|
*/ |
|
130
|
12 |
|
public function createIndex(string $name, string $table, array|string $columns, ?string $indexType = null, ?string $indexMethod = null): string |
|
131
|
|
|
{ |
|
132
|
12 |
|
return 'CREATE ' . ($indexType ? ($indexType . ' ') : '') . 'INDEX ' |
|
133
|
12 |
|
. $this->quoter->quoteTableName($name) . ' ON ' |
|
134
|
12 |
|
. $this->quoter->quoteTableName($table) |
|
135
|
12 |
|
. ($indexMethod !== null ? " USING $indexMethod" : '') |
|
136
|
12 |
|
. ' (' . $this->queryBuilder->buildColumns($columns) . ')'; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @throws NotSupportedException |
|
141
|
|
|
*/ |
|
142
|
1 |
|
public function dropDefaultValue(string $name, string $table): string |
|
143
|
|
|
{ |
|
144
|
1 |
|
throw new NotSupportedException(__METHOD__ . '()' . ' is not supported by PostgreSQL.'); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
3 |
|
public function dropIndex(string $name, string $table): string |
|
148
|
|
|
{ |
|
149
|
3 |
|
if (str_contains($table, '.') && !str_contains($name, '.')) { |
|
150
|
1 |
|
if (str_contains($table, '{{')) { |
|
151
|
1 |
|
$table = preg_replace('/{{(.*?)}}/', '\1', $table); |
|
152
|
1 |
|
[$schema] = explode('.', $table); |
|
153
|
1 |
|
if (!str_contains($schema, '%')) { |
|
154
|
1 |
|
$name = $schema . '.' . $name; |
|
155
|
|
|
} else { |
|
156
|
1 |
|
$name = '{{' . $schema . '.' . $name . '}}'; |
|
157
|
|
|
} |
|
158
|
|
|
} else { |
|
159
|
|
|
[$schema] = explode('.', $table); |
|
160
|
|
|
$name = $schema . '.' . $name; |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
3 |
|
return 'DROP INDEX ' . $this->quoter->quoteTableName($name); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
2 |
|
public function renameTable(string $oldName, string $newName): string |
|
168
|
|
|
{ |
|
169
|
|
|
return 'ALTER TABLE ' |
|
170
|
2 |
|
. $this->quoter->quoteTableName($oldName) |
|
171
|
|
|
. ' RENAME TO ' |
|
172
|
2 |
|
. $this->quoter->quoteTableName($newName); |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
|