1 package SQL::Translator::Parser::MySQL;
5 SQL::Translator::Parser::MySQL - parser for MySQL
10 use SQL::Translator::Parser::MySQL;
12 my $translator = SQL::Translator->new;
13 $translator->parser("SQL::Translator::Parser::MySQL");
17 The grammar is influenced heavily by Tim Bunce's "mysql2ora" grammar.
19 Here's the word from the MySQL site
20 (http://www.mysql.com/doc/en/CREATE_TABLE.html):
22 CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name [(create_definition,...)]
23 [table_options] [select_statement]
27 CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name LIKE old_table_name;
30 col_name type [NOT NULL | NULL] [DEFAULT default_value] [AUTO_INCREMENT]
31 [PRIMARY KEY] [reference_definition]
32 or PRIMARY KEY (index_col_name,...)
33 or KEY [index_name] (index_col_name,...)
34 or INDEX [index_name] (index_col_name,...)
35 or UNIQUE [INDEX] [index_name] (index_col_name,...)
36 or FULLTEXT [INDEX] [index_name] (index_col_name,...)
37 or [CONSTRAINT symbol] FOREIGN KEY [index_name] (index_col_name,...)
38 [reference_definition]
42 TINYINT[(length)] [UNSIGNED] [ZEROFILL]
43 or SMALLINT[(length)] [UNSIGNED] [ZEROFILL]
44 or MEDIUMINT[(length)] [UNSIGNED] [ZEROFILL]
45 or INT[(length)] [UNSIGNED] [ZEROFILL]
46 or INTEGER[(length)] [UNSIGNED] [ZEROFILL]
47 or BIGINT[(length)] [UNSIGNED] [ZEROFILL]
48 or REAL[(length,decimals)] [UNSIGNED] [ZEROFILL]
49 or DOUBLE[(length,decimals)] [UNSIGNED] [ZEROFILL]
50 or FLOAT[(length,decimals)] [UNSIGNED] [ZEROFILL]
51 or DECIMAL(length,decimals) [UNSIGNED] [ZEROFILL]
52 or NUMERIC(length,decimals) [UNSIGNED] [ZEROFILL]
53 or CHAR(length) [BINARY]
54 or VARCHAR(length) [BINARY]
67 or ENUM(value1,value2,value3,...)
68 or SET(value1,value2,value3,...)
74 REFERENCES tbl_name [(index_col_name,...)]
75 [MATCH FULL | MATCH PARTIAL]
76 [ON DELETE reference_option]
77 [ON UPDATE reference_option]
80 RESTRICT | CASCADE | SET NULL | NO ACTION | SET DEFAULT
83 TYPE = {BDB | HEAP | ISAM | InnoDB | MERGE | MRG_MYISAM | MYISAM }
84 or ENGINE = {BDB | HEAP | ISAM | InnoDB | MERGE | MRG_MYISAM | MYISAM }
87 or [ DEFAULT ] CHARACTER SET charset_name
89 or COLLATE collation_name
93 or PACK_KEYS = {0 | 1 | DEFAULT}
94 or PASSWORD = "string"
95 or DELAY_KEY_WRITE = {0 | 1}
96 or ROW_FORMAT= { default | dynamic | fixed | compressed }
97 or RAID_TYPE= {1 | STRIPED | RAID0 } RAID_CHUNKS=# RAID_CHUNKSIZE=#
98 or UNION = (table_name,[table_name...])
99 or INSERT_METHOD= {NO | FIRST | LAST }
100 or DATA DIRECTORY="absolute path to directory"
101 or INDEX DIRECTORY="absolute path to directory"
104 A subset of the ALTER TABLE syntax that allows addition of foreign keys:
106 ALTER [IGNORE] TABLE tbl_name alter_specification [, alter_specification] ...
109 ADD [CONSTRAINT [symbol]]
110 FOREIGN KEY [index_name] (index_col_name,...)
111 [reference_definition]
113 A subset of INSERT that we ignore:
119 This parser takes a single optional parser_arg C<mysql_parser_version>, which
120 provides the desired version for the target database. Any statement in the processed
121 dump file, that is commented with a version higher than the one supplied, will be stripped.
123 The default C<mysql_parser_version> is set to the conservative value of 40000 (MySQL 4.0)
125 Valid version specifiers for C<mysql_parser_version> are listed L<here|SQL::Translator::Utils/parse_mysql_version>
127 More information about the MySQL comment-syntax: L<http://dev.mysql.com/doc/refman/5.0/en/comments.html>
135 our $VERSION = '1.59';
138 $DEBUG = 0 unless defined $DEBUG;
141 use Storable qw(dclone);
142 use DBI qw(:sql_types);
143 use SQL::Translator::Utils qw/parse_mysql_version ddl_parser_instance/;
145 use base qw(Exporter);
146 our @EXPORT_OK = qw(parse);
148 our %type_mapping = ();
150 use constant DEFAULT_PARSER_VERSION => 40000;
152 our $GRAMMAR = << 'END_OF_GRAMMAR';
155 my ( $database_name, %tables, $table_order, @table_comments, %views,
156 $view_order, %procedures, $proc_order );
161 # The "eofile" rule makes the parser fail if any "statement" rule
162 # fails. Otherwise, the first successful match by a "statement"
163 # won't cause the failure needed to know that the parse, as a whole,
166 startrule : statement(s) eofile {
168 database_name => $database_name,
171 procedures => \%procedures,
188 use : /use/i WORD "$delimiter"
190 $database_name = $item[2];
191 @table_comments = ();
194 set : /set/i not_delimiter "$delimiter"
195 { @table_comments = () }
197 drop : /drop/i TABLE not_delimiter "$delimiter"
199 drop : /drop/i WORD(s) "$delimiter"
200 { @table_comments = () }
207 # MySQL strings, unlike common SQL strings, can be double-quoted or
208 # single-quoted, and you can escape the delmiters by doubling (but only the
209 # delimiter) or by backslashing.
211 /'(\\.|''|[^\\\'])*'/ |
212 /"(\\.|""|[^\\\"])*"/
213 # For reference, std sql str: /(?:(?:\')(?:[^\']*(?:(?:\'\')[^\']*)*)(?:\'))//
215 nonstring : /[^;\'"]+/
217 statement_body : string | nonstring
219 insert : /insert/i statement_body(s?) "$delimiter"
221 delimiter : /delimiter/i /[\S]+/
222 { $delimiter = $item[2] }
224 empty_statement : "$delimiter"
226 alter : ALTER TABLE table_name alter_specification(s /,/) "$delimiter"
228 my $table_name = $item{'table_name'};
229 die "Cannot ALTER table '$table_name'; it does not exist"
230 unless $tables{ $table_name };
231 for my $definition ( @{ $item[4] } ) {
232 $definition->{'extra'}->{'alter'} = 1;
233 push @{ $tables{ $table_name }{'constraints'} }, $definition;
237 alter_specification : ADD foreign_key_def
238 { $return = $item[2] }
240 create : CREATE /database/i WORD "$delimiter"
241 { @table_comments = () }
243 create : CREATE TEMPORARY(?) TABLE opt_if_not_exists(?) table_name '(' create_definition(s /,/) /(,\s*)?\)/ table_option(s?) "$delimiter"
245 my $table_name = $item{'table_name'};
246 die "There is more than one definition for $table_name"
247 if ($tables{$table_name});
249 $tables{ $table_name }{'order'} = ++$table_order;
250 $tables{ $table_name }{'table_name'} = $table_name;
252 if ( @table_comments ) {
253 $tables{ $table_name }{'comments'} = [ @table_comments ];
254 @table_comments = ();
258 for my $definition ( @{ $item[7] } ) {
259 if ( $definition->{'supertype'} eq 'field' ) {
260 my $field_name = $definition->{'name'};
261 $tables{ $table_name }{'fields'}{ $field_name } =
262 { %$definition, order => $i };
265 if ( $definition->{'is_primary_key'} ) {
266 push @{ $tables{ $table_name }{'constraints'} },
268 type => 'primary_key',
269 fields => [ $field_name ],
274 elsif ( $definition->{'supertype'} eq 'constraint' ) {
275 push @{ $tables{ $table_name }{'constraints'} }, $definition;
277 elsif ( $definition->{'supertype'} eq 'index' ) {
278 push @{ $tables{ $table_name }{'indices'} }, $definition;
282 if ( my @options = @{ $item{'table_option(s?)'} } ) {
283 for my $option ( @options ) {
284 my ( $key, $value ) = each %$option;
285 if ( $key eq 'comment' ) {
286 push @{ $tables{ $table_name }{'comments'} }, $value;
289 push @{ $tables{ $table_name }{'table_options'} }, $option;
297 opt_if_not_exists : /if not exists/i
299 create : CREATE UNIQUE(?) /(index|key)/i index_name /on/i table_name '(' field_name(s /,/) ')' "$delimiter"
301 @table_comments = ();
302 push @{ $tables{ $item{'table_name'} }{'indices'} },
305 type => $item[2][0] ? 'unique' : 'normal',
311 create : CREATE /trigger/i NAME not_delimiter "$delimiter"
313 @table_comments = ();
316 create : CREATE PROCEDURE NAME not_delimiter "$delimiter"
318 @table_comments = ();
319 my $func_name = $item[3];
321 my $sql = "$item[1] $item[2] $item[3] $item[4]";
323 $procedures{ $func_name }{'order'} = ++$proc_order;
324 $procedures{ $func_name }{'name'} = $func_name;
325 $procedures{ $func_name }{'owner'} = $owner;
326 $procedures{ $func_name }{'sql'} = $sql;
329 PROCEDURE : /procedure/i
332 create : CREATE or_replace(?) create_view_option(s?) /view/i NAME /as/i view_select_statement "$delimiter"
334 @table_comments = ();
335 my $view_name = $item{'NAME'};
336 my $select_sql = $item{'view_select_statement'};
337 my $options = $item{'create_view_option(s?)'};
340 grep { defined and length }
341 map { ref $_ eq 'ARRAY' ? @$_ : $_ }
343 $item{'or_replace(?)'},
351 $_->{'alias'} ? ' as ' . $_->{'alias'} : ''
354 @{ $select_sql->{'columns'} || [] }
361 $_->{'alias'} ? ' as ' . $_->{'alias'} : ''
364 @{ $select_sql->{'from'}{'tables'} || [] }
366 $select_sql->{'from'}{'where'}
367 ? 'where ' . $select_sql->{'from'}{'where'}
372 # Hack to strip database from function calls in SQL
373 $sql =~ s#`\w+`\.(`\w+`\()##g;
375 $views{ $view_name }{'order'} = ++$view_order;
376 $views{ $view_name }{'name'} = $view_name;
377 $views{ $view_name }{'sql'} = $sql;
378 $views{ $view_name }{'options'} = $options;
379 $views{ $view_name }{'select'} = $item{'view_select_statement'};
382 create_view_option : view_algorithm | view_sql_security | view_definer
384 or_replace : /or replace/i
386 view_algorithm : /algorithm/i /=/ WORD
388 $return = "$item[1]=$item[3]";
391 view_definer : /definer=\S+/i
393 view_sql_security : /sql \s+ security \s+ (definer|invoker)/ixs
395 not_delimiter : /.*?(?=$delimiter)/is
397 view_select_statement : /[(]?/ /select/i view_column_def /from/i view_table_def /[)]?/
400 columns => $item{'view_column_def'},
401 from => $item{'view_table_def'},
405 view_column_def : /(.*?)(?=\bfrom\b)/ixs
407 # split on commas not in parens,
408 # e.g., "concat_ws(\' \', first, last) as first_last"
409 my @tmp = $1 =~ /((?:[^(,]+|\(.*?\))+)/g;
411 for my $col ( @tmp ) {
412 my ( $name, $alias ) = map {
416 } split /\s+as\s+/i, $col;
418 push @cols, { name => $name, alias => $alias || '' };
424 not_delimiter : /.*?(?=$delimiter)/is
426 view_table_def : not_delimiter
428 my $clause = $item[1];
429 my $where = $1 if $clause =~ s/\bwhere \s+ (.*)//ixs;
430 $clause =~ s/[)]\s*$//;
433 for my $tbl ( split( /\s*,\s*/, $clause ) ) {
434 my ( $name, $alias ) = split /\s+as\s+/i, $tbl;
435 push @tables, { name => $name, alias => $alias || '' };
440 where => $where || '',
444 view_column_alias : /as/i WORD
445 { $return = $item[2] }
447 create_definition : constraint
453 comment : /^\s*(?:#|-{2}).*\n/
455 my $comment = $item[1];
456 $comment =~ s/^\s*(#|--)\s*//;
457 $comment =~ s/\s*$//;
461 comment : m{ / \* (?! \!) .*? \* / }xs
463 my $comment = $item[2];
464 $comment = substr($comment, 0, -2);
465 $comment =~ s/^\s*|\s*$//g;
469 comment_like_command : m{/\*!(\d+)?}s
471 comment_end : m{ \* / }xs
473 field_comment : /^\s*(?:#|-{2}).*\n/
475 my $comment = $item[1];
476 $comment =~ s/^\s*(#|--)\s*//;
477 $comment =~ s/\s*$//;
482 field_comment2 : /comment/i /'.*?'/
484 my $comment = $item[2];
492 field : field_comment(s?) field_name data_type field_qualifier(s?) field_comment2(?) reference_definition(?) on_update(?) field_comment(s?)
494 my %qualifiers = map { %$_ } @{ $item{'field_qualifier(s?)'} || [] };
495 if ( my @type_quals = @{ $item{'data_type'}{'qualifiers'} || [] } ) {
496 $qualifiers{ $_ } = 1 for @type_quals;
499 my $null = defined $qualifiers{'not_null'}
500 ? $qualifiers{'not_null'} : 1;
501 delete $qualifiers{'not_null'};
503 my @comments = ( @{ $item[1] }, @{ $item[5] }, @{ $item[8] } );
506 supertype => 'field',
507 name => $item{'field_name'},
508 data_type => $item{'data_type'}{'type'},
509 size => $item{'data_type'}{'size'},
510 list => $item{'data_type'}{'list'},
512 constraints => $item{'reference_definition(?)'},
513 comments => [ @comments ],
519 field_qualifier : not_null
522 null => $item{'not_null'},
526 field_qualifier : default_val
529 default => $item{'default_val'},
533 field_qualifier : auto_inc
536 is_auto_inc => $item{'auto_inc'},
540 field_qualifier : primary_key
543 is_primary_key => $item{'primary_key'},
547 field_qualifier : unsigned
550 is_unsigned => $item{'unsigned'},
554 field_qualifier : /character set/i WORD
557 'CHARACTER SET' => $item[2],
561 field_qualifier : /collate/i WORD
568 field_qualifier : /on update/i CURRENT_TIMESTAMP
571 'ON UPDATE' => $item[2],
575 field_qualifier : /unique/i KEY(?)
582 field_qualifier : KEY
589 reference_definition : /references/i table_name parens_field_list(?) match_type(?) on_delete(?) on_update(?)
592 type => 'foreign_key',
593 reference_table => $item[2],
594 reference_fields => $item[3][0],
595 match_type => $item[4][0],
596 on_delete => $item[5][0],
597 on_update => $item[6][0],
601 match_type : /match full/i { 'full' }
603 /match partial/i { 'partial' }
605 on_delete : /on delete/i reference_option
609 /on update/i CURRENT_TIMESTAMP
612 /on update/i reference_option
615 reference_option: /restrict/i |
633 data_type : WORD parens_value_list(s?) type_qualifier(s?)
636 my $size; # field size, applicable only to non-set fields
637 my $list; # set list, applicable only to sets (duh)
639 if ( uc($type) =~ /^(SET|ENUM)$/ ) {
653 qualifiers => $item[3],
657 parens_field_list : '(' field_name(s /,/) ')'
660 parens_value_list : '(' VALUE(s /,/) ')'
663 type_qualifier : /(BINARY|UNSIGNED|ZEROFILL)/i
668 create_index : /create/i /index/i
670 not_null : /not/i /null/i
676 unsigned : /unsigned/i { $return = 0 }
679 /default/i CURRENT_TIMESTAMP
686 $item[2] =~ s/^\s*'|'\s*$//g or $item[2] =~ s/^\s*"|"\s*$//g;
692 $item[2] =~ s/b['"]([01]+)['"]/$1/g;
696 /default/i /[\w\d:.-]+/
701 auto_inc : /auto_increment/i { 1 }
703 primary_key : /primary/i /key/i { 1 }
705 constraint : primary_key_def
710 foreign_key_def : foreign_key_def_begin parens_field_list reference_definition
713 supertype => 'constraint',
714 type => 'foreign_key',
717 %{ $item{'reference_definition'} },
721 foreign_key_def_begin : /constraint/i /foreign key/i WORD
722 { $return = $item[3] }
724 /constraint/i NAME /foreign key/i
725 { $return = $item[2] }
727 /constraint/i /foreign key/i
731 { $return = $item[2] }
736 primary_key_def : primary_key index_type(?) '(' name_with_opt_paren(s /,/) ')' index_type(?)
739 supertype => 'constraint',
740 type => 'primary_key',
742 options => $item[2][0] || $item[6][0],
745 # In theory, and according to the doc, names should not be allowed here, but
746 # MySQL accept (and ignores) them, so we are not going to be less :)
747 | primary_key index_name_not_using(?) '(' name_with_opt_paren(s /,/) ')' index_type(?)
750 supertype => 'constraint',
751 type => 'primary_key',
753 options => $item[6][0],
757 unique_key_def : UNIQUE KEY(?) index_name_not_using(?) index_type(?) '(' name_with_opt_paren(s /,/) ')' index_type(?)
760 supertype => 'constraint',
764 options => $item[4][0] || $item[8][0],
768 normal_index : KEY index_name_not_using(?) index_type(?) '(' name_with_opt_paren(s /,/) ')' index_type(?)
771 supertype => 'index',
775 options => $item[3][0] || $item[7][0],
779 index_name_not_using : QUOTED_NAME
780 | /(\b(?!using)\w+\b)/ { $return = ($1 =~ /^using/i) ? undef : $1 }
782 index_type : /using (btree|hash|rtree)/i { $return = uc $1 }
784 fulltext_index : /fulltext/i KEY(?) index_name(?) '(' name_with_opt_paren(s /,/) ')'
787 supertype => 'index',
789 name => $item{'index_name(?)'}[0],
794 spatial_index : /spatial/i KEY(?) index_name(?) '(' name_with_opt_paren(s /,/) ')'
797 supertype => 'index',
799 name => $item{'index_name(?)'}[0],
804 name_with_opt_paren : NAME parens_value_list(s?)
805 { $item[2][0] ? "$item[1]($item[2][0][0])" : $item[1] }
809 KEY : /key/i | /index/i
811 table_option : /comment/i /=/ /'.*?'/
813 my $comment = $item[3];
816 $return = { comment => $comment };
818 | /(default )?(charset|character set)/i /\s*=?\s*/ WORD
820 $return = { 'CHARACTER SET' => $item[3] };
824 $return = { 'COLLATE' => $item[2] }
826 | /union/i /\s*=\s*/ '(' table_name(s /,/) ')'
828 $return = { $item[1] => $item[4] };
830 | WORD /\s*=\s*/ MAYBE_QUOTED_WORD
832 $return = { $item[1] => $item[3] };
835 MAYBE_QUOTED_WORD: /\w+/
849 TEMPORARY : /temporary/i
865 QUOTED_NAME : BACKTICK /[^`]+/ BACKTICK
867 | DOUBLE_QUOTE /[^"]+/ DOUBLE_QUOTE
869 | SINGLE_QUOTE /[^']+/ SINGLE_QUOTE
875 VALUE : /[-+]?\.?\d+(?:[eE]\d+)?/
879 # remove leading/trailing quotes
881 $val =~ s/^['"]|['"]$//g;
887 # always a scalar-ref, so that it is treated as a function and not quoted by consumers
889 /current_timestamp(\(\))?/i { \'CURRENT_TIMESTAMP' }
890 | /now\(\)/i { \'CURRENT_TIMESTAMP' }
895 my ( $translator, $data ) = @_;
897 # Enable warnings within the Parse::RecDescent module.
898 # Make sure the parser dies when it encounters an error
899 local $::RD_ERRORS = 1 unless defined $::RD_ERRORS;
900 # Enable warnings. This will warn on unused rules &c.
901 local $::RD_WARN = 1 unless defined $::RD_WARN;
902 # Give out hints to help fix problems.
903 local $::RD_HINT = 1 unless defined $::RD_HINT;
904 local $::RD_TRACE = $translator->trace ? 1 : undef;
905 local $DEBUG = $translator->debug;
907 my $parser = ddl_parser_instance('MySQL');
909 # Preprocess for MySQL-specific and not-before-version comments
911 my $parser_version = parse_mysql_version(
912 $translator->parser_args->{mysql_parser_version}, 'mysql'
913 ) || DEFAULT_PARSER_VERSION;
916 s#/\*!(\d{5})?(.*?)\*/#($1 && $1 > $parser_version ? '' : $2)#es
918 # do nothing; is there a better way to write this? -- ky
921 my $result = $parser->startrule($data);
922 return $translator->error( "Parse failed." ) unless defined $result;
923 warn "Parse result:".Dumper( $result ) if $DEBUG;
925 my $schema = $translator->schema;
926 $schema->name($result->{'database_name'}) if $result->{'database_name'};
929 $result->{'tables'}{ $a }{'order'}
931 $result->{'tables'}{ $b }{'order'}
932 } keys %{ $result->{'tables'} };
934 for my $table_name ( @tables ) {
935 my $tdata = $result->{tables}{ $table_name };
936 my $table = $schema->add_table(
937 name => $tdata->{'table_name'},
938 ) or die $schema->error;
940 $table->comments( $tdata->{'comments'} );
943 $tdata->{'fields'}->{$a}->{'order'}
945 $tdata->{'fields'}->{$b}->{'order'}
946 } keys %{ $tdata->{'fields'} };
948 for my $fname ( @fields ) {
949 my $fdata = $tdata->{'fields'}{ $fname };
950 my $field = $table->add_field(
951 name => $fdata->{'name'},
952 data_type => $fdata->{'data_type'},
953 size => $fdata->{'size'},
954 default_value => $fdata->{'default'},
955 is_auto_increment => $fdata->{'is_auto_inc'},
956 is_nullable => $fdata->{'null'},
957 comments => $fdata->{'comments'},
958 ) or die $table->error;
960 $table->primary_key( $field->name ) if $fdata->{'is_primary_key'};
962 for my $qual ( qw[ binary unsigned zerofill list collate ],
963 'character set', 'on update' ) {
964 if ( my $val = $fdata->{ $qual } || $fdata->{ uc $qual } ) {
965 next if ref $val eq 'ARRAY' && !@$val;
966 $field->extra( $qual, $val );
970 if ( $fdata->{'has_index'} ) {
974 fields => $fdata->{'name'},
975 ) or die $table->error;
978 if ( $fdata->{'is_unique'} ) {
979 $table->add_constraint(
982 fields => $fdata->{'name'},
983 ) or die $table->error;
986 for my $cdata ( @{ $fdata->{'constraints'} } ) {
987 next unless $cdata->{'type'} eq 'foreign_key';
988 $cdata->{'fields'} ||= [ $field->name ];
989 push @{ $tdata->{'constraints'} }, $cdata;
994 for my $idata ( @{ $tdata->{'indices'} || [] } ) {
995 my $index = $table->add_index(
996 name => $idata->{'name'},
997 type => uc $idata->{'type'},
998 fields => $idata->{'fields'},
999 ) or die $table->error;
1002 if ( my @options = @{ $tdata->{'table_options'} || [] } ) {
1003 my @cleaned_options;
1004 my @ignore_opts = $translator->parser_args->{'ignore_opts'}
1005 ? split( /,/, $translator->parser_args->{'ignore_opts'} )
1008 my $ignores = { map { $_ => 1 } @ignore_opts };
1009 foreach my $option (@options) {
1010 # make sure the option isn't in ignore list
1011 my ($option_key) = keys %$option;
1012 if ( !exists $ignores->{$option_key} ) {
1013 push @cleaned_options, $option;
1017 @cleaned_options = @options;
1019 $table->options( \@cleaned_options ) or die $table->error;
1022 for my $cdata ( @{ $tdata->{'constraints'} || [] } ) {
1023 my $constraint = $table->add_constraint(
1024 name => $cdata->{'name'},
1025 type => $cdata->{'type'},
1026 fields => $cdata->{'fields'},
1027 reference_table => $cdata->{'reference_table'},
1028 reference_fields => $cdata->{'reference_fields'},
1029 match_type => $cdata->{'match_type'} || '',
1030 on_delete => $cdata->{'on_delete'}
1031 || $cdata->{'on_delete_do'},
1032 on_update => $cdata->{'on_update'}
1033 || $cdata->{'on_update_do'},
1034 ) or die $table->error;
1037 # After the constrains and PK/idxs have been created,
1038 # we normalize fields
1039 normalize_field($_) for $table->get_fields;
1042 my @procedures = sort {
1043 $result->{procedures}->{ $a }->{'order'}
1045 $result->{procedures}->{ $b }->{'order'}
1046 } keys %{ $result->{procedures} };
1048 for my $proc_name ( @procedures ) {
1049 $schema->add_procedure(
1051 owner => $result->{procedures}->{$proc_name}->{owner},
1052 sql => $result->{procedures}->{$proc_name}->{sql},
1057 $result->{views}->{ $a }->{'order'}
1059 $result->{views}->{ $b }->{'order'}
1060 } keys %{ $result->{views} };
1062 for my $view_name ( @views ) {
1063 my $view = $result->{'views'}{ $view_name };
1064 my @flds = map { $_->{'alias'} || $_->{'name'} }
1065 @{ $view->{'select'}{'columns'} || [] };
1066 my @from = map { $_->{'alias'} || $_->{'name'} }
1067 @{ $view->{'from'}{'tables'} || [] };
1071 sql => $view->{'sql'},
1072 order => $view->{'order'},
1075 options => $view->{'options'}
1082 # Takes a field, and returns
1083 sub normalize_field {
1085 my ($size, $type, $list, $unsigned, $changed);
1087 $size = $field->size;
1088 $type = $field->data_type;
1089 $list = $field->extra->{list} || [];
1090 $unsigned = defined($field->extra->{unsigned});
1092 if ( !ref $size && $size eq 0 ) {
1093 if ( lc $type eq 'tinyint' ) {
1094 $changed = $size != 4 - $unsigned;
1095 $size = 4 - $unsigned;
1097 elsif ( lc $type eq 'smallint' ) {
1098 $changed = $size != 6 - $unsigned;
1099 $size = 6 - $unsigned;
1101 elsif ( lc $type eq 'mediumint' ) {
1102 $changed = $size != 9 - $unsigned;
1103 $size = 9 - $unsigned;
1105 elsif ( $type =~ /^int(eger)?$/i ) {
1106 $changed = $size != 11 - $unsigned || $type ne 'int';
1108 $size = 11 - $unsigned;
1110 elsif ( lc $type eq 'bigint' ) {
1111 $changed = $size != 20;
1114 elsif ( lc $type =~ /(float|double|decimal|numeric|real|fixed|dec)/ ) {
1115 my $old_size = (ref $size || '') eq 'ARRAY' ? $size : [];
1116 $changed = @$old_size != 2
1117 || $old_size->[0] != 8
1118 || $old_size->[1] != 2;
1123 if ( $type =~ /^tiny(text|blob)$/i ) {
1124 $changed = $size != 255;
1127 elsif ( $type =~ /^(blob|text)$/i ) {
1128 $changed = $size != 65_535;
1131 elsif ( $type =~ /^medium(blob|text)$/i ) {
1132 $changed = $size != 16_777_215;
1135 elsif ( $type =~ /^long(blob|text)$/i ) {
1136 $changed = $size != 4_294_967_295;
1137 $size = 4_294_967_295;
1140 if ( $field->data_type =~ /(set|enum)/i && !$field->size ) {
1141 my %extra = $field->extra;
1143 for my $len ( map { length } @{ $extra{'list'} || [] } ) {
1144 $longest = $len if $len > $longest;
1147 $size = $longest if $longest;
1152 # We only want to clone the field, not *everything*
1154 local $field->{table} = undef;
1155 $field->parsed_field( dclone( $field ) );
1156 $field->parsed_field->{table} = $field->table;
1158 $field->size( $size );
1159 $field->data_type( $type );
1160 $field->sql_data_type( $type_mapping{ lc $type } )
1161 if exists $type_mapping{ lc $type };
1162 $field->extra->{list} = $list if @$list;
1168 # -------------------------------------------------------------------
1169 # Where man is not nature is barren.
1171 # -------------------------------------------------------------------
1177 Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>,
1178 Chris Mungall E<lt>cjm@fruitfly.orgE<gt>.
1182 Parse::RecDescent, SQL::Translator::Schema.