1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Soluble\Schema\Source\Mysql; |
4
|
|
|
|
5
|
|
|
use Soluble\Schema\Db\Wrapper\MysqlConnectionAdapter; |
6
|
|
|
use ArrayObject; |
7
|
|
|
use Zend\Config\Config; |
8
|
|
|
use Soluble\Schema\Exception; |
9
|
|
|
|
10
|
|
|
class MysqlDriver51 extends AbstractMysqlDriver |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var MysqlConnectionAdapter |
15
|
|
|
*/ |
16
|
|
|
protected $adapter; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Schema name |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $schema; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* {@inheritdoc} |
26
|
|
|
*/ |
27
|
23 |
|
public function __construct(MysqlConnectionAdapter $adapter, $schema) |
28
|
|
|
{ |
29
|
23 |
|
parent::__construct($adapter, $schema); |
30
|
23 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritdoc} |
34
|
|
|
* @throws Exception\ErrorException |
35
|
|
|
*/ |
36
|
6 |
|
public function getSchemaConfig($table = null, $include_options = true) |
37
|
|
|
{ |
38
|
6 |
|
$schema = $this->schema; |
|
|
|
|
39
|
|
|
|
40
|
6 |
|
$results = $this->executeQuery($table); |
41
|
|
|
|
42
|
6 |
|
$references = array(); |
43
|
6 |
|
$config = new Config(array('tables' => array()), true); |
44
|
6 |
|
$tables = $config->offsetGet('tables'); |
45
|
|
|
|
46
|
6 |
|
foreach ($results as $r) { |
47
|
|
|
// Setting table information |
48
|
3 |
|
$table_name = $r['table_name']; |
49
|
3 |
|
if (!$tables->offsetExists($table_name)) { |
50
|
|
|
$table_def = array( |
51
|
3 |
|
'name' => $table_name, |
52
|
3 |
|
'columns' => array(), |
53
|
3 |
|
'primary_keys' => array(), |
54
|
3 |
|
'unique_keys' => array(), |
55
|
3 |
|
'foreign_keys' => array(), |
56
|
3 |
|
'references' => array(), |
57
|
3 |
|
'indexes' => array(), |
58
|
3 |
|
); |
59
|
3 |
|
if ($include_options) { |
60
|
3 |
|
$table_def['options'] = array( |
61
|
3 |
|
'comment' => $r['table_comment'], |
62
|
3 |
|
'collation' => $r['table_collation'], |
63
|
3 |
|
'type' => $r['table_type'], |
64
|
3 |
|
'engine' => $r['engine'] |
65
|
3 |
|
); |
66
|
3 |
|
} |
67
|
3 |
|
$tables->offsetSet($table_name, $table_def); |
68
|
3 |
|
} |
69
|
3 |
|
$table = $tables->offsetGet($table_name); |
70
|
3 |
|
$columns = $table->columns; |
71
|
3 |
|
$column_name = $r['column_name']; |
72
|
|
|
|
73
|
3 |
|
$data_type = strtolower($r['data_type']); |
74
|
|
|
|
75
|
|
|
$col_def = array( |
76
|
3 |
|
'type' => $data_type, |
77
|
3 |
|
'primary' => ($r['constraint_type'] == 'PRIMARY KEY'), |
78
|
3 |
|
'nullable' => ($r['is_nullable'] == 'YES'), |
79
|
3 |
|
'default' => $r['column_default'] |
80
|
3 |
|
); |
81
|
3 |
|
if (($r['constraint_type'] == 'PRIMARY KEY')) { |
82
|
2 |
|
$col_def['primary'] = true; |
83
|
2 |
|
$col_def['autoincrement'] = ($r['extra'] == 'auto_increment'); |
84
|
2 |
|
} |
85
|
|
|
|
86
|
3 |
|
$has_charset = false; |
87
|
3 |
|
if (in_array($data_type, array('int', 'tinyint', 'mediumint', 'bigint', 'int', 'smallint', 'year'))) { |
88
|
3 |
|
$col_def['unsigned'] = (bool) preg_match('/unsigned/', strtolower($r['column_type'])); |
89
|
3 |
|
$col_def['precision'] = is_numeric($r['numeric_precision']) ? (int) $r['numeric_precision'] : null; |
90
|
3 |
|
} elseif (in_array($data_type, array('real', 'double precision', 'decimal', 'numeric', 'float', 'dec', 'fixed'))) { |
91
|
1 |
|
$col_def['precision'] = is_numeric($r['numeric_precision']) ? (int) $r['numeric_precision'] : null; |
92
|
1 |
|
$col_def['scale'] = is_numeric($r['numeric_scale']) ? (int) $r['numeric_scale'] : null; |
93
|
2 |
|
} elseif (in_array($data_type, array('timestamp', 'date', 'time', 'datetime'))) { |
94
|
|
|
// nothing yet |
95
|
2 |
|
} elseif (in_array($data_type, array('char', 'varchar', 'binary', 'varbinary', 'text', 'tinytext', 'mediumtext', 'longtext'))) { |
96
|
2 |
|
$col_def['octet_length'] = is_numeric($r['character_octet_length']) ? (int) $r['character_octet_length'] : null; |
97
|
2 |
|
$col_def['length'] = is_numeric($r['character_maximum_length']) ? (int) $r['character_maximum_length'] : null; |
98
|
2 |
|
$has_charset = true; |
99
|
2 |
|
} elseif (in_array($data_type, array('blob', 'tinyblob', 'mediumblob', 'longblob'))) { |
100
|
1 |
|
$col_def['octet_length'] = (int) $r['character_octet_length']; |
101
|
1 |
|
$col_def['length'] = (int) $r['character_maximum_length']; |
102
|
1 |
|
} elseif (in_array($data_type, array('enum', 'set'))) { |
103
|
1 |
|
$col_def['octet_length'] = (int) $r['character_octet_length']; |
104
|
1 |
|
$col_def['length'] = (int) $r['character_maximum_length']; |
105
|
1 |
|
$def = $r['column_type']; |
106
|
|
|
|
107
|
1 |
|
preg_match_all("/'([^']+)'/", $def, $matches); |
108
|
1 |
|
if (is_array($matches[1]) && count($matches) > 0) { |
109
|
1 |
|
$col_def['values'] = $matches[1]; |
110
|
1 |
|
} |
111
|
1 |
|
} |
112
|
|
|
|
113
|
3 |
|
if ($include_options) { |
114
|
3 |
|
$col_def['options'] = array( |
115
|
3 |
|
'comment' => $r['column_comment'], |
116
|
3 |
|
'definition' => $r['column_type'], |
117
|
3 |
|
'column_key' => $r['column_key'], |
118
|
3 |
|
'ordinal_position' => $r['ordinal_position'], |
119
|
3 |
|
'constraint_type' => $r['constraint_type'], // 'PRIMARY KEY', 'FOREIGN_KEY', 'UNIQUE' |
120
|
|
|
); |
121
|
3 |
|
if ($has_charset) { |
122
|
2 |
|
$col_def['options']['charset'] = $r['character_set_name']; |
123
|
2 |
|
$col_def['options']['collation'] = $r['collation_name']; |
124
|
2 |
|
} |
125
|
3 |
|
} |
126
|
|
|
|
127
|
3 |
|
$columns[$column_name] = $col_def; |
128
|
|
|
|
129
|
3 |
|
$foreign_keys = $table->foreign_keys; |
130
|
3 |
|
$unique_keys = $table->unique_keys; |
131
|
|
|
|
132
|
3 |
|
$constraint_name = $r['constraint_name']; |
133
|
3 |
|
$referenced_table_name = $r['referenced_table_name']; |
134
|
3 |
|
$referenced_column_name = $r['referenced_column_name']; |
135
|
3 |
|
switch ($r['constraint_type']) { |
136
|
3 |
|
case 'PRIMARY KEY': |
137
|
2 |
|
$table->primary_keys = array_merge($table->primary_keys->toArray(), (array) $column_name); |
138
|
2 |
|
break; |
139
|
3 |
|
case 'UNIQUE': |
140
|
2 |
|
if (!$unique_keys->offsetExists($constraint_name)) { |
141
|
2 |
|
$unique_keys[$constraint_name] = array(); |
142
|
2 |
|
} |
143
|
2 |
|
$unique_keys[$constraint_name] = array_merge($unique_keys[$constraint_name]->toArray(), (array) $column_name); |
144
|
2 |
|
break; |
145
|
3 |
|
case 'FOREIGN KEY': |
146
|
|
|
/* |
147
|
|
|
if (!$foreign_keys->offsetExists($constraint_name)) { |
148
|
|
|
$foreign_keys[$constraint_name] = array(); |
149
|
|
|
} |
150
|
|
|
* |
151
|
|
|
*/ |
152
|
|
|
$fk = array( |
153
|
1 |
|
'referenced_table' => $referenced_table_name, |
154
|
1 |
|
'referenced_column' => $referenced_column_name, |
155
|
|
|
'constraint_name' => $constraint_name |
156
|
1 |
|
); |
157
|
1 |
|
$foreign_keys[$column_name] = $fk; |
158
|
|
|
//$table->references[$referenced_table_name] = array($column_name => $r['referenced_column_name']); |
159
|
|
|
|
160
|
1 |
|
if (!array_key_exists($referenced_table_name, $references)) { |
161
|
1 |
|
$references[$referenced_table_name] = array(); |
162
|
1 |
|
} |
163
|
|
|
|
164
|
1 |
|
$k = "$table_name:$referenced_column_name->$column_name"; |
165
|
1 |
|
$references[$referenced_table_name][$k] = array( |
166
|
1 |
|
'column' => $column_name, |
167
|
1 |
|
'referencing_table' => $table_name, |
168
|
1 |
|
'referencing_column' => $referenced_column_name, |
169
|
|
|
'constraint_name' => $constraint_name |
170
|
1 |
|
); |
171
|
1 |
|
break; |
172
|
3 |
|
} |
173
|
6 |
|
} |
174
|
|
|
|
175
|
6 |
|
foreach ($references as $referenced_table_name => $refs) { |
176
|
1 |
|
if ($tables->offsetExists($referenced_table_name)) { |
177
|
1 |
|
$table = $tables[$referenced_table_name]; |
178
|
1 |
|
$table->references = $refs; |
179
|
1 |
|
} |
180
|
6 |
|
} |
181
|
|
|
|
182
|
6 |
|
$array = new ArrayObject($config->toArray()); |
183
|
6 |
|
unset($config); |
184
|
6 |
|
return $array; |
185
|
|
|
|
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Return information schema query |
190
|
|
|
* |
191
|
|
|
* @param string|null $table |
192
|
|
|
* @return string |
193
|
|
|
*/ |
194
|
6 |
|
protected function getQuery($table = null) { |
195
|
|
|
|
196
|
6 |
|
$qSchema = $this->adapter->quoteValue($this->schema); |
197
|
|
|
|
198
|
6 |
|
if ($table !== null) { |
199
|
4 |
|
$qTable = $this->adapter->quoteValue($table); |
200
|
4 |
|
$table_clause = "and (t.TABLE_NAME = $qTable or (kcu.referenced_table_name = $qTable and kcu.constraint_name = 'FOREIGN KEY'))"; |
201
|
4 |
|
$table_join_condition = "(t.table_name = kcu.table_name or kcu.referenced_table_name = t.table_name)"; |
202
|
4 |
|
} else { |
203
|
2 |
|
$table_join_condition = "t.table_name = kcu.table_name"; |
204
|
2 |
|
$table_clause = ''; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
$query = " |
208
|
|
|
|
209
|
|
|
SELECT |
210
|
|
|
t.table_name, |
211
|
|
|
c.column_name, |
212
|
|
|
c.data_type, |
213
|
|
|
c.column_type, |
214
|
|
|
|
215
|
|
|
c.extra, |
216
|
|
|
|
217
|
|
|
tc.constraint_type, |
218
|
|
|
kcu.constraint_name, |
219
|
|
|
kcu.referenced_table_name, |
220
|
|
|
kcu.referenced_column_name, |
221
|
|
|
|
222
|
|
|
c.column_default, |
223
|
|
|
c.is_nullable, |
224
|
|
|
c.numeric_precision, |
225
|
|
|
c.numeric_scale, |
226
|
|
|
c.character_octet_length, |
227
|
|
|
c.character_maximum_length, |
228
|
|
|
c.ordinal_position, |
229
|
|
|
|
230
|
|
|
c.column_key, -- UNI/MUL/PRI |
231
|
|
|
c.character_set_name, |
232
|
|
|
|
233
|
|
|
|
234
|
|
|
c.collation_name, |
235
|
|
|
|
236
|
|
|
c.column_comment, |
237
|
|
|
|
238
|
|
|
t.table_type, |
239
|
|
|
t.engine, |
240
|
|
|
t.table_comment, |
241
|
|
|
t.table_collation |
242
|
|
|
|
243
|
|
|
FROM `INFORMATION_SCHEMA`.`COLUMNS` c |
244
|
|
|
INNER JOIN `INFORMATION_SCHEMA`.`TABLES` t on c.TABLE_NAME = t.TABLE_NAME |
245
|
|
|
LEFT JOIN `INFORMATION_SCHEMA`.`KEY_COLUMN_USAGE` kcu |
246
|
|
|
on ( |
247
|
|
|
$table_join_condition |
248
|
|
|
and kcu.table_schema = t.table_schema |
249
|
|
|
and kcu.column_name = c.column_name |
250
|
|
|
) |
251
|
|
|
LEFT JOIN |
252
|
|
|
`INFORMATION_SCHEMA`.`TABLE_CONSTRAINTS` tc |
253
|
|
|
on ( |
254
|
|
|
t.table_name = tc.table_name |
255
|
|
|
and tc.table_schema = t.table_schema |
256
|
|
|
and tc.constraint_name = kcu.constraint_name |
257
|
|
|
) |
258
|
|
|
|
259
|
|
|
|
260
|
6 |
|
where c.TABLE_SCHEMA = $qSchema |
261
|
6 |
|
and t.TABLE_SCHEMA = $qSchema |
262
|
6 |
|
$table_clause |
263
|
6 |
|
and (kcu.table_schema = $qSchema or kcu.table_schema is null) |
264
|
|
|
|
265
|
|
|
and (kcu.column_name = c.column_name or kcu.column_name is null) |
266
|
|
|
order by t.table_name, c.ordinal_position |
267
|
6 |
|
"; |
268
|
|
|
|
269
|
6 |
|
return $query; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* Execute information schema query |
274
|
|
|
* |
275
|
|
|
* @param string|null $table table name or null |
276
|
|
|
* @return ArrayObject |
277
|
|
|
* @throws Exception\ErrorException |
278
|
|
|
*/ |
279
|
6 |
|
protected function executeQuery($table = null) |
280
|
|
|
{ |
281
|
6 |
|
$query = $this->getQuery($table); |
282
|
6 |
|
$this->disableInnoDbStats(); |
283
|
|
|
try { |
284
|
6 |
|
$results = $this->adapter->query($query); |
285
|
6 |
|
} catch (\Exception $e) { |
286
|
|
|
//@codeCoverageIgnoreStart |
287
|
|
|
$this->restoreInnoDbStats(); |
288
|
|
|
throw new Exception\ErrorException(__METHOD__ . ": " . $e->getMessage()); |
289
|
|
|
//@codeCoverageIgnoreEnd |
290
|
|
|
} |
291
|
6 |
|
$this->restoreInnoDbStats(); |
292
|
6 |
|
return $results; |
293
|
|
|
} |
294
|
|
|
} |
295
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.