TablePrefixAdapter::createTable()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 7
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 3
crap 1
1
<?php
2
3
/**
4
 * MIT License
5
 * For full license information, please view the LICENSE file that was distributed with this source code.
6
 */
7
8
namespace Phinx\Db\Adapter;
9
10
use BadMethodCallException;
11
use InvalidArgumentException;
12
use Phinx\Db\Action\AddColumn;
13
use Phinx\Db\Action\AddForeignKey;
14
use Phinx\Db\Action\AddIndex;
15
use Phinx\Db\Action\ChangeColumn;
16
use Phinx\Db\Action\ChangeComment;
17
use Phinx\Db\Action\ChangePrimaryKey;
18
use Phinx\Db\Action\DropForeignKey;
19
use Phinx\Db\Action\DropIndex;
20
use Phinx\Db\Action\DropTable;
21
use Phinx\Db\Action\RemoveColumn;
22
use Phinx\Db\Action\RenameColumn;
23
use Phinx\Db\Action\RenameTable;
24
use Phinx\Db\Table\Column;
25
use Phinx\Db\Table\ForeignKey;
26
use Phinx\Db\Table\Index;
27
use Phinx\Db\Table\Table;
28
29
/**
30
 * Table prefix/suffix adapter.
31
 *
32
 * Used for inserting a prefix or suffix into table names.
33
 *
34
 * @author Samuel Fisher <[email protected]>
35
 */
