Skip to content

Commit

Permalink
Full support for 5.7 field types (#16)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
iamcal committed May 4, 2021
1 parent de24971 commit 4f897e9
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 45 deletions.
21 changes: 0 additions & 21 deletions .travis.yml

This file was deleted.

12 changes: 11 additions & 1 deletion README.md
Expand Up @@ -128,6 +128,16 @@ MySQL table definitions have a *lot* of options, so some things just aren't supp
* `UNION` table properties
* `TABLESPACE` table properties
* table partitions
* Spatial field types
* `FLOAT[(bits)]` fields
* Deprecated `YEAR(2|4)` fields
* `ASCII` attribute as a shorthand for `CHARACTER SET latin1`
* `UNICODE` attribute as a shorthand for `CHARACTER SET ucs2`
* `NATIONAL` modified for `CHAR` and `VARCHAR` fields

If you need support for one of these features, open an issue or (better) send a pull request with tests.

The specs for each of the four field groupings can be found here:
* https://dev.mysql.com/doc/refman/5.7/en/numeric-type-syntax.html
* https://dev.mysql.com/doc/refman/5.7/en/date-and-time-type-syntax.html
* https://dev.mysql.com/doc/refman/5.7/en/string-type-syntax.html
* https://dev.mysql.com/doc/refman/5.7/en/spatial-type-overview.html
62 changes: 39 additions & 23 deletions src/SQLParser.php
Expand Up @@ -221,15 +221,13 @@ function walk($tokens, $sql, $source_map){
);
}

private function generateTableKey(array $table)
{
if (!is_null($table['database'])) {
return $table['database'] . '.' . $table['name'];
} else {
return $table['name'];
}
}

private function generateTableKey(array $table){
if (!is_null($table['database'])){
return $table['database'] . '.' . $table['name'];
}else{
return $table['name'];
}
}

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

Expand All @@ -241,14 +239,15 @@ function parse_create_table($tokens, $i, $num){
#
# name
#
$database = null;

$database = null;
$name = $this->decode_identifier($tokens[$i++]);

if (isset($tokens[$i]) && $tokens[$i] === '.') {
$i++;
$database = $name;
$name = $this->decode_identifier($tokens[$i++]);
}
if (isset($tokens[$i]) && $tokens[$i] === '.'){
$i++;
$database = $name;
$name = $this->decode_identifier($tokens[$i++]);
}


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

$like_database = null;
if (isset($tokens[$i]) && $tokens[$i] === '.') {
$i++;
$like_database = $old_name;
$old_name = $this->decode_identifier($tokens[$i++]);
}
$like_database = null;
if (isset($tokens[$i]) && $tokens[$i] === '.'){
$i++;
$like_database = $old_name;
$old_name = $this->decode_identifier($tokens[$i++]);
}

return array(
'name' => $name,
'database' => $database,
'like' => $old_name,
'like_database' => $like_database,
'like_database' => $like_database,
);
}

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

$table = array(
'name' => $name,
'database' => $database,
'database' => $database,
'fields' => $fields,
'indexes' => $indexes,
'props' => $props,
Expand Down Expand Up @@ -583,6 +582,17 @@ function parse_field($tokens){
case 'BLOB':
case 'MEDIUMBLOB':
case 'LONGBLOB':
case 'JSON':
case 'GEOMETRY':
case 'POINT':
case 'LINESTRING':
case 'POLYGON':
case 'MULTIPOINT':
case 'MULTILINESTRING':
case 'MULTIPOLYGON':
case 'GEOMETRYCOLLECTION':
case 'BOOLEAN':
case 'BOOL':

# nothing more to read
break;
Expand Down Expand Up @@ -622,6 +632,7 @@ function parse_field($tokens){
# REAL[(length,decimals)] [UNSIGNED] [ZEROFILL]
case 'REAL':
case 'DOUBLE':
case 'DOUBLE PRECISION':
case 'FLOAT':

$this->parse_field_length_decimals($tokens, $f);
Expand All @@ -633,6 +644,8 @@ function parse_field($tokens){
# DECIMAL[(length[,decimals])] [UNSIGNED] [ZEROFILL]
case 'DECIMAL':
case 'NUMERIC':
case 'DEC':
case 'FIXED':

$this->parse_field_length_decimals($tokens, $f);
$this->parse_field_length($tokens, $f);
Expand Down Expand Up @@ -667,6 +680,7 @@ function parse_field($tokens){

# VARCHAR(length) [BINARY] [CHARACTER SET charset_name] [COLLATE collation_name]
case 'VARCHAR':
case 'CHARACTER VARYING':

$this->parse_field_binary($tokens, $f);
$this->parse_field_length($tokens, $f);
Expand Down Expand Up @@ -836,6 +850,8 @@ function _extract_tokens($sql, &$source_map){
'SET NULL',
'NO ACTION',
'SET DEFAULT',
'DOUBLE PRECISION',
'CHARACTER VARYING',
);

$singles = array(
Expand Down
7 changes: 7 additions & 0 deletions tests/FieldTest.php
Expand Up @@ -304,6 +304,13 @@ function testSpatials(){
# GEOMETRYCOLLECTION
}

function testJson(){

# TODO

# JSON
}

function testFieldOptions(){

# TODO
Expand Down

0 comments on commit 4f897e9

Please sign in to comment.