|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Spiral Framework. |
|
4
|
|
|
* |
|
5
|
|
|
* @license MIT |
|
6
|
|
|
* @author Anton Titov (Wolfy-J) |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Spiral\Database\Schemas; |
|
10
|
|
|
|
|
11
|
|
|
use Spiral\Database\Entities\Driver; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Holds set of DBMS specific element operations. |
|
15
|
|
|
* |
|
16
|
|
|
* @todo deprecate in order to replace with Phinx and/or migration engine |
|
17
|
|
|
*/ |
|
18
|
|
|
abstract class AbstractCommander |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var Driver |
|
22
|
|
|
*/ |
|
23
|
|
|
private $driver = null; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param Driver $driver |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct(Driver $driver) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->driver = $driver; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Associated driver. |
|
35
|
|
|
* |
|
36
|
|
|
* @return Driver |
|
37
|
|
|
*/ |
|
38
|
|
|
public function driver() |
|
39
|
|
|
{ |
|
40
|
|
|
return $this->driver; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Create table! |
|
45
|
|
|
* |
|
46
|
|
|
* @param AbstractTable $table |
|
47
|
|
|
* |
|
48
|
|
|
* @return self |
|
49
|
|
|
*/ |
|
50
|
|
|
public function createTable(AbstractTable $table) |
|
51
|
|
|
{ |
|
52
|
|
|
//Executing! |
|
53
|
|
|
$this->run($this->createStatement($table)); |
|
54
|
|
|
|
|
55
|
|
|
//Not all databases support adding index while table creation, so we can do it after |
|
56
|
|
|
foreach ($table->getIndexes() as $index) { |
|
57
|
|
|
$this->addIndex($table, $index); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return $this; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Drop table from database. |
|
65
|
|
|
* |
|
66
|
|
|
* @param string $table |
|
67
|
|
|
*/ |
|
68
|
|
|
public function dropTable($table) |
|
69
|
|
|
{ |
|
70
|
|
|
$this->run("DROP TABLE {$this->quote($table)}"); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Rename table from one name to another. |
|
75
|
|
|
* |
|
76
|
|
|
* @param string $table |
|
77
|
|
|
* @param string $name |
|
78
|
|
|
* |
|
79
|
|
|
* @return self |
|
80
|
|
|
*/ |
|
81
|
|
|
public function renameTable($table, $name) |
|
82
|
|
|
{ |
|
83
|
|
|
$this->run("ALTER TABLE {$this->quote($table)} RENAME TO {$this->quote($name)}"); |
|
84
|
|
|
|
|
85
|
|
|
return $this; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Driver specific column add command. |
|
90
|
|
|
* |
|
91
|
|
|
* @param AbstractTable $table |
|
92
|
|
|
* @param AbstractColumn $column |
|
93
|
|
|
* |
|
94
|
|
|
* @return self |
|
95
|
|
|
*/ |
|
96
|
|
|
public function addColumn(AbstractTable $table, AbstractColumn $column) |
|
97
|
|
|
{ |
|
98
|
|
|
$this->run("ALTER TABLE {$table->getName(true)} ADD COLUMN {$column->sqlStatement()}"); |
|
99
|
|
|
|
|
100
|
|
|
return $this; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Driver specific column remove (drop) command. |
|
105
|
|
|
* |
|
106
|
|
|
* @param AbstractTable $table |
|
107
|
|
|
* @param AbstractColumn $column |
|
108
|
|
|
* |
|
109
|
|
|
* @return self |
|
110
|
|
|
*/ |
|
111
|
|
|
public function dropColumn(AbstractTable $table, AbstractColumn $column) |
|
112
|
|
|
{ |
|
113
|
|
|
foreach ($column->getConstraints() as $constraint) { |
|
114
|
|
|
//We have to erase all associated constraints |
|
115
|
|
|
$this->dropConstrain($table, $constraint); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
$this->run("ALTER TABLE {$table->getName(true)} DROP COLUMN {$column->getName(true)}"); |
|
119
|
|
|
|
|
120
|
|
|
return $this; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Driver specific column alter command. |
|
125
|
|
|
* |
|
126
|
|
|
* @param AbstractTable $table |
|
127
|
|
|
* @param AbstractColumn $initial |
|
128
|
|
|
* @param AbstractColumn $column |
|
129
|
|
|
* |
|
130
|
|
|
* @return self |
|
131
|
|
|
*/ |
|
132
|
|
|
abstract public function alterColumn( |
|
133
|
|
|
AbstractTable $table, |
|
134
|
|
|
AbstractColumn $initial, |
|
135
|
|
|
AbstractColumn $column |
|
136
|
|
|
); |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Driver specific index adding command. |
|
140
|
|
|
* |
|
141
|
|
|
* @param AbstractTable $table |
|
142
|
|
|
* @param AbstractIndex $index |
|
143
|
|
|
* |
|
144
|
|
|
* @return self |
|
145
|
|
|
*/ |
|
146
|
|
|
public function addIndex(AbstractTable $table, AbstractIndex $index) |
|
|
|
|
|
|
147
|
|
|
{ |
|
148
|
|
|
$this->run("CREATE {$index->sqlStatement()}"); |
|
149
|
|
|
|
|
150
|
|
|
return $this; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Driver specific index remove (drop) command. |
|
155
|
|
|
* |
|
156
|
|
|
* @param AbstractTable $table |
|
157
|
|
|
* @param AbstractIndex $index |
|
158
|
|
|
* |
|
159
|
|
|
* @return self |
|
160
|
|
|
*/ |
|
161
|
|
|
public function dropIndex(AbstractTable $table, AbstractIndex $index) |
|
162
|
|
|
{ |
|
163
|
|
|
$this->run("DROP INDEX {$index->getName(true)}"); |
|
164
|
|
|
|
|
165
|
|
|
return $this; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Driver specific index alter command, by default it will remove and add index. |
|
170
|
|
|
* |
|
171
|
|
|
* @param AbstractTable $table |
|
172
|
|
|
* @param AbstractIndex $initial |
|
173
|
|
|
* @param AbstractIndex $index |
|
174
|
|
|
* |
|
175
|
|
|
* @return self |
|
176
|
|
|
*/ |
|
177
|
|
|
public function alterIndex(AbstractTable $table, AbstractIndex $initial, AbstractIndex $index) |
|
178
|
|
|
{ |
|
179
|
|
|
return $this->dropIndex($table, $initial)->addIndex($table, $index); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Driver specific foreign key adding command. |
|
184
|
|
|
* |
|
185
|
|
|
* @param AbstractTable $table |
|
186
|
|
|
* @param AbstractReference $foreign |
|
187
|
|
|
* |
|
188
|
|
|
* @return self |
|
189
|
|
|
*/ |
|
190
|
|
|
public function addForeign(AbstractTable $table, AbstractReference $foreign) |
|
191
|
|
|
{ |
|
192
|
|
|
$this->run("ALTER TABLE {$table->getName(true)} ADD {$foreign->sqlStatement()}"); |
|
193
|
|
|
|
|
194
|
|
|
return $this; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Driver specific foreign key remove (drop) command. |
|
199
|
|
|
* |
|
200
|
|
|
* @param AbstractTable $table |
|
201
|
|
|
* @param AbstractReference $foreign |
|
202
|
|
|
* |
|
203
|
|
|
* @return self |
|
204
|
|
|
*/ |
|
205
|
|
|
public function dropForeign(AbstractTable $table, AbstractReference $foreign) |
|
206
|
|
|
{ |
|
207
|
|
|
return $this->dropConstrain($table, $foreign->getName()); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* Driver specific foreign key alter command, by default it will remove and add foreign key. |
|
212
|
|
|
* |
|
213
|
|
|
* @param AbstractTable $table |
|
214
|
|
|
* @param AbstractReference $initial |
|
215
|
|
|
* @param AbstractReference $foreign |
|
216
|
|
|
* |
|
217
|
|
|
* @return self |
|
218
|
|
|
*/ |
|
219
|
|
|
public function alterForeign( |
|
220
|
|
|
AbstractTable $table, |
|
221
|
|
|
AbstractReference $initial, |
|
222
|
|
|
AbstractReference $foreign |
|
223
|
|
|
) { |
|
224
|
|
|
return $this->dropForeign($table, $initial)->addForeign($table, $foreign); |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* Drop column constraint using it's name. |
|
229
|
|
|
* |
|
230
|
|
|
* @param AbstractTable $table |
|
231
|
|
|
* @param string $constraint |
|
232
|
|
|
* |
|
233
|
|
|
* @return self |
|
234
|
|
|
*/ |
|
235
|
|
|
public function dropConstrain(AbstractTable $table, $constraint) |
|
236
|
|
|
{ |
|
237
|
|
|
$this->run("ALTER TABLE {$table->getName(true)} DROP CONSTRAINT {$this->quote($constraint)}"); |
|
238
|
|
|
|
|
239
|
|
|
return $this; |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* Execute statement. |
|
244
|
|
|
* |
|
245
|
|
|
* @param string $statement |
|
246
|
|
|
* @param array $parameters |
|
247
|
|
|
* |
|
248
|
|
|
* @return \PDOStatement |
|
249
|
|
|
*/ |
|
250
|
|
|
protected function run($statement, array $parameters = []) |
|
251
|
|
|
{ |
|
252
|
|
|
return $this->driver->statement($statement, $parameters); |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
/** |
|
256
|
|
|
* Quote identifier. |
|
257
|
|
|
* |
|
258
|
|
|
* @param string $identifier |
|
259
|
|
|
* |
|
260
|
|
|
* @return string |
|
261
|
|
|
*/ |
|
262
|
|
|
protected function quote($identifier) |
|
263
|
|
|
{ |
|
264
|
|
|
return $this->driver->identifier($identifier); |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
/** |
|
268
|
|
|
* Get statement needed to create table. |
|
269
|
|
|
* |
|
270
|
|
|
* @param AbstractTable $table |
|
271
|
|
|
* |
|
272
|
|
|
* @return string |
|
273
|
|
|
*/ |
|
274
|
|
|
protected function createStatement(AbstractTable $table) |
|
275
|
|
|
{ |
|
276
|
|
|
$statement = ["CREATE TABLE {$table->getName(true)} ("]; |
|
277
|
|
|
$innerStatement = []; |
|
278
|
|
|
|
|
279
|
|
|
//Columns |
|
280
|
|
|
foreach ($table->getColumns() as $column) { |
|
281
|
|
|
$innerStatement[] = $column->sqlStatement(); |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
//Primary key |
|
285
|
|
|
if (!empty($table->getPrimaryKeys())) { |
|
286
|
|
|
$primaryKeys = array_map([$this, 'quote'], $table->getPrimaryKeys()); |
|
287
|
|
|
|
|
288
|
|
|
$innerStatement[] = 'PRIMARY KEY (' . implode(', ', $primaryKeys) . ')'; |
|
289
|
|
|
} |
|
290
|
|
|
|
|
291
|
|
|
//Constraints and foreign keys |
|
292
|
|
|
foreach ($table->getForeigns() as $reference) { |
|
293
|
|
|
$innerStatement[] = $reference->sqlStatement(); |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
$statement[] = ' ' . implode(",\n ", $innerStatement); |
|
297
|
|
|
$statement[] = ')'; |
|
298
|
|
|
|
|
299
|
|
|
return implode("\n", $statement); |
|
300
|
|
|
} |
|
301
|
|
|
} |
|
302
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.