1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Spiral Framework. |
4
|
|
|
* |
5
|
|
|
* @license MIT |
6
|
|
|
* @author Anton Titov (Wolfy-J) |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Spiral\Database\Drivers\MySQL\Schemas; |
10
|
|
|
|
11
|
|
|
use Spiral\Database\Entities\Schemas\AbstractColumn; |
12
|
|
|
use Spiral\Database\Entities\Schemas\AbstractCommander; |
13
|
|
|
use Spiral\Database\Entities\Schemas\AbstractIndex; |
14
|
|
|
use Spiral\Database\Entities\Schemas\AbstractReference; |
15
|
|
|
use Spiral\Database\Entities\Schemas\AbstractTable; |
16
|
|
|
use Spiral\Database\Exceptions\SchemaException; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* MySQL commander. |
20
|
|
|
*/ |
21
|
|
|
class Commander extends AbstractCommander |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* {@inheritdoc} |
25
|
|
|
*/ |
26
|
|
View Code Duplication |
public function alterColumn( |
|
|
|
|
27
|
|
|
AbstractTable $table, |
28
|
|
|
AbstractColumn $initial, |
29
|
|
|
AbstractColumn $column |
30
|
|
|
) { |
31
|
|
|
$query = "ALTER TABLE {table} CHANGE {column} {statement}"; |
32
|
|
|
|
33
|
|
|
$query = \Spiral\interpolate($query, [ |
34
|
|
|
'table' => $table->getName(true), |
35
|
|
|
'column' => $initial->getName(true), |
36
|
|
|
'statement' => $column->sqlStatement() |
37
|
|
|
]); |
38
|
|
|
|
39
|
|
|
$this->run($query); |
40
|
|
|
|
41
|
|
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
|
|
public function dropIndex(AbstractTable $table, AbstractIndex $index) |
48
|
|
|
{ |
49
|
|
|
$this->run("DROP INDEX {$index->getName(true)} ON {$table->getName(true)}"); |
50
|
|
|
|
51
|
|
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
|
View Code Duplication |
public function alterIndex(AbstractTable $table, AbstractIndex $initial, AbstractIndex $index) |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
$query = \Spiral\interpolate("ALTER TABLE {table} DROP INDEX {index}, ADD {statement}", [ |
60
|
|
|
'table' => $table->getName(true), |
61
|
|
|
'index' => $initial->getName(true), |
62
|
|
|
'statement' => $index->sqlStatement(false) |
63
|
|
|
]); |
64
|
|
|
|
65
|
|
|
$this->run($query); |
66
|
|
|
|
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
|
|
public function dropForeign(AbstractTable $table, AbstractReference $foreign) |
74
|
|
|
{ |
75
|
|
|
$this->run("ALTER TABLE {$table->getName(true)} DROP FOREIGN KEY {$foreign->getName(true)}"); |
76
|
|
|
|
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get statement needed to create table. |
82
|
|
|
* |
83
|
|
|
* @param AbstractTable $table |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
|
protected function createStatement(AbstractTable $table) |
87
|
|
|
{ |
88
|
|
|
if (!$table instanceof TableSchema) { |
89
|
|
|
throw new SchemaException("MySQL commander can process only MySQL tables."); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return parent::createStatement($table) . " ENGINE {$table->getEngine()}"; |
93
|
|
|
} |
94
|
|
|
} |
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.