From: Jaime Soriano Pastor Date: Thu, 10 Nov 2011 18:49:17 +0000 (+0100) Subject: Default bits and double quoted strings are parsed now X-Git-Tag: v0.11011~60 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=bc9932bfd;p=dbsrgits%2FSQL-Translator.git Default bits and double quoted strings are parsed now --- diff --git a/AUTHORS b/AUTHORS index caa40e2..e36b7c1 100644 --- a/AUTHORS +++ b/AUTHORS @@ -22,6 +22,7 @@ The following people have contributed to the SQLFairy project: - Geoff Cant - Gudmundur A. Thorisson - Guillermo Roditi +- Jaime Soriano Pastor - Jason Williams - Jonathan Yu - John Goulah diff --git a/Changes b/Changes index 5185de6..ee490e2 100644 --- a/Changes +++ b/Changes @@ -10,6 +10,7 @@ * Oracle does not accept ON DELETE/UPDATE RESTRICT (though it is the actual default) fix by not adding the ON DELETE/UPDATE clause at all * Changed dependency on Digest::SHA1 to the core-bundled Digest::SHA (RT#67989) +* Support for double quoted and bit strings as default values in MySQL parser # ---------------------------------------------------------- # 0.11010 2011-10-05 diff --git a/lib/SQL/Translator/Parser/MySQL.pm b/lib/SQL/Translator/Parser/MySQL.pm index f997a1d..0da058b 100644 --- a/lib/SQL/Translator/Parser/MySQL.pm +++ b/lib/SQL/Translator/Parser/MySQL.pm @@ -203,6 +203,10 @@ drop : /drop/i TABLE /[^;]+/ "$delimiter" drop : /drop/i WORD(s) "$delimiter" { @table_comments = () } +bit: + /(b'[01]+')/ | + /(b"[01]+")/ + string : # MySQL strings, unlike common SQL strings, can be double-quoted or # single-quoted, and you can escape the delmiters by doubling (but only the @@ -583,9 +587,20 @@ default_val : $return = \$item[2]; } | - /default/i /'(?:.*?(?:\\'|''))*.*?'|(?:')?[\w\d:.-]*(?:')?/ + /default/i string + { + $item[2] =~ s/^\s*'|'\s*$//g or $item[2] =~ s/^\s*"|"\s*$//g; + $return = $item[2]; + } + | + /default/i bit + { + $item[2] =~ s/b['"]([01]+)['"]/$1/g; + $return = $item[2]; + } + | + /default/i /(?:')?[\w\d:.-]*(?:')?/ { - $item[2] =~ s/^\s*'|'\s*$//g; $return = $item[2]; } diff --git a/t/02mysql-parser.t b/t/02mysql-parser.t index 321f2ff..62fa70b 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(317, "SQL::Translator::Parser::MySQL"); + maybe_plan(329, "SQL::Translator::Parser::MySQL"); SQL::Translator::Parser::MySQL->import('parse'); } @@ -77,11 +77,13 @@ BEGIN { unsuccessful date default '0000-00-00', i1 int(11) default '0' not null, s1 set('a','b','c') default 'b', - e1 enum('a','b','c') default 'c', + e1 enum('a','b','c') default "c", name varchar(30) default NULL, foo_type enum('vk','ck') NOT NULL default 'vk', date timestamp, time_stamp2 timestamp, + foo_enabled bit(1) default b'0', + bar_enabled bit(1) default b"1", KEY (i1), UNIQUE (date, i1) USING BTREE, KEY date_idx (date), @@ -98,7 +100,7 @@ BEGIN { is( $table->name, 'check', 'Found "check" table' ); my @fields = $table->get_fields; - is( scalar @fields, 10, 'Right number of fields (10)' ); + is( scalar @fields, 12, 'Right number of fields (12)' ); my $f1 = shift @fields; is( $f1->name, 'check_id', 'First field name is "check_id"' ); is( $f1->data_type, 'int', 'Type is "int"' ); @@ -189,6 +191,22 @@ BEGIN { is( $f10->default_value, undef, 'Default value is undefined' ); is( $f10->is_primary_key, 0, 'Field is not PK' ); + my $f11 = shift @fields; + is( $f11->name, 'foo_enabled', 'Eleventh field name is "foo_enabled"' ); + is( $f11->data_type, 'bit', 'Type is "bit"' ); + is( $f11->size, 1, 'Size is "1"' ); + is( $f11->is_nullable, 1, 'Field can be null' ); + is( $f11->default_value, '0', 'Default value is 0' ); + is( $f11->is_primary_key, 0, 'Field is not PK' ); + + my $f12 = shift @fields; + is( $f12->name, 'bar_enabled', 'Twelveth field name is "bar_enabled"' ); + is( $f12->data_type, 'bit', 'Type is "bit"' ); + is( $f12->size, 1, 'Size is "1"' ); + is( $f12->is_nullable, 1, 'Field can be null' ); + is( $f12->default_value, '1', 'Default value is 1' ); + is( $f12->is_primary_key, 0, 'Field is not PK' ); + my @indices = $table->get_indices; is( scalar @indices, 3, 'Right number of indices (3)' );