1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Spiral Framework. |
4
|
|
|
* |
5
|
|
|
* @license MIT |
6
|
|
|
* @author Anton Titov (Wolfy-J) |
7
|
|
|
*/ |
8
|
|
|
namespace Spiral\Database\Drivers\SQLServer\Schemas; |
9
|
|
|
|
10
|
|
|
use Spiral\Database\Entities\Schemas\AbstractTable; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* SQLServer table schema. |
14
|
|
|
*/ |
15
|
|
|
class TableSchema extends AbstractTable |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* {@inheritdoc} |
19
|
|
|
*/ |
20
|
|
|
protected function loadColumns() |
21
|
|
|
{ |
22
|
|
|
$query = 'SELECT * FROM information_schema.columns INNER JOIN sys.columns AS sysColumns ' |
23
|
|
|
. 'ON (object_name(object_id) = table_name AND sysColumns.name = COLUMN_NAME) ' |
24
|
|
|
. 'WHERE table_name = ?'; |
25
|
|
|
|
26
|
|
|
foreach ($this->driver->query($query, [$this->getName()]) as $column) { |
27
|
|
|
$this->registerColumn($this->columnSchema($column['COLUMN_NAME'], $column)); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
return $this; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* {@inheritdoc} |
35
|
|
|
* |
36
|
|
|
* @link http://stackoverflow.com/questions/765867/list-of-all-index-index-columns-in-sql-server-db |
37
|
|
|
*/ |
38
|
|
|
protected function loadIndexes() |
39
|
|
|
{ |
40
|
|
|
$query = "SELECT indexes.name AS indexName, cl.name AS columnName, " |
41
|
|
|
. "is_primary_key AS isPrimary, is_unique AS isUnique\n" |
42
|
|
|
. "FROM sys.indexes AS indexes\n" |
43
|
|
|
. "INNER JOIN sys.index_columns as columns\n" |
44
|
|
|
. " ON indexes.object_id = columns.object_id AND indexes.index_id = columns.index_id\n" |
45
|
|
|
. "INNER JOIN sys.columns AS cl\n" |
46
|
|
|
. " ON columns.object_id = cl.object_id AND columns.column_id = cl.column_id\n" |
47
|
|
|
. "INNER JOIN sys.tables AS t\n" |
48
|
|
|
. " ON indexes.object_id = t.object_id\n" |
49
|
|
|
. "WHERE t.name = ? ORDER BY indexes.name, indexes.index_id, columns.index_column_id"; |
50
|
|
|
|
51
|
|
|
$indexes = []; |
52
|
|
|
$primaryKeys = []; |
53
|
|
|
foreach ($this->driver->query($query, [$this->getName()]) as $index) { |
54
|
|
|
if ($index['isPrimary']) { |
55
|
|
|
$primaryKeys[] = $index['columnName']; |
56
|
|
|
continue; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$indexes[$index['indexName']][] = $index; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$this->setPrimaryKeys($primaryKeys); |
63
|
|
|
foreach ($indexes as $index => $schema) { |
64
|
|
|
$this->registerIndex($this->indexSchema($index, $schema)); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
|
View Code Duplication |
protected function loadReferences() |
|
|
|
|
74
|
|
|
{ |
75
|
|
|
$references = $this->driver->query("sp_fkeys @fktable_name = ?", [$this->getName()]); |
76
|
|
|
foreach ($references as $reference) { |
77
|
|
|
$this->registerReference($this->referenceSchema($reference['FK_NAME'], $reference)); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritdoc} |
83
|
|
|
*/ |
84
|
|
|
protected function columnSchema($name, $schema = null) |
85
|
|
|
{ |
86
|
|
|
return new ColumnSchema($this, $name, $schema); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritdoc} |
91
|
|
|
*/ |
92
|
|
|
protected function indexSchema($name, $schema = null) |
93
|
|
|
{ |
94
|
|
|
return new IndexSchema($this, $name, $schema); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* {@inheritdoc} |
99
|
|
|
*/ |
100
|
|
|
protected function referenceSchema($name, $schema = null) |
101
|
|
|
{ |
102
|
|
|
return new ReferenceSchema($this, $name, $schema); |
103
|
|
|
} |
104
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.