X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FSQL%2FTranslator%2FParser%2FMySQL.pm;h=01c95da1999c823821bd5db9015e10b603b57dcf;hb=9304bd8e0a9e14206ede4b508363c9738c45a7d2;hp=cd1b31be01774dfc9cd91a8257c208ec4f3c1ca3;hpb=629b76f99c2e35e96a2a5cc4fc4b7dc552dab64f;p=dbsrgits%2FSQL-Translator.git diff --git a/lib/SQL/Translator/Parser/MySQL.pm b/lib/SQL/Translator/Parser/MySQL.pm index cd1b31b..01c95da 100644 --- a/lib/SQL/Translator/Parser/MySQL.pm +++ b/lib/SQL/Translator/Parser/MySQL.pm @@ -1,7 +1,7 @@ package SQL::Translator::Parser::MySQL; # ------------------------------------------------------------------- -# $Id: MySQL.pm,v 1.12 2003-02-25 14:55:35 kycl4rk Exp $ +# $Id: MySQL.pm,v 1.32 2003-08-17 07:44:06 rossta Exp $ # ------------------------------------------------------------------- # Copyright (C) 2003 Ken Y. Clark , # darren chamberlain , @@ -123,7 +123,7 @@ Here's the word from the MySQL site use strict; use vars qw[ $DEBUG $VERSION $GRAMMAR @EXPORT_OK ]; -$VERSION = sprintf "%d.%02d", q$Revision: 1.12 $ =~ /(\d+)\.(\d+)/; +$VERSION = sprintf "%d.%02d", q$Revision: 1.32 $ =~ /(\d+)\.(\d+)/; $DEBUG = 0 unless defined $DEBUG; use Data::Dumper; @@ -138,12 +138,11 @@ $::RD_ERRORS = 1; # Make sure the parser dies when it encounters an error $::RD_WARN = 1; # Enable warnings. This will warn on unused rules &c. $::RD_HINT = 1; # Give out hints to help fix problems. -my $parser; # should we do this? There's no programmic way to - # change the grammar, so I think this is safe. - $GRAMMAR = q! -{ our ( %tables, $table_order ) } +{ + our ( %tables, $table_order ); +} # # The "eofile" rule makes the parser fail if any "statement" rule @@ -156,28 +155,37 @@ startrule : statement(s) eofile { \%tables } eofile : /^\Z/ statement : comment + | use + | set | drop | create | +use : /use/i WORD ';' + +set : /set/i /[^;]+/ ';' + drop : /drop/i WORD(s) ';' -create : create_table table_name '(' create_definition(s /,/) ')' table_option(s?) ';' +create : CREATE /database/i WORD ';' + +create : CREATE TEMPORARY(?) TABLE opt_if_not_exists(?) table_name '(' create_definition(s /,/) ')' table_option(s?) ';' { my $table_name = $item{'table_name'}; $tables{ $table_name }{'order'} = ++$table_order; $tables{ $table_name }{'table_name'} = $table_name; my $i = 1; - for my $definition ( @{ $item[4] } ) { - if ( $definition->{'type'} eq 'field' ) { + for my $definition ( @{ $item[7] } ) { + if ( $definition->{'supertype'} eq 'field' ) { + my $field_name = $definition->{'name'}; $tables{ $table_name }{'fields'}{ $field_name } = { %$definition, order => $i }; $i++; if ( $definition->{'is_primary_key'} ) { - push @{ $tables{ $table_name }{'indices'} }, + push @{ $tables{ $table_name }{'constraints'} }, { type => 'primary_key', fields => [ $field_name ], @@ -185,20 +193,36 @@ create : create_table table_name '(' create_definition(s /,/) ')' table_option(s ; } } - else { + elsif ( $definition->{'supertype'} eq 'constraint' ) { + # prob get rid of this? +# for my $field ( @{ $definition->{'fields'} } ) { +# push @{ +# $tables{$table_name}{'fields'}{$field}{'constraints'} +# }, +# $definition; +# } + + # this should be the only one needed + push @{ $tables{ $table_name }{'constraints'} }, $definition; + } + elsif ( $definition->{'supertype'} eq 'index' ) { push @{ $tables{ $table_name }{'indices'} }, $definition; } } - for my $opt ( @{ $item{'table_option'} } ) { + for my $opt ( @{ $item{'table_option(s?)'} } ) { if ( my ( $key, $val ) = each %$opt ) { $tables{ $table_name }{'table_options'}{ $key } = $val; } } + + 1; } -create : /CREATE/i unique(?) /(INDEX|KEY)/i index_name /on/i table_name '(' field_name(s /,/) ')' ';' +opt_if_not_exists : /if not exists/i + +create : CREATE UNIQUE(?) /(index|key)/i index_name /on/i table_name '(' field_name(s /,/) ')' ';' { push @{ $tables{ $item{'table_name'} }{'indices'} }, { @@ -209,30 +233,40 @@ create : /CREATE/i unique(?) /(INDEX|KEY)/i index_name /on/i table_name '(' fiel ; } -create_definition : index +create_definition : constraint + | index | field | -comment : /^\s*(?:#|-{2}).*\n/ +comment : /^\s*(?:#|-{2}).*\n/ { + my $comment = $item[1]; + $comment =~ s/^\s*(#|-{2})\s*//; + $comment =~ s/\s*$//; + $return = $comment; +} blank : /\s*/ -field : field_name data_type field_qualifier(s?) +field : comment(s?) field_name data_type field_qualifier(s?) reference_definition(?) comment(s?) { - my %qualifiers = map { %$_ } @{ $item{'field_qualifier'} || [] }; + my %qualifiers = map { %$_ } @{ $item{'field_qualifier(s?)'} || [] }; my $null = defined $item{'not_null'} ? $item{'not_null'} : 1; delete $qualifiers{'not_null'}; if ( my @type_quals = @{ $item{'data_type'}{'qualifiers'} || [] } ) { $qualifiers{ $_ } = 1 for @type_quals; } + my @comments = ( @{ $item[1] }, @{ $item[6] } ); + $return = { - type => 'field', - name => $item{'field_name'}, - data_type => $item{'data_type'}{'type'}, - size => $item{'data_type'}{'size'}, - list => $item{'data_type'}{'list'}, - null => $null, + supertype => 'field', + name => $item{'field_name'}, + data_type => $item{'data_type'}{'type'}, + size => $item{'data_type'}{'size'}, + list => $item{'data_type'}{'list'}, + null => $null, + constraints => $item{'reference_definition(?)'}, + comments => [ @comments ], %qualifiers, } } @@ -273,14 +307,49 @@ field_qualifier : unsigned } } -index : primary_key_index - | unique_index +field_qualifier : /character set/i WORD + { + $return = { + character_set => $item[2], + } + } + +reference_definition : /references/i table_name parens_field_list(?) match_type(?) on_delete_do(?) on_update_do(?) + { + $return = { + type => 'foreign_key', + reference_table => $item[2], + reference_fields => $item[3][0], + match_type => $item[4][0], + on_delete_do => $item[5][0], + on_update_do => $item[6][0], + } + } + +match_type : /match full/i { 'match_full' } + | + /match partial/i { 'match_partial' } + +on_delete_do : /on delete/i reference_option + { $item[2] } + +on_update_do : /on update/i reference_option + { $item[2] } + +reference_option: /restrict/i | + /cascade/i | + /set null/i | + /no action/i | + /set default/i + { $item[1] } + +index : normal_index | fulltext_index - | normal_index + | -table_name : WORD +table_name : NAME -field_name : WORD +field_name : NAME index_name : WORD @@ -299,6 +368,28 @@ data_type : WORD parens_value_list(s?) type_qualifier(s?) $list = []; } + unless ( @{ $size || [] } ) { + 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)?$/ ) { + $type = 'int'; + $size = [11]; + } + elsif ( lc $type eq 'bigint' ) { + $size = [20]; + } + elsif ( lc $type =~ /(float|double|decimal|numeric|real|fixed|dec)/ ) { + $size = [8,2]; + } + } + $return = { type => $type, size => $size, @@ -307,6 +398,9 @@ data_type : WORD parens_value_list(s?) type_qualifier(s?) } } +parens_field_list : '(' field_name(s /,/) ')' + { $item[2] } + parens_value_list : '(' VALUE(s /,/) ')' { $item[2] } @@ -315,22 +409,13 @@ type_qualifier : /(BINARY|UNSIGNED|ZEROFILL)/i field_type : WORD -field_size : '(' num_range ')' { $item{'num_range'} } - -num_range : DIGITS ',' DIGITS - { $return = $item[1].','.$item[3] } - | DIGITS - { $return = $item[1] } - -create_table : /create/i /table/i - create_index : /create/i /index/i not_null : /not/i /null/i { $return = 0 } unsigned : /unsigned/i { $return = 0 } -default_val : /default/i /(?:')?[\w\d.-]*(?:')?/ +default_val : /default/i /(?:')?[\w\d:.-]*(?:')?/ { $item[2] =~ s/'//g; $return = $item[2]; @@ -340,56 +425,82 @@ auto_inc : /auto_increment/i { 1 } primary_key : /primary/i /key/i { 1 } -primary_key_index : primary_key index_name(?) '(' field_name(s /,/) ')' +constraint : primary_key_def + | unique_key_def + | foreign_key_def + | + +foreign_key_def : opt_constraint(?) /foreign key/i WORD(?) parens_field_list reference_definition + { + $return = { + supertype => 'constraint', + type => 'foreign_key', + name => $item[3][0], + fields => $item[4], + %{ $item{'reference_definition'} }, + } + } + +opt_constraint : /constraint/i WORD + +primary_key_def : primary_key index_name(?) '(' name_with_opt_paren(s /,/) ')' { - $return = { - name => $item{'index_name'}[0], - type => 'primary_key', - fields => $item[4], - } + $return = { + supertype => 'constraint', + name => $item{'index_name(?)'}[0], + type => 'primary_key', + fields => $item[4], + }; } -normal_index : key index_name(?) '(' name_with_opt_paren(s /,/) ')' +unique_key_def : UNIQUE KEY(?) index_name(?) '(' name_with_opt_paren(s /,/) ')' { - $return = { - name => $item{'index_name'}[0], - type => 'normal', - fields => $item[4], + $return = { + supertype => 'constraint', + name => $item{'index_name(?)'}[0], + type => 'unique', + fields => $item[5], } } -unique_index : unique key(?) index_name(?) '(' name_with_opt_paren(s /,/) ')' +normal_index : KEY index_name(?) '(' name_with_opt_paren(s /,/) ')' { - $return = { - name => $item{'index_name'}[0], - type => 'unique', - fields => $item[5], + $return = { + supertype => 'index', + type => 'normal', + name => $item{'index_name(?)'}[0], + fields => $item[4], } } -fulltext_index : fulltext key(?) index_name(?) '(' name_with_opt_paren(s /,/) ')' +fulltext_index : /fulltext/i KEY(?) index_name(?) '(' name_with_opt_paren(s /,/) ')' { - $return = { - name => $item{'index_name'}[0], - type => 'fulltext', - fields => $item[5], + $return = { + supertype => 'index', + type => 'fulltext', + 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] } -fulltext : /fulltext/i { 1 } +UNIQUE : /unique/i { 1 } -unique : /unique/i { 1 } - -key : /key/i | /index/i +KEY : /key/i | /index/i table_option : /[^\s;]*/ { $return = { split /=/, $item[1] } } +CREATE : /create/i + +TEMPORARY : /temporary/i + +TABLE : /table/i + WORD : /\w+/ DIGITS : /\d+/ @@ -403,8 +514,13 @@ NAME : "`" /\w+/ "`" VALUE : /[-+]?\.?\d+(?:[eE]\d+)?/ { $item[1] } - | /'.*?'/ # XXX doesn't handle embedded quotes - { $item[1] } + | /'.*?'/ + { + # remove leading/trailing quotes + my $val = $item[1]; + $val =~ s/^['"]|['"]$//g; + $return = $val; + } | /NULL/ { 'NULL' } @@ -413,10 +529,10 @@ VALUE : /[-+]?\.?\d+(?:[eE]\d+)?/ # ------------------------------------------------------------------- sub parse { my ( $translator, $data ) = @_; - $parser ||= Parse::RecDescent->new($GRAMMAR); + my $parser = Parse::RecDescent->new($GRAMMAR); - $::RD_TRACE = $translator->trace ? 1 : undef; - $DEBUG = $translator->debug; + local $::RD_TRACE = $translator->trace ? 1 : undef; + local $DEBUG = $translator->debug; unless (defined $parser) { return $translator->error("Error instantiating Parse::RecDescent ". @@ -424,27 +540,110 @@ sub parse { } my $result = $parser->startrule($data); - die "Parse failed.\n" unless defined $result; - warn Dumper($result) if $DEBUG; - return $result; + return $translator->error( "Parse failed." ) unless defined $result; + warn Dumper( $result ) if $DEBUG; + + my $schema = $translator->schema; + my @tables = sort { + $result->{ $a }->{'order'} <=> $result->{ $b }->{'order'} + } keys %{ $result }; + + for my $table_name ( @tables ) { + my $tdata = $result->{ $table_name }; + my $table = $schema->add_table( + name => $tdata->{'table_name'}, + ) or die $schema->error; + +# for my $opt ( @{ $tdata->{'table_options'} } ) { +# if ( my ( $key, $val ) = each %$opt ) { +# $tables->options( +# } +# } + + my @fields = sort { + $tdata->{'fields'}->{$a}->{'order'} + <=> + $tdata->{'fields'}->{$b}->{'order'} + } keys %{ $tdata->{'fields'} }; + + for my $fname ( @fields ) { + my $fdata = $tdata->{'fields'}{ $fname }; + my $field = $table->add_field( + name => $fdata->{'name'}, + data_type => $fdata->{'data_type'}, + size => $fdata->{'size'}, + default_value => $fdata->{'default'}, + is_auto_increment => $fdata->{'is_auto_inc'}, + is_nullable => $fdata->{'null'}, + comments => $fdata->{'comments'}, + ) or die $table->error; + + $table->primary_key( $field->name ) if $fdata->{'is_primary_key'}; + + for my $qual ( qw[ binary unsigned zerofill list ] ) { + if ( my $val = $fdata->{ $qual } || $fdata->{ uc $qual } ) { + next if ref $val eq 'ARRAY' && !@$val; + $field->extra( $qual, $val ); + } + } + + 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'} || [] } ) { + my $index = $table->add_index( + name => $idata->{'name'}, + type => uc $idata->{'type'}, + fields => $idata->{'fields'}, + ) or die $table->error; + } + + for my $cdata ( @{ $tdata->{'constraints'} || [] } ) { + my $constraint = $table->add_constraint( + name => $cdata->{'name'}, + type => $cdata->{'type'}, + fields => $cdata->{'fields'}, + reference_table => $cdata->{'reference_table'}, + reference_fields => $cdata->{'reference_fields'}, + match_type => $cdata->{'match_type'} || '', + on_delete => $cdata->{'on_delete_do'}, + on_update => $cdata->{'on_update_do'}, + ) or die $table->error; + } + } + + return 1; } 1; -#----------------------------------------------------- +# ------------------------------------------------------------------- # Where man is not nature is barren. # William Blake -#----------------------------------------------------- +# ------------------------------------------------------------------- =pod =head1 AUTHOR Ken Y. Clark Ekclark@cpan.orgE, -Chris Mungall +Chris Mungall Ecjm@fruitfly.orgE. =head1 SEE ALSO -perl(1), Parse::RecDescent. +perl(1), Parse::RecDescent, SQL::Translator::Schema. =cut