1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Phinx |
4
|
|
|
* |
5
|
|
|
* (The MIT license) |
6
|
|
|
* Copyright (c) 2015 Rob Morgan |
7
|
|
|
* |
8
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
9
|
|
|
* of this software and associated * documentation files (the "Software"), to |
10
|
|
|
* deal in the Software without restriction, including without limitation the |
11
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
12
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is |
13
|
|
|
* furnished to do so, subject to the following conditions: |
14
|
|
|
* |
15
|
|
|
* The above copyright notice and this permission notice shall be included in |
16
|
|
|
* all copies or substantial portions of the Software. |
17
|
|
|
* |
18
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
19
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
20
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
21
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
22
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
23
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
24
|
|
|
* IN THE SOFTWARE. |
25
|
|
|
* |
26
|
|
|
* @package Phinx |
27
|
|
|
* @subpackage Phinx\Db\Adapter |
28
|
|
|
*/ |
29
|
|
|
namespace Phinx\Db\Adapter; |
30
|
|
|
|
31
|
|
|
use Phinx\Db\Action\AddColumn; |
32
|
|
|
use Phinx\Db\Action\AddForeignKey; |
33
|
|
|
use Phinx\Db\Action\AddIndex; |
34
|
|
|
use Phinx\Db\Action\ChangeColumn; |
35
|
|
|
use Phinx\Db\Action\DropForeignKey; |
36
|
|
|
use Phinx\Db\Action\DropIndex; |
37
|
|
|
use Phinx\Db\Action\DropTable; |
38
|
|
|
use Phinx\Db\Action\RemoveColumn; |
39
|
|
|
use Phinx\Db\Action\RenameColumn; |
40
|
|
|
use Phinx\Db\Action\RenameTable; |
41
|
|
|
use Phinx\Db\Table\Column; |
42
|
|
|
use Phinx\Db\Table\ForeignKey; |
43
|
|
|
use Phinx\Db\Table\Index; |
44
|
|
|
use Phinx\Db\Table\Table; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Table prefix/suffix adapter. |
48
|
|
|
* |
49
|
|
|
* Used for inserting a prefix or suffix into table names. |
50
|
|
|
* |
51
|
|
|
* @author Samuel Fisher <[email protected]> |
52
|
|
|
*/ |
53
|
|
|
class TablePrefixAdapter extends AdapterWrapper implements DirectActionInterface |
54
|
|
|
{ |
55
|
|
|
/** |
56
|
2 |
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
2 |
|
public function getAdapterType() |
59
|
2 |
|
{ |
60
|
|
|
return 'TablePrefixAdapter'; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
3 |
|
*/ |
66
|
|
|
public function hasTable($tableName) |
67
|
3 |
|
{ |
68
|
3 |
|
$adapterTableName = $this->getAdapterTableName($tableName); |
69
|
3 |
|
|
70
|
|
|
return parent::hasTable($adapterTableName); |
71
|
3 |
|
} |
72
|
1 |
|
|
73
|
1 |
|
/** |
74
|
1 |
|
* {@inheritdoc} |
75
|
3 |
|
*/ |
76
|
|
|
public function createTable(Table $table, array $columns = [], array $indexes = []) |
77
|
3 |
|
{ |
78
|
3 |
|
$adapterTable = new Table( |
79
|
|
|
$this->getAdapterTableName($table->getName()), |
80
|
|
|
$table->getOptions() |
81
|
|
|
); |
82
|
|
|
parent::createTable($adapterTable, $columns, $indexes); |
83
|
1 |
|
} |
84
|
|
|
|
85
|
1 |
|
/** |
86
|
1 |
|
* {@inheritdoc} |
87
|
1 |
|
*/ |
88
|
1 |
|
public function renameTable($tableName, $newTableName) |
89
|
|
|
{ |
90
|
|
|
$adapter = $this->getAdapter(); |
91
|
|
|
if (!$adapter instanceof DirectActionInterface) { |
92
|
|
|
throw new \BadMethodCallException('The underlying adapter does not implement DirectActionInterface'); |
93
|
1 |
|
} |
94
|
|
|
|
95
|
1 |
|
$adapterTableName = $this->getAdapterTableName($tableName); |
96
|
1 |
|
$adapterNewTableName = $this->getAdapterTableName($newTableName); |
97
|
1 |
|
$adapter->renameTable($adapterTableName, $adapterNewTableName); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* {@inheritdoc} |
102
|
|
|
*/ |
103
|
|
View Code Duplication |
public function dropTable($tableName) |
|
|
|
|
104
|
|
|
{ |
105
|
|
|
$adapter = $this->getAdapter(); |
106
|
|
|
if (!$adapter instanceof DirectActionInterface) { |
107
|
|
|
throw new \BadMethodCallException('The underlying adapter does not implement DirectActionInterface'); |
108
|
|
|
} |
109
|
|
|
$adapterTableName = $this->getAdapterTableName($tableName); |
110
|
|
|
$adapter->dropTable($adapterTableName); |
111
|
1 |
|
} |
112
|
|
|
|
113
|
1 |
|
/** |
114
|
1 |
|
* {@inheritdoc} |
115
|
|
|
*/ |
116
|
|
|
public function truncateTable($tableName) |
117
|
|
|
{ |
118
|
|
|
$adapterTableName = $this->getAdapterTableName($tableName); |
119
|
|
|
parent::truncateTable($adapterTableName); |
120
|
1 |
|
} |
121
|
|
|
|
122
|
1 |
|
/** |
123
|
1 |
|
* {@inheritdoc} |
124
|
|
|
*/ |
125
|
|
|
public function getColumns($tableName) |
126
|
|
|
{ |
127
|
|
|
$adapterTableName = $this->getAdapterTableName($tableName); |
128
|
|
|
|
129
|
1 |
|
return parent::getColumns($adapterTableName); |
130
|
|
|
} |
131
|
1 |
|
|
132
|
1 |
|
/** |
133
|
1 |
|
* {@inheritdoc} |
134
|
1 |
|
*/ |
135
|
1 |
|
public function hasColumn($tableName, $columnName) |
136
|
|
|
{ |
137
|
|
|
$adapterTableName = $this->getAdapterTableName($tableName); |
138
|
|
|
|
139
|
|
|
return parent::hasColumn($adapterTableName, $columnName); |
140
|
1 |
|
} |
141
|
|
|
|
142
|
1 |
|
/** |
143
|
1 |
|
* {@inheritdoc} |
144
|
1 |
|
*/ |
145
|
|
View Code Duplication |
public function addColumn(Table $table, Column $column) |
|
|
|
|
146
|
|
|
{ |
147
|
|
|
$adapter = $this->getAdapter(); |
148
|
|
|
if (!$adapter instanceof DirectActionInterface) { |
149
|
1 |
|
throw new \BadMethodCallException('The underlying adapter does not implement DirectActionInterface'); |
150
|
|
|
} |
151
|
1 |
|
$adapterTableName = $this->getAdapterTableName($table->getName()); |
152
|
1 |
|
$adapterTable = new Table($adapterTableName, $table->getOptions()); |
153
|
|
|
$adapter->addColumn($adapterTable, $column); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* {@inheritdoc} |
158
|
1 |
|
*/ |
159
|
|
View Code Duplication |
public function renameColumn($tableName, $columnName, $newColumnName) |
|
|
|
|
160
|
1 |
|
{ |
161
|
1 |
|
$adapter = $this->getAdapter(); |
162
|
1 |
|
if (!$adapter instanceof DirectActionInterface) { |
163
|
|
|
throw new \BadMethodCallException('The underlying adapter does not implement DirectActionInterface'); |
164
|
|
|
} |
165
|
|
|
$adapterTableName = $this->getAdapterTableName($tableName); |
166
|
|
|
$adapter->renameColumn($adapterTableName, $columnName, $newColumnName); |
167
|
1 |
|
} |
168
|
|
|
|
169
|
1 |
|
/** |
170
|
1 |
|
* {@inheritdoc} |
171
|
|
|
*/ |
172
|
|
View Code Duplication |
public function changeColumn($tableName, $columnName, Column $newColumn) |
|
|
|
|
173
|
|
|
{ |
174
|
|
|
$adapter = $this->getAdapter(); |
175
|
|
|
if (!$adapter instanceof DirectActionInterface) { |
176
|
|
|
throw new \BadMethodCallException('The underlying adapter does not implement DirectActionInterface'); |
177
|
|
|
} |
178
|
|
|
$adapterTableName = $this->getAdapterTableName($tableName); |
179
|
|
|
$adapter->changeColumn($adapterTableName, $columnName, $newColumn); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* {@inheritdoc} |
184
|
|
|
*/ |
185
|
|
View Code Duplication |
public function dropColumn($tableName, $columnName) |
|
|
|
|
186
|
|
|
{ |
187
|
|
|
$adapter = $this->getAdapter(); |
188
|
|
|
if (!$adapter instanceof DirectActionInterface) { |
189
|
|
|
throw new \BadMethodCallException('The underlying adapter does not implement DirectActionInterface'); |
190
|
|
|
} |
191
|
|
|
$adapterTableName = $this->getAdapterTableName($tableName); |
192
|
|
|
$adapter->dropColumn($adapterTableName, $columnName); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
1 |
|
* {@inheritdoc} |
197
|
|
|
*/ |
198
|
1 |
|
public function hasIndex($tableName, $columns) |
199
|
1 |
|
{ |
200
|
1 |
|
$adapterTableName = $this->getAdapterTableName($tableName); |
201
|
|
|
|
202
|
|
|
return parent::hasIndex($adapterTableName, $columns); |
203
|
|
|
} |
204
|
|
|
|
205
|
1 |
|
/** |
206
|
|
|
* {@inheritdoc} |
207
|
1 |
|
*/ |
208
|
1 |
|
public function hasIndexByName($tableName, $indexName) |
209
|
1 |
|
{ |
210
|
|
|
$adapterTableName = $this->getAdapterTableName($tableName); |
211
|
|
|
|
212
|
|
|
return parent::hasIndexByName($adapterTableName, $indexName); |
213
|
|
|
} |
214
|
1 |
|
|
215
|
|
|
/** |
216
|
1 |
|
* {@inheritdoc} |
217
|
1 |
|
*/ |
218
|
|
|
public function addIndex(Table $table, Index $index) |
219
|
|
|
{ |
220
|
|
|
$adapter = $this->getAdapter(); |
221
|
|
|
if (!$adapter instanceof DirectActionInterface) { |
222
|
|
|
throw new \BadMethodCallException('The underlying adapter does not implement DirectActionInterface'); |
223
|
1 |
|
} |
224
|
|
|
$adapterTable = new Table($table->getName(), $table->getOptions()); |
225
|
1 |
|
$adapter->addIndex($adapterTable, $index); |
226
|
1 |
|
} |
227
|
1 |
|
|
228
|
1 |
|
/** |
229
|
1 |
|
* {@inheritdoc} |
230
|
|
|
*/ |
231
|
|
View Code Duplication |
public function dropIndex($tableName, $columns) |
|
|
|
|
232
|
|
|
{ |
233
|
|
|
$adapter = $this->getAdapter(); |
234
|
1 |
|
if (!$adapter instanceof DirectActionInterface) { |
235
|
|
|
throw new \BadMethodCallException('The underlying adapter does not implement DirectActionInterface'); |
236
|
1 |
|
} |
237
|
1 |
|
$adapterTableName = $this->getAdapterTableName($tableName); |
238
|
1 |
|
$adapter->dropIndex($adapterTableName, $columns); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* {@inheritdoc} |
243
|
|
|
*/ |
244
|
|
View Code Duplication |
public function dropIndexByName($tableName, $indexName) |
|
|
|
|
245
|
|
|
{ |
246
|
|
|
$adapter = $this->getAdapter(); |
247
|
|
|
if (!$adapter instanceof DirectActionInterface) { |
248
|
|
|
throw new \BadMethodCallException('The underlying adapter does not implement DirectActionInterface'); |
249
|
|
|
} |
250
|
|
|
$adapterTableName = $this->getAdapterTableName($tableName); |
251
|
|
|
$adapter->dropIndexByName($adapterTableName, $indexName); |
252
|
|
|
} |
253
|
|
|
|
254
|
1 |
|
/** |
255
|
|
|
* {@inheritdoc} |
256
|
1 |
|
*/ |
257
|
1 |
|
public function hasForeignKey($tableName, $columns, $constraint = null) |
258
|
1 |
|
{ |
259
|
1 |
|
$adapterTableName = $this->getAdapterTableName($tableName); |
260
|
1 |
|
|
261
|
|
|
return parent::hasForeignKey($adapterTableName, $columns, $constraint); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* {@inheritdoc} |
266
|
|
|
*/ |
267
|
19 |
View Code Duplication |
public function addForeignKey(Table $table, ForeignKey $foreignKey) |
|
|
|
|
268
|
|
|
{ |
269
|
19 |
|
$adapter = $this->getAdapter(); |
270
|
|
|
if (!$adapter instanceof DirectActionInterface) { |
271
|
|
|
throw new \BadMethodCallException('The underlying adapter does not implement DirectActionInterface'); |
272
|
|
|
} |
273
|
|
|
$adapterTableName = $this->getAdapterTableName($table->getName()); |
274
|
|
|
$adapterTable = new Table($adapterTableName, $table->getOptions()); |
275
|
|
|
$adapter->addForeignKey($adapterTable, $foreignKey); |
276
|
|
|
} |
277
|
19 |
|
|
278
|
|
|
/** |
279
|
19 |
|
* {@inheritdoc} |
280
|
|
|
*/ |
281
|
|
View Code Duplication |
public function dropForeignKey($tableName, $columns, $constraint = null) |
|
|
|
|
282
|
|
|
{ |
283
|
|
|
$adapter = $this->getAdapter(); |
284
|
|
|
if (!$adapter instanceof DirectActionInterface) { |
285
|
|
|
throw new \BadMethodCallException('The underlying adapter does not implement DirectActionInterface'); |
286
|
|
|
} |
287
|
|
|
$adapterTableName = $this->getAdapterTableName($tableName); |
288
|
19 |
|
$adapter->dropForeignKey($adapterTableName, $columns, $constraint); |
289
|
|
|
} |
290
|
19 |
|
|
291
|
|
|
/** |
292
|
|
|
* {@inheritdoc} |
293
|
|
|
*/ |
294
|
|
|
public function insert(Table $table, $row) |
295
|
|
|
{ |
296
|
|
|
$adapterTableName = $this->getAdapterTableName($table->getName()); |
297
|
|
|
$adapterTable = new Table($adapterTableName, $table->getOptions()); |
298
|
|
|
parent::insert($adapterTable, $row); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* {@inheritdoc} |
303
|
|
|
*/ |
304
|
|
|
public function bulkinsert(Table $table, $rows) |
305
|
|
|
{ |
306
|
|
|
$adapterTableName = $this->getAdapterTableName($table->getName()); |
307
|
|
|
$adapterTable = new Table($adapterTableName, $table->getOptions()); |
308
|
|
|
parent::bulkinsert($adapterTable, $rows); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* Gets the table prefix. |
313
|
|
|
* |
314
|
|
|
* @return string |
315
|
|
|
*/ |
316
|
|
|
public function getPrefix() |
317
|
|
|
{ |
318
|
|
|
return (string)$this->getOption('table_prefix'); |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* Gets the table suffix. |
323
|
|
|
* |
324
|
|
|
* @return string |
325
|
|
|
*/ |
326
|
|
|
public function getSuffix() |
327
|
|
|
{ |
328
|
|
|
return (string)$this->getOption('table_suffix'); |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* Applies the prefix and suffix to the table name. |
333
|
|
|
* |
334
|
|
|
* @param string $tableName |
335
|
|
|
* @return string |
336
|
|
|
*/ |
337
|
|
|
public function getAdapterTableName($tableName) |
338
|
|
|
{ |
339
|
|
|
return $this->getPrefix() . $tableName . $this->getSuffix(); |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* {@inheritdoc} |
344
|
|
|
*/ |
345
|
|
|
public function executeActions(Table $table, array $actions) |
346
|
|
|
{ |
347
|
|
|
$adapterTableName = $this->getAdapterTableName($table->getName()); |
348
|
|
|
$adapterTable = new Table($adapterTableName, $table->getOptions()); |
349
|
|
|
|
350
|
|
|
foreach ($actions as $k => $action) { |
351
|
|
|
switch (true) { |
352
|
|
|
case ($action instanceof AddColumn): |
353
|
|
|
$actions[$k] = new AddColumn($adapterTable, $action->getColumn()); |
354
|
|
|
break; |
355
|
|
|
|
356
|
|
|
case ($action instanceof AddIndex): |
357
|
|
|
$actions[$k] = new AddIndex($adapterTable, $action->getIndex()); |
358
|
|
|
break; |
359
|
|
|
|
360
|
|
|
case ($action instanceof AddForeignKey): |
361
|
|
|
$foreignKey = clone $action->getForeignKey(); |
362
|
|
|
$refTable = $foreignKey->getReferencedTable(); |
363
|
|
|
$refTableName = $this->getAdapterTableName($refTable->getName()); |
364
|
|
|
$foreignKey->setReferencedTable(new Table($refTableName, $refTable->getOptions())); |
365
|
|
|
$actions[$k] = new AddForeignKey($adapterTable, $foreignKey); |
366
|
|
|
break; |
367
|
|
|
|
368
|
|
|
case ($action instanceof ChangeColumn): |
369
|
|
|
$actions[$k] = new ChangeColumn($adapterTable, $action->getColumnName(), $action->getColumn()); |
370
|
|
|
break; |
371
|
|
|
|
372
|
|
|
case ($action instanceof DropForeignKey): |
373
|
|
|
$actions[$k] = new DropForeignKey($adapterTable, $action->getForeignKey()); |
374
|
|
|
break; |
375
|
|
|
|
376
|
|
|
case ($action instanceof DropIndex): |
377
|
|
|
$actions[$k] = new DropIndex($adapterTable, $action->getIndex()); |
378
|
|
|
break; |
379
|
|
|
|
380
|
|
|
case ($action instanceof DropTable): |
381
|
|
|
$actions[$k] = new DropTable($adapterTable); |
382
|
|
|
break; |
383
|
|
|
|
384
|
|
|
case ($action instanceof RemoveColumn): |
385
|
|
|
$actions[$k] = new RemoveColumn($adapterTable, $action->getColumn()); |
386
|
|
|
break; |
387
|
|
|
|
388
|
|
|
case ($action instanceof RenameColumn): |
389
|
|
|
$actions[$k] = new RenameColumn($adapterTable, $action->getColumn(), $action->getNewName()); |
390
|
|
|
break; |
391
|
|
|
|
392
|
|
|
case ($action instanceof RenameTable): |
393
|
|
|
$actions[$k] = new RenameTable($adapterTable, $action->getNewName()); |
394
|
|
|
break; |
395
|
|
|
|
396
|
|
|
default: |
397
|
|
|
throw new \InvalidArgumentException( |
398
|
|
|
sprintf("Forgot to implement table prefixing for action: '%s'", get_class($action)) |
399
|
|
|
); |
400
|
|
|
} |
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
parent::executeActions($adapterTable, $actions); |
404
|
|
|
} |
405
|
|
|
} |
406
|
|
|
|
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.