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\AbstractColumn; |
11
|
|
|
use Spiral\Database\Entities\Schemas\AbstractCommander; |
12
|
|
|
use Spiral\Database\Entities\Schemas\AbstractIndex; |
13
|
|
|
use Spiral\Database\Entities\Schemas\AbstractTable; |
14
|
|
|
use Spiral\Database\Exceptions\SchemaException; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* SQLServer commander. |
18
|
|
|
*/ |
19
|
|
|
class Commander extends AbstractCommander |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Rename table from one name to another. |
23
|
|
|
* |
24
|
|
|
* @param string $table |
25
|
|
|
* @param string $name |
26
|
|
|
* @return self |
27
|
|
|
*/ |
28
|
|
|
public function renameTable($table, $name) |
29
|
|
|
{ |
30
|
|
|
$this->run("sp_rename @objname = ?, @newname = ?", [$table, $name]); |
31
|
|
|
|
32
|
|
|
return $this; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Driver specific column add command. |
37
|
|
|
* |
38
|
|
|
* @param AbstractTable $table |
39
|
|
|
* @param AbstractColumn $column |
40
|
|
|
* @return self |
41
|
|
|
*/ |
42
|
|
|
public function addColumn(AbstractTable $table, AbstractColumn $column) |
43
|
|
|
{ |
44
|
|
|
$this->run("ALTER TABLE {$table->getName(true)} ADD {$column->sqlStatement()}"); |
45
|
|
|
|
46
|
|
|
return $this; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Driver specific column alter command. |
51
|
|
|
* |
52
|
|
|
* @param AbstractTable $table |
53
|
|
|
* @param AbstractColumn $initial |
54
|
|
|
* @param AbstractColumn $column |
55
|
|
|
* @return self |
56
|
|
|
*/ |
57
|
|
|
public function alterColumn( |
58
|
|
|
AbstractTable $table, |
59
|
|
|
AbstractColumn $initial, |
60
|
|
|
AbstractColumn $column |
61
|
|
|
) { |
62
|
|
|
if (!$initial instanceof ColumnSchema || !$column instanceof ColumnSchema) { |
63
|
|
|
throw new SchemaException("SQlServer commander can work only with Postgres columns."); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if ($column->getName() != $initial->getName()) { |
67
|
|
|
//Renaming is separate operation |
68
|
|
|
$this->run("sp_rename ?, ?, 'COLUMN'", [ |
69
|
|
|
$table->getName() . '.' . $initial->getName(), |
70
|
|
|
$column->getName() |
71
|
|
|
]); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
//In SQLServer we have to drop ALL related indexes and foreign keys while |
75
|
|
|
//applying type change... yeah... |
76
|
|
|
|
77
|
|
|
$indexesBackup = []; |
78
|
|
|
$foreignBackup = []; |
79
|
|
|
foreach ($table->getIndexes() as $index) { |
80
|
|
|
if (in_array($column->getName(), $index->getColumns())) { |
81
|
|
|
$indexesBackup[] = $index; |
82
|
|
|
$this->dropIndex($table, $index); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
foreach ($table->getForeigns() as $foreign) { |
87
|
|
|
if ($foreign->getColumn() == $column->getName()) { |
88
|
|
|
$foreignBackup[] = $foreign; |
89
|
|
|
$this->dropForeign($table, $foreign); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
//Column will recreate needed constraints |
94
|
|
|
foreach ($column->getConstraints() as $constraint) { |
95
|
|
|
$this->dropConstrain($table, $constraint); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
foreach ($column->alteringOperations($initial) as $operation) { |
99
|
|
|
$this->run("ALTER TABLE {$table->getName(true)} {$operation}"); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
//Restoring indexes and foreign keys |
103
|
|
|
foreach ($indexesBackup as $index) { |
104
|
|
|
$this->addIndex($table, $index); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
foreach ($foreignBackup as $foreign) { |
108
|
|
|
$this->addForeign($table, $foreign); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Driver specific index remove (drop) command. |
116
|
|
|
* |
117
|
|
|
* @param AbstractTable $table |
118
|
|
|
* @param AbstractIndex $index |
119
|
|
|
* @return self |
120
|
|
|
*/ |
121
|
|
|
public function dropIndex(AbstractTable $table, AbstractIndex $index) |
122
|
|
|
{ |
123
|
|
|
$this->run("DROP INDEX {$index->getName(true)} ON {$table->getName(true)}"); |
124
|
|
|
|
125
|
|
|
return $this; |
126
|
|
|
} |
127
|
|
|
} |