1
|
|
|
<?php |
2
|
|
|
namespace yentu\manipulators; |
3
|
|
|
|
4
|
|
|
use yentu\Parameters; |
5
|
|
|
|
6
|
|
|
class Mysql extends AbstractDatabaseManipulator |
7
|
|
|
{ |
8
|
|
|
|
9
|
|
|
private $autoIncrementPending; |
10
|
|
|
private $placeholders = array(); |
11
|
|
|
|
12
|
|
|
#[\Override] |
13
|
|
|
public function convertTypes($type, $direction, $length = null) |
14
|
|
|
{ |
15
|
|
|
$types = array( |
16
|
|
|
'integer' => 'integer', |
17
|
|
|
'int' => 'integer', |
18
|
|
|
'decimal' => 'integer', |
19
|
|
|
'bigint' => 'bigint', |
20
|
|
|
'varchar' => 'string', |
21
|
|
|
'char' => 'string', |
22
|
|
|
'double' => 'double', |
23
|
|
|
'datetime' => 'timestamp', |
24
|
|
|
'timestamp' => 'timestamp', |
25
|
|
|
'text' => 'text', |
26
|
|
|
'tinytext' => 'text', |
27
|
|
|
'mediumtext' => 'text', |
28
|
|
|
'boolean' => 'boolean', |
29
|
|
|
'tinyint' => 'boolean', |
30
|
|
|
'date' => 'date', |
31
|
|
|
'blob' => 'blob' |
32
|
|
|
); |
33
|
|
|
|
34
|
|
|
switch ($direction) { |
35
|
|
|
case self::CONVERT_TO_YENTU: |
36
|
|
|
$destinationType = $types[strtolower($type)]; |
37
|
|
|
break; |
38
|
|
|
|
39
|
|
|
case self::CONVERT_TO_DRIVER: |
40
|
|
|
$destinationType = array_search(strtolower($type), $types); |
41
|
|
|
break; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
if ($destinationType == '') { |
|
|
|
|
45
|
|
|
throw new \yentu\exceptions\DatabaseManipulatorException("Invalid data type {$type} requested"); |
46
|
|
|
} else if ($destinationType == 'varchar') { |
47
|
|
|
$destinationType .= $length === null ? '' : "($length)"; |
48
|
|
|
} |
49
|
|
|
return $destinationType; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Returns an identifier quoted table name. |
54
|
|
|
* In cases where a schema is not available, just the table is returned. |
55
|
|
|
* However, in cases where both a schema and a table are available a dot |
56
|
|
|
* separated version of the name is returned. |
57
|
|
|
* |
58
|
|
|
* @param string $name |
59
|
|
|
* @param string $schema |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
|
|
private function buildTableName($name, $schema) |
63
|
|
|
{ |
64
|
|
|
return ($schema === false || $schema == '' ? '' : "`{$schema}`.") . "`$name`"; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
#[\Override] |
68
|
|
|
protected function _addAutoPrimaryKey($details) |
69
|
|
|
{ |
70
|
|
|
$table = $this->getDescription()->getTable($details); |
71
|
|
|
$column = ($table['columns'][$details['column']]); |
72
|
|
|
|
73
|
|
|
if (count($table['primary_key']) > 0) { |
74
|
|
|
$this->query( |
75
|
|
|
sprintf('ALTER TABLE %s MODIFY `%s` %s %s AUTO_INCREMENT', $this->buildTableName($details['table'], $details['schema']), $details['column'], $this->convertTypes( |
76
|
|
|
$column['type'], self::CONVERT_TO_DRIVER, isset($column['length']) ? $column['length'] : 255 |
77
|
|
|
), $column['nulls'] === false ? 'NOT NULL' : '' |
78
|
|
|
) |
79
|
|
|
); |
80
|
|
|
} else { |
81
|
|
|
$this->autoIncrementPending = $details; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
#[\Override] |
86
|
|
|
protected function _addColumn($details) |
87
|
|
|
{ |
88
|
|
|
$tableName = $this->buildTableName($details['table'], $details['schema']); |
89
|
|
|
|
90
|
|
|
$this->query( |
91
|
|
|
sprintf('ALTER TABLE %s ADD COLUMN `%s` %s %s', $tableName, $details['name'], $this->convertTypes( |
92
|
|
|
$details['type'], self::CONVERT_TO_DRIVER, $details['length'] == '' ? 255 : $details['length'] |
93
|
|
|
), $details['nulls'] === false ? 'NOT NULL' : '' |
94
|
|
|
) |
95
|
|
|
); |
96
|
|
|
|
97
|
|
|
if (isset($this->placeholders[$tableName])) { |
98
|
|
|
$this->query(sprintf('ALTER TABLE %s DROP COLUMN `__yentu_placeholder_col`', $tableName)); |
99
|
|
|
unset($this->placeholders[$tableName]); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
#[\Override] |
104
|
|
|
protected function _addForeignKey($details) |
105
|
|
|
{ |
106
|
|
|
$this->query( |
107
|
|
|
sprintf( |
108
|
|
|
'ALTER TABLE %s ADD CONSTRAINT `%s` FOREIGN KEY (`%s`) REFERENCES %s (`%s`) ON DELETE %s ON UPDATE %s', $this->buildTableName($details['table'], $details['schema']), $details['name'], implode('`,`', $details['columns']), $this->buildTableName($details['foreign_table'], $details['foreign_schema']), implode('","', $details['foreign_columns']), $details['on_delete'] == '' ? 'NO ACTION' : $details['on_delete'], $details['on_update'] == '' ? 'NO ACTION' : $details['on_update'] |
109
|
|
|
) |
110
|
|
|
); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
#[\Override] |
114
|
|
|
protected function _addIndex($details) |
115
|
|
|
{ |
116
|
|
|
$this->query( |
117
|
|
|
sprintf( |
118
|
|
|
'CREATE %s INDEX `%s` ON %s (`%s`)', $details['unique'] ? 'UNIQUE' : '', $details['name'], $this->buildTableName($details['table'], $details['schema']), implode('`, `', $details['columns']) |
119
|
|
|
) |
120
|
|
|
); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
#[\Override] |
124
|
|
|
protected function _addPrimaryKey($details) |
125
|
|
|
{ |
126
|
|
|
$this->query( |
127
|
|
|
sprintf('ALTER TABLE %s ADD PRIMARY KEY (`%s`)', $this->buildTableName($details['table'], $details['schema']), implode('`,`', $details['columns']) |
128
|
|
|
) |
129
|
|
|
); |
130
|
|
|
if (is_array($this->autoIncrementPending)) { |
131
|
|
|
$this->_addAutoPrimaryKey($this->autoIncrementPending); |
132
|
|
|
$this->autoIncrementPending = null; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
#[\Override] |
137
|
|
|
protected function _addSchema($name) |
138
|
|
|
{ |
139
|
|
|
$this->query(sprintf('CREATE SCHEMA `%s`', $name)); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
#[\Override] |
143
|
|
|
protected function _addTable($details) |
144
|
|
|
{ |
145
|
|
|
$this->query(sprintf('CREATE TABLE %s (__yentu_placeholder_col INT)', $this->buildTableName($details['name'], $details['schema']))); |
146
|
|
|
$this->placeholders[$this->buildTableName($details['name'], $details['schema'])] = true; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
#[\Override] |
150
|
|
|
protected function _addUniqueKey($details) |
151
|
|
|
{ |
152
|
|
|
$this->query( |
153
|
|
|
sprintf( |
154
|
|
|
'ALTER TABLE %s ADD CONSTRAINT `%s` UNIQUE (`%s`)', $this->buildTableName($details['table'], $details['schema']), $details['name'], implode('`,`', $details['columns']) |
155
|
|
|
) |
156
|
|
|
); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
#[\Override] |
160
|
|
|
protected function _addView($details) |
161
|
|
|
{ |
162
|
|
|
if ($details['schema'] != null) { |
163
|
|
|
$this->query(sprintf('USE `%s`', $details['schema'])); |
164
|
|
|
} |
165
|
|
|
$this->query(sprintf('CREATE VIEW %s AS %s', $this->buildTableName($details['name'], $details['schema']), $details['definition'])); |
166
|
|
|
if ($details['schema'] != null) { |
167
|
|
|
$this->query(sprintf('USE `%s`', $this->getDefaultSchema())); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
private function changeColumn($details) |
172
|
|
|
{ |
173
|
|
|
$this->query( |
174
|
|
|
sprintf('ALTER TABLE %s CHANGE `%s` `%s` %s %s %s', $this->buildTableName($details['to']['table'], $details['to']['schema']), $details['from']['name'], $details['to']['name'], $this->convertTypes( |
175
|
|
|
$details['to']['type'], self::CONVERT_TO_DRIVER, $details['to']['length'] == '' ? 255 : $details['to']['length'] |
176
|
|
|
), $details['to']['nulls'] === false ? 'NOT NULL' : 'NULL', $details['to']['default'] === null ? |
177
|
|
|
($details['to']['nulls'] === false ? '' : 'DEFAULT NULL') : |
178
|
|
|
"DEFAULT {$details['to']['default']}" |
179
|
|
|
) |
180
|
|
|
); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
#[\Override] |
184
|
|
|
protected function _changeColumnName($details) |
185
|
|
|
{ |
186
|
|
|
$this->changeColumn($details); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
#[\Override] |
190
|
|
|
protected function _changeColumnNulls($details) |
191
|
|
|
{ |
192
|
|
|
$this->changeColumn($details); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
#[\Override] |
196
|
|
|
protected function _changeColumnDefault($details) |
197
|
|
|
{ |
198
|
|
|
$this->changeColumn($details); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
#[\Override] |
202
|
|
|
protected function _changeViewDefinition($details) |
203
|
|
|
{ |
204
|
|
|
$this->query(sprintf("CREATE OR REPLACE VIEW %s AS %s", $this->buildTableName($details['to']['name'], $details['to']['schema']), $details['to']['definition'])); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
#[\Override] |
208
|
|
|
protected function _changeTableName($details) |
209
|
|
|
{ |
210
|
|
|
$this->query( |
211
|
|
|
sprintf( |
212
|
|
|
"RENAME TABLE %s TO %s", $this->buildTableName($details['from']['name'], $details['from']['schema']), $this->buildTableName($details['to']['name'], $details['to']['schema'] |
213
|
|
|
) |
214
|
|
|
)); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
#[\Override] |
218
|
|
|
protected function _dropAutoPrimaryKey($details) |
219
|
|
|
{ |
220
|
|
|
$description = $this->getDescription(); |
221
|
|
|
$column = ($description['tables'][$details['table']]['columns'][$details['column']]); |
222
|
|
|
|
223
|
|
|
$this->query( |
224
|
|
|
sprintf('ALTER TABLE %s MODIFY `%s` %s %s', $this->buildTableName($details['table'], $details['schema']), $details['column'], $this->convertTypes( |
225
|
|
|
$column['type'], self::CONVERT_TO_DRIVER, $column['length'] == '' ? 255 : $column['length'] |
226
|
|
|
), $column['nulls'] === false ? 'NOT NULL' : '' |
227
|
|
|
) |
228
|
|
|
); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
#[\Override] |
232
|
|
|
protected function _dropColumn($details) |
233
|
|
|
{ |
234
|
|
|
$description = $this->getDescription(); |
235
|
|
|
if (count($description['tables'][$details['table']]['columns']) === 0) { |
236
|
|
|
$this->_addColumn( |
237
|
|
|
Parameters::wrap(array( |
238
|
|
|
'table' => $details['table'], |
239
|
|
|
'name' => '__yentu_placeholder_col', |
240
|
|
|
'type' => 'integer' |
241
|
|
|
), ['schema', 'length', 'nulls'] |
242
|
|
|
) |
243
|
|
|
); |
244
|
|
|
} |
245
|
|
|
$this->query( |
246
|
|
|
sprintf( |
247
|
|
|
'ALTER TABLE %s DROP COLUMN `%s`', $this->buildTableName($details['table'], $details['schema']), $details['name'] |
248
|
|
|
) |
249
|
|
|
); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
#[\Override] |
253
|
|
|
protected function _dropForeignKey($details) |
254
|
|
|
{ |
255
|
|
|
$this->query( |
256
|
|
|
sprintf( |
257
|
|
|
'ALTER TABLE %s DROP FOREIGN KEY `%s`', $this->buildTableName($details['table'], $details['schema']), $details['name'] |
258
|
|
|
) |
259
|
|
|
); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
#[\Override] |
263
|
|
|
protected function _dropIndex($details) |
264
|
|
|
{ |
265
|
|
|
$this->query( |
266
|
|
|
sprintf( |
267
|
|
|
'ALTER TABLE %s DROP INDEX `%s`', $this->buildTableName($details['table'], $details['schema']), $details['name'] |
268
|
|
|
) |
269
|
|
|
); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
#[\Override] |
273
|
|
|
protected function _dropPrimaryKey($details) |
274
|
|
|
{ |
275
|
|
|
try { |
276
|
|
|
$this->query( |
277
|
|
|
sprintf( |
278
|
|
|
'ALTER TABLE %s DROP PRIMARY KEY', $this->buildTableName($details['table'], $details['schema']), $details['name'] |
279
|
|
|
) |
280
|
|
|
); |
281
|
|
|
} catch (\yentu\exceptions\DatabaseManipulatorException $e) { |
282
|
|
|
$this->_dropAutoPrimaryKey(array( |
283
|
|
|
'column' => $details['columns'][0], |
284
|
|
|
'schema' => $details['schema'], |
285
|
|
|
'table' => $details['table'] |
286
|
|
|
)); |
287
|
|
|
$this->_dropPrimaryKey($details); |
288
|
|
|
} |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
#[\Override] |
292
|
|
|
protected function _dropSchema($name) |
293
|
|
|
{ |
294
|
|
|
$this->query(sprintf('DROP SCHEMA `%s`', $name)); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
#[\Override] |
298
|
|
|
protected function _dropTable($details) |
299
|
|
|
{ |
300
|
|
|
$this->query(sprintf('DROP TABLE %s', $this->buildTableName($details['name'], $details['schema']))); |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
#[\Override] |
304
|
|
|
protected function _dropUniqueKey($details) |
305
|
|
|
{ |
306
|
|
|
$this->query( |
307
|
|
|
sprintf( |
308
|
|
|
'ALTER TABLE %s DROP KEY `%s`', $this->buildTableName($details['table'], $details['schema']), $details['name'] |
309
|
|
|
) |
310
|
|
|
); |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
#[\Override] |
314
|
|
|
protected function _dropView($details) |
315
|
|
|
{ |
316
|
|
|
$this->query(sprintf('DROP VIEW %s', $this->buildTableName($details['name'], $details['schema']))); |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
protected function describe() |
320
|
|
|
{ |
321
|
|
|
$descriptor = new \yentu\descriptors\Mysql($this); |
|
|
|
|
322
|
|
|
return $descriptor->describe(); |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
protected function getDriverName() |
326
|
|
|
{ |
327
|
|
|
return 'mysql'; |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
#[\Override] |
331
|
|
|
public function quoteIdentifier(string $identifier):string |
332
|
|
|
{ |
333
|
|
|
return "`$identifier`"; |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
} |
337
|
|
|
|