1 package SQL::Translator::Parser::PostgreSQL;
3 # -------------------------------------------------------------------
5 # -------------------------------------------------------------------
6 # Copyright (C) 2002-4 SQLFairy Authors
8 # This program is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License as
10 # published by the Free Software Foundation; version 2.
12 # This program is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 # -------------------------------------------------------------------
25 SQL::Translator::Parser::PostgreSQL - parser for PostgreSQL
30 use SQL::Translator::Parser::PostgreSQL;
32 my $translator = SQL::Translator->new;
33 $translator->parser("SQL::Translator::Parser::PostgreSQL");
37 The grammar was started from the MySQL parsers. Here is the description
41 (http://www.postgresql.org/docs/view.php?version=7.3&idoc=1&file=sql-createtable.html)
43 CREATE [ [ LOCAL ] { TEMPORARY | TEMP } ] TABLE table_name (
44 { column_name data_type [ DEFAULT default_expr ]
45 [ column_constraint [, ... ] ]
46 | table_constraint } [, ... ]
48 [ INHERITS ( parent_table [, ... ] ) ]
49 [ WITH OIDS | WITHOUT OIDS ]
51 where column_constraint is:
53 [ CONSTRAINT constraint_name ]
54 { NOT NULL | NULL | UNIQUE | PRIMARY KEY |
56 REFERENCES reftable [ ( refcolumn ) ] [ MATCH FULL | MATCH PARTIAL ]
57 [ ON DELETE action ] [ ON UPDATE action ] }
58 [ DEFERRABLE | NOT DEFERRABLE ]
59 [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]
61 and table_constraint is:
63 [ CONSTRAINT constraint_name ]
64 { UNIQUE ( column_name [, ... ] ) |
65 PRIMARY KEY ( column_name [, ... ] ) |
66 CHECK ( expression ) |
67 FOREIGN KEY ( column_name [, ... ] )
68 REFERENCES reftable [ ( refcolumn [, ... ] ) ]
69 [ MATCH FULL | MATCH PARTIAL ]
70 [ ON DELETE action ] [ ON UPDATE action ] }
71 [ DEFERRABLE | NOT DEFERRABLE ]
72 [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]
75 (http://www.postgresql.org/docs/view.php?version=7.3&idoc=1&file=sql-createindex.html)
77 CREATE [ UNIQUE ] INDEX index_name ON table
78 [ USING acc_method ] ( column [ ops_name ] [, ...] )
80 CREATE [ UNIQUE ] INDEX index_name ON table
81 [ USING acc_method ] ( func_name( column [, ... ]) [ ops_name ] )
86 ALTER TABLE [ ONLY ] table [ * ]
87 ADD [ COLUMN ] column type [ column_constraint [ ... ] ]
88 ALTER TABLE [ ONLY ] table [ * ]
89 ALTER [ COLUMN ] column { SET DEFAULT value | DROP DEFAULT }
90 ALTER TABLE [ ONLY ] table [ * ]
91 ALTER [ COLUMN ] column SET STATISTICS integer
92 ALTER TABLE [ ONLY ] table [ * ]
93 RENAME [ COLUMN ] column TO newcolumn
97 ADD table_constraint_definition
98 ALTER TABLE [ ONLY ] table
99 DROP CONSTRAINT constraint { RESTRICT | CASCADE }
105 CREATE [ OR REPLACE ] VIEW view [ ( column name list ) ] AS SELECT query
110 use vars qw[ $DEBUG $VERSION $GRAMMAR @EXPORT_OK ];
111 $VERSION = sprintf "%d.%02d", q$Revision$ =~ /(\d+)\.(\d+)/;
112 $DEBUG = 0 unless defined $DEBUG;
115 use Parse::RecDescent;
117 use base qw(Exporter);
119 @EXPORT_OK = qw(parse);
121 # Enable warnings within the Parse::RecDescent module.
122 $::RD_ERRORS = 1; # Make sure the parser dies when it encounters an error
123 $::RD_WARN = 1; # Enable warnings. This will warn on unused rules &c.
124 $::RD_HINT = 1; # Give out hints to help fix problems.
126 my $parser; # should we do this? There's no programmic way to
127 # change the grammar, so I think this is safe.
131 { my ( %tables, $table_order, $field_order, @table_comments) }
134 # The "eofile" rule makes the parser fail if any "statement" rule
135 # fails. Otherwise, the first successful match by a "statement"
136 # won't cause the failure needed to know that the parse, as a whole,
139 startrule : statement(s) eofile { \%tables }
162 connect : /^\s*\\\connect.*\n/
164 set : /set/i /[^;]*/ ';'
166 revoke : /revoke/i WORD(s /,/) /on/i TABLE(?) table_name /from/i name_with_opt_quotes(s /,/) ';'
168 my $table_info = $item{'table_name'};
169 my $schema_name = $table_info->{'schema_name'};
170 my $table_name = $table_info->{'table_name'};
171 push @{ $tables{ $table_name }{'permissions'} }, {
178 revoke : /revoke/i WORD(s /,/) /on/i SCHEMA(?) schema_name /from/i name_with_opt_quotes(s /,/) ';'
181 grant : /grant/i WORD(s /,/) /on/i TABLE(?) table_name /to/i name_with_opt_quotes(s /,/) ';'
183 my $table_info = $item{'table_name'};
184 my $schema_name = $table_info->{'schema_name'};
185 my $table_name = $table_info->{'table_name'};
186 push @{ $tables{ $table_name }{'permissions'} }, {
193 grant : /grant/i WORD(s /,/) /on/i SCHEMA(?) schema_name /to/i name_with_opt_quotes(s /,/) ';'
196 drop : /drop/i /[^;]*/ ';'
199 /'(\\.|''|[^\\\'])*'/
201 nonstring : /[^;\'"]+/
203 statement_body : (string | nonstring)(s?)
205 insert : /insert/i statement_body ';'
207 update : /update/i statement_body ';'
212 create : CREATE temporary_table(?) TABLE table_name '(' create_definition(s? /,/) ')' table_option(s?) ';'
214 my $table_info = $item{'table_name'};
215 my $schema_name = $table_info->{'schema_name'};
216 my $table_name = $table_info->{'table_name'};
217 $tables{ $table_name }{'order'} = ++$table_order;
218 $tables{ $table_name }{'schema_name'} = $schema_name;
219 $tables{ $table_name }{'table_name'} = $table_name;
221 $tables{ $table_name }{'temporary'} = $item[2][0];
223 if ( @table_comments ) {
224 $tables{ $table_name }{'comments'} = [ @table_comments ];
225 @table_comments = ();
229 for my $definition ( @{ $item[6] } ) {
230 if ( $definition->{'supertype'} eq 'field' ) {
231 my $field_name = $definition->{'name'};
232 $tables{ $table_name }{'fields'}{ $field_name } =
233 { %$definition, order => $field_order++ };
235 for my $constraint ( @{ $definition->{'constraints'} || [] } ) {
236 $constraint->{'fields'} = [ $field_name ];
237 push @{ $tables{ $table_name }{'constraints'} },
241 elsif ( $definition->{'supertype'} eq 'constraint' ) {
242 push @{ $tables{ $table_name }{'constraints'} }, $definition;
244 elsif ( $definition->{'supertype'} eq 'index' ) {
245 push @{ $tables{ $table_name }{'indices'} }, $definition;
249 for my $option ( @{ $item[8] } ) {
250 $tables{ $table_name }{'table_options(s?)'}{ $option->{'type'} } =
257 create : CREATE unique(?) /(index|key)/i index_name /on/i table_name using_method(?) '(' field_name(s /,/) ')' where_predicate(?) ';'
259 my $table_info = $item{'table_name'};
260 my $schema_name = $table_info->{'schema_name'};
261 my $table_name = $table_info->{'table_name'};
262 push @{ $tables{ $table_name }{'indices'} },
264 name => $item{'index_name'},
265 supertype => $item{'unique'}[0] ? 'constraint' : 'index',
266 type => $item{'unique'}[0] ? 'unique' : 'normal',
268 method => $item{'using_method'}[0],
275 # Create anything else (e.g., domain, etc.)
277 create : CREATE WORD /[^;]+/ ';'
278 { @table_comments = (); }
280 using_method : /using/i WORD { $item[2] }
282 where_predicate : /where/i /[^;]+/
284 create_definition : field
288 comment : /^\s*(?:#|-{2})(.*)\n/
290 my $comment = $item[1];
291 $comment =~ s/^\s*(#|-*)\s*//;
292 $comment =~ s/\s*$//;
294 push @table_comments, $comment;
297 comment_on_table : /comment/i /on/i /table/i table_name /is/i comment_phrase ';'
299 my $table_info = $item{'table_name'};
300 my $schema_name = $table_info->{'schema_name'};
301 my $table_name = $table_info->{'table_name'};
302 push @{ $tables{ $table_name }{'comments'} }, $item{'comment_phrase'};
305 comment_on_column : /comment/i /on/i /column/i column_name /is/i comment_phrase ';'
307 my $table_name = $item[4]->{'table'};
308 my $field_name = $item[4]->{'field'};
309 if ($tables{ $table_name }{'fields'}{ $field_name } ) {
310 push @{ $tables{ $table_name }{'fields'}{ $field_name }{'comments'} },
311 $item{'comment_phrase'};
314 die "No such column as $table_name.$field_name";
318 comment_on_other : /comment/i /on/i /\w+/ /\w+/ /is/i comment_phrase ';'
320 push(@table_comments, $item{'comment_phrase'});
323 # [added by cjm 20041019]
324 # [TODO: other comment-on types]
325 # for now we just have a general mechanism for handling other
326 # kinds of comments than table/column; I'm not sure of the best
327 # way to incorporate these into the datamodel
329 # this is the exhaustive list of types of comment:
330 #COMMENT ON DATABASE my_database IS 'Development Database';
331 #COMMENT ON INDEX my_index IS 'Enforces uniqueness on employee id';
332 #COMMENT ON RULE my_rule IS 'Logs UPDATES of employee records';
333 #COMMENT ON SEQUENCE my_sequence IS 'Used to generate primary keys';
334 #COMMENT ON TABLE my_table IS 'Employee Information';
335 #COMMENT ON TYPE my_type IS 'Complex Number support';
336 #COMMENT ON VIEW my_view IS 'View of departmental costs';
337 #COMMENT ON COLUMN my_table.my_field IS 'Employee ID number';
338 #COMMENT ON TRIGGER my_trigger ON my_table IS 'Used for R.I.';
340 # this is tested by test 08
342 column_name : NAME '.' NAME
343 { $return = { table => $item[1], field => $item[3] } }
345 comment_phrase : /null/i
348 comment_phrase : /'/ comment_phrase_unquoted(s) /'/
349 { my $phrase = join(' ', @{ $item[2] });
352 # [cjm TODO: double-single quotes in a comment_phrase]
353 comment_phrase_unquoted : /[^\']*/
354 { $return = $item[1] }
357 xxxcomment_phrase : /'.*?'|NULL/
359 my $val = $item[1] || '';
364 field : field_comment(s?) field_name data_type field_meta(s?) field_comment(s?)
366 my ( $default, @constraints, $is_pk );
368 for my $meta ( @{ $item[4] } ) {
369 if ( $meta->{'type'} eq 'default' ) {
373 elsif ( $meta->{'type'} eq 'not_null' ) {
376 elsif ( $meta->{'type'} eq 'primary_key' ) {
380 push @constraints, $meta if $meta->{'supertype'} eq 'constraint';
383 my @comments = ( @{ $item[1] }, @{ $item[5] } );
386 supertype => 'field',
387 name => $item{'field_name'},
388 data_type => $item{'data_type'}{'type'},
389 size => $item{'data_type'}{'size'},
390 is_nullable => $is_nullable,
391 default => $default->{'value'},
392 constraints => [ @constraints ],
393 comments => [ @comments ],
394 is_primary_key => $is_pk || 0,
395 is_auto_increment => $item{'data_type'}{'is_auto_increment'},
400 field_comment : /^\s*(?:#|-{2})(.*)\n/
402 my $comment = $item[1];
403 $comment =~ s/^\s*(#|-*)\s*//;
404 $comment =~ s/\s*$//;
408 field_meta : default_val
411 column_constraint : constraint_name(?) column_constraint_type deferrable(?) deferred(?)
413 my $desc = $item{'column_constraint_type'};
414 my $type = $desc->{'type'};
415 my $fields = $desc->{'fields'} || [];
416 my $expression = $desc->{'expression'} || '';
419 supertype => 'constraint',
420 name => $item{'constraint_name'}[0] || '',
422 expression => $type eq 'check' ? $expression : '',
423 deferrable => $item{'deferrable'},
424 deferred => $item{'deferred'},
425 reference_table => $desc->{'reference_table'},
426 reference_fields => $desc->{'reference_fields'},
427 match_type => $desc->{'match_type'},
428 on_delete => $desc->{'on_delete'} || $desc->{'on_delete_do'},
429 on_update => $desc->{'on_update'} || $desc->{'on_update_do'},
433 constraint_name : /constraint/i name_with_opt_quotes { $item[2] }
435 column_constraint_type : /not null/i { $return = { type => 'not_null' } }
438 { $return = { type => 'null' } }
441 { $return = { type => 'unique' } }
444 { $return = { type => 'primary_key' } }
446 /check/i '(' /[^)]+/ ')'
447 { $return = { type => 'check', expression => $item[3] } }
449 /references/i table_name parens_word_list(?) match_type(?) key_action(s?)
451 my $table_info = $item{'table_name'};
452 my $schema_name = $table_info->{'schema_name'};
453 my $table_name = $table_info->{'table_name'};
454 my ( $on_delete, $on_update );
455 for my $action ( @{ $item[5] || [] } ) {
456 $on_delete = $action->{'action'} if $action->{'type'} eq 'delete';
457 $on_update = $action->{'action'} if $action->{'type'} eq 'update';
461 type => 'foreign_key',
462 reference_table => $table_name,
463 reference_fields => $item[3][0],
464 match_type => $item[4][0],
465 on_delete => $on_delete,
466 on_update => $on_update,
470 table_name : schema_qualification(?) name_with_opt_quotes {
471 $return = { schema_name => $item[1], table_name => $item[2] }
474 schema_qualification : name_with_opt_quotes '.'
476 schema_name : name_with_opt_quotes
478 field_name : name_with_opt_quotes
480 name_with_opt_quotes : double_quote(?) NAME double_quote(?) { $item[2] }
486 data_type : pg_data_type parens_value_list(?)
488 my $data_type = $item[1];
491 # We can deduce some sizes from the data type's name.
493 if ( my $size = $item[2][0] ) {
494 $data_type->{'size'} = $size;
497 $return = $data_type;
519 $return = { type => 'interval' };
522 /(integer|int4?)/i # interval must come before this
538 /(double precision|float8?)/i
546 /(bigserial|serial8)/i
551 is_auto_increment => 1,
560 is_auto_increment => 1,
564 /(bit varying|varbit)/i
566 $return = { type => 'varbit' };
571 $return = { type => 'varchar' };
576 $return = { type => 'char' };
581 $return = { type => 'boolean' };
586 $return = { type => 'bytea' };
589 /(timestamptz|timestamp)(?:\(\d\))?( with(out)? time zone)?/i
591 $return = { type => 'timestamp' };
602 /(bit|box|cidr|circle|date|inet|line|lseg|macaddr|money|numeric|decimal|path|point|polygon|timetz|time|varchar)/i
604 $return = { type => $item[1] };
607 parens_value_list : '(' VALUE(s /,/) ')'
611 parens_word_list : '(' name_with_opt_quotes(s /,/) ')'
614 field_size : '(' num_range ')' { $item{'num_range'} }
616 num_range : DIGITS ',' DIGITS
617 { $return = $item[1].','.$item[3] }
619 { $return = $item[1] }
621 table_constraint : comment(s?) constraint_name(?) table_constraint_type deferrable(?) deferred(?) comment(s?)
623 my $desc = $item{'table_constraint_type'};
624 my $type = $desc->{'type'};
625 my $fields = $desc->{'fields'};
626 my $expression = $desc->{'expression'};
627 my @comments = ( @{ $item[1] }, @{ $item[-1] } );
630 name => $item{'constraint_name'}[0] || '',
631 supertype => 'constraint',
633 fields => $type ne 'check' ? $fields : [],
634 expression => $type eq 'check' ? $expression : '',
635 deferrable => $item{'deferrable'},
636 deferred => $item{'deferred'},
637 reference_table => $desc->{'reference_table'},
638 reference_fields => $desc->{'reference_fields'},
639 match_type => $desc->{'match_type'}[0],
640 on_delete => $desc->{'on_delete'} || $desc->{'on_delete_do'},
641 on_update => $desc->{'on_update'} || $desc->{'on_update_do'},
642 comments => [ @comments ],
646 table_constraint_type : /primary key/i '(' name_with_opt_quotes(s /,/) ')'
649 type => 'primary_key',
654 /unique/i '(' name_with_opt_quotes(s /,/) ')'
662 /check/i '(' /[^)]+/ ')'
666 expression => $item[3],
670 /foreign key/i '(' name_with_opt_quotes(s /,/) ')' /references/i table_name parens_word_list(?) match_type(?) key_action(s?)
672 my ( $on_delete, $on_update );
673 for my $action ( @{ $item[9] || [] } ) {
674 $on_delete = $action->{'action'} if $action->{'type'} eq 'delete';
675 $on_update = $action->{'action'} if $action->{'type'} eq 'update';
679 supertype => 'constraint',
680 type => 'foreign_key',
682 reference_table => $item[6]->{'table_name'},
683 reference_fields => $item[7][0],
684 match_type => $item[8][0],
685 on_delete => $on_delete || '',
686 on_update => $on_update || '',
690 deferrable : not(?) /deferrable/i
692 $return = ( $item[1] =~ /not/i ) ? 0 : 1;
695 deferred : /initially/i /(deferred|immediate)/i { $item[2] }
697 match_type : /match full/i { 'match_full' }
699 /match partial/i { 'match_partial' }
701 key_action : key_delete
705 key_delete : /on delete/i key_mutation
713 key_update : /on update/i key_mutation
721 key_mutation : /no action/i { $return = 'no_action' }
723 /restrict/i { $return = 'restrict' }
725 /cascade/i { $return = 'cascade' }
727 /set null/i { $return = 'set null' }
729 /set default/i { $return = 'set default' }
731 alter : alter_table table_name add_column field ';'
733 my $field_def = $item[4];
734 $tables{ $item[2]->{'table_name'} }{'fields'}{ $field_def->{'name'} } = {
735 %$field_def, order => $field_order++
740 alter : alter_table table_name ADD table_constraint ';'
742 my $table_name = $item[2]->{'table_name'};
743 my $constraint = $item[4];
744 push @{ $tables{ $table_name }{'constraints'} }, $constraint;
748 alter : alter_table table_name drop_column NAME restrict_or_cascade(?) ';'
750 $tables{ $item[2]->{'table_name'} }{'fields'}{ $item[4] }{'drop'} = 1;
754 alter : alter_table table_name alter_column NAME alter_default_val ';'
756 $tables{ $item[2]->{'table_name'} }{'fields'}{ $item[4] }{'default'} =
762 # These will just parse for now but won't affect the structure. - ky
764 alter : alter_table table_name /rename/i /to/i NAME ';'
767 alter : alter_table table_name alter_column NAME SET /statistics/i INTEGER ';'
770 alter : alter_table table_name alter_column NAME SET /storage/i storage_type ';'
773 alter : alter_table table_name rename_column NAME /to/i NAME ';'
776 alter : alter_table table_name DROP /constraint/i NAME restrict_or_cascade ';'
779 alter : alter_table table_name /owner/i /to/i NAME ';'
782 alter : alter_sequence NAME /owned/i /by/i column_name ';'
785 storage_type : /(plain|external|extended|main)/i
787 temporary: /temp(orary)?\\b/i
789 temporary_table: temporary
794 alter_default_val : SET default_val
796 $return = { value => $item[2]->{'value'} }
800 $return = { value => undef }
804 # This is a little tricky to get right, at least WRT to making the
805 # tests pass. The problem is that the constraints are stored just as
806 # a list (no name access), and the tests expect the constraints in a
807 # particular order. I'm going to leave the rule but disable the code
810 alter : alter_table table_name alter_column NAME alter_nullable ';'
812 # my $table_name = $item[2]->{'table_name'};
813 # my $field_name = $item[4];
814 # my $is_nullable = $item[5]->{'is_nullable'};
816 # $tables{ $table_name }{'fields'}{ $field_name }{'is_nullable'} =
819 # if ( $is_nullable ) {
821 # push @{ $tables{ $table_name }{'constraints'} }, {
822 # type => 'not_null',
823 # fields => [ $field_name ],
828 # 0 .. $#{ $tables{ $table_name }{'constraints'} || [] }
830 # my $c = $tables{ $table_name }{'constraints'}[ $i ] or next;
831 # my $fields = join( '', @{ $c->{'fields'} || [] } ) or next;
832 # if ( $c->{'type'} eq 'not_null' && $fields eq $field_name ) {
833 # delete $tables{ $table_name }{'constraints'}[ $i ];
842 alter_nullable : SET not_null
844 $return = { is_nullable => 0 }
848 $return = { is_nullable => 1 }
851 not_null : /not/i /null/i
855 add_column : ADD COLUMN(?)
857 alter_table : ALTER TABLE ONLY(?)
859 alter_sequence : ALTER SEQUENCE
861 drop_column : DROP COLUMN(?)
863 alter_column : ALTER COLUMN(?)
865 rename_column : /rename/i COLUMN(?)
867 restrict_or_cascade : /restrict/i |
870 # Handle functions that can be called
871 select : SELECT select_function ';'
874 # Read the setval function but don't do anything with it because this parser
875 # isn't handling sequences
876 select_function : schema_qualification(?) /setval/i '(' VALUE /,/ VALUE /,/ /(true|false)/i ')'
879 # Skipping all COPY commands
880 copy : COPY WORD /[^;]+/ ';' { 1 }
883 # The "\." allows reading in from STDIN but this isn't needed for schema
884 # creation, so it is skipped.
889 # End basically useless stuff. - ky
892 create_table : CREATE TABLE
894 create_index : CREATE /index/i
896 default_val : DEFAULT /(\d+|'[^']*'|\w+\(.*\))|\w+/
898 my $val = defined $item[2] ? $item[2] : '';
901 supertype => 'constraint',
909 supertype => 'constraint',
915 name_with_opt_paren : NAME parens_value_list(s?)
916 { $item[2][0] ? "$item[1]($item[2][0][0])" : $item[1] }
918 unique : /unique/i { 1 }
920 key : /key/i | /index/i
922 table_option : /inherits/i '(' name_with_opt_quotes(s /,/) ')'
924 $return = { type => 'inherits', table_name => $item[3] }
929 $return = { type => $item[1] =~ /out/i ? 'without_oids' : 'with_oids' }
950 SEMICOLON : /\s*;\n?/
952 SEQUENCE : /sequence/i
975 VALUE : /[-+]?\.?\d+(?:[eE]\d+)?/
977 | /'.*?'/ # XXX doesn't handle embedded quotes
984 # -------------------------------------------------------------------
986 my ( $translator, $data ) = @_;
987 $parser ||= Parse::RecDescent->new($GRAMMAR);
989 $::RD_TRACE = $translator->trace ? 1 : undef;
990 $DEBUG = $translator->debug;
992 unless (defined $parser) {
993 return $translator->error("Error instantiating Parse::RecDescent ".
994 "instance: Bad grammer");
997 my $result = $parser->startrule($data);
998 die "Parse failed.\n" unless defined $result;
999 warn Dumper($result) if $DEBUG;
1001 my $schema = $translator->schema;
1003 ( $result->{ $a }{'order'} || 0 ) <=> ( $result->{ $b }{'order'} || 0 )
1004 } keys %{ $result };
1006 for my $table_name ( @tables ) {
1007 my $tdata = $result->{ $table_name };
1008 my $table = $schema->add_table(
1009 #schema => $tdata->{'schema_name'},
1010 name => $tdata->{'table_name'},
1011 ) or die "Couldn't create table '$table_name': " . $schema->error;
1013 $table->extra(temporary => 1) if $tdata->{'temporary'};
1015 $table->comments( $tdata->{'comments'} );
1018 $tdata->{'fields'}{ $a }{'order'}
1020 $tdata->{'fields'}{ $b }{'order'}
1021 } keys %{ $tdata->{'fields'} };
1023 for my $fname ( @fields ) {
1024 my $fdata = $tdata->{'fields'}{ $fname };
1025 next if $fdata->{'drop'};
1026 my $field = $table->add_field(
1027 name => $fdata->{'name'},
1028 data_type => $fdata->{'data_type'},
1029 size => $fdata->{'size'},
1030 default_value => $fdata->{'default'},
1031 is_auto_increment => $fdata->{'is_auto_increment'},
1032 is_nullable => $fdata->{'is_nullable'},
1033 comments => $fdata->{'comments'},
1034 ) or die $table->error;
1036 $table->primary_key( $field->name ) if $fdata->{'is_primary_key'};
1038 for my $cdata ( @{ $fdata->{'constraints'} } ) {
1039 next unless $cdata->{'type'} eq 'foreign_key';
1040 $cdata->{'fields'} ||= [ $field->name ];
1041 push @{ $tdata->{'constraints'} }, $cdata;
1045 for my $idata ( @{ $tdata->{'indices'} || [] } ) {
1046 my $index = $table->add_index(
1047 name => $idata->{'name'},
1048 type => uc $idata->{'type'},
1049 fields => $idata->{'fields'},
1050 ) or die $table->error . ' ' . $table->name;
1053 for my $cdata ( @{ $tdata->{'constraints'} || [] } ) {
1054 my $constraint = $table->add_constraint(
1055 name => $cdata->{'name'},
1056 type => $cdata->{'type'},
1057 fields => $cdata->{'fields'},
1058 reference_table => $cdata->{'reference_table'},
1059 reference_fields => $cdata->{'reference_fields'},
1060 match_type => $cdata->{'match_type'} || '',
1061 on_delete => $cdata->{'on_delete'} || $cdata->{'on_delete_do'},
1062 on_update => $cdata->{'on_update'} || $cdata->{'on_update_do'},
1063 expression => $cdata->{'expression'},
1064 ) or die "Can't add constraint of type '" .
1065 $cdata->{'type'} . "' to table '" . $table->name .
1066 "': " . $table->error;
1075 # -------------------------------------------------------------------
1076 # Rescue the drowning and tie your shoestrings.
1077 # Henry David Thoreau
1078 # -------------------------------------------------------------------
1084 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>,
1085 Allen Day E<lt>allenday@ucla.eduE<gt>.
1089 perl(1), Parse::RecDescent.