36
class TablePrefixAdapter extends AdapterWrapper implements DirectActionInterface
37
{
38
    /**
39
     * @inheritDoc
40
     */
41
    public function getAdapterType()
42
    {
43
        return 'TablePrefixAdapter';
44
    }
45
46
    /**
47
     * @inheritDoc
48
     */
49
    public function hasTable($tableName)
50
    {
51
        $adapterTableName = $this->getAdapterTableName($tableName);
52
53
        return parent::hasTable($adapterTableName);
54
    }
55
56 2
    /**
57
     * @inheritDoc
58 2
     */
59 2
    public function createTable(Table $table, array $columns = [], array $indexes = [])
60
    {
61
        $adapterTable = new Table(
62
            $this->getAdapterTableName($table->getName()),
63
            $table->getOptions()
64
        );
65 3
        parent::createTable($adapterTable, $columns, $indexes);
66
    }
67 3
68 3
    /**
69 3
     * {@inheritDoc}
70
     *
71 3
     * @throws \BadMethodCallException
72 1
     * @return void
73 1
     */
74 1
    public function changePrimaryKey(Table $table, $newColumns)
75 3
    {
76
        $adapter = $this->getAdapter();
77 3
        if (!$adapter instanceof DirectActionInterface) {
78 3
            throw new BadMethodCallException('The underlying adapter does not implement DirectActionInterface');
79
        }
80
81
        $adapterTable = new Table(
82
            $this->getAdapterTableName($table->getName()),
83 1
            $table->getOptions()
84
        );
85 1
        $adapter->changePrimaryKey($adapterTable, $newColumns);
86 1
    }
87 1
88 1
    /**
89
     * {@inheritDoc}
90
     *
91
     * @throws \BadMethodCallException
92
     * @return void
93 1
     */
94
    public function changeComment(Table $table, $newComment)
95 1
    {
96 1
        $adapter = $this->getAdapter();
97 1
        if (!$adapter instanceof DirectActionInterface) {
98
            throw new BadMethodCallException('The underlying adapter does not implement DirectActionInterface');
99
        }
100
101
        $adapterTable = new Table(
102
            $this->getAdapterTableName($table->getName()),
103
            $table->getOptions()
104
        );
105
        $adapter->changeComment($adapterTable, $newComment);
106
    }
107
108
    /**
109
     * {@inheritDoc}
110
     *
111 1
     * @throws \BadMethodCallException
112
     * @return void
113 1
     */
114 1
    public function renameTable($tableName, $newTableName)
115
    {
116
        $adapter = $this->getAdapter();
117
        if (!$adapter instanceof DirectActionInterface) {
118
            throw new BadMethodCallException('The underlying adapter does not implement DirectActionInterface');
119
        }
120 1
121
        $adapterTableName = $this->getAdapterTableName($tableName);
122 1
        $adapterNewTableName = $this->getAdapterTableName($newTableName);
123 1
        $adapter->renameTable($adapterTableName, $adapterNewTableName);
124
    }
125
126
    /**
127
     * {@inheritDoc}
128
     *
129 1
     * @throws \BadMethodCallException
130
     * @return void
131 1
     */
132 1
    public function dropTable($tableName)
133 1
    {
134 1
        $adapter = $this->getAdapter();
135 1
        if (!$adapter instanceof DirectActionInterface) {
136
            throw new BadMethodCallException('The underlying adapter does not implement DirectActionInterface');
137
        }
138
        $adapterTableName = $this->getAdapterTableName($tableName);
139
        $adapter->dropTable($adapterTableName);
140 1
    }
141
142 1
    /**
143 1
     * @inheritDoc
144 1
     */
145
    public function truncateTable($tableName)
146
    {
147
        $adapterTableName = $this->getAdapterTableName($tableName);
148
        parent::truncateTable($adapterTableName);
149 1
    }
150
151 1
    /**
152 1
     * @inheritDoc
153
     */
154
    public function getColumns($tableName)
155
    {
156
        $adapterTableName = $this->getAdapterTableName($tableName);
157
158 1
        return parent::getColumns($adapterTableName);
159
    }
160 1
161 1
    /**
162 1
     * @inheritDoc
163
     */
164
    public function hasColumn($tableName, $columnName)
165
    {
166
        $adapterTableName = $this->getAdapterTableName($tableName);
167 1
168
        return parent::hasColumn($adapterTableName, $columnName);
169 1
    }
170 1
171
    /**
172
     * {@inheritDoc}
173
     *
174
     * @throws \BadMethodCallException
175
     * @return void
176
     */
177
    public function addColumn(Table $table, Column $column)
178
    {
179
        $adapter = $this->getAdapter();
180
        if (!$adapter instanceof DirectActionInterface) {
181
            throw new BadMethodCallException('The underlying adapter does not implement DirectActionInterface');
182
        }
183
        $adapterTableName = $this->getAdapterTableName($table->getName());
184
        $adapterTable = new Table($adapterTableName, $table->getOptions());
185
        $adapter->addColumn($adapterTable, $column);
186
    }
187
188
    /**
189
     * {@inheritDoc}
190
     *
191
     * @throws \BadMethodCallException
192
     * @return void
193
     */
194
    public function renameColumn($tableName, $columnName, $newColumnName)
195
    {
196 1
        $adapter = $this->getAdapter();
197
        if (!$adapter instanceof DirectActionInterface) {
198 1
            throw new BadMethodCallException('The underlying adapter does not implement DirectActionInterface');
199 1
        }
200 1
        $adapterTableName = $this->getAdapterTableName($tableName);
201
        $adapter->renameColumn($adapterTableName, $columnName, $newColumnName);
202
    }
203
204
    /**
205 1
     * {@inheritDoc}
206
     *
207 1
     * @throws \BadMethodCallException
208 1
     * @return void
209 1
     */
210
    public function changeColumn($tableName, $columnName, Column $newColumn)
211
    {
212
        $adapter = $this->getAdapter();
213
        if (!$adapter instanceof DirectActionInterface) {
214 1
            throw new BadMethodCallException('The underlying adapter does not implement DirectActionInterface');
215
        }
216 1
        $adapterTableName = $this->getAdapterTableName($tableName);
217 1
        $adapter->changeColumn($adapterTableName, $columnName, $newColumn);
218
    }
219
220
    /**
221
     * {@inheritDoc}
222
     *
223 1
     * @throws \BadMethodCallException
224
     * @return void
225 1
     */
226 1
    public function dropColumn($tableName, $columnName)
227 1
    {
228 1
        $adapter = $this->getAdapter();
229 1
        if (!$adapter instanceof DirectActionInterface) {
230
            throw new BadMethodCallException('The underlying adapter does not implement DirectActionInterface');
231
        }
232
        $adapterTableName = $this->getAdapterTableName($tableName);
233
        $adapter->dropColumn($adapterTableName, $columnName);
234 1
    }
235
236 1
    /**
237 1
     * @inheritDoc
238 1
     */
239
    public function hasIndex($tableName, $columns)
240
    {
241
        $adapterTableName = $this->getAdapterTableName($tableName);
242
243
        return parent::hasIndex($adapterTableName, $columns);
244
    }
245
246
    /**
247
     * @inheritDoc
248
     */
249
    public function hasIndexByName($tableName, $indexName)
250
    {
251
        $adapterTableName = $this->getAdapterTableName($tableName);
252
253
        return parent::hasIndexByName($adapterTableName, $indexName);
254 1
    }
255
256 1
    /**
257 1
     * {@inheritDoc}
258 1
     *
259 1
     * @throws \BadMethodCallException
260 1
     * @return void
261
     */
262
    public function addIndex(Table $table, Index $index)
263
    {
264
        $adapter = $this->getAdapter();
265
        if (!$adapter instanceof DirectActionInterface) {
266
            throw new BadMethodCallException('The underlying adapter does not implement DirectActionInterface');
267 19
        }
268
        $adapterTable = new Table($table->getName(), $table->getOptions());
269 19
        $adapter->addIndex($adapterTable, $index);
270
    }
271
272
    /**
273
     * {@inheritDoc}
274
     *
275
     * @throws \BadMethodCallException
276
     * @return void
277 19
     */
278
    public function dropIndex($tableName, $columns)
279 19
    {
280
        $adapter = $this->getAdapter();
281
        if (!$adapter instanceof DirectActionInterface) {
282
            throw new BadMethodCallException('The underlying adapter does not implement DirectActionInterface');
283
        }
284
        $adapterTableName = $this->getAdapterTableName($tableName);
285
        $adapter->dropIndex($adapterTableName, $columns);
286
    }
287
288 19
    /**
289
     * {@inheritDoc}
290 19
     *
291
     * @throws \BadMethodCallException
292
     * @return void
293
     */
294
    public function dropIndexByName($tableName, $indexName)
295
    {
296
        $adapter = $this->getAdapter();
297
        if (!$adapter instanceof DirectActionInterface) {
298
            throw new BadMethodCallException('The underlying adapter does not implement DirectActionInterface');
299
        }
300
        $adapterTableName = $this->getAdapterTableName($tableName);
301
        $adapter->dropIndexByName($adapterTableName, $indexName);
302
    }
303
304
    /**
305
     * @inheritDoc
306
     */
307
    public function hasPrimaryKey($tableName, $columns, $constraint = null)
308
    {
309
        $adapterTableName = $this->getAdapterTableName($tableName);
310
311
        return parent::hasPrimaryKey($adapterTableName, $columns, $constraint);
312
    }
313
314
    /**
315
     * @inheritDoc
316
     */
317
    public function hasForeignKey($tableName, $columns, $constraint = null)
318
    {
319
        $adapterTableName = $this->getAdapterTableName($tableName);
320
321
        return parent::hasForeignKey($adapterTableName, $columns, $constraint);
322
    }
323
324
    /**
325
     * {@inheritDoc}
326
     *
327
     * @throws \BadMethodCallException
328
     * @return void
329
     */
330
    public function addForeignKey(Table $table, ForeignKey $foreignKey)
331
    {
332
        $adapter = $this->getAdapter();
333
        if (!$adapter instanceof DirectActionInterface) {
334
            throw new BadMethodCallException('The underlying adapter does not implement DirectActionInterface');
335
        }
336
        $adapterTableName = $this->getAdapterTableName($table->getName());
337
        $adapterTable = new Table($adapterTableName, $table->getOptions());
338
        $adapter->addForeignKey($adapterTable, $foreignKey);
339
    }
340
341
    /**
342
     * {@inheritDoc}
343
     *
344
     * @throws \BadMethodCallException
345
     * @return void
346
     */
347
    public function dropForeignKey($tableName, $columns, $constraint = null)
348
    {
349
        $adapter = $this->getAdapter();
350
        if (!$adapter instanceof DirectActionInterface) {
351
            throw new BadMethodCallException('The underlying adapter does not implement DirectActionInterface');
352
        }
353
        $adapterTableName = $this->getAdapterTableName($tableName);
354
        $adapter->dropForeignKey($adapterTableName, $columns, $constraint);
355
    }
356
357
    /**
358
     * @inheritDoc
359
     */
360
    public function insert(Table $table, $row)
361
    {
362
        $adapterTableName = $this->getAdapterTableName($table->getName());
363
        $adapterTable = new Table($adapterTableName, $table->getOptions());
364
        parent::insert($adapterTable, $row);
365
    }
366
367
    /**
368
     * @inheritDoc
369
     */
370
    public function bulkinsert(Table $table, $rows)
371
    {
372
        $adapterTableName = $this->getAdapterTableName($table->getName());
373
        $adapterTable = new Table($adapterTableName, $table->getOptions());
374
        parent::bulkinsert($adapterTable, $rows);
375
    }
376
377
    /**
378
     * Gets the table prefix.
379
     *
380
     * @return string
381
     */
382
    public function getPrefix()
383
    {
384
        return (string)$this->getOption('table_prefix');
385
    }
386
387
    /**
388
     * Gets the table suffix.
389
     *
390
     * @return string
391
     */
392
    public function getSuffix()
393
    {
394
        return (string)$this->getOption('table_suffix');
395
    }
396
397
    /**
398
     * Applies the prefix and suffix to the table name.
399
     *
400
     * @param string $tableName Table name
401
     * @return string
402
     */
403
    public function getAdapterTableName($tableName)
404
    {
405
        return $this->getPrefix() . $tableName . $this->getSuffix();
406
    }
407
408
    /**
409
     * {@inheritDoc}
410
     *
411
     * @throws \InvalidArgumentException
412
     * @return void
413
     */
414
    public function executeActions(Table $table, array $actions)
415
    {
416
        $adapterTableName = $this->getAdapterTableName($table->getName());
417
        $adapterTable = new Table($adapterTableName, $table->getOptions());
418
419
        foreach ($actions as $k => $action) {
420
            switch (true) {
421
                case $action instanceof AddColumn:
422
                    $actions[$k] = new AddColumn($adapterTable, $action->getColumn());
423
                    break;
424
425
                case $action instanceof AddIndex:
426
                    $actions[$k] = new AddIndex($adapterTable, $action->getIndex());
427
                    break;
428
429
                case $action instanceof AddForeignKey:
430
                    $foreignKey = clone $action->getForeignKey();
431
                    $refTable = $foreignKey->getReferencedTable();
432
                    $refTableName = $this->getAdapterTableName($refTable->getName());
433
                    $foreignKey->setReferencedTable(new Table($refTableName, $refTable->getOptions()));
434
                    $actions[$k] = new AddForeignKey($adapterTable, $foreignKey);
435
                    break;
436
437
                case $action instanceof ChangeColumn:
438
                    $actions[$k] = new ChangeColumn($adapterTable, $action->getColumnName(), $action->getColumn());
439
                    break;
440
441
                case $action instanceof DropForeignKey:
442
                    $actions[$k] = new DropForeignKey($adapterTable, $action->getForeignKey());
443
                    break;
444
445
                case $action instanceof DropIndex:
446
                    $actions[$k] = new DropIndex($adapterTable, $action->getIndex());
447
                    break;
448
449
                case $action instanceof DropTable:
450
                    $actions[$k] = new DropTable($adapterTable);
451
                    break;
452
453
                case $action instanceof RemoveColumn:
454
                    $actions[$k] = new RemoveColumn($adapterTable, $action->getColumn());
455
                    break;
456
457
                case $action instanceof RenameColumn:
458
                    $actions[$k] = new RenameColumn($adapterTable, $action->getColumn(), $action->getNewName());
459
                    break;
460
461
                case $action instanceof RenameTable:
462
                    $actions[$k] = new RenameTable($adapterTable, $this->getAdapterTableName($action->getNewName()));
463
                    break;
464
465
                case $action instanceof ChangePrimaryKey:
466
                    $actions[$k] = new ChangePrimaryKey($adapterTable, $action->getNewColumns());
467
                    break;
468
469
                case $action instanceof ChangeComment:
470
                    $actions[$k] = new ChangeComment($adapterTable, $action->getNewComment());
471
                    break;
472
473
                default:
474
                    throw new InvalidArgumentException(
475
                        sprintf("Forgot to implement table prefixing for action: '%s'", get_class($action))
476
                    );
477
            }
478
        }
479
480
        parent::executeActions($adapterTable, $actions);
481
    }
482
}
483