X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FSQL%2FTranslator%2FParser%2FMySQL.pm;h=316000431cc2b43b4c03bd25afbb1640c20b54f0;hb=935800450f88b0500c4fa7c3b174cd22b5f9eb56;hp=89a2194a21125320d1936ef37ddb8b931c728ba9;hpb=4d4385499fa84e31f78abe4cacf517e4a11c7b8f;p=dbsrgits%2FSQL-Translator.git diff --git a/lib/SQL/Translator/Parser/MySQL.pm b/lib/SQL/Translator/Parser/MySQL.pm index 89a2194..3160004 100644 --- a/lib/SQL/Translator/Parser/MySQL.pm +++ b/lib/SQL/Translator/Parser/MySQL.pm @@ -1,25 +1,5 @@ package SQL::Translator::Parser::MySQL; -# ------------------------------------------------------------------- -# $Id: MySQL.pm,v 1.58 2007-03-19 17:15:24 duality72 Exp $ -# ------------------------------------------------------------------- -# Copyright (C) 2002-4 SQLFairy Authors -# -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License as -# published by the Free Software Foundation; version 2. -# -# This program is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA -# 02111-1307 USA -# ------------------------------------------------------------------- - =head1 NAME SQL::Translator::Parser::MySQL - parser for MySQL @@ -101,9 +81,12 @@ Here's the word from the MySQL site table_options: TYPE = {BDB | HEAP | ISAM | InnoDB | MERGE | MRG_MYISAM | MYISAM } + or ENGINE = {BDB | HEAP | ISAM | InnoDB | MERGE | MRG_MYISAM | MYISAM } or AUTO_INCREMENT = # or AVG_ROW_LENGTH = # + or [ DEFAULT ] CHARACTER SET charset_name or CHECKSUM = {0 | 1} + or COLLATE collation_name or COMMENT = "string" or MAX_ROWS = # or MIN_ROWS = # @@ -117,6 +100,7 @@ Here's the word from the MySQL site or DATA DIRECTORY="absolute path to directory" or INDEX DIRECTORY="absolute path to directory" + A subset of the ALTER TABLE syntax that allows addition of foreign keys: ALTER [IGNORE] TABLE tbl_name alter_specification [, alter_specification] ... @@ -130,18 +114,35 @@ A subset of INSERT that we ignore: INSERT anything +=head1 ARGUMENTS + +This parser takes a single optional parser_arg C, which +provides the desired version for the target database. Any statement in the processed +dump file, that is commented with a version higher than the one supplied, will be stripped. + +Valid version specifiers for C are listed L + +More information about the MySQL comment-syntax: L + + =cut use strict; use vars qw[ $DEBUG $VERSION $GRAMMAR @EXPORT_OK ]; -$VERSION = sprintf "%d.%02d", q$Revision: 1.58 $ =~ /(\d+)\.(\d+)/; +$VERSION = '1.59'; $DEBUG = 0 unless defined $DEBUG; use Data::Dumper; use Parse::RecDescent; use Exporter; +use Storable qw(dclone); +use DBI qw(:sql_types); use base qw(Exporter); +use SQL::Translator::Utils qw/parse_mysql_version/; + +our %type_mapping = (); + @EXPORT_OK = qw(parse); # Enable warnings within the Parse::RecDescent module. @@ -154,7 +155,8 @@ use constant DEFAULT_PARSER_VERSION => 30000; $GRAMMAR = << 'END_OF_GRAMMAR'; { - my ( $database_name, %tables, $table_order, @table_comments, %views, $view_order, %procedures, $proc_order ); + my ( $database_name, %tables, $table_order, @table_comments, %views, + $view_order, %procedures, $proc_order ); my $delimiter = ';'; } @@ -165,7 +167,12 @@ $GRAMMAR = << 'END_OF_GRAMMAR'; # failed. -ky # startrule : statement(s) eofile { - { tables => \%tables, database_name => $database_name, views => \%views, procedures =>\%procedures } + { + database_name => $database_name, + tables => \%tables, + views => \%views, + procedures => \%procedures, + } } eofile : /^\Z/ @@ -206,12 +213,12 @@ string : nonstring : /[^;\'"]+/ -statement_body : (string | nonstring)(s?) +statement_body : string | nonstring -insert : /insert/i statement_body "$delimiter" +insert : /insert/i statement_body(s?) "$delimiter" delimiter : /delimiter/i /[\S]+/ - { $delimiter = $item[2] } + { $delimiter = $item[2] } empty_statement : "$delimiter" @@ -291,20 +298,20 @@ create : CREATE UNIQUE(?) /(index|key)/i index_name /on/i table_name '(' field_n push @{ $tables{ $item{'table_name'} }{'indices'} }, { name => $item[4], - type => $item[2] ? 'unique' : 'normal', + type => $item[2][0] ? 'unique' : 'normal', fields => $item[8], } ; } create : CREATE /trigger/i NAME not_delimiter "$delimiter" - { - @table_comments = (); - } + { + @table_comments = (); + } create : CREATE PROCEDURE NAME not_delimiter "$delimiter" - { - @table_comments = (); + { + @table_comments = (); my $func_name = $item[3]; my $owner = ''; my $sql = "$item[1] $item[2] $item[3] $item[4]"; @@ -313,16 +320,17 @@ create : CREATE PROCEDURE NAME not_delimiter "$delimiter" $procedures{ $func_name }{'name'} = $func_name; $procedures{ $func_name }{'owner'} = $owner; $procedures{ $func_name }{'sql'} = $sql; - } + } PROCEDURE : /procedure/i - | /function/i + | /function/i -create : CREATE algorithm /view/i NAME not_delimiter "$delimiter" - { - @table_comments = (); - my $view_name = $item[4]; - my $sql = "$item[1] $item[2] $item[3] $item[4] $item[5]"; +create : CREATE replace(?) algorithm(?) /view/i NAME not_delimiter "$delimiter" + { + @table_comments = (); + my $view_name = $item[5]; + my $sql = join(q{ }, grep { defined and length } $item[1], $item[2]->[0], $item[3]->[0]) + . " $item[4] $item[5] $item[6]"; # Hack to strip database from function calls in SQL $sql =~ s#`\w+`\.(`\w+`\()##g; @@ -330,12 +338,14 @@ create : CREATE algorithm /view/i NAME not_delimiter "$delimiter" $views{ $view_name }{'order'} = ++$view_order; $views{ $view_name }{'name'} = $view_name; $views{ $view_name }{'sql'} = $sql; - } + } + +replace : /or replace/i algorithm : /algorithm/i /=/ WORD - { - $return = "$item[1]=$item[3]"; - } + { + $return = "$item[1]=$item[3]"; + } not_delimiter : /.*?(?=$delimiter)/is @@ -512,6 +522,7 @@ reference_option: /restrict/i | index : normal_index | fulltext_index + | spatial_index | table_name : NAME @@ -535,42 +546,6 @@ data_type : WORD parens_value_list(s?) type_qualifier(s?) $list = []; } - if ( @{ $size || [] } == 0 && !$thisparser->{local}{sqlt_parser_args}{no_default_sizes} ) { - if ( lc $type eq 'tinyint' ) { - $size = 4; - } - elsif ( lc $type eq 'smallint' ) { - $size = 6; - } - elsif ( lc $type eq 'mediumint' ) { - $size = 9; - } - elsif ( $type =~ /^int(eger)?$/i ) { - $type = 'int'; - $size = 11; - } - elsif ( lc $type eq 'bigint' ) { - $size = 20; - } - elsif ( - lc $type =~ /(float|double|decimal|numeric|real|fixed|dec)/ - ) { - $size = [8,2]; - } - } - - if ( $type =~ /^tiny(text|blob)$/i ) { - $size = 255; - } - elsif ( $type =~ /^(blob|text)$/i ) { - $size = 65_535; - } - elsif ( $type =~ /^medium(blob|text)$/i ) { - $size = 16_777_215; - } - elsif ( $type =~ /^long(blob|text)$/i ) { - $size = 4_294_967_295; - } $return = { type => $type, @@ -601,19 +576,13 @@ not_null : /not/i /null/i unsigned : /unsigned/i { $return = 0 } -#default_val : /default/i /(?:')?[\s\w\d:.-]*(?:')?/ -# { -# $item[2] =~ s/'//g; -# $return = $item[2]; -# } - default_val : /default/i 'CURRENT_TIMESTAMP' { - $return = $item[2]; + $return = \$item[2]; } | - /default/i /'(?:.*?\\')*.*?'|(?:')?[\w\d:.-]*(?:')?/ + /default/i /'(?:.*?(?:\\'|''))*.*?'|(?:')?[\w\d:.-]*(?:')?/ { $item[2] =~ s/^\s*'|'\s*$//g; $return = $item[2]; @@ -654,36 +623,44 @@ foreign_key_def_begin : /constraint/i /foreign key/i WORD /foreign key/i { $return = '' } -primary_key_def : primary_key index_name(?) '(' name_with_opt_paren(s /,/) ')' +primary_key_def : primary_key index_name_not_using(?) index_type(?) '(' name_with_opt_paren(s /,/) ')' index_type(?) { $return = { supertype => 'constraint', - name => $item{'index_name(?)'}[0], + name => $item[2][0], type => 'primary_key', - fields => $item[4], + fields => $item[5], + options => $item[3][0] || $item[7][0], }; } -unique_key_def : UNIQUE KEY(?) index_name(?) '(' name_with_opt_paren(s /,/) ')' +unique_key_def : UNIQUE KEY(?) index_name_not_using(?) index_type(?) '(' name_with_opt_paren(s /,/) ')' index_type(?) { $return = { supertype => 'constraint', - name => $item{'index_name(?)'}[0], + name => $item[3][0], type => 'unique', - fields => $item[5], + fields => $item[6], + options => $item[4][0] || $item[8][0], } } -normal_index : KEY index_name(?) '(' name_with_opt_paren(s /,/) ')' +normal_index : KEY index_name_not_using(?) index_type(?) '(' name_with_opt_paren(s /,/) ')' index_type(?) { $return = { supertype => 'index', type => 'normal', - name => $item{'index_name(?)'}[0], - fields => $item[4], - } + name => $item[2][0], + fields => $item[5], + options => $item[3][0] || $item[7][0], + } } +index_name_not_using : QUOTED_NAME + | /(\b(?!using)\w+\b)/ { $return = ($1 =~ /^using/i) ? undef : $1 } + +index_type : /using (btree|hash|rtree)/i { $return = uc $1 } + fulltext_index : /fulltext/i KEY(?) index_name(?) '(' name_with_opt_paren(s /,/) ')' { $return = { @@ -694,10 +671,20 @@ fulltext_index : /fulltext/i KEY(?) index_name(?) '(' name_with_opt_paren(s /,/) } } +spatial_index : /spatial/i KEY(?) index_name(?) '(' name_with_opt_paren(s /,/) ')' + { + $return = { + supertype => 'index', + type => 'spatial', + name => $item{'index_name(?)'}[0], + fields => $item[5], + } + } + name_with_opt_paren : NAME parens_value_list(s?) { $item[2][0] ? "$item[1]($item[2][0][0])" : $item[1] } -UNIQUE : /unique/i { 1 } +UNIQUE : /unique/i KEY : /key/i | /index/i @@ -708,15 +695,29 @@ table_option : /comment/i /=/ /'.*?'/ $comment =~ s/'$//; $return = { comment => $comment }; } - | /(default )?(charset|character set)/i /\s*=\s*/ WORD + | /(default )?(charset|character set)/i /\s*=?\s*/ WORD { $return = { 'CHARACTER SET' => $item[3] }; } - | WORD /\s*=\s*/ WORD + | /collate/i WORD + { + $return = { 'COLLATE' => $item[2] } + } + | /union/i /\s*=\s*/ '(' table_name(s /,/) ')' { + $return = { $item[1] => $item[4] }; + } + | 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 @@ -735,12 +736,19 @@ DIGITS : /\d+/ COMMA : ',' -NAME : "`" /\w+/ "`" +BACKTICK : '`' + +DOUBLE_QUOTE: '"' + +QUOTED_NAME : BACKTICK /[^`]+/ BACKTICK { $item[2] } - | /\w+/ - { $item[1] } + | DOUBLE_QUOTE /[^"]+/ DOUBLE_QUOTE + { $item[2] } + +NAME: QUOTED_NAME + | /\w+/ -VALUE : /[-+]?\.?\d+(?:[eE]\d+)?/ +VALUE : /[-+]?\.?\d+(?:[eE]\d+)?/ { $item[1] } | /'.*?'/ { @@ -753,16 +761,15 @@ VALUE : /[-+]?\.?\d+(?:[eE]\d+)?/ { 'NULL' } CURRENT_TIMESTAMP : /current_timestamp(\(\))?/i - | /now\(\)/i - { 'CURRENT_TIMESTAMP' } - + | /now\(\)/i + { 'CURRENT_TIMESTAMP' } + END_OF_GRAMMAR # ------------------------------------------------------------------- sub parse { my ( $translator, $data ) = @_; my $parser = Parse::RecDescent->new($GRAMMAR); - local $::RD_TRACE = $translator->trace ? 1 : undef; local $DEBUG = $translator->debug; @@ -770,13 +777,18 @@ sub parse { return $translator->error("Error instantiating Parse::RecDescent ". "instance: Bad grammer"); } - - # This is the only way to get args into the productions/actions - $parser->{local}{sqlt_parser_args} = $translator->parser_args; - # Preprocess for MySQL-specific and not-before-version comments from mysqldump - my $parser_version = $translator->parser_args->{mysql_parser_version} || DEFAULT_PARSER_VERSION; - while ( $data =~ s#/\*!(\d{5})?(.*?)\*/#($1 && $1 > $parser_version ? '' : $2)#es ) {} + # Preprocess for MySQL-specific and not-before-version comments + # from mysqldump + my $parser_version = parse_mysql_version( + $translator->parser_args->{mysql_parser_version}, 'mysql' + ) || DEFAULT_PARSER_VERSION; + + while ( $data =~ + s#/\*!(\d{5})?(.*?)\*/#($1 && $1 > $parser_version ? '' : $2)#es + ) { + # do nothing; is there a better way to write this? -- ky + } my $result = $parser->startrule($data); return $translator->error( "Parse failed." ) unless defined $result; @@ -820,7 +832,7 @@ sub parse { $table->primary_key( $field->name ) if $fdata->{'is_primary_key'}; for my $qual ( qw[ binary unsigned zerofill list collate ], - 'character set', 'on update' ) { + 'character set', 'on update' ) { if ( my $val = $fdata->{ $qual } || $fdata->{ uc $qual } ) { next if ref $val eq 'ARRAY' && !@$val; $field->extra( $qual, $val ); @@ -843,20 +855,12 @@ sub parse { ) or die $table->error; } - if ( $field->data_type =~ /(set|enum)/i && !$field->size ) { - my %extra = $field->extra; - my $longest = 0; - for my $len ( map { length } @{ $extra{'list'} || [] } ) { - $longest = $len if $len > $longest; - } - $field->size( $longest ) if $longest; - } - for my $cdata ( @{ $fdata->{'constraints'} } ) { next unless $cdata->{'type'} eq 'foreign_key'; $cdata->{'fields'} ||= [ $field->name ]; push @{ $tdata->{'constraints'} }, $cdata; } + } for my $idata ( @{ $tdata->{'indices'} || [] } ) { @@ -868,7 +872,23 @@ sub parse { } if ( my @options = @{ $tdata->{'table_options'} || [] } ) { - $table->options( \@options ) or die $table->error; + my @cleaned_options; + my @ignore_opts = $translator->parser_args->{'ignore_opts'} + ? split( /,/, $translator->parser_args->{'ignore_opts'} ) + : (); + if (@ignore_opts) { + my $ignores = { map { $_ => 1 } @ignore_opts }; + foreach my $option (@options) { + # make sure the option isn't in ignore list + my ($option_key) = keys %$option; + if ( !exists $ignores->{$option_key} ) { + push @cleaned_options, $option; + } + } + } else { + @cleaned_options = @options; + } + $table->options( \@cleaned_options ) or die $table->error; } for my $cdata ( @{ $tdata->{'constraints'} || [] } ) { @@ -879,36 +899,130 @@ sub parse { reference_table => $cdata->{'reference_table'}, reference_fields => $cdata->{'reference_fields'}, match_type => $cdata->{'match_type'} || '', - on_delete => $cdata->{'on_delete'} || $cdata->{'on_delete_do'}, - on_update => $cdata->{'on_update'} || $cdata->{'on_update_do'}, + on_delete => $cdata->{'on_delete'} + || $cdata->{'on_delete_do'}, + on_update => $cdata->{'on_update'} + || $cdata->{'on_update_do'}, ) or die $table->error; } + + # After the constrains and PK/idxs have been created, + # we normalize fields + normalize_field($_) for $table->get_fields; } my @procedures = sort { - $result->{procedures}->{ $a }->{'order'} <=> $result->{procedures}->{ $b }->{'order'} + $result->{procedures}->{ $a }->{'order'} + <=> + $result->{procedures}->{ $b }->{'order'} } keys %{ $result->{procedures} }; - foreach my $proc_name (@procedures) { - $schema->add_procedure( - name => $proc_name, - owner => $result->{procedures}->{$proc_name}->{owner}, - sql => $result->{procedures}->{$proc_name}->{sql}, - ); - } + for my $proc_name ( @procedures ) { + $schema->add_procedure( + name => $proc_name, + owner => $result->{procedures}->{$proc_name}->{owner}, + sql => $result->{procedures}->{$proc_name}->{sql}, + ); + } my @views = sort { - $result->{views}->{ $a }->{'order'} <=> $result->{views}->{ $b }->{'order'} + $result->{views}->{ $a }->{'order'} + <=> + $result->{views}->{ $b }->{'order'} } keys %{ $result->{views} }; - foreach my $view_name (keys %{ $result->{views} }) { - $schema->add_view( - name => $view_name, - sql => $result->{views}->{$view_name}->{sql}, - ); + + for my $view_name ( @views ) { + $schema->add_view( + name => $view_name, + sql => $result->{'views'}->{$view_name}->{sql}, + ); } return 1; } +# Takes a field, and returns +sub normalize_field { + my ($field) = @_; + my ($size, $type, $list, $changed) = @_; + + $size = $field->size; + $type = $field->data_type; + $list = $field->extra->{list} || []; + + if ( !ref $size && $size eq 0 ) { + if ( lc $type eq 'tinyint' ) { + $changed = $size != 4; + $size = 4; + } + elsif ( lc $type eq 'smallint' ) { + $changed = $size != 6; + $size = 6; + } + elsif ( lc $type eq 'mediumint' ) { + $changed = $size != 9; + $size = 9; + } + elsif ( $type =~ /^int(eger)?$/i ) { + $changed = $size != 11 || $type ne 'int'; + $type = 'int'; + $size = 11; + } + elsif ( lc $type eq 'bigint' ) { + $changed = $size != 20; + $size = 20; + } + elsif ( lc $type =~ /(float|double|decimal|numeric|real|fixed|dec)/ ) { + my $old_size = (ref $size || '') eq 'ARRAY' ? $size : []; + $changed = @$old_size != 2 + || $old_size->[0] != 8 + || $old_size->[1] != 2; + $size = [8,2]; + } + } + + if ( $type =~ /^tiny(text|blob)$/i ) { + $changed = $size != 255; + $size = 255; + } + elsif ( $type =~ /^(blob|text)$/i ) { + $changed = $size != 65_535; + $size = 65_535; + } + elsif ( $type =~ /^medium(blob|text)$/i ) { + $changed = $size != 16_777_215; + $size = 16_777_215; + } + elsif ( $type =~ /^long(blob|text)$/i ) { + $changed = $size != 4_294_967_295; + $size = 4_294_967_295; + } + + if ( $field->data_type =~ /(set|enum)/i && !$field->size ) { + my %extra = $field->extra; + my $longest = 0; + for my $len ( map { length } @{ $extra{'list'} || [] } ) { + $longest = $len if $len > $longest; + } + $changed = 1; + $size = $longest if $longest; + } + + + if ( $changed ) { + # We only want to clone the field, not *everything* + { + local $field->{table} = undef; + $field->parsed_field( dclone( $field ) ); + $field->parsed_field->{table} = $field->table; + } + $field->size( $size ); + $field->data_type( $type ); + $field->sql_data_type( $type_mapping{ lc $type } ) + if exists $type_mapping{ lc $type }; + $field->extra->{list} = $list if @$list; + } +} + 1; # -------------------------------------------------------------------