1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Mssql; |
6
|
|
|
|
7
|
|
|
use Exception; |
8
|
|
|
use Throwable; |
9
|
|
|
use Yiisoft\Db\Exception\InvalidArgumentException; |
10
|
|
|
use Yiisoft\Db\Exception\InvalidConfigException; |
11
|
|
|
use Yiisoft\Db\Exception\NotSupportedException; |
12
|
|
|
use Yiisoft\Db\Expression\Expression; |
13
|
|
|
use Yiisoft\Db\QueryBuilder\AbstractDDLQueryBuilder; |
14
|
|
|
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface; |
15
|
|
|
use Yiisoft\Db\Schema\ColumnSchemaBuilderInterface; |
16
|
|
|
use Yiisoft\Db\Schema\QuoterInterface; |
17
|
|
|
use Yiisoft\Db\Schema\SchemaInterface; |
18
|
|
|
|
19
|
|
|
use function array_diff; |
20
|
|
|
|
21
|
|
|
final class DDLQueryBuilder extends AbstractDDLQueryBuilder |
22
|
516 |
|
{ |
23
|
|
|
public function __construct( |
24
|
|
|
private QueryBuilderInterface $queryBuilder, |
25
|
|
|
private QuoterInterface $quoter, |
26
|
|
|
private SchemaInterface $schema |
27
|
516 |
|
) { |
28
|
|
|
parent::__construct($queryBuilder, $quoter, $schema); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @throws InvalidArgumentException |
33
|
5 |
|
*/ |
34
|
|
|
public function addCommentOnColumn(string $table, string $column, string $comment): string |
35
|
5 |
|
{ |
36
|
|
|
return $this->buildAddCommentSql($comment, $table, $column); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @throws InvalidArgumentException |
41
|
4 |
|
*/ |
42
|
|
|
public function addCommentOnTable(string $table, string $comment): string |
43
|
4 |
|
{ |
44
|
|
|
return $this->buildAddCommentSql($comment, $table); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @throws Exception |
49
|
5 |
|
*/ |
50
|
|
|
public function addDefaultValue(string $name, string $table, string $column, mixed $value): string |
51
|
5 |
|
{ |
52
|
5 |
|
return 'ALTER TABLE ' |
53
|
5 |
|
. $this->quoter->quoteTableName($table) |
54
|
5 |
|
. ' ADD CONSTRAINT ' |
55
|
5 |
|
. $this->quoter->quoteColumnName($name) |
56
|
5 |
|
. ' DEFAULT ' . (string) $this->quoter->quoteValue($value) |
57
|
|
|
. ' FOR ' . $this->quoter->quoteColumnName($column); |
58
|
|
|
} |
59
|
2 |
|
|
60
|
|
|
public function alterColumn(string $table, string $column, ColumnSchemaBuilderInterface|string $type): string |
61
|
2 |
|
{ |
62
|
2 |
|
$sqlAfter = [$this->dropConstraintsForColumn($table, $column, 'D')]; |
63
|
2 |
|
|
64
|
2 |
|
$columnName = $this->quoter->quoteColumnName($column); |
65
|
2 |
|
$tableName = $this->quoter->quoteTableName($table); |
66
|
2 |
|
$constraintBase = preg_replace('/[^a-z0-9_]/i', '', $table . '_' . $column); |
67
|
|
|
|
68
|
|
|
if ($type instanceof ColumnSchemaBuilderInterface) { |
69
|
|
|
$type->setAlterColumnFormat(); |
|
|
|
|
70
|
|
|
|
71
|
|
|
$defaultValue = $type->getDefault(); |
72
|
|
|
if ($defaultValue !== null) { |
73
|
|
|
$sqlAfter[] = $this->addDefaultValue( |
74
|
|
|
"DF_{$constraintBase}", |
75
|
3 |
|
$table, |
76
|
|
|
$column, |
77
|
3 |
|
$defaultValue |
78
|
|
|
); |
79
|
|
|
} |
80
|
3 |
|
|
81
|
3 |
|
$checkValue = $type->getCheck(); |
82
|
|
|
if ($checkValue !== null) { |
83
|
3 |
|
$sqlAfter[] = "ALTER TABLE {$tableName} ADD CONSTRAINT " . |
84
|
3 |
|
$this->quoter->quoteColumnName("CK_{$constraintBase}") . |
85
|
3 |
|
" CHECK (" . ($defaultValue instanceof Expression ? $checkValue : new Expression($checkValue)) . ")"; |
86
|
3 |
|
} |
87
|
3 |
|
|
88
|
|
|
if ($type->isUnique()) { |
89
|
3 |
|
$sqlAfter[] = "ALTER TABLE {$tableName} ADD CONSTRAINT " . $this->quoter->quoteColumnName("UQ_{$constraintBase}") . " UNIQUE ({$columnName})"; |
90
|
3 |
|
} |
91
|
3 |
|
} |
92
|
|
|
|
93
|
|
|
return 'ALTER TABLE ' . $tableName |
94
|
3 |
|
. ' ALTER COLUMN ' |
95
|
|
|
. $columnName . ' ' |
96
|
|
|
. $this->queryBuilder->getColumnType($type) . "\n" |
97
|
|
|
. implode("\n", $sqlAfter); |
98
|
|
|
} |
99
|
|
|
|
100
|
3 |
|
/** |
101
|
|
|
* @throws InvalidConfigException |
102
|
3 |
|
* @throws NotSupportedException |
103
|
|
|
* @throws Throwable |
104
|
|
|
* @throws \Yiisoft\Db\Exception\Exception |
105
|
|
|
*/ |
106
|
|
|
public function checkIntegrity(string $schema = '', string $table = '', bool $check = true): string |
107
|
|
|
{ |
108
|
3 |
|
$enable = $check ? 'CHECK' : 'NOCHECK'; |
109
|
|
|
|
110
|
3 |
|
/** @var Schema */ |
111
|
|
|
$schemaInstance = $this->schema; |
112
|
|
|
$defaultSchema = $schema ?: $schemaInstance->getDefaultSchema() ?? ''; |
113
|
4 |
|
/** @psalm-var string[] */ |
114
|
|
|
$tableNames = $schemaInstance->getTableSchema($table) |
115
|
4 |
|
? [$table] : $schemaInstance->getTableNames($defaultSchema); |
116
|
4 |
|
$viewNames = $schemaInstance->getViewNames($defaultSchema); |
117
|
4 |
|
$tableNames = array_diff($tableNames, $viewNames); |
118
|
4 |
|
$command = ''; |
119
|
|
|
|
120
|
|
|
foreach ($tableNames as $tableName) { |
121
|
3 |
|
$tableName = $this->quoter->quoteTableName("$defaultSchema.$tableName"); |
122
|
|
|
$command .= "ALTER TABLE $tableName $enable CONSTRAINT ALL; "; |
123
|
3 |
|
} |
124
|
3 |
|
|
125
|
3 |
|
return $command; |
126
|
|
|
} |
127
|
|
|
|
128
|
2 |
|
/** |
129
|
|
|
* @throws InvalidArgumentException |
130
|
2 |
|
*/ |
131
|
2 |
|
public function dropCommentFromColumn(string $table, string $column): string |
132
|
2 |
|
{ |
133
|
2 |
|
return $this->buildRemoveCommentSql($table, $column); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @throws InvalidArgumentException |
138
|
|
|
*/ |
139
|
|
|
public function dropCommentFromTable(string $table): string |
140
|
|
|
{ |
141
|
|
|
return $this->buildRemoveCommentSql($table); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function dropDefaultValue(string $name, string $table): string |
145
|
|
|
{ |
146
|
|
|
return 'ALTER TABLE ' |
147
|
|
|
. $this->quoter->quoteTableName($table) |
148
|
|
|
. ' DROP CONSTRAINT ' |
149
|
|
|
. $this->quoter->quoteColumnName($name); |
150
|
|
|
} |
151
|
9 |
|
|
152
|
|
|
public function dropColumn(string $table, string $column): string |
153
|
9 |
|
{ |
154
|
|
|
return $this->dropConstraintsForColumn($table, $column) |
155
|
9 |
|
. "\nALTER TABLE " |
156
|
2 |
|
. $this->quoter->quoteTableName($table) |
157
|
|
|
. ' DROP COLUMN ' |
158
|
|
|
. $this->quoter->quoteColumnName($column); |
159
|
7 |
|
} |
160
|
7 |
|
|
161
|
7 |
|
public function renameTable(string $oldName, string $newName): string |
162
|
7 |
|
{ |
163
|
7 |
|
return 'sp_rename ' |
164
|
7 |
|
. $this->quoter->quoteTableName($oldName) . ', ' |
165
|
|
|
. $this->quoter->quoteTableName($newName); |
166
|
7 |
|
} |
167
|
7 |
|
|
168
|
7 |
|
public function renameColumn(string $table, string $oldName, string $newName): string |
169
|
7 |
|
{ |
170
|
|
|
return 'sp_rename ' |
171
|
7 |
|
. "'" . $this->quoter->quoteTableName($table) . '.' . $this->quoter->quoteColumnName($oldName) . "'" . ', ' |
172
|
|
|
. $this->quoter->quoteColumnName($newName) . ', ' |
173
|
|
|
. "'COLUMN'"; |
174
|
|
|
} |
175
|
|
|
|
176
|
7 |
|
/** |
177
|
7 |
|
* Builds a SQL command for adding or updating a comment to a table or a column. The command built will check if a |
178
|
7 |
|
* comment already exists. If so, it will be updated, otherwise, it will be added. |
179
|
|
|
* |
180
|
|
|
* @param string $comment The text of the comment to be added. The comment will be properly quoted by the method. |
181
|
7 |
|
* @param string $table The table to be commented or whose column is to be commented. The table name will be |
182
|
|
|
* properly quoted by the method. |
183
|
7 |
|
* @param string|null $column Optional, the name of the column to be commented. If empty, the command will add the |
184
|
7 |
|
* comment to the table instead. The column name will be properly quoted by the method. |
185
|
|
|
* |
186
|
|
|
* @throws Exception |
187
|
|
|
* @throws InvalidArgumentException If the table does not exist. |
188
|
|
|
* |
189
|
|
|
* @return string The SQL statement for adding a comment. |
190
|
|
|
*/ |
191
|
|
|
private function buildAddCommentSql(string $comment, string $table, string $column = null): string |
192
|
|
|
{ |
193
|
|
|
$tableSchema = $this->schema->getTableSchema($table); |
194
|
|
|
|
195
|
|
|
if ($tableSchema === null) { |
196
|
|
|
throw new InvalidArgumentException("Table not found: $table"); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
$schemaName = $tableSchema->getSchemaName() |
200
|
|
|
? "N'" . (string) $tableSchema->getSchemaName() . "'" : 'SCHEMA_NAME()'; |
201
|
6 |
|
$tableName = 'N' . (string) $this->quoter->quoteValue($tableSchema->getName()); |
202
|
|
|
$columnName = $column ? 'N' . (string) $this->quoter->quoteValue($column) : null; |
203
|
6 |
|
$comment = 'N' . (string) $this->quoter->quoteValue($comment); |
204
|
|
|
$functionParams = " |
205
|
6 |
|
@name = N'MS_description', |
206
|
2 |
|
@value = $comment, |
207
|
|
|
@level0type = N'SCHEMA', @level0name = $schemaName, |
208
|
|
|
@level1type = N'TABLE', @level1name = $tableName" |
209
|
4 |
|
. ($column ? ", @level2type = N'COLUMN', @level2name = $columnName" : '') . ';'; |
210
|
4 |
|
|
211
|
4 |
|
return " |
212
|
4 |
|
IF NOT EXISTS ( |
213
|
|
|
SELECT 1 |
214
|
4 |
|
FROM fn_listextendedproperty ( |
215
|
|
|
N'MS_description', |
216
|
|
|
'SCHEMA', $schemaName, |
217
|
|
|
'TABLE', $tableName, |
218
|
|
|
" . ($column ? "'COLUMN', $columnName " : ' DEFAULT, DEFAULT ') . " |
219
|
4 |
|
) |
220
|
4 |
|
) |
221
|
4 |
|
EXEC sys.sp_addextendedproperty $functionParams |
222
|
|
|
ELSE |
223
|
|
|
EXEC sys.sp_updateextendedproperty $functionParams |
224
|
|
|
"; |
225
|
|
|
} |
226
|
4 |
|
|
227
|
4 |
|
/** |
228
|
4 |
|
* Builds a SQL command for removing a comment from a table or a column. The command built will check if a comment |
229
|
|
|
* already exists before trying to perform the removal. |
230
|
|
|
* |
231
|
|
|
* @param string $table The table that will have the comment removed or whose column will have the comment removed. |
232
|
|
|
* The table name will be properly quoted by the method. |
233
|
|
|
* @param string|null $column Optional, the name of the column whose comment will be removed. If empty, the command |
234
|
|
|
* will remove the comment from the table instead. The column name will be properly quoted by the method. |
235
|
|
|
* |
236
|
|
|
* @throws Exception |
237
|
|
|
* @throws InvalidArgumentException If the table does not exist. |
238
|
|
|
* |
239
|
|
|
* @return string The SQL statement for removing the comment. |
240
|
|
|
*/ |
241
|
|
|
private function buildRemoveCommentSql(string $table, string $column = null): string |
242
|
|
|
{ |
243
|
|
|
$tableSchema = $this->schema->getTableSchema($table); |
244
|
|
|
|
245
|
|
|
if ($tableSchema === null) { |
246
|
|
|
throw new InvalidArgumentException("Table not found: $table"); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
$schemaName = $tableSchema->getSchemaName() |
250
|
|
|
? "N'" . (string) $tableSchema->getSchemaName() . "'" : 'SCHEMA_NAME()'; |
251
|
|
|
$tableName = 'N' . (string) $this->quoter->quoteValue($tableSchema->getName()); |
252
|
|
|
$columnName = $column ? 'N' . (string) $this->quoter->quoteValue($column) : null; |
253
|
|
|
|
254
|
|
|
return " |
255
|
|
|
IF EXISTS ( |
256
|
|
|
SELECT 1 |
257
|
|
|
FROM fn_listextendedproperty ( |
258
|
|
|
N'MS_description', |
259
|
|
|
'SCHEMA', $schemaName, |
260
|
|
|
'TABLE', $tableName, |
261
|
|
|
" . ($column ? "'COLUMN', $columnName " : ' DEFAULT, DEFAULT ') . " |
262
|
|
|
) |
263
|
|
|
) |
264
|
|
|
EXEC sys.sp_dropextendedproperty |
265
|
|
|
@name = N'MS_description', |
266
|
|
|
@level0type = N'SCHEMA', @level0name = $schemaName, |
267
|
|
|
@level1type = N'TABLE', @level1name = $tableName" |
268
|
|
|
. ($column ? ", @level2type = N'COLUMN', @level2name = $columnName" : '') . ';'; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Builds a SQL statement for dropping constraints for column of table. |
273
|
|
|
* |
274
|
|
|
* @param string $table the table whose constraint is to be dropped. The name will be properly quoted by the method. |
275
|
|
|
* @param string $column the column whose constraint is to be dropped. The name will be properly quoted by the method. |
276
|
|
|
* @param string $type type of constraint, leave empty for all type of constraints(for example: D - default, 'UQ' - unique, 'C' - check) |
277
|
|
|
* @return string the DROP CONSTRAINTS SQL |
278
|
|
|
*@see https://docs.microsoft.com/sql/relational-databases/system-catalog-views/sys-objects-transact-sql |
279
|
|
|
*/ |
280
|
|
|
private function dropConstraintsForColumn(string $table, string $column, string $type=''): string |
281
|
|
|
{ |
282
|
|
|
return "DECLARE @tableName VARCHAR(MAX) = '" . $this->quoter->quoteTableName($table) . "' |
283
|
|
|
DECLARE @columnName VARCHAR(MAX) = '{$column}' |
284
|
|
|
WHILE 1=1 BEGIN |
285
|
|
|
DECLARE @constraintName NVARCHAR(128) |
286
|
|
|
SET @constraintName = (SELECT TOP 1 OBJECT_NAME(cons.[object_id]) |
287
|
|
|
FROM ( |
288
|
|
|
SELECT sc.[constid] object_id |
289
|
|
|
FROM [sys].[sysconstraints] sc |
290
|
|
|
JOIN [sys].[columns] c ON c.[object_id]=sc.[id] AND c.[column_id]=sc.[colid] AND c.[name]=@columnName |
291
|
|
|
WHERE sc.[id] = OBJECT_ID(@tableName) |
292
|
|
|
UNION |
293
|
|
|
SELECT object_id(i.[name]) FROM [sys].[indexes] i |
294
|
|
|
JOIN [sys].[columns] c ON c.[object_id]=i.[object_id] AND c.[name]=@columnName |
295
|
|
|
JOIN [sys].[index_columns] ic ON ic.[object_id]=i.[object_id] AND i.[index_id]=ic.[index_id] AND c.[column_id]=ic.[column_id] |
296
|
|
|
WHERE i.[is_unique_constraint]=1 and i.[object_id]=OBJECT_ID(@tableName) |
297
|
|
|
) cons |
298
|
|
|
JOIN [sys].[objects] so ON so.[object_id]=cons.[object_id] |
299
|
|
|
" . (!empty($type) ? " WHERE so.[type]='{$type}'" : "") . ") |
300
|
|
|
IF @constraintName IS NULL BREAK |
301
|
|
|
EXEC (N'ALTER TABLE ' + @tableName + ' DROP CONSTRAINT [' + @constraintName + ']') |
302
|
|
|
END"; |
303
|
|
|
} |
304
|
|
|
} |
305
|
|
|
|