/
MysqlSchema.php
325 lines (308 loc) · 9.11 KB
/
MysqlSchema.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<?php
/**
* PHP Version 5.4
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since CakePHP(tm) v 3.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Database\Schema;
use Cake\Database\Schema\Table;
use Cake\Error;
/**
* Schema dialect/support for MySQL
*/
class MysqlSchema {
/**
* The driver instance being used.
*
* @var Cake\Database\Driver\Mysql
*/
protected $_driver;
/**
* Constructor
*
* @param Cake\Database\Driver $driver The driver to use.
*/
public function __construct($driver) {
$this->_driver = $driver;
}
/**
* Get the SQL to list the tables in MySQL
*
* @param array $config The connection configuration to use for
* getting tables from.
* @return array An array of (sql, params) to execute.
*/
public function listTablesSql(array $config) {
return ["SHOW TABLES FROM " . $this->_driver->quoteIdentifier($config['database']), []];
}
/**
* Get the SQL to describe a table in MySQL.
*
* @param string $table The table name to describe.
* @return array An array of (sql, params) to execute.
*/
public function describeTableSql($table) {
return ["SHOW FULL COLUMNS FROM " . $this->_driver->quoteIdentifier($table), []];
}
/**
* Convert a MySQL column type into an abstract type.
*
* The returned type will be a type that Cake\Database\Type can handle.
*
* @param string $column The column type + length
* @return array Array of column information.
* @throws Cake\Error\Exception When column type cannot be parsed.
*/
public function convertColumn($column) {
preg_match('/([a-z]+)(?:\(([0-9,]+)\))?/i', $column, $matches);
if (empty($matches)) {
throw new Error\Exception(__d('cake_dev', 'Unable to parse column type from "%s"', $column));
}
$col = strtolower($matches[1]);
$length = $precision = null;
if (isset($matches[2])) {
$length = $matches[2];
if (strpos($matches[2], ',') !== false) {
list($length, $precision) = explode(',', $length);
}
$length = (int)$length;
$precision = (int)$precision;
}
if (in_array($col, array('date', 'time', 'datetime', 'timestamp'))) {
return ['type' => $col, 'length' => null];
}
if (($col === 'tinyint' && $length === 1) || $col === 'boolean') {
return ['type' => 'boolean', 'length' => null];
}
if (strpos($col, 'bigint') !== false || $col === 'bigint') {
return ['type' => 'biginteger', 'length' => $length];
}
if (strpos($col, 'int') !== false) {
return ['type' => 'integer', 'length' => $length];
}
if ($col === 'char') {
return ['type' => 'string', 'fixed' => true, 'length' => $length];
}
if (strpos($col, 'char') !== false || $col === 'tinytext') {
return ['type' => 'string', 'length' => $length];
}
if (strpos($col, 'text') !== false) {
return ['type' => 'text', 'length' => $length];
}
if (strpos($col, 'blob') !== false || $col === 'binary') {
return ['type' => 'binary', 'length' => $length];
}
if (strpos($col, 'float') !== false || strpos($col, 'double') !== false) {
return ['type' => 'float', 'length' => $length, 'precision' => $precision];
}
if (strpos($col, 'decimal') !== false) {
return ['type' => 'decimal', 'length' => $length, 'precision' => $precision];
}
return ['type' => 'text', 'length' => null];
}
/**
* Convert field description results into abstract schema fields.
*
* @param Cake\Database\Schema\Table $table The table object to append fields to.
* @param array $row The row data from describeTableSql
* @param array $fieldParams Additional field parameters to parse.
* @return void
*/
public function convertFieldDescription(Table $table, $row, $fieldParams = []) {
$field = $this->convertColumn($row['Type']);
$field += [
'null' => $row['Null'] === 'YES' ? true : false,
'default' => $row['Default'],
];
foreach ($fieldParams as $key => $metadata) {
if (!empty($row[$metadata['column']])) {
$field[$key] = $row[$metadata['column']];
}
}
$table->addColumn($row['Field'], $field);
if (!empty($row['Key']) && $row['Key'] === 'PRI') {
$table->addIndex('primary', [
'type' => Table::INDEX_PRIMARY,
'columns' => [$row['Field']]
]);
}
}
/**
* Get additional column meta data used in schema reflections.
*
* @return array
*/
public function extraSchemaColumns() {
return [
'charset' => [
'column' => false,
],
'collate' => [
'column' => 'Collation',
],
'comment' => [
'column' => 'Comment',
]
];
}
/**
* Generate the SQL to create a table.
*
* @param string $table The name of the table.
* @param array $lines The lines (columns + indexes) to go inside the table.
* @return string A complete CREATE TABLE statement
*/
public function createTableSql($table, $lines) {
$content = implode(",\n", $lines);
return sprintf("CREATE TABLE `%s` (\n%s\n);", $table, $content);
}
/**
* Generate the SQL fragment for a single column in MySQL
*
* @param Cake\Database\Schema\Table $table The table object the column is in.
* @param string $name The name of the column.
* @return string SQL fragment.
*/
public function columnSql(Table $table, $name) {
$data = $table->column($name);
$out = $this->_driver->quoteIdentifier($name);
$typeMap = [
'integer' => ' INTEGER',
'biginteger' => ' BIGINT',
'boolean' => ' BOOLEAN',
'binary' => ' BLOB',
'float' => ' FLOAT',
'decimal' => ' DECIMAL',
'text' => ' TEXT',
'date' => ' DATE',
'time' => ' TIME',
'datetime' => ' DATETIME',
'timestamp' => ' TIMESTAMP',
];
$specialMap = [
'string' => true,
];
if (isset($typeMap[$data['type']])) {
$out .= $typeMap[$data['type']];
}
if (isset($specialMap[$data['type']])) {
switch ($data['type']) {
case 'string':
$out .= !empty($data['fixed']) ? ' CHAR' : ' VARCHAR';
if (!isset($data['length'])) {
$data['length'] = 255;
}
break;
}
}
$hasLength = ['integer', 'string'];
if (in_array($data['type'], $hasLength, true) && isset($data['length'])) {
$out .= '(' . (int)$data['length'] . ')';
}
$hasPrecision = ['float', 'decimal'];
if (
in_array($data['type'], $hasPrecision, true) &&
(isset($data['length']) || isset($data['precision']))
) {
$out .= '(' . (int)$data['length'] . ',' . (int)$data['precision'] . ')';
}
if (isset($data['null']) && $data['null'] === false) {
$out .= ' NOT NULL';
}
if (in_array($data['type'], ['integer', 'biginteger']) && in_array($name, (array)$table->primaryKey())) {
$out .= ' AUTO_INCREMENT';
}
if (isset($data['null']) && $data['null'] === true) {
$out .= $data['type'] === 'timestamp' ? ' NULL' : ' DEFAULT NULL';
unset($data['default']);
}
if (isset($data['default']) && $data['type'] !== 'timestamp') {
$out .= ' DEFAULT ' . $this->_value($data['default']);
}
if (
isset($data['default']) &&
$data['type'] === 'timestamp' &&
strtolower($data['default']) === 'current_timestamp'
) {
$out .= ' DEFAULT CURRENT_TIMESTAMP';
}
if (isset($data['comment'])) {
$out .= ' COMMENT ' . $this->_value($data['comment']);
}
return $out;
}
/**
* Escapes values for use in schema definitions.
*
* @param mixed $value The value to escape.
* @return string String for use in schema definitions.
*/
protected function _value($value) {
if (is_null($value)) {
return 'NULL';
}
if ($value === false) {
return 'FALSE';
}
if ($value === true) {
return 'TRUE';
}
if (is_float($value)) {
return str_replace(',', '.', strval($value));
}
if ((is_int($value) || $value === '0') || (
is_numeric($value) && strpos($value, ',') === false &&
$value[0] != '0' && strpos($value, 'e') === false)
) {
return $value;
}
return $this->_driver->quote($value, \PDO::PARAM_STR);
}
/**
* Generate the SQL fragment for a single index in MySQL
*
* @param Cake\Database\Schema\Table $table The table object the column is in.
* @param string $name The name of the column.
* @return string SQL fragment.
*/
public function indexSql(Table $table, $name) {
$data = $table->index($name);
if ($data['type'] === Table::INDEX_PRIMARY) {
$columns = array_map(
[$this->_driver, 'quoteIdentifier'],
$data['columns']
);
return sprintf('PRIMARY KEY (%s)', implode(', ', $columns));
}
if ($data['type'] === Table::INDEX_UNIQUE) {
$out = 'UNIQUE KEY ';
}
if ($data['type'] === Table::INDEX_INDEX) {
$out = 'KEY ';
}
if ($data['type'] === Table::INDEX_FULLTEXT) {
$out = 'FULLTEXT KEY ';
}
$out .= $this->_driver->quoteIdentifier($name);
$columns = array_map(
[$this->_driver, 'quoteIdentifier'],
$data['columns']
);
foreach ($data['columns'] as $i => $column) {
if (isset($data['length'][$column])) {
$columns[$i] .= sprintf('(%d)', $data['length'][$column]);
}
}
return $out . ' (' . implode(', ', $columns) . ')';
}
}