From: Peter Rabbitson Date: Mon, 9 Mar 2009 01:54:37 +0000 (+0000) Subject: MySQL parser patch by Tokuhiro Matsuno X-Git-Tag: v0.11008~224 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=476669771373e3003296ed64401a7a5b134e259c;p=dbsrgits%2FSQL-Translator.git MySQL parser patch by Tokuhiro Matsuno --- diff --git a/lib/SQL/Translator/Parser/MySQL.pm b/lib/SQL/Translator/Parser/MySQL.pm index db55604..adeb987 100644 --- a/lib/SQL/Translator/Parser/MySQL.pm +++ b/lib/SQL/Translator/Parser/MySQL.pm @@ -715,11 +715,17 @@ table_option : /comment/i /=/ /'.*?'/ { $return = { $item[1] => $item[4] }; } - | WORD /\s*=\s*/ WORD - { + | WORD /\s*=\s*/ MAYBE_QUOTED_WORD + { $return = { $item[1] => $item[3] }; } - + +MAYBE_QUOTED_WORD: /\w+/ + | /'(\w+)'/ + { $return = $1 } + | /"(\w+)"/ + { $return = $1 } + default : /default/i ADD : /add/i diff --git a/t/02mysql-parser.t b/t/02mysql-parser.t index 903dfe6..a515009 100644 --- a/t/02mysql-parser.t +++ b/t/02mysql-parser.t @@ -11,7 +11,7 @@ use SQL::Translator::Utils qw//; use Test::SQL::Translator qw(maybe_plan); BEGIN { - maybe_plan(265, "SQL::Translator::Parser::MySQL"); + maybe_plan(292, "SQL::Translator::Parser::MySQL"); SQL::Translator::Parser::MySQL->import('parse'); } @@ -726,3 +726,51 @@ ok ($@, 'Exception thrown on invalid version string'); is( $c->type, PRIMARY_KEY, 'Constraint is a PK' ); is( join(',', $c->fields), 'id', 'Constraint is on "id"' ); } + +{ + my @data = ( + q|create table quote ( + id int(11) NOT NULL auto_increment, + PRIMARY KEY (id) + ) ENGINE="innodb";|, + q|create table quote ( + id int(11) NOT NULL auto_increment, + PRIMARY KEY (id) + ) ENGINE='innodb';|, + q|create table quote ( + id int(11) NOT NULL auto_increment, + PRIMARY KEY (id) + ) ENGINE=innodb;|, + ); + for my $data (@data) { + my $tr = SQL::Translator->new; + + my $val = parse($tr, $data); + my $schema = $tr->schema; + is( $schema->is_valid, 1, 'Schema is valid' ); + my @tables = $schema->get_tables; + is( scalar @tables, 1, 'Right number of tables (1)' ); + my $table = shift @tables; + is( $table->name, 'quote', 'Found "quote" table' ); + + my $tableTypeFound = 0; + for my $t_option_ref ( $table->options ) { + my($key, $value) = %{$t_option_ref}; + if ( $key eq 'ENGINE' ) { + is($value, 'innodb', 'Table has right table engine option' ); + $tableTypeFound = 1; + } + } + + fail('Table did not have a type option') unless $tableTypeFound; + + my @fields = $table->get_fields; + my $f1 = shift @fields; + is( $f1->name, 'id', 'First field name is "id"' ); + is( $f1->data_type, 'int', 'Type is "int"' ); + is( $f1->size, 11, 'Size is "11"' ); + is( $f1->is_nullable, 0, 'Field cannot be null' ); + is( $f1->is_primary_key, 1, 'Field is PK' ); + } +} +