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