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