1 package SQL::Translator::Parser::PostgreSQL;
5 SQL::Translator::Parser::PostgreSQL - parser for PostgreSQL
10 use SQL::Translator::Parser::PostgreSQL;
12 my $translator = SQL::Translator->new;
13 $translator->parser("SQL::Translator::Parser::PostgreSQL");
17 The grammar was started from the MySQL parsers. Here is the description
21 (http://www.postgresql.org/docs/view.php?version=7.3&idoc=1&file=sql-createtable.html)
23 CREATE [ [ LOCAL ] { TEMPORARY | TEMP } ] TABLE table_name (
24 { column_name data_type [ DEFAULT default_expr ]
25 [ column_constraint [, ... ] ]
26 | table_constraint } [, ... ]
28 [ INHERITS ( parent_table [, ... ] ) ]
29 [ WITH OIDS | WITHOUT OIDS ]
31 where column_constraint is:
33 [ CONSTRAINT constraint_name ]
34 { NOT NULL | NULL | UNIQUE | PRIMARY KEY |
36 REFERENCES reftable [ ( refcolumn ) ] [ MATCH FULL | MATCH PARTIAL ]
37 [ ON DELETE action ] [ ON UPDATE action ] }
38 [ DEFERRABLE | NOT DEFERRABLE ]
39 [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]
41 and table_constraint is:
43 [ CONSTRAINT constraint_name ]
44 { UNIQUE ( column_name [, ... ] ) |
45 PRIMARY KEY ( column_name [, ... ] ) |
46 CHECK ( expression ) |
47 FOREIGN KEY ( column_name [, ... ] )
48 REFERENCES reftable [ ( refcolumn [, ... ] ) ]
49 [ MATCH FULL | MATCH PARTIAL ]
50 [ ON DELETE action ] [ ON UPDATE action ] }
51 [ DEFERRABLE | NOT DEFERRABLE ]
52 [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]
55 (http://www.postgresql.org/docs/view.php?version=7.3&idoc=1&file=sql-createindex.html)
57 CREATE [ UNIQUE ] INDEX index_name ON table
58 [ USING acc_method ] ( column [ ops_name ] [, ...] )
60 CREATE [ UNIQUE ] INDEX index_name ON table
61 [ USING acc_method ] ( func_name( column [, ... ]) [ ops_name ] )
66 ALTER TABLE [ ONLY ] table [ * ]
67 ADD [ COLUMN ] column type [ column_constraint [ ... ] ]
68 ALTER TABLE [ ONLY ] table [ * ]
69 ALTER [ COLUMN ] column { SET DEFAULT value | DROP DEFAULT }
70 ALTER TABLE [ ONLY ] table [ * ]
71 ALTER [ COLUMN ] column SET STATISTICS integer
72 ALTER TABLE [ ONLY ] table [ * ]
73 RENAME [ COLUMN ] column TO newcolumn
77 ADD table_constraint_definition
78 ALTER TABLE [ ONLY ] table
79 DROP CONSTRAINT constraint { RESTRICT | CASCADE }
85 CREATE [ OR REPLACE ] VIEW view [ ( column name list ) ] AS SELECT query
92 our $VERSION = '1.59';
95 $DEBUG = 0 unless defined $DEBUG;
98 use SQL::Translator::Utils qw/ddl_parser_instance/;
100 use base qw(Exporter);
101 our @EXPORT_OK = qw(parse);
103 our $GRAMMAR = <<'END_OF_GRAMMAR';
105 { my ( %tables, @views, @triggers, $table_order, $field_order, @table_comments) }
108 # The "eofile" rule makes the parser fail if any "statement" rule
109 # fails. Otherwise, the first successful match by a "statement"
110 # won't cause the failure needed to know that the parse, as a whole,
113 startrule : statement(s) eofile {
117 triggers => \@triggers,
142 commit : /commit/i ';'
144 connect : /^\s*\\connect.*\n/
146 set : /set/i /[^;]*/ ';'
148 revoke : /revoke/i WORD(s /,/) /on/i TABLE(?) table_id /from/i NAME(s /,/) ';'
150 my $table_info = $item{'table_id'};
151 my $schema_name = $table_info->{'schema_name'};
152 my $table_name = $table_info->{'table_name'};
153 push @{ $tables{ $table_name }{'permissions'} }, {
160 revoke : /revoke/i WORD(s /,/) /on/i SCHEMA(?) schema_name /from/i NAME(s /,/) ';'
163 grant : /grant/i WORD(s /,/) /on/i TABLE(?) table_id /to/i NAME(s /,/) ';'
165 my $table_info = $item{'table_id'};
166 my $schema_name = $table_info->{'schema_name'};
167 my $table_name = $table_info->{'table_name'};
168 push @{ $tables{ $table_name }{'permissions'} }, {
175 grant : /grant/i WORD(s /,/) /on/i SCHEMA(?) schema_name /to/i NAME(s /,/) ';'
178 drop : /drop/i /[^;]*/ ';'
183 nonstring : /[^;\'"]+/
185 statement_body : string | nonstring
187 insert : /insert/i statement_body(s?) ';'
189 update : /update/i statement_body(s?) ';'
194 create : CREATE temporary(?) TABLE table_id '(' create_definition(s? /,/) ')' table_option(s?) ';'
196 my $table_info = $item{'table_id'};
197 my $schema_name = $table_info->{'schema_name'};
198 my $table_name = $table_info->{'table_name'};
199 $tables{ $table_name }{'order'} = ++$table_order;
200 $tables{ $table_name }{'schema_name'} = $schema_name;
201 $tables{ $table_name }{'table_name'} = $table_name;
203 $tables{ $table_name }{'temporary'} = $item[2][0];
205 if ( @table_comments ) {
206 $tables{ $table_name }{'comments'} = [ @table_comments ];
207 @table_comments = ();
211 for my $definition ( @{ $item[6] } ) {
212 if ( $definition->{'supertype'} eq 'field' ) {
213 my $field_name = $definition->{'name'};
214 $tables{ $table_name }{'fields'}{ $field_name } =
215 { %$definition, order => $field_order++ };
217 for my $constraint ( @{ $definition->{'constraints'} || [] } ) {
218 $constraint->{'fields'} = [ $field_name ];
219 push @{ $tables{ $table_name }{'constraints'} },
223 elsif ( $definition->{'supertype'} eq 'constraint' ) {
224 push @{ $tables{ $table_name }{'constraints'} }, $definition;
226 elsif ( $definition->{'supertype'} eq 'index' ) {
227 push @{ $tables{ $table_name }{'indices'} }, $definition;
231 for my $option ( @{ $item[8] } ) {
232 $tables{ $table_name }{'table_options(s?)'}{ $option->{'type'} } =
239 create : CREATE unique(?) /(index|key)/i index_name /on/i table_id using_method(?) '(' field_name(s /,/) ')' where_predicate(?) ';'
241 my $table_info = $item{'table_id'};
242 my $schema_name = $table_info->{'schema_name'};
243 my $table_name = $table_info->{'table_name'};
244 push @{ $tables{ $table_name }{'indices'} },
246 name => $item{'index_name'},
247 supertype => $item{'unique'}[0] ? 'constraint' : 'index',
248 type => $item{'unique'}[0] ? 'unique' : 'normal',
250 method => $item{'using_method'}[0],
255 create : CREATE or_replace(?) temporary(?) VIEW view_id view_fields(?) /AS/i view_target ';'
258 schema_name => $item{view_id}{schema_name},
259 view_name => $item{view_id}{view_name},
260 sql => $item{view_target},
262 is_temporary => $item[3][0],
268 trigger_scope : /FOR/i /EACH/i /(ROW|STATEMENT)/i { $return = lc $1 }
270 before_or_after : /(before|after)/i { $return = lc $1 }
272 trigger_action : /.+/
274 database_event : /insert|update|delete/i
275 database_events : database_event(s /OR/)
277 create : CREATE /TRIGGER/i trigger_name before_or_after database_events /ON/i table_id trigger_scope(?) trigger_action
279 # Hack to pass roundtrip tests which have trigger statements terminated by double semicolon
280 # and expect the returned data to have the same
281 my $action = $item{trigger_action};
285 name => $item{trigger_name},
286 perform_action_when => $item{before_or_after},
287 database_events => $item{database_events},
288 on_table => $item{table_id}{table_name},
289 scope => $item{'trigger_scope(?)'}[0],
295 # Create anything else (e.g., domain, etc.)
297 create : CREATE WORD /[^;]+/ ';'
298 { @table_comments = (); }
300 using_method : /using/i WORD { $item[2] }
302 where_predicate : /where/i /[^;]+/
304 create_definition : field
308 comment : /^\s*(?:#|-{2})(.*)\n/
310 my $comment = $item[1];
311 $comment =~ s/^\s*(#|-*)\s*//;
312 $comment =~ s/\s*$//;
314 push @table_comments, $comment;
317 comment_on_table : /comment/i /on/i /table/i table_id /is/i comment_phrase ';'
319 my $table_info = $item{'table_id'};
320 my $schema_name = $table_info->{'schema_name'};
321 my $table_name = $table_info->{'table_name'};
322 push @{ $tables{ $table_name }{'comments'} }, $item{'comment_phrase'};
325 comment_on_column : /comment/i /on/i /column/i column_name /is/i comment_phrase ';'
327 my $table_name = $item[4]->{'table'};
328 my $field_name = $item[4]->{'field'};
329 if ($tables{ $table_name }{'fields'}{ $field_name } ) {
330 push @{ $tables{ $table_name }{'fields'}{ $field_name }{'comments'} },
331 $item{'comment_phrase'};
334 die "No such column as $table_name.$field_name";
338 comment_on_other : /comment/i /on/i /\w+/ /\w+/ /is/i comment_phrase ';'
340 push(@table_comments, $item{'comment_phrase'});
343 # [added by cjm 20041019]
344 # [TODO: other comment-on types]
345 # for now we just have a general mechanism for handling other
346 # kinds of comments than table/column; I'm not sure of the best
347 # way to incorporate these into the datamodel
349 # this is the exhaustive list of types of comment:
350 #COMMENT ON DATABASE my_database IS 'Development Database';
351 #COMMENT ON INDEX my_index IS 'Enforces uniqueness on employee id';
352 #COMMENT ON RULE my_rule IS 'Logs UPDATES of employee records';
353 #COMMENT ON SEQUENCE my_sequence IS 'Used to generate primary keys';
354 #COMMENT ON TABLE my_table IS 'Employee Information';
355 #COMMENT ON TYPE my_type IS 'Complex Number support';
356 #COMMENT ON VIEW my_view IS 'View of departmental costs';
357 #COMMENT ON COLUMN my_table.my_field IS 'Employee ID number';
358 #COMMENT ON TRIGGER my_trigger ON my_table IS 'Used for R.I.';
360 # this is tested by test 08
362 column_name : NAME '.' NAME
363 { $return = { table => $item[1], field => $item[3] } }
365 comment_phrase : /null/i
369 field : field_comment(s?) field_name data_type field_meta(s?) field_comment(s?)
371 my ( $default, @constraints, $is_pk );
373 for my $meta ( @{ $item[4] } ) {
374 if ( $meta->{'type'} eq 'default' ) {
378 elsif ( $meta->{'type'} eq 'not_null' ) {
381 elsif ( $meta->{'type'} eq 'primary_key' ) {
385 push @constraints, $meta if $meta->{'supertype'} eq 'constraint';
388 my @comments = ( @{ $item[1] }, @{ $item[5] } );
391 supertype => 'field',
392 name => $item{'field_name'},
393 data_type => $item{'data_type'}{'type'},
394 size => $item{'data_type'}{'size'},
395 is_nullable => $is_nullable,
396 default => $default->{'value'},
397 constraints => [ @constraints ],
398 comments => [ @comments ],
399 is_primary_key => $is_pk || 0,
400 is_auto_increment => $item{'data_type'}{'is_auto_increment'},
405 field_comment : /^\s*(?:#|-{2})(.*)\n/
407 my $comment = $item[1];
408 $comment =~ s/^\s*(#|-*)\s*//;
409 $comment =~ s/\s*$//;
413 field_meta : default_val
416 view_fields : '(' field_name(s /,/) ')'
417 { $return = join (',', @{$item[2]} ) }
419 column_constraint : constraint_name(?) column_constraint_type deferrable(?) deferred(?)
421 my $desc = $item{'column_constraint_type'};
422 my $type = $desc->{'type'};
423 my $fields = $desc->{'fields'} || [];
424 my $expression = $desc->{'expression'} || '';
427 supertype => 'constraint',
428 name => $item{'constraint_name'}[0] || '',
430 expression => $type eq 'check' ? $expression : '',
431 deferrable => $item{'deferrable'},
432 deferred => $item{'deferred'},
433 reference_table => $desc->{'reference_table'},
434 reference_fields => $desc->{'reference_fields'},
435 match_type => $desc->{'match_type'},
436 on_delete => $desc->{'on_delete'} || $desc->{'on_delete_do'},
437 on_update => $desc->{'on_update'} || $desc->{'on_update_do'},
441 constraint_name : /constraint/i NAME { $item[2] }
443 column_constraint_type : /not null/i { $return = { type => 'not_null' } }
446 { $return = { type => 'null' } }
449 { $return = { type => 'unique' } }
452 { $return = { type => 'primary_key' } }
454 /check/i '(' /[^)]+/ ')'
455 { $return = { type => 'check', expression => $item[3] } }
457 /references/i table_id parens_word_list(?) match_type(?) key_action(s?)
459 my $table_info = $item{'table_id'};
460 my $schema_name = $table_info->{'schema_name'};
461 my $table_name = $table_info->{'table_name'};
462 my ( $on_delete, $on_update );
463 for my $action ( @{ $item[5] || [] } ) {
464 $on_delete = $action->{'action'} if $action->{'type'} eq 'delete';
465 $on_update = $action->{'action'} if $action->{'type'} eq 'update';
469 type => 'foreign_key',
470 reference_table => $table_name,
471 reference_fields => $item[3][0],
472 match_type => $item[4][0],
473 on_delete => $on_delete,
474 on_update => $on_update,
478 table_id : schema_qualification(?) NAME {
479 $return = { schema_name => $item[1][0], table_name => $item[2] }
482 view_id : schema_qualification(?) NAME {
483 $return = { schema_name => $item[1][0], view_name => $item[2] }
486 view_target : /select|with/i /[^;]+/ {
487 $return = "$item[1] $item[2]";
490 # SELECT views _may_ support outer parens, and we used to produce
491 # such sql, although non-standard. Use ugly lookeahead to parse
492 view_target : '(' /select/i / [^;]+ (?= \) ) /x ')' {
493 $return = "$item[2] $item[3]"
498 schema_qualification : NAME '.'
508 array_indicator : '[' ']'
509 { $return = $item[1].$item[2] }
511 data_type : pg_data_type parens_value_list(?) array_indicator(?)
513 my $data_type = $item[1];
515 $data_type->{type} .= $item[3][0] if $item[3][0];
518 # We can deduce some sizes from the data type's name.
520 if ( my @size = @{$item[2]} ) {
521 $data_type->{'size'} = (@size == 1 ? $size[0] : \@size);
524 $return = $data_type;
546 $return = { type => 'interval' };
549 /(integer|int4?)/i # interval must come before this
565 /(double precision|float8?)/i
573 /(bigserial|serial8)/i
578 is_auto_increment => 1,
587 is_auto_increment => 1,
591 /(bit varying|varbit)/i
593 $return = { type => 'varbit' };
598 $return = { type => 'varchar' };
603 $return = { type => 'char' };
608 $return = { type => 'boolean' };
613 $return = { type => 'bytea' };
616 /(timestamptz|timestamp)(?:\(\d\))?( with(?:out)? time zone)?/i
618 $return = { type => 'timestamp' . ($2||'') };
629 /(bit|box|cidr|circle|date|inet|line|lseg|macaddr|money|numeric|decimal|path|point|polygon|timetz|time|varchar|json|hstore)/i
631 $return = { type => $item[1] };
634 parens_value_list : '(' VALUE(s /,/) ')'
638 parens_word_list : '(' NAME(s /,/) ')'
641 field_size : '(' num_range ')' { $item{'num_range'} }
643 num_range : DIGITS ',' DIGITS
644 { $return = $item[1].','.$item[3] }
646 { $return = $item[1] }
648 table_constraint : comment(s?) constraint_name(?) table_constraint_type deferrable(?) deferred(?) comment(s?)
650 my $desc = $item{'table_constraint_type'};
651 my $type = $desc->{'type'};
652 my $fields = $desc->{'fields'};
653 my $expression = $desc->{'expression'};
654 my @comments = ( @{ $item[1] }, @{ $item[-1] } );
657 name => $item[2][0] || '',
658 supertype => 'constraint',
660 fields => $type ne 'check' ? $fields : [],
661 expression => $type eq 'check' ? $expression : '',
662 deferrable => $item{'deferrable'},
663 deferred => $item{'deferred'},
664 reference_table => $desc->{'reference_table'},
665 reference_fields => $desc->{'reference_fields'},
666 match_type => $desc->{'match_type'},
667 on_delete => $desc->{'on_delete'} || $desc->{'on_delete_do'},
668 on_update => $desc->{'on_update'} || $desc->{'on_update_do'},
669 comments => [ @comments ],
673 table_constraint_type : /primary key/i '(' NAME(s /,/) ')'
676 type => 'primary_key',
681 /unique/i '(' NAME(s /,/) ')'
689 /check/i '(' /[^)]+/ ')'
693 expression => $item[3],
697 /foreign key/i '(' NAME(s /,/) ')' /references/i table_id parens_word_list(?) match_type(?) key_action(s?)
699 my ( $on_delete, $on_update );
700 for my $action ( @{ $item[9] || [] } ) {
701 $on_delete = $action->{'action'} if $action->{'type'} eq 'delete';
702 $on_update = $action->{'action'} if $action->{'type'} eq 'update';
706 supertype => 'constraint',
707 type => 'foreign_key',
709 reference_table => $item[6]->{'table_name'},
710 reference_fields => $item[7][0],
711 match_type => $item[8][0],
712 on_delete => $on_delete || '',
713 on_update => $on_update || '',
717 deferrable : not(?) /deferrable/i
719 $return = ( $item[1] =~ /not/i ) ? 0 : 1;
722 deferred : /initially/i /(deferred|immediate)/i { $item[2] }
724 match_type : /match/i /partial|full|simple/i { $item[2] }
726 key_action : key_delete
730 key_delete : /on delete/i key_mutation
738 key_update : /on update/i key_mutation
746 key_mutation : /no action/i { $return = 'no_action' }
748 /restrict/i { $return = 'restrict' }
750 /cascade/i { $return = 'cascade' }
752 /set null/i { $return = 'set null' }
754 /set default/i { $return = 'set default' }
756 alter : alter_table table_id add_column field ';'
758 my $field_def = $item[4];
759 $tables{ $item[2]->{'table_name'} }{'fields'}{ $field_def->{'name'} } = {
760 %$field_def, order => $field_order++
765 alter : alter_table table_id ADD table_constraint ';'
767 my $table_name = $item[2]->{'table_name'};
768 my $constraint = $item[4];
769 push @{ $tables{ $table_name }{'constraints'} }, $constraint;
773 alter : alter_table table_id drop_column NAME restrict_or_cascade(?) ';'
775 $tables{ $item[2]->{'table_name'} }{'fields'}{ $item[4] }{'drop'} = 1;
779 alter : alter_table table_id alter_column NAME alter_default_val ';'
781 $tables{ $item[2]->{'table_name'} }{'fields'}{ $item[4] }{'default'} =
787 # These will just parse for now but won't affect the structure. - ky
789 alter : alter_table table_id /rename/i /to/i NAME ';'
792 alter : alter_table table_id alter_column NAME SET /statistics/i INTEGER ';'
795 alter : alter_table table_id alter_column NAME SET /storage/i storage_type ';'
798 alter : alter_table table_id rename_column NAME /to/i NAME ';'
801 alter : alter_table table_id DROP /constraint/i NAME restrict_or_cascade ';'
804 alter : alter_table table_id /owner/i /to/i NAME ';'
807 alter : alter_sequence NAME /owned/i /by/i column_name ';'
810 storage_type : /(plain|external|extended|main)/i
812 temporary : /temp(orary)?\b/i
817 or_replace : /or replace/i
819 alter_default_val : SET default_val
821 $return = { value => $item[2]->{'value'} }
825 $return = { value => undef }
829 # This is a little tricky to get right, at least WRT to making the
830 # tests pass. The problem is that the constraints are stored just as
831 # a list (no name access), and the tests expect the constraints in a
832 # particular order. I'm going to leave the rule but disable the code
835 alter : alter_table table_id alter_column NAME alter_nullable ';'
837 # my $table_name = $item[2]->{'table_name'};
838 # my $field_name = $item[4];
839 # my $is_nullable = $item[5]->{'is_nullable'};
841 # $tables{ $table_name }{'fields'}{ $field_name }{'is_nullable'} =
844 # if ( $is_nullable ) {
846 # push @{ $tables{ $table_name }{'constraints'} }, {
847 # type => 'not_null',
848 # fields => [ $field_name ],
853 # 0 .. $#{ $tables{ $table_name }{'constraints'} || [] }
855 # my $c = $tables{ $table_name }{'constraints'}[ $i ] or next;
856 # my $fields = join( '', @{ $c->{'fields'} || [] } ) or next;
857 # if ( $c->{'type'} eq 'not_null' && $fields eq $field_name ) {
858 # delete $tables{ $table_name }{'constraints'}[ $i ];
867 alter_nullable : SET not_null
869 $return = { is_nullable => 0 }
873 $return = { is_nullable => 1 }
876 not_null : /not/i /null/i
880 add_column : ADD COLUMN(?)
882 alter_table : ALTER TABLE ONLY(?)
884 alter_sequence : ALTER SEQUENCE
886 drop_column : DROP COLUMN(?)
888 alter_column : ALTER COLUMN(?)
890 rename_column : /rename/i COLUMN(?)
892 restrict_or_cascade : /restrict/i |
895 # Handle functions that can be called
896 select : SELECT select_function ';'
899 # Read the setval function but don't do anything with it because this parser
900 # isn't handling sequences
901 select_function : schema_qualification(?) /setval/i '(' VALUE /,/ VALUE /,/ /(true|false)/i ')'
904 # Skipping all COPY commands
905 copy : COPY WORD /[^;]+/ ';' { 1 }
908 # The "\." allows reading in from STDIN but this isn't needed for schema
909 # creation, so it is skipped.
914 # End basically useless stuff. - ky
917 create_table : CREATE TABLE
919 create_index : CREATE /index/i
921 default_val : DEFAULT DEFAULT_VALUE ( '::' data_type )(?)
924 $val =~ s/^\((\d+)\)\z/$1/; # for example (0)::smallint
926 supertype => 'constraint',
934 supertype => 'constraint',
940 DEFAULT_VALUE : VALUE
945 name_with_opt_paren : NAME parens_value_list(s?)
946 { $item[2][0] ? "$item[1]($item[2][0][0])" : $item[1] }
948 unique : /unique/i { 1 }
950 key : /key/i | /index/i
952 table_option : /inherits/i '(' NAME(s /,/) ')'
954 $return = { type => 'inherits', table_name => $item[3] }
959 $return = { type => $item[1] =~ /out/i ? 'without_oids' : 'with_oids' }
982 SEMICOLON : /\s*;\n?/
984 SEQUENCE : /sequence/i
1003 DQSTRING : '"' /((?:[^"]|"")+)/ '"'
1004 { ($return = $item[2]) =~ s/""/"/g; }
1006 SQSTRING : "'" /((?:[^']|'')*)/ "'"
1007 { ($return = $item[2]) =~ s/''/'/g }
1009 VALUE : /[-+]?\d*\.?\d+(?:[eE]\d+)?/
1017 my ( $translator, $data ) = @_;
1019 # Enable warnings within the Parse::RecDescent module.
1020 local $::RD_ERRORS = 1 unless defined $::RD_ERRORS; # Make sure the parser dies when it encounters an error
1021 local $::RD_WARN = 1 unless defined $::RD_WARN; # Enable warnings. This will warn on unused rules &c.
1022 local $::RD_HINT = 1 unless defined $::RD_HINT; # Give out hints to help fix problems.
1024 local $::RD_TRACE = $translator->trace ? 1 : undef;
1025 local $DEBUG = $translator->debug;
1027 my $parser = ddl_parser_instance('PostgreSQL');
1029 my $result = $parser->startrule($data);
1030 die "Parse failed.\n" unless defined $result;
1031 warn Dumper($result) if $DEBUG;
1033 my $schema = $translator->schema;
1035 ( $result->{tables}{ $a }{'order'} || 0 ) <=> ( $result->{tables}{ $b }{'order'} || 0 )
1036 } keys %{ $result->{tables} };
1038 for my $table_name ( @tables ) {
1039 my $tdata = $result->{tables}{ $table_name };
1040 my $table = $schema->add_table(
1041 #schema => $tdata->{'schema_name'},
1042 name => $tdata->{'table_name'},
1043 ) or die "Couldn't create table '$table_name': " . $schema->error;
1045 $table->extra(temporary => 1) if $tdata->{'temporary'};
1047 $table->comments( $tdata->{'comments'} );
1050 $tdata->{'fields'}{ $a }{'order'}
1052 $tdata->{'fields'}{ $b }{'order'}
1053 } keys %{ $tdata->{'fields'} };
1055 for my $fname ( @fields ) {
1056 my $fdata = $tdata->{'fields'}{ $fname };
1057 next if $fdata->{'drop'};
1058 my $field = $table->add_field(
1059 name => $fdata->{'name'},
1060 data_type => $fdata->{'data_type'},
1061 size => $fdata->{'size'},
1062 default_value => $fdata->{'default'},
1063 is_auto_increment => $fdata->{'is_auto_increment'},
1064 is_nullable => $fdata->{'is_nullable'},
1065 comments => $fdata->{'comments'},
1066 ) or die $table->error;
1068 $table->primary_key( $field->name ) if $fdata->{'is_primary_key'};
1070 for my $cdata ( @{ $fdata->{'constraints'} } ) {
1071 next unless $cdata->{'type'} eq 'foreign_key';
1072 $cdata->{'fields'} ||= [ $field->name ];
1073 push @{ $tdata->{'constraints'} }, $cdata;
1077 for my $idata ( @{ $tdata->{'indices'} || [] } ) {
1078 my $index = $table->add_index(
1079 name => $idata->{'name'},
1080 type => uc $idata->{'type'},
1081 fields => $idata->{'fields'},
1082 ) or die $table->error . ' ' . $table->name;
1085 for my $cdata ( @{ $tdata->{'constraints'} || [] } ) {
1086 my $constraint = $table->add_constraint(
1087 name => $cdata->{'name'},
1088 type => $cdata->{'type'},
1089 fields => $cdata->{'fields'},
1090 reference_table => $cdata->{'reference_table'},
1091 reference_fields => $cdata->{'reference_fields'},
1092 match_type => $cdata->{'match_type'} || '',
1093 on_delete => $cdata->{'on_delete'} || $cdata->{'on_delete_do'},
1094 on_update => $cdata->{'on_update'} || $cdata->{'on_update_do'},
1095 expression => $cdata->{'expression'},
1096 ) or die "Can't add constraint of type '" .
1097 $cdata->{'type'} . "' to table '" . $table->name .
1098 "': " . $table->error;
1102 for my $vinfo (@{$result->{views}}) {
1103 my $sql = $vinfo->{sql};
1104 $sql =~ s/\A\s+|\s+\z//g;
1105 my $view = $schema->add_view (
1106 name => $vinfo->{view_name},
1108 fields => $vinfo->{fields},
1111 $view->extra ( temporary => 1 ) if $vinfo->{is_temporary};
1114 for my $trigger (@{ $result->{triggers} }) {
1115 $schema->add_trigger( %$trigger );
1123 # -------------------------------------------------------------------
1124 # Rescue the drowning and tie your shoestrings.
1125 # Henry David Thoreau
1126 # -------------------------------------------------------------------
1132 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>,
1133 Allen Day E<lt>allenday@ucla.eduE<gt>.
1137 perl(1), Parse::RecDescent.