Skip to content

Commit 4f897e9

Browse files
authored
Full support for 5.7 field types (#16)
* JSON is a type with no params * spatial types are all param-less too * added some missing field type aliases * added boolean type (plus alias) * updated the list of "unsupported", to cover all the 5.7 field types i've found * normalize whitespace
1 parent de24971 commit 4f897e9

4 files changed

Lines changed: 57 additions & 45 deletions

File tree

.travis.yml

Lines changed: 0 additions & 21 deletions
This file was deleted.

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,16 @@ MySQL table definitions have a *lot* of options, so some things just aren't supp
128128
* `UNION` table properties
129129
* `TABLESPACE` table properties
130130
* table partitions
131-
* Spatial field types
131+
* `FLOAT[(bits)]` fields
132+
* Deprecated `YEAR(2|4)` fields
133+
* `ASCII` attribute as a shorthand for `CHARACTER SET latin1`
134+
* `UNICODE` attribute as a shorthand for `CHARACTER SET ucs2`
135+
* `NATIONAL` modified for `CHAR` and `VARCHAR` fields
132136

133137
If you need support for one of these features, open an issue or (better) send a pull request with tests.
138+
139+
The specs for each of the four field groupings can be found here:
140+
* https://dev.mysql.com/doc/refman/5.7/en/numeric-type-syntax.html
141+
* https://dev.mysql.com/doc/refman/5.7/en/date-and-time-type-syntax.html
142+
* https://dev.mysql.com/doc/refman/5.7/en/string-type-syntax.html
143+
* https://dev.mysql.com/doc/refman/5.7/en/spatial-type-overview.html

src/SQLParser.php

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -221,15 +221,13 @@ function walk($tokens, $sql, $source_map){
221221
);
222222
}
223223

224-
private function generateTableKey(array $table)
225-
{
226-
if (!is_null($table['database'])) {
227-
return $table['database'] . '.' . $table['name'];
228-
} else {
229-
return $table['name'];
230-
}
231-
}
232-
224+
private function generateTableKey(array $table){
225+
if (!is_null($table['database'])){
226+
return $table['database'] . '.' . $table['name'];
227+
}else{
228+
return $table['name'];
229+
}
230+
}
233231

234232
function parse_create_table($tokens, $i, $num){
235233

@@ -241,14 +239,15 @@ function parse_create_table($tokens, $i, $num){
241239
#
242240
# name
243241
#
244-
$database = null;
242+
243+
$database = null;
245244
$name = $this->decode_identifier($tokens[$i++]);
246245

247-
if (isset($tokens[$i]) && $tokens[$i] === '.') {
248-
$i++;
249-
$database = $name;
250-
$name = $this->decode_identifier($tokens[$i++]);
251-
}
246+
if (isset($tokens[$i]) && $tokens[$i] === '.'){
247+
$i++;
248+
$database = $name;
249+
$name = $this->decode_identifier($tokens[$i++]);
250+
}
252251

253252

254253
#
@@ -259,18 +258,18 @@ function parse_create_table($tokens, $i, $num){
259258
$i++;
260259
$old_name = $this->decode_identifier($tokens[$i++]);
261260

262-
$like_database = null;
263-
if (isset($tokens[$i]) && $tokens[$i] === '.') {
264-
$i++;
265-
$like_database = $old_name;
266-
$old_name = $this->decode_identifier($tokens[$i++]);
267-
}
261+
$like_database = null;
262+
if (isset($tokens[$i]) && $tokens[$i] === '.'){
263+
$i++;
264+
$like_database = $old_name;
265+
$old_name = $this->decode_identifier($tokens[$i++]);
266+
}
268267

269268
return array(
270269
'name' => $name,
271270
'database' => $database,
272271
'like' => $old_name,
273-
'like_database' => $like_database,
272+
'like_database' => $like_database,
274273
);
275274
}
276275

@@ -293,7 +292,7 @@ function parse_create_table($tokens, $i, $num){
293292

294293
$table = array(
295294
'name' => $name,
296-
'database' => $database,
295+
'database' => $database,
297296
'fields' => $fields,
298297
'indexes' => $indexes,
299298
'props' => $props,
@@ -583,6 +582,17 @@ function parse_field($tokens){
583582
case 'BLOB':
584583
case 'MEDIUMBLOB':
585584
case 'LONGBLOB':
585+
case 'JSON':
586+
case 'GEOMETRY':
587+
case 'POINT':
588+
case 'LINESTRING':
589+
case 'POLYGON':
590+
case 'MULTIPOINT':
591+
case 'MULTILINESTRING':
592+
case 'MULTIPOLYGON':
593+
case 'GEOMETRYCOLLECTION':
594+
case 'BOOLEAN':
595+
case 'BOOL':
586596

587597
# nothing more to read
588598
break;
@@ -622,6 +632,7 @@ function parse_field($tokens){
622632
# REAL[(length,decimals)] [UNSIGNED] [ZEROFILL]
623633
case 'REAL':
624634
case 'DOUBLE':
635+
case 'DOUBLE PRECISION':
625636
case 'FLOAT':
626637

627638
$this->parse_field_length_decimals($tokens, $f);
@@ -633,6 +644,8 @@ function parse_field($tokens){
633644
# DECIMAL[(length[,decimals])] [UNSIGNED] [ZEROFILL]
634645
case 'DECIMAL':
635646
case 'NUMERIC':
647+
case 'DEC':
648+
case 'FIXED':
636649

637650
$this->parse_field_length_decimals($tokens, $f);
638651
$this->parse_field_length($tokens, $f);
@@ -667,6 +680,7 @@ function parse_field($tokens){
667680

668681
# VARCHAR(length) [BINARY] [CHARACTER SET charset_name] [COLLATE collation_name]
669682
case 'VARCHAR':
683+
case 'CHARACTER VARYING':
670684

671685
$this->parse_field_binary($tokens, $f);
672686
$this->parse_field_length($tokens, $f);
@@ -836,6 +850,8 @@ function _extract_tokens($sql, &$source_map){
836850
'SET NULL',
837851
'NO ACTION',
838852
'SET DEFAULT',
853+
'DOUBLE PRECISION',
854+
'CHARACTER VARYING',
839855
);
840856

841857
$singles = array(

tests/FieldTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,13 @@ function testSpatials(){
304304
# GEOMETRYCOLLECTION
305305
}
306306

307+
function testJson(){
308+
309+
# TODO
310+
311+
# JSON
312+
}
313+
307314
function testFieldOptions(){
308315

309316
# TODO

0 commit comments

Comments
 (0)