|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* components |
|
4
|
|
|
* |
|
5
|
|
|
* @author Wolfy-J |
|
6
|
|
|
*/ |
|
7
|
|
|
namespace Spiral\Database\Drivers\MySQL\Schemas; |
|
8
|
|
|
|
|
9
|
|
|
use Spiral\Database\Exceptions\SchemaException; |
|
10
|
|
|
use Spiral\Database\Schemas\Prototypes\AbstractColumn; |
|
11
|
|
|
use Spiral\Database\Schemas\Prototypes\AbstractIndex; |
|
12
|
|
|
use Spiral\Database\Schemas\Prototypes\AbstractReference; |
|
13
|
|
|
use Spiral\Database\Schemas\Prototypes\AbstractTable; |
|
14
|
|
|
use Spiral\Database\Schemas\TableState; |
|
15
|
|
|
|
|
16
|
|
|
class MySQLTable extends AbstractTable |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* List of most common MySQL table engines. |
|
20
|
|
|
*/ |
|
21
|
|
|
const ENGINE_INNODB = 'InnoDB'; |
|
22
|
|
|
const ENGINE_MYISAM = 'MyISAM'; |
|
23
|
|
|
const ENGINE_MEMORY = 'Memory'; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* MySQL table engine. |
|
27
|
|
|
* |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
private $engine = self::ENGINE_INNODB; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Populate table schema with values from database. |
|
34
|
|
|
* |
|
35
|
|
|
* @param TableState $state |
|
36
|
|
|
*/ |
|
37
|
|
|
protected function initSchema(TableState $state) |
|
38
|
|
|
{ |
|
39
|
|
|
parent::initSchema($state); |
|
40
|
|
|
|
|
41
|
|
|
//Reading table schema |
|
42
|
|
|
$this->engine = $this->driver->query('SHOW TABLE STATUS WHERE `Name` = ?', [ |
|
43
|
|
|
$state->getName() |
|
44
|
|
|
])->fetch()['Engine']; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Change table engine. Such operation will be applied only at moment of table creation. |
|
49
|
|
|
* |
|
50
|
|
|
* @param string $engine |
|
51
|
|
|
* |
|
52
|
|
|
* @return $this |
|
53
|
|
|
* |
|
54
|
|
|
* @throws SchemaException |
|
55
|
|
|
*/ |
|
56
|
|
|
public function setEngine($engine) |
|
57
|
|
|
{ |
|
58
|
|
|
if ($this->exists()) { |
|
59
|
|
|
throw new SchemaException('Table engine can be set only at moment of creation'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$this->engine = $engine; |
|
63
|
|
|
|
|
64
|
|
|
return $this; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @return string |
|
69
|
|
|
*/ |
|
70
|
|
|
public function getEngine() |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->engine; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* {@inheritdoc} |
|
77
|
|
|
*/ |
|
78
|
|
View Code Duplication |
protected function fetchColumns(): array |
|
|
|
|
|
|
79
|
|
|
{ |
|
80
|
|
|
$query = "SHOW FULL COLUMNS FROM {$this->driver->identifier($this->getName())}"; |
|
81
|
|
|
|
|
82
|
|
|
$result = []; |
|
83
|
|
|
foreach ($this->driver->query($query) as $schema) { |
|
84
|
|
|
$result[] = MySQLColumn::createInstance($this->getName(), $schema); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return $result; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* {@inheritdoc} |
|
92
|
|
|
*/ |
|
93
|
|
|
protected function fetchIndexes(): array |
|
94
|
|
|
{ |
|
95
|
|
|
$query = "SHOW INDEXES FROM {$this->driver->identifier($this->getName())}"; |
|
96
|
|
|
|
|
97
|
|
|
//Gluing all index definitions together |
|
98
|
|
|
$schemas = []; |
|
99
|
|
|
foreach ($this->driver->query($query) as $index) { |
|
100
|
|
|
if ($index['Key_name'] == 'PRIMARY') { |
|
101
|
|
|
//Skipping PRIMARY index |
|
102
|
|
|
continue; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
$schemas[$index['Key_name']][] = $index; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
$result = []; |
|
109
|
|
|
foreach ($schemas as $name => $index) { |
|
110
|
|
|
$result[] = MySQLIndex::createInstance($this->getName(), $name, $index); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return $result; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* {@inheritdoc} |
|
118
|
|
|
*/ |
|
119
|
|
|
protected function fetchReferences(): array |
|
120
|
|
|
{ |
|
121
|
|
|
$references = $this->driver->query( |
|
122
|
|
|
'SELECT * FROM `information_schema`.`referential_constraints` WHERE `constraint_schema` = ? AND `table_name` = ?', |
|
123
|
|
|
[$this->driver->getSource(), $this->getName()] |
|
124
|
|
|
); |
|
125
|
|
|
|
|
126
|
|
|
$result = []; |
|
127
|
|
View Code Duplication |
foreach ($references as $schema) { |
|
|
|
|
|
|
128
|
|
|
$column = $this->driver->query( |
|
129
|
|
|
'SELECT * FROM `information_schema`.`key_column_usage` WHERE `constraint_name` = ? AND `table_schema` = ? AND `table_name` = ?', |
|
130
|
|
|
[$schema['CONSTRAINT_NAME'], $this->driver->getSource(), $this->getName()] |
|
131
|
|
|
)->fetch(); |
|
132
|
|
|
|
|
133
|
|
|
$result[] = MySQLReference::createInstance( |
|
134
|
|
|
$this->getName(), |
|
135
|
|
|
$this->getPrefix(), |
|
136
|
|
|
$schema + $column |
|
137
|
|
|
); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
return $result; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Fetching primary keys from table. |
|
145
|
|
|
* |
|
146
|
|
|
* @return array |
|
147
|
|
|
*/ |
|
148
|
|
|
protected function fetchPrimaryKeys(): array |
|
149
|
|
|
{ |
|
150
|
|
|
$query = "SHOW INDEXES FROM {$this->driver->identifier($this->getName())}"; |
|
151
|
|
|
|
|
152
|
|
|
$primaryKeys = []; |
|
153
|
|
|
foreach ($this->driver->query($query) as $index) { |
|
154
|
|
|
if ($index['Key_name'] == 'PRIMARY') { |
|
155
|
|
|
$primaryKeys[] = $index['Column_name']; |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
return $primaryKeys; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* {@inheritdoc} |
|
164
|
|
|
*/ |
|
165
|
|
|
protected function createColumn(string $name): AbstractColumn |
|
166
|
|
|
{ |
|
167
|
|
|
return new MySQLColumn($this->getName(), $name); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* {@inheritdoc} |
|
172
|
|
|
*/ |
|
173
|
|
|
protected function createIndex(string $name): AbstractIndex |
|
174
|
|
|
{ |
|
175
|
|
|
return new MySQLIndex($this->getName(), $name); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* {@inheritdoc} |
|
180
|
|
|
*/ |
|
181
|
|
|
protected function createForeign(string $column): AbstractReference |
|
182
|
|
|
{ |
|
183
|
|
|
return new MySQLReference($this->getName(), $this->getPrefix(), $column); |
|
184
|
|
|
} |
|
185
|
|
|
} |
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.