Completed
Push — master ( e33650...15989c )
by José
02:42
created

TablePrefixAdapter::dropTable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 4
cts 4
cp 1
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
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\ChangeComment;
36
use Phinx\Db\Action\ChangePrimaryKey;
37
use Phinx\Db\Action\DropForeignKey;
38
use Phinx\Db\Action\DropIndex;
39
use Phinx\Db\Action\DropTable;
40
use Phinx\Db\Action\RemoveColumn;
41
use Phinx\Db\Action\RenameColumn;
42
use Phinx\Db\Action\RenameTable;
43
use Phinx\Db\Table\Column;
44
use Phinx\Db\Table\ForeignKey;
45
use Phinx\Db\Table\Index;
46
use Phinx\Db\Table\Table;
47
48
/**
49
 * Table prefix/suffix adapter.
50
 *
51
 * Used for inserting a prefix or suffix into table names.
52
 *
53
 * @author Samuel Fisher <[email protected]>
54
 */
55
class TablePrefixAdapter extends AdapterWrapper implements DirectActionInterface
56 2
{
57
    /**
58 2
     * {@inheritdoc}
59 2
     */
60
    public function getAdapterType()
61
    {
62
        return 'TablePrefixAdapter';
63
    }
64
65 3
    /**
66
     * {@inheritdoc}
67 3
     */
68 3
    public function hasTable($tableName)
69 3
    {
70
        $adapterTableName = $this->getAdapterTableName($tableName);
71 3
72 1
        return parent::hasTable($adapterTableName);
73 1
    }
74 1
75 3
    /**
76
     * {@inheritdoc}
77 3
     */
78 3
    public function createTable(Table $table, array $columns = [], array $indexes = [])
79
    {
80
        $adapterTable = new Table(
81
            $this->getAdapterTableName($table->getName()),
82
            $table->getOptions()
83 1
        );
84
        parent::createTable($adapterTable, $columns, $indexes);
85 1
    }
86 1
87 1
    /**
88 1
     * {@inheritdoc}
89
     */
90
    public function changePrimaryKey(Table $table, $newColumns)
91
    {
92
        $adapter = $this->getAdapter();
93 1
        if (!$adapter instanceof DirectActionInterface) {
94
            throw new \BadMethodCallException('The underlying adapter does not implement DirectActionInterface');
95 1
        }
96 1
97 1
        $adapterTable = new Table(
98
            $this->getAdapterTableName($table->getName()),
99
            $table->getOptions()
100
        );
101
        $adapter->changePrimaryKey($adapterTable, $newColumns);
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107
    public function changeComment(Table $table, $newComment)
108
    {
109
        $adapter = $this->getAdapter();
110
        if (!$adapter instanceof DirectActionInterface) {
111 1
            throw new \BadMethodCallException('The underlying adapter does not implement DirectActionInterface');
112
        }
113 1
114 1
        $adapterTable = new Table(
115
            $this->getAdapterTableName($table->getName()),
116
            $table->getOptions()
117
        );
118
        $adapter->changeComment($adapterTable, $newComment);
119
    }
120 1
121
    /**
122 1
     * {@inheritdoc}
123 1
     */
124
    public function renameTable($tableName, $newTableName)
125
    {
126
        $adapter = $this->getAdapter();
127
        if (!$adapter instanceof DirectActionInterface) {
128
            throw new \BadMethodCallException('The underlying adapter does not implement DirectActionInterface');
129 1
        }
130
131 1
        $adapterTableName = $this->getAdapterTableName($tableName);
132 1
        $adapterNewTableName = $this->getAdapterTableName($newTableName);
133 1
        $adapter->renameTable($adapterTableName, $adapterNewTableName);
134 1
    }
135 1
136
    /**
137
     * {@inheritdoc}
138
     */
139
    public function dropTable($tableName)
140 1
    {
141
        $adapter = $this->getAdapter();
142 1
        if (!$adapter instanceof DirectActionInterface) {
143 1
            throw new \BadMethodCallException('The underlying adapter does not implement DirectActionInterface');
144 1
        }
145
        $adapterTableName = $this->getAdapterTableName($tableName);
146
        $adapter->dropTable($adapterTableName);
147
    }
148
149 1
    /**
150
     * {@inheritdoc}
151 1
     */
152 1
    public function truncateTable($tableName)
153
    {
154
        $adapterTableName = $this->getAdapterTableName($tableName);
155
        parent::truncateTable($adapterTableName);
156
    }
157
158 1
    /**
159
     * {@inheritdoc}
160 1
     */
161 1
    public function getColumns($tableName)
162 1
    {
163
        $adapterTableName = $this->getAdapterTableName($tableName);
164
165
        return parent::getColumns($adapterTableName);
166
    }
167 1
168
    /**
169 1
     * {@inheritdoc}
170 1
     */
171
    public function hasColumn($tableName, $columnName)
172
    {
173
        $adapterTableName = $this->getAdapterTableName($tableName);
174
175
        return parent::hasColumn($adapterTableName, $columnName);
176
    }
177
178
    /**
179
     * {@inheritdoc}
180
     */
181 View Code Duplication
    public function addColumn(Table $table, Column $column)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
182
    {
183
        $adapter = $this->getAdapter();
184
        if (!$adapter instanceof DirectActionInterface) {
185
            throw new \BadMethodCallException('The underlying adapter does not implement DirectActionInterface');
186
        }
187
        $adapterTableName = $this->getAdapterTableName($table->getName());
188
        $adapterTable = new Table($adapterTableName, $table->getOptions());
189
        $adapter->addColumn($adapterTable, $column);
190
    }
191
192
    /**
193
     * {@inheritdoc}
194
     */
195
    public function renameColumn($tableName, $columnName, $newColumnName)
196 1
    {
197
        $adapter = $this->getAdapter();
198 1
        if (!$adapter instanceof DirectActionInterface) {
199 1
            throw new \BadMethodCallException('The underlying adapter does not implement DirectActionInterface');
200 1
        }
201
        $adapterTableName = $this->getAdapterTableName($tableName);
202
        $adapter->renameColumn($adapterTableName, $columnName, $newColumnName);
203
    }
204
205 1
    /**
206
     * {@inheritdoc}
207 1
     */
208 1
    public function changeColumn($tableName, $columnName, Column $newColumn)
209 1
    {
210
        $adapter = $this->getAdapter();
211
        if (!$adapter instanceof DirectActionInterface) {
212
            throw new \BadMethodCallException('The underlying adapter does not implement DirectActionInterface');
213
        }
214 1
        $adapterTableName = $this->getAdapterTableName($tableName);
215
        $adapter->changeColumn($adapterTableName, $columnName, $newColumn);
216 1
    }
217 1
218
    /**
219
     * {@inheritdoc}
220
     */
221
    public function dropColumn($tableName, $columnName)
222
    {
223 1
        $adapter = $this->getAdapter();
224
        if (!$adapter instanceof DirectActionInterface) {
225 1
            throw new \BadMethodCallException('The underlying adapter does not implement DirectActionInterface');
226 1
        }
227 1
        $adapterTableName = $this->getAdapterTableName($tableName);
228 1
        $adapter->dropColumn($adapterTableName, $columnName);
229 1
    }
230
231
    /**
232
     * {@inheritdoc}
233
     */
234 1
    public function hasIndex($tableName, $columns)
235
    {
236 1
        $adapterTableName = $this->getAdapterTableName($tableName);
237 1
238 1
        return parent::hasIndex($adapterTableName, $columns);
239
    }
240
241
    /**
242
     * {@inheritdoc}
243
     */
244
    public function hasIndexByName($tableName, $indexName)
245
    {
246
        $adapterTableName = $this->getAdapterTableName($tableName);
247
248
        return parent::hasIndexByName($adapterTableName, $indexName);
249
    }
250
251
    /**
252
     * {@inheritdoc}
253
     */
254 1
    public function addIndex(Table $table, Index $index)
255
    {
256 1
        $adapter = $this->getAdapter();
257 1
        if (!$adapter instanceof DirectActionInterface) {
258 1
            throw new \BadMethodCallException('The underlying adapter does not implement DirectActionInterface');
259 1
        }
260 1
        $adapterTable = new Table($table->getName(), $table->getOptions());
261
        $adapter->addIndex($adapterTable, $index);
262
    }
263
264
    /**
265
     * {@inheritdoc}
266
     */
267 19
    public function dropIndex($tableName, $columns)
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($tableName);
274
        $adapter->dropIndex($adapterTableName, $columns);
275
    }
276
277 19
    /**
278
     * {@inheritdoc}
279 19
     */
280
    public function dropIndexByName($tableName, $indexName)
281
    {
282
        $adapter = $this->getAdapter();
283
        if (!$adapter instanceof DirectActionInterface) {
284
            throw new \BadMethodCallException('The underlying adapter does not implement DirectActionInterface');
285
        }
286
        $adapterTableName = $this->getAdapterTableName($tableName);
287
        $adapter->dropIndexByName($adapterTableName, $indexName);
288 19
    }
289
290 19
    /**
291
     * {@inheritdoc}
292
     */
293
    public function hasPrimaryKey($tableName, $columns, $constraint = null)
294
    {
295
        $adapterTableName = $this->getAdapterTableName($tableName);
296
297
        return parent::hasPrimaryKey($adapterTableName, $columns, $constraint);
298
    }
299
300
    /**
301
     * {@inheritdoc}
302
     */
303
    public function hasForeignKey($tableName, $columns, $constraint = null)
304
    {
305
        $adapterTableName = $this->getAdapterTableName($tableName);
306
307
        return parent::hasForeignKey($adapterTableName, $columns, $constraint);
308
    }
309
310
    /**
311
     * {@inheritdoc}
312
     */
313 View Code Duplication
    public function addForeignKey(Table $table, ForeignKey $foreignKey)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
314
    {
315
        $adapter = $this->getAdapter();
316
        if (!$adapter instanceof DirectActionInterface) {
317
            throw new \BadMethodCallException('The underlying adapter does not implement DirectActionInterface');
318
        }
319
        $adapterTableName = $this->getAdapterTableName($table->getName());
320
        $adapterTable = new Table($adapterTableName, $table->getOptions());
321
        $adapter->addForeignKey($adapterTable, $foreignKey);
322
    }
323
324
    /**
325
     * {@inheritdoc}
326
     */
327
    public function dropForeignKey($tableName, $columns, $constraint = null)
328
    {
329
        $adapter = $this->getAdapter();
330
        if (!$adapter instanceof DirectActionInterface) {
331
            throw new \BadMethodCallException('The underlying adapter does not implement DirectActionInterface');
332
        }
333
        $adapterTableName = $this->getAdapterTableName($tableName);
334
        $adapter->dropForeignKey($adapterTableName, $columns, $constraint);
335
    }
336
337
    /**
338
     * {@inheritdoc}
339
     */
340
    public function insert(Table $table, $row)
341
    {
342
        $adapterTableName = $this->getAdapterTableName($table->getName());
343
        $adapterTable = new Table($adapterTableName, $table->getOptions());
344
        parent::insert($adapterTable, $row);
345
    }
346
347
    /**
348
     * {@inheritdoc}
349
     */
350
    public function bulkinsert(Table $table, $rows)
351
    {
352
        $adapterTableName = $this->getAdapterTableName($table->getName());
353
        $adapterTable = new Table($adapterTableName, $table->getOptions());
354
        parent::bulkinsert($adapterTable, $rows);
355
    }
356
357
    /**
358
     * Gets the table prefix.
359
     *
360
     * @return string
361
     */
362
    public function getPrefix()
363
    {
364
        return (string)$this->getOption('table_prefix');
365
    }
366
367
    /**
368
     * Gets the table suffix.
369
     *
370
     * @return string
371
     */
372
    public function getSuffix()
373
    {
374
        return (string)$this->getOption('table_suffix');
375
    }
376
377
    /**
378
     * Applies the prefix and suffix to the table name.
379
     *
380
     * @param string $tableName
381
     * @return string
382
     */
383
    public function getAdapterTableName($tableName)
384
    {
385
        return $this->getPrefix() . $tableName . $this->getSuffix();
386
    }
387
388
    /**
389
     * {@inheritdoc}
390
     */
391
    public function executeActions(Table $table, array $actions)
392
    {
393
        $adapterTableName = $this->getAdapterTableName($table->getName());
394
        $adapterTable = new Table($adapterTableName, $table->getOptions());
395
396
        foreach ($actions as $k => $action) {
397
            switch (true) {
398
                case ($action instanceof AddColumn):
399
                    $actions[$k] = new AddColumn($adapterTable, $action->getColumn());
400
                    break;
401
402
                case ($action instanceof AddIndex):
403
                    $actions[$k] = new AddIndex($adapterTable, $action->getIndex());
404
                    break;
405
406
                case ($action instanceof AddForeignKey):
407
                    $foreignKey = clone $action->getForeignKey();
408
                    $refTable = $foreignKey->getReferencedTable();
409
                    $refTableName = $this->getAdapterTableName($refTable->getName());
410
                    $foreignKey->setReferencedTable(new Table($refTableName, $refTable->getOptions()));
411
                    $actions[$k] = new AddForeignKey($adapterTable, $foreignKey);
412
                    break;
413
414
                case ($action instanceof ChangeColumn):
415
                    $actions[$k] = new ChangeColumn($adapterTable, $action->getColumnName(), $action->getColumn());
416
                    break;
417
418
                case ($action instanceof DropForeignKey):
419
                    $actions[$k] = new DropForeignKey($adapterTable, $action->getForeignKey());
420
                    break;
421
422
                case ($action instanceof DropIndex):
423
                    $actions[$k] = new DropIndex($adapterTable, $action->getIndex());
424
                    break;
425
426
                case ($action instanceof DropTable):
427
                    $actions[$k] = new DropTable($adapterTable);
428
                    break;
429
430
                case ($action instanceof RemoveColumn):
431
                    $actions[$k] = new RemoveColumn($adapterTable, $action->getColumn());
432
                    break;
433
434
                case ($action instanceof RenameColumn):
435
                    $actions[$k] = new RenameColumn($adapterTable, $action->getColumn(), $action->getNewName());
436
                    break;
437
438
                case ($action instanceof RenameTable):
439
                    $actions[$k] = new RenameTable($adapterTable, $action->getNewName());
440
                    break;
441
442
                case ($action instanceof ChangePrimaryKey):
443
                    $actions[$k] = new ChangePrimaryKey($adapterTable, $action->getNewColumns());
444
                    break;
445
446
                case ($action instanceof ChangeComment):
447
                    $actions[$k] = new ChangeComment($adapterTable, $action->getNewComment());
448
                    break;
449
450
                default:
451
                    throw new \InvalidArgumentException(
452
                        sprintf("Forgot to implement table prefixing for action: '%s'", get_class($action))
453
                    );
454
            }
455
        }
456
457
        parent::executeActions($adapterTable, $actions);
458
    }
459
}
460