1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link http://www.yiiframework.com/ |
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
5
|
|
|
* @license http://www.yiiframework.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace yii\db\mssql; |
9
|
|
|
|
10
|
|
|
use yii\db\CheckConstraint; |
11
|
|
|
use yii\db\ColumnSchema; |
12
|
|
|
use yii\db\Constraint; |
13
|
|
|
use yii\db\ConstraintFinderTrait; |
14
|
|
|
use yii\db\DefaultValueConstraint; |
15
|
|
|
use yii\db\ForeignKeyConstraint; |
16
|
|
|
use yii\db\IndexConstraint; |
17
|
|
|
use yii\db\ViewFinderTrait; |
18
|
|
|
use yii\helpers\ArrayHelper; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Schema is the class for retrieving metadata from MS SQL Server databases (version 2008 and above). |
22
|
|
|
* |
23
|
|
|
* @author Timur Ruziev <[email protected]> |
24
|
|
|
* @since 2.0 |
25
|
|
|
*/ |
26
|
|
|
class Schema extends \yii\db\Schema |
27
|
|
|
{ |
28
|
|
|
use ViewFinderTrait; |
29
|
|
|
use ConstraintFinderTrait; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string the default schema used for the current session. |
33
|
|
|
*/ |
34
|
|
|
public $defaultSchema = 'dbo'; |
35
|
|
|
/** |
36
|
|
|
* @var array mapping from physical column types (keys) to abstract column types (values) |
37
|
|
|
*/ |
38
|
|
|
public $typeMap = [ |
39
|
|
|
// exact numbers |
40
|
|
|
'bigint' => self::TYPE_BIGINT, |
41
|
|
|
'numeric' => self::TYPE_DECIMAL, |
42
|
|
|
'bit' => self::TYPE_SMALLINT, |
43
|
|
|
'smallint' => self::TYPE_SMALLINT, |
44
|
|
|
'decimal' => self::TYPE_DECIMAL, |
45
|
|
|
'smallmoney' => self::TYPE_MONEY, |
46
|
|
|
'int' => self::TYPE_INTEGER, |
47
|
|
|
'tinyint' => self::TYPE_SMALLINT, |
48
|
|
|
'money' => self::TYPE_MONEY, |
49
|
|
|
// approximate numbers |
50
|
|
|
'float' => self::TYPE_FLOAT, |
51
|
|
|
'double' => self::TYPE_DOUBLE, |
52
|
|
|
'real' => self::TYPE_FLOAT, |
53
|
|
|
// date and time |
54
|
|
|
'date' => self::TYPE_DATE, |
55
|
|
|
'datetimeoffset' => self::TYPE_DATETIME, |
56
|
|
|
'datetime2' => self::TYPE_DATETIME, |
57
|
|
|
'smalldatetime' => self::TYPE_DATETIME, |
58
|
|
|
'datetime' => self::TYPE_DATETIME, |
59
|
|
|
'time' => self::TYPE_TIME, |
60
|
|
|
// character strings |
61
|
|
|
'char' => self::TYPE_CHAR, |
62
|
|
|
'varchar' => self::TYPE_STRING, |
63
|
|
|
'text' => self::TYPE_TEXT, |
64
|
|
|
// unicode character strings |
65
|
|
|
'nchar' => self::TYPE_CHAR, |
66
|
|
|
'nvarchar' => self::TYPE_STRING, |
67
|
|
|
'ntext' => self::TYPE_TEXT, |
68
|
|
|
// binary strings |
69
|
|
|
'binary' => self::TYPE_BINARY, |
70
|
|
|
'varbinary' => self::TYPE_BINARY, |
71
|
|
|
'image' => self::TYPE_BINARY, |
72
|
|
|
// other data types |
73
|
|
|
// 'cursor' type cannot be used with tables |
74
|
|
|
'timestamp' => self::TYPE_TIMESTAMP, |
75
|
|
|
'hierarchyid' => self::TYPE_STRING, |
76
|
|
|
'uniqueidentifier' => self::TYPE_STRING, |
77
|
|
|
'sql_variant' => self::TYPE_STRING, |
78
|
|
|
'xml' => self::TYPE_STRING, |
79
|
|
|
'table' => self::TYPE_STRING, |
80
|
|
|
]; |
81
|
|
|
|
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Resolves the table name and schema name (if any). |
85
|
|
|
* @param string $name the table name |
86
|
|
|
* @return TableSchema resolved table, schema, etc. names. |
87
|
|
|
*/ |
88
|
|
|
protected function resolveTableName($name) |
89
|
|
|
{ |
90
|
|
|
$resolvedName = new TableSchema(); |
91
|
|
|
$parts = explode('.', str_replace(['[', ']'], '', $name)); |
92
|
|
|
$partCount = count($parts); |
93
|
|
|
if ($partCount === 4) { |
94
|
|
|
// server name, catalog name, schema name and table name passed |
95
|
|
|
$resolvedName->catalogName = $parts[1]; |
96
|
|
|
$resolvedName->schemaName = $parts[2]; |
97
|
|
|
$resolvedName->name = $parts[3]; |
98
|
|
|
$resolvedName->fullName = $resolvedName->catalogName . '.' . $resolvedName->schemaName . '.' . $resolvedName->name; |
99
|
|
|
} elseif ($partCount === 3) { |
100
|
|
|
// catalog name, schema name and table name passed |
101
|
|
|
$resolvedName->catalogName = $parts[0]; |
102
|
|
|
$resolvedName->schemaName = $parts[1]; |
103
|
|
|
$resolvedName->name = $parts[2]; |
104
|
|
|
$resolvedName->fullName = $resolvedName->catalogName . '.' . $resolvedName->schemaName . '.' . $resolvedName->name; |
105
|
|
|
} elseif ($partCount === 2) { |
106
|
|
|
// only schema name and table name passed |
107
|
|
|
$resolvedName->schemaName = $parts[0]; |
108
|
|
|
$resolvedName->name = $parts[1]; |
109
|
|
|
$resolvedName->fullName = ($resolvedName->schemaName !== $this->defaultSchema ? $resolvedName->schemaName . '.' : '') . $resolvedName->name; |
110
|
|
|
} else { |
111
|
|
|
// only table name passed |
112
|
|
|
$resolvedName->schemaName = $this->defaultSchema; |
113
|
|
|
$resolvedName->fullName = $resolvedName->name = $parts[0]; |
114
|
|
|
} |
115
|
|
|
return $resolvedName; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @inheritDoc |
120
|
|
|
*/ |
121
|
|
|
protected function findSchemaNames() |
122
|
|
|
{ |
123
|
|
|
$sql = <<<SQL |
124
|
|
|
SELECT ns.nspname AS schema_name |
125
|
|
|
FROM pg_namespace ns |
126
|
|
|
WHERE ns.nspname != 'information_schema' AND ns.nspname NOT LIKE 'pg_%' |
127
|
|
|
ORDER BY ns.nspname |
128
|
|
|
SQL; |
129
|
|
|
return $this->db->createCommand($sql)->queryColumn(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @inheritDoc |
134
|
|
|
*/ |
135
|
|
|
protected function findTableNames($schema = '') |
136
|
|
|
{ |
137
|
|
|
if ($schema === '') { |
138
|
|
|
$schema = $this->defaultSchema; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$sql = <<<SQL |
142
|
|
|
SELECT [t].[table_name] |
143
|
|
|
FROM [INFORMATION_SCHEMA].[TABLES] AS [t] |
144
|
|
|
WHERE [t].[table_schema] = :schema AND [t].[table_type] IN ('BASE TABLE', 'VIEW') |
145
|
|
|
ORDER BY [t].[table_name] |
146
|
|
|
SQL; |
147
|
|
|
|
148
|
|
|
return $this->db->createCommand($sql, [':schema' => $schema])->queryColumn(); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @inheritDoc |
153
|
|
|
*/ |
154
|
|
|
protected function loadTableSchema($name) |
155
|
|
|
{ |
156
|
|
|
$table = new TableSchema(); |
157
|
|
|
$this->resolveTableNames($table, $name); |
158
|
|
|
$this->findPrimaryKeys($table); |
159
|
|
|
if ($this->findColumns($table)) { |
160
|
|
|
$this->findForeignKeys($table); |
161
|
|
|
return $table; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return null; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @inheritDoc |
169
|
|
|
*/ |
170
|
|
|
protected function loadTablePrimaryKey($tableName) |
171
|
|
|
{ |
172
|
|
|
return $this->loadTableConstraints($tableName, 'primaryKey'); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @inheritDoc |
177
|
|
|
*/ |
178
|
|
|
protected function loadTableForeignKeys($tableName) |
179
|
|
|
{ |
180
|
|
|
return $this->loadTableConstraints($tableName, 'foreignKeys'); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @inheritDoc |
185
|
|
|
*/ |
186
|
|
|
protected function loadTableIndexes($tableName) |
187
|
|
|
{ |
188
|
|
|
static $sql = <<<SQL |
189
|
|
|
SELECT |
190
|
|
|
[i].[name] AS [name], |
191
|
|
|
[iccol].[name] AS [column_name], |
192
|
|
|
[i].[is_unique] AS [index_is_unique], |
193
|
|
|
[i].[is_primary_key] AS [index_is_primary] |
194
|
|
|
FROM [sys].[indexes] AS [i] |
195
|
|
|
INNER JOIN [sys].[index_columns] AS [ic] |
196
|
|
|
ON [ic].[object_id] = [i].[object_id] AND [ic].[index_id] = [i].[index_id] |
197
|
|
|
INNER JOIN [sys].[columns] AS [iccol] |
198
|
|
|
ON [iccol].[object_id] = [ic].[object_id] AND [iccol].[column_id] = [ic].[column_id] |
199
|
|
|
WHERE [i].[object_id] = OBJECT_ID(:fullName) |
200
|
|
|
ORDER BY [ic].[key_ordinal] ASC |
201
|
|
|
SQL; |
202
|
|
|
|
203
|
|
|
$resolvedName = $this->resolveTableName($tableName); |
204
|
|
|
$indexes = $this->db->createCommand($sql, [ |
205
|
|
|
':fullName' => $resolvedName->fullName, |
206
|
|
|
])->queryAll(); |
207
|
|
|
$indexes = $this->normalizePdoRowKeyCase($indexes, true); |
208
|
|
|
$indexes = ArrayHelper::index($indexes, null, 'name'); |
209
|
|
|
$result = []; |
210
|
|
|
foreach ($indexes as $name => $index) { |
211
|
|
|
$result[] = new IndexConstraint([ |
212
|
|
|
'isPrimary' => (bool) $index[0]['index_is_primary'], |
213
|
|
|
'isUnique' => (bool) $index[0]['index_is_unique'], |
214
|
|
|
'name' => $name, |
215
|
|
|
'columnNames' => ArrayHelper::getColumn($index, 'column_name'), |
216
|
|
|
]); |
217
|
|
|
} |
218
|
|
|
return $result; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @inheritDoc |
223
|
|
|
*/ |
224
|
|
|
protected function loadTableUniques($tableName) |
225
|
|
|
{ |
226
|
|
|
return $this->loadTableConstraints($tableName, 'uniques'); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @inheritDoc |
231
|
|
|
*/ |
232
|
|
|
protected function loadTableChecks($tableName) |
233
|
|
|
{ |
234
|
|
|
return $this->loadTableConstraints($tableName, 'checks'); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @inheritDoc |
239
|
|
|
*/ |
240
|
|
|
protected function loadTableDefaultValues($tableName) |
241
|
|
|
{ |
242
|
|
|
return $this->loadTableConstraints($tableName, 'defaults'); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* @inheritdoc |
247
|
|
|
*/ |
248
|
|
|
public function createSavepoint($name) |
249
|
|
|
{ |
250
|
|
|
$this->db->createCommand("SAVE TRANSACTION $name")->execute(); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* @inheritdoc |
255
|
|
|
*/ |
256
|
|
|
public function releaseSavepoint($name) |
257
|
|
|
{ |
258
|
|
|
// does nothing as MSSQL does not support this |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* @inheritdoc |
263
|
|
|
*/ |
264
|
|
|
public function rollBackSavepoint($name) |
265
|
|
|
{ |
266
|
|
|
$this->db->createCommand("ROLLBACK TRANSACTION $name")->execute(); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Quotes a table name for use in a query. |
271
|
|
|
* A simple table name has no schema prefix. |
272
|
|
|
* @param string $name table name. |
273
|
|
|
* @return string the properly quoted table name. |
274
|
|
|
*/ |
275
|
|
|
public function quoteSimpleTableName($name) |
276
|
|
|
{ |
277
|
|
|
return strpos($name, '[') === false ? "[{$name}]" : $name; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Quotes a column name for use in a query. |
282
|
|
|
* A simple column name has no prefix. |
283
|
|
|
* @param string $name column name. |
284
|
|
|
* @return string the properly quoted column name. |
285
|
|
|
*/ |
286
|
|
|
public function quoteSimpleColumnName($name) |
287
|
|
|
{ |
288
|
|
|
return strpos($name, '[') === false && $name !== '*' ? "[{$name}]" : $name; |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* Creates a query builder for the MSSQL database. |
293
|
|
|
* @return QueryBuilder query builder interface. |
294
|
|
|
*/ |
295
|
|
|
public function createQueryBuilder() |
296
|
|
|
{ |
297
|
|
|
return new QueryBuilder($this->db); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* Resolves the table name and schema name (if any). |
302
|
|
|
* @param TableSchema $table the table metadata object |
303
|
|
|
* @param string $name the table name |
304
|
|
|
*/ |
305
|
|
|
protected function resolveTableNames($table, $name) |
306
|
|
|
{ |
307
|
|
|
$parts = explode('.', str_replace(['[', ']'], '', $name)); |
308
|
|
|
$partCount = count($parts); |
309
|
|
|
if ($partCount === 4) { |
310
|
|
|
// server name, catalog name, schema name and table name passed |
311
|
|
|
$table->catalogName = $parts[1]; |
312
|
|
|
$table->schemaName = $parts[2]; |
313
|
|
|
$table->name = $parts[3]; |
314
|
|
|
$table->fullName = $table->catalogName . '.' . $table->schemaName . '.' . $table->name; |
315
|
|
|
} elseif ($partCount === 3) { |
316
|
|
|
// catalog name, schema name and table name passed |
317
|
|
|
$table->catalogName = $parts[0]; |
318
|
|
|
$table->schemaName = $parts[1]; |
319
|
|
|
$table->name = $parts[2]; |
320
|
|
|
$table->fullName = $table->catalogName . '.' . $table->schemaName . '.' . $table->name; |
321
|
|
|
} elseif ($partCount === 2) { |
322
|
|
|
// only schema name and table name passed |
323
|
|
|
$table->schemaName = $parts[0]; |
324
|
|
|
$table->name = $parts[1]; |
325
|
|
|
$table->fullName = $table->schemaName !== $this->defaultSchema ? $table->schemaName . '.' . $table->name : $table->name; |
326
|
|
|
} else { |
327
|
|
|
// only table name passed |
328
|
|
|
$table->schemaName = $this->defaultSchema; |
329
|
|
|
$table->fullName = $table->name = $parts[0]; |
330
|
|
|
} |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* Loads the column information into a [[ColumnSchema]] object. |
335
|
|
|
* @param array $info column information |
336
|
|
|
* @return ColumnSchema the column schema object |
337
|
|
|
*/ |
338
|
|
|
protected function loadColumnSchema($info) |
339
|
|
|
{ |
340
|
|
|
$column = $this->createColumnSchema(); |
341
|
|
|
|
342
|
|
|
$column->name = $info['column_name']; |
343
|
|
|
$column->allowNull = $info['is_nullable'] === 'YES'; |
344
|
|
|
$column->dbType = $info['data_type']; |
345
|
|
|
$column->enumValues = []; // mssql has only vague equivalents to enum |
346
|
|
|
$column->isPrimaryKey = null; // primary key will be determined in findColumns() method |
347
|
|
|
$column->autoIncrement = $info['is_identity'] == 1; |
348
|
|
|
$column->unsigned = stripos($column->dbType, 'unsigned') !== false; |
349
|
|
|
$column->comment = $info['comment'] === null ? '' : $info['comment']; |
350
|
|
|
|
351
|
|
|
$column->type = self::TYPE_STRING; |
352
|
|
|
if (preg_match('/^(\w+)(?:\(([^\)]+)\))?/', $column->dbType, $matches)) { |
353
|
|
|
$type = $matches[1]; |
354
|
|
|
if (isset($this->typeMap[$type])) { |
355
|
|
|
$column->type = $this->typeMap[$type]; |
356
|
|
|
} |
357
|
|
|
if (!empty($matches[2])) { |
358
|
|
|
$values = explode(',', $matches[2]); |
359
|
|
|
$column->size = $column->precision = (int) $values[0]; |
360
|
|
|
if (isset($values[1])) { |
361
|
|
|
$column->scale = (int) $values[1]; |
362
|
|
|
} |
363
|
|
|
if ($column->size === 1 && ($type === 'tinyint' || $type === 'bit')) { |
364
|
|
|
$column->type = 'boolean'; |
365
|
|
|
} elseif ($type === 'bit') { |
366
|
|
|
if ($column->size > 32) { |
367
|
|
|
$column->type = 'bigint'; |
368
|
|
|
} elseif ($column->size === 32) { |
369
|
|
|
$column->type = 'integer'; |
370
|
|
|
} |
371
|
|
|
} |
372
|
|
|
} |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
$column->phpType = $this->getColumnPhpType($column); |
376
|
|
|
|
377
|
|
|
if ($info['column_default'] === '(NULL)') { |
378
|
|
|
$info['column_default'] = null; |
379
|
|
|
} |
380
|
|
|
if (!$column->isPrimaryKey && ($column->type !== 'timestamp' || $info['column_default'] !== 'CURRENT_TIMESTAMP')) { |
381
|
|
|
$column->defaultValue = $column->phpTypecast($info['column_default']); |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
return $column; |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
/** |
388
|
|
|
* Collects the metadata of table columns. |
389
|
|
|
* @param TableSchema $table the table metadata |
390
|
|
|
* @return bool whether the table exists in the database |
391
|
|
|
*/ |
392
|
|
|
protected function findColumns($table) |
393
|
|
|
{ |
394
|
|
|
$columnsTableName = 'INFORMATION_SCHEMA.COLUMNS'; |
395
|
|
|
$whereSql = "[t1].[table_name] = '{$table->name}'"; |
396
|
|
|
if ($table->catalogName !== null) { |
397
|
|
|
$columnsTableName = "{$table->catalogName}.{$columnsTableName}"; |
398
|
|
|
$whereSql .= " AND [t1].[table_catalog] = '{$table->catalogName}'"; |
399
|
|
|
} |
400
|
|
|
if ($table->schemaName !== null) { |
401
|
|
|
$whereSql .= " AND [t1].[table_schema] = '{$table->schemaName}'"; |
402
|
|
|
} |
403
|
|
|
$columnsTableName = $this->quoteTableName($columnsTableName); |
404
|
|
|
|
405
|
|
|
$sql = <<<SQL |
406
|
|
|
SELECT |
407
|
|
|
[t1].[column_name], |
408
|
|
|
[t1].[is_nullable], |
409
|
|
|
[t1].[data_type], |
410
|
|
|
[t1].[column_default], |
411
|
|
|
COLUMNPROPERTY(OBJECT_ID([t1].[table_schema] + '.' + [t1].[table_name]), [t1].[column_name], 'IsIdentity') AS is_identity, |
412
|
|
|
( |
413
|
|
|
SELECT CONVERT(VARCHAR, [t2].[value]) |
414
|
|
|
FROM [sys].[extended_properties] AS [t2] |
415
|
|
|
WHERE |
416
|
|
|
[t2].[class] = 1 AND |
417
|
|
|
[t2].[class_desc] = 'OBJECT_OR_COLUMN' AND |
418
|
|
|
[t2].[name] = 'MS_Description' AND |
419
|
|
|
[t2].[major_id] = OBJECT_ID([t1].[TABLE_SCHEMA] + '.' + [t1].[table_name]) AND |
420
|
|
|
[t2].[minor_id] = COLUMNPROPERTY(OBJECT_ID([t1].[TABLE_SCHEMA] + '.' + [t1].[TABLE_NAME]), [t1].[COLUMN_NAME], 'ColumnID') |
421
|
|
|
) as comment |
422
|
|
|
FROM {$columnsTableName} AS [t1] |
423
|
|
|
WHERE {$whereSql} |
424
|
|
|
SQL; |
425
|
|
|
|
426
|
|
|
try { |
427
|
|
|
$columns = $this->db->createCommand($sql)->queryAll(); |
428
|
|
|
if (empty($columns)) { |
429
|
|
|
return false; |
430
|
|
|
} |
431
|
|
|
} catch (\Exception $e) { |
432
|
|
|
return false; |
433
|
|
|
} |
434
|
|
|
foreach ($columns as $column) { |
435
|
|
|
$column = $this->loadColumnSchema($column); |
436
|
|
|
foreach ($table->primaryKey as $primaryKey) { |
437
|
|
|
if (strcasecmp($column->name, $primaryKey) === 0) { |
438
|
|
|
$column->isPrimaryKey = true; |
439
|
|
|
break; |
440
|
|
|
} |
441
|
|
|
} |
442
|
|
|
if ($column->isPrimaryKey && $column->autoIncrement) { |
443
|
|
|
$table->sequenceName = ''; |
444
|
|
|
} |
445
|
|
|
$table->columns[$column->name] = $column; |
446
|
|
|
} |
447
|
|
|
|
448
|
|
|
return true; |
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
/** |
452
|
|
|
* Collects the constraint details for the given table and constraint type. |
453
|
|
|
* @param TableSchema $table |
454
|
|
|
* @param string $type either PRIMARY KEY or UNIQUE |
455
|
|
|
* @return array each entry contains index_name and field_name |
456
|
|
|
* @since 2.0.4 |
457
|
|
|
*/ |
458
|
|
|
protected function findTableConstraints($table, $type) |
459
|
|
|
{ |
460
|
|
|
$keyColumnUsageTableName = 'INFORMATION_SCHEMA.KEY_COLUMN_USAGE'; |
461
|
|
|
$tableConstraintsTableName = 'INFORMATION_SCHEMA.TABLE_CONSTRAINTS'; |
462
|
|
|
if ($table->catalogName !== null) { |
463
|
|
|
$keyColumnUsageTableName = $table->catalogName . '.' . $keyColumnUsageTableName; |
464
|
|
|
$tableConstraintsTableName = $table->catalogName . '.' . $tableConstraintsTableName; |
465
|
|
|
} |
466
|
|
|
$keyColumnUsageTableName = $this->quoteTableName($keyColumnUsageTableName); |
467
|
|
|
$tableConstraintsTableName = $this->quoteTableName($tableConstraintsTableName); |
468
|
|
|
|
469
|
|
|
$sql = <<<SQL |
470
|
|
|
SELECT |
471
|
|
|
[kcu].[constraint_name] AS [index_name], |
472
|
|
|
[kcu].[column_name] AS [field_name] |
473
|
|
|
FROM {$keyColumnUsageTableName} AS [kcu] |
474
|
|
|
LEFT JOIN {$tableConstraintsTableName} AS [tc] ON |
475
|
|
|
[kcu].[table_schema] = [tc].[table_schema] AND |
476
|
|
|
[kcu].[table_name] = [tc].[table_name] AND |
477
|
|
|
[kcu].[constraint_name] = [tc].[constraint_name] |
478
|
|
|
WHERE |
479
|
|
|
[tc].[constraint_type] = :type AND |
480
|
|
|
[kcu].[table_name] = :tableName AND |
481
|
|
|
[kcu].[table_schema] = :schemaName |
482
|
|
|
SQL; |
483
|
|
|
|
484
|
|
|
return $this->db |
485
|
|
|
->createCommand($sql, [ |
486
|
|
|
':tableName' => $table->name, |
487
|
|
|
':schemaName' => $table->schemaName, |
488
|
|
|
':type' => $type, |
489
|
|
|
]) |
490
|
|
|
->queryAll(); |
491
|
|
|
} |
492
|
|
|
|
493
|
|
|
/** |
494
|
|
|
* Collects the primary key column details for the given table. |
495
|
|
|
* @param TableSchema $table the table metadata |
496
|
|
|
*/ |
497
|
|
|
protected function findPrimaryKeys($table) |
498
|
|
|
{ |
499
|
|
|
$result = []; |
500
|
|
|
foreach ($this->findTableConstraints($table, 'PRIMARY KEY') as $row) { |
501
|
|
|
$result[] = $row['field_name']; |
502
|
|
|
} |
503
|
|
|
$table->primaryKey = $result; |
504
|
|
|
} |
505
|
|
|
|
506
|
|
|
/** |
507
|
|
|
* Collects the foreign key column details for the given table. |
508
|
|
|
* @param TableSchema $table the table metadata |
509
|
|
|
*/ |
510
|
|
|
protected function findForeignKeys($table) |
511
|
|
|
{ |
512
|
|
|
$object = $table->name; |
513
|
|
|
if ($table->schemaName !== null) { |
514
|
|
|
$object = $table->schemaName . '.' . $object; |
515
|
|
|
} |
516
|
|
|
if ($table->catalogName !== null) { |
517
|
|
|
$object = $table->catalogName . '.' . $object; |
518
|
|
|
} |
519
|
|
|
|
520
|
|
|
// please refer to the following page for more details: |
521
|
|
|
// http://msdn2.microsoft.com/en-us/library/aa175805(SQL.80).aspx |
522
|
|
|
$sql = <<<SQL |
523
|
|
|
SELECT |
524
|
|
|
[fk].[name] AS [fk_name], |
525
|
|
|
[cp].[name] AS [fk_column_name], |
526
|
|
|
OBJECT_NAME([fk].[referenced_object_id]) AS [uq_table_name], |
527
|
|
|
[cr].[name] AS [uq_column_name] |
528
|
|
|
FROM |
529
|
|
|
[sys].[foreign_keys] AS [fk] |
530
|
|
|
INNER JOIN [sys].[foreign_key_columns] AS [fkc] ON |
531
|
|
|
[fk].[object_id] = [fkc].[constraint_object_id] |
532
|
|
|
INNER JOIN [sys].[columns] AS [cp] ON |
533
|
|
|
[fk].[parent_object_id] = [cp].[object_id] AND |
534
|
|
|
[fkc].[parent_column_id] = [cp].[column_id] |
535
|
|
|
INNER JOIN [sys].[columns] AS [cr] ON |
536
|
|
|
[fk].[referenced_object_id] = [cr].[object_id] AND |
537
|
|
|
[fkc].[referenced_column_id] = [cr].[column_id] |
538
|
|
|
WHERE |
539
|
|
|
[fk].[parent_object_id] = OBJECT_ID(:object) |
540
|
|
|
SQL; |
541
|
|
|
|
542
|
|
|
$rows = $this->db->createCommand($sql, [ |
543
|
|
|
':object' => $object, |
544
|
|
|
])->queryAll(); |
545
|
|
|
|
546
|
|
|
$table->foreignKeys = []; |
547
|
|
|
foreach ($rows as $row) { |
548
|
|
|
$table->foreignKeys[$row['fk_name']] = [$row['uq_table_name'], $row['fk_column_name'] => $row['uq_column_name']]; |
549
|
|
|
} |
550
|
|
|
} |
551
|
|
|
|
552
|
|
|
/** |
553
|
|
|
* @inheritdoc |
554
|
|
|
*/ |
555
|
|
|
protected function findViewNames($schema = '') |
556
|
|
|
{ |
557
|
|
|
if ($schema === '') { |
558
|
|
|
$schema = $this->defaultSchema; |
559
|
|
|
} |
560
|
|
|
|
561
|
|
|
$sql = <<<SQL |
562
|
|
|
SELECT [t].[table_name] |
563
|
|
|
FROM [INFORMATION_SCHEMA].[TABLES] AS [t] |
564
|
|
|
WHERE [t].[table_schema] = :schema AND [t].[table_type] = 'VIEW' |
565
|
|
|
ORDER BY [t].[table_name] |
566
|
|
|
SQL; |
567
|
|
|
|
568
|
|
|
return $this->db->createCommand($sql, [':schema' => $schema])->queryColumn(); |
569
|
|
|
} |
570
|
|
|
|
571
|
|
|
/** |
572
|
|
|
* Returns all unique indexes for the given table. |
573
|
|
|
* Each array element is of the following structure: |
574
|
|
|
* |
575
|
|
|
* ```php |
576
|
|
|
* [ |
577
|
|
|
* 'IndexName1' => ['col1' [, ...]], |
578
|
|
|
* 'IndexName2' => ['col2' [, ...]], |
579
|
|
|
* ] |
580
|
|
|
* ``` |
581
|
|
|
* |
582
|
|
|
* @param TableSchema $table the table metadata |
583
|
|
|
* @return array all unique indexes for the given table. |
584
|
|
|
* @since 2.0.4 |
585
|
|
|
*/ |
586
|
|
|
public function findUniqueIndexes($table) |
587
|
|
|
{ |
588
|
|
|
$result = []; |
589
|
|
|
foreach ($this->findTableConstraints($table, 'UNIQUE') as $row) { |
590
|
|
|
$result[$row['index_name']][] = $row['field_name']; |
591
|
|
|
} |
592
|
|
|
return $result; |
593
|
|
|
} |
594
|
|
|
|
595
|
|
|
/** |
596
|
|
|
* Loads multiple types of constraints and returns the specified ones. |
597
|
|
|
* @param string $tableName table name. |
598
|
|
|
* @param string $returnType return type: |
599
|
|
|
* - primaryKey |
600
|
|
|
* - foreignKeys |
601
|
|
|
* - uniques |
602
|
|
|
* - checks |
603
|
|
|
* - defaults |
604
|
|
|
* @return mixed constraints. |
605
|
|
|
*/ |
606
|
|
|
private function loadTableConstraints($tableName, $returnType) |
607
|
|
|
{ |
608
|
|
|
static $sql = <<<SQL |
609
|
|
|
SELECT |
610
|
|
|
[o].[name] AS [name], |
611
|
|
|
COALESCE([ccol].[name], [dcol].[name], [fccol].[name], [kiccol].[name]) AS [column_name], |
612
|
|
|
RTRIM([o].[type]) AS [type], |
613
|
|
|
OBJECT_SCHEMA_NAME([f].[referenced_object_id]) AS [foreign_table_schema], |
614
|
|
|
OBJECT_NAME([f].[referenced_object_id]) AS [foreign_table_name], |
615
|
|
|
[ffccol].[name] AS [foreign_column_name], |
616
|
|
|
[f].[update_referential_action_desc] AS [on_update], |
617
|
|
|
[f].[delete_referential_action_desc] AS [on_delete], |
618
|
|
|
[c].[definition] AS [check_expr], |
619
|
|
|
[d].[definition] AS [default_expr] |
620
|
|
|
FROM (SELECT OBJECT_ID(:fullName) AS [object_id]) AS [t] |
621
|
|
|
INNER JOIN [sys].[objects] AS [o] |
622
|
|
|
ON [o].[parent_object_id] = [t].[object_id] AND [o].[type] IN ('PK', 'UQ', 'C', 'D', 'F') |
623
|
|
|
LEFT JOIN [sys].[check_constraints] AS [c] |
624
|
|
|
ON [c].[object_id] = [o].[object_id] |
625
|
|
|
LEFT JOIN [sys].[columns] AS [ccol] |
626
|
|
|
ON [ccol].[object_id] = [c].[parent_object_id] AND [ccol].[column_id] = [c].[parent_column_id] |
627
|
|
|
LEFT JOIN [sys].[default_constraints] AS [d] |
628
|
|
|
ON [d].[object_id] = [o].[object_id] |
629
|
|
|
LEFT JOIN [sys].[columns] AS [dcol] |
630
|
|
|
ON [dcol].[object_id] = [d].[parent_object_id] AND [dcol].[column_id] = [d].[parent_column_id] |
631
|
|
|
LEFT JOIN [sys].[key_constraints] AS [k] |
632
|
|
|
ON [k].[object_id] = [o].[object_id] |
633
|
|
|
LEFT JOIN [sys].[index_columns] AS [kic] |
634
|
|
|
ON [kic].[object_id] = [k].[parent_object_id] AND [kic].[index_id] = [k].[unique_index_id] |
635
|
|
|
LEFT JOIN [sys].[columns] AS [kiccol] |
636
|
|
|
ON [kiccol].[object_id] = [kic].[object_id] AND [kiccol].[column_id] = [kic].[column_id] |
637
|
|
|
LEFT JOIN [sys].[foreign_keys] AS [f] |
638
|
|
|
ON [f].[object_id] = [o].[object_id] |
639
|
|
|
LEFT JOIN [sys].[foreign_key_columns] AS [fc] |
640
|
|
|
ON [fc].[constraint_object_id] = [o].[object_id] |
641
|
|
|
LEFT JOIN [sys].[columns] AS [fccol] |
642
|
|
|
ON [fccol].[object_id] = [fc].[parent_object_id] AND [fccol].[column_id] = [fc].[parent_column_id] |
643
|
|
|
LEFT JOIN [sys].[columns] AS [ffccol] |
644
|
|
|
ON [ffccol].[object_id] = [fc].[referenced_object_id] AND [ffccol].[column_id] = [fc].[referenced_column_id] |
645
|
|
|
ORDER BY [kic].[key_ordinal] ASC, [fc].[constraint_column_id] ASC |
646
|
|
|
SQL; |
647
|
|
|
|
648
|
|
|
$resolvedName = $this->resolveTableName($tableName); |
649
|
|
|
$constraints = $this->db->createCommand($sql, [ |
650
|
|
|
':fullName' => $resolvedName->fullName, |
651
|
|
|
])->queryAll(); |
652
|
|
|
$constraints = $this->normalizePdoRowKeyCase($constraints, true); |
653
|
|
|
$constraints = ArrayHelper::index($constraints, null, ['type', 'name']); |
654
|
|
|
$result = [ |
655
|
|
|
'primaryKey' => null, |
656
|
|
|
'foreignKeys' => [], |
657
|
|
|
'uniques' => [], |
658
|
|
|
'checks' => [], |
659
|
|
|
'defaults' => [], |
660
|
|
|
]; |
661
|
|
|
foreach ($constraints as $type => $names) { |
662
|
|
|
foreach ($names as $name => $constraint) { |
663
|
|
|
switch ($type) { |
664
|
|
|
case 'PK': |
665
|
|
|
$result['primaryKey'] = new Constraint([ |
666
|
|
|
'name' => $name, |
667
|
|
|
'columnNames' => ArrayHelper::getColumn($constraint, 'column_name'), |
668
|
|
|
]); |
669
|
|
|
break; |
670
|
|
|
case 'F': |
671
|
|
|
$result['foreignKeys'][] = new ForeignKeyConstraint([ |
672
|
|
|
'name' => $name, |
673
|
|
|
'columnNames' => ArrayHelper::getColumn($constraint, 'column_name'), |
674
|
|
|
'foreignSchemaName' => $constraint[0]['foreign_table_schema'], |
675
|
|
|
'foreignTableName' => $constraint[0]['foreign_table_name'], |
676
|
|
|
'foreignColumnNames' => ArrayHelper::getColumn($constraint, 'foreign_column_name'), |
677
|
|
|
'onDelete' => str_replace('_', '', $constraint[0]['on_delete']), |
678
|
|
|
'onUpdate' => str_replace('_', '', $constraint[0]['on_update']), |
679
|
|
|
]); |
680
|
|
|
break; |
681
|
|
|
case 'UQ': |
682
|
|
|
$result['uniques'][] = new Constraint([ |
683
|
|
|
'name' => $name, |
684
|
|
|
'columnNames' => ArrayHelper::getColumn($constraint, 'column_name'), |
685
|
|
|
]); |
686
|
|
|
break; |
687
|
|
|
case 'C': |
688
|
|
|
$result['checks'][] = new CheckConstraint([ |
689
|
|
|
'name' => $name, |
690
|
|
|
'columnNames' => ArrayHelper::getColumn($constraint, 'column_name'), |
691
|
|
|
'expression' => $constraint[0]['check_expr'], |
692
|
|
|
]); |
693
|
|
|
break; |
694
|
|
|
case 'D': |
695
|
|
|
$result['defaults'][] = new DefaultValueConstraint([ |
696
|
|
|
'name' => $name, |
697
|
|
|
'columnNames' => ArrayHelper::getColumn($constraint, 'column_name'), |
698
|
|
|
'value' => $constraint[0]['default_expr'], |
699
|
|
|
]); |
700
|
|
|
break; |
701
|
|
|
} |
702
|
|
|
} |
703
|
|
|
} |
704
|
|
|
foreach ($result as $type => $data) { |
705
|
|
|
$this->setTableMetadata($tableName, $type, $data); |
706
|
|
|
} |
707
|
|
|
return $result[$returnType]; |
708
|
|
|
} |
709
|
|
|
} |
710
|
|
|
|