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
91 use vars qw[ $DEBUG $VERSION $GRAMMAR @EXPORT_OK ];
93 $DEBUG = 0 unless defined $DEBUG;
96 use Parse::RecDescent;
98 use base qw(Exporter);
100 @EXPORT_OK = qw(parse);
102 # Enable warnings within the Parse::RecDescent module.
103 $::RD_ERRORS = 1; # Make sure the parser dies when it encounters an error
104 $::RD_WARN = 1; # Enable warnings. This will warn on unused rules &c.
105 $::RD_HINT = 1; # Give out hints to help fix problems.
109 { my ( %tables, @views, $table_order, $field_order, @table_comments) }
112 # The "eofile" rule makes the parser fail if any "statement" rule
113 # fails. Otherwise, the first successful match by a "statement"
114 # won't cause the failure needed to know that the parse, as a whole,
117 startrule : statement(s) eofile { { tables => \%tables, views => \@views } }
141 commit : /commit/i ';'
143 connect : /^\s*\\\connect.*\n/
145 set : /set/i /[^;]*/ ';'
147 revoke : /revoke/i WORD(s /,/) /on/i TABLE(?) table_id /from/i name_with_opt_quotes(s /,/) ';'
149 my $table_info = $item{'table_id'};
150 my $schema_name = $table_info->{'schema_name'};
151 my $table_name = $table_info->{'table_name'};
152 push @{ $tables{ $table_name }{'permissions'} }, {
159 revoke : /revoke/i WORD(s /,/) /on/i SCHEMA(?) schema_name /from/i name_with_opt_quotes(s /,/) ';'
162 grant : /grant/i WORD(s /,/) /on/i TABLE(?) table_id /to/i name_with_opt_quotes(s /,/) ';'
164 my $table_info = $item{'table_id'};
165 my $schema_name = $table_info->{'schema_name'};
166 my $table_name = $table_info->{'table_name'};
167 push @{ $tables{ $table_name }{'permissions'} }, {
174 grant : /grant/i WORD(s /,/) /on/i SCHEMA(?) schema_name /to/i name_with_opt_quotes(s /,/) ';'
177 drop : /drop/i /[^;]*/ ';'
180 /'(\\.|''|[^\\\'])*'/
182 nonstring : /[^;\'"]+/
184 statement_body : string | nonstring
186 insert : /insert/i statement_body(s?) ';'
188 update : /update/i statement_body(s?) ';'
193 create : CREATE temporary(?) TABLE table_id '(' create_definition(s? /,/) ')' table_option(s?) ';'
195 my $table_info = $item{'table_id'};
196 my $schema_name = $table_info->{'schema_name'};
197 my $table_name = $table_info->{'table_name'};
198 $tables{ $table_name }{'order'} = ++$table_order;
199 $tables{ $table_name }{'schema_name'} = $schema_name;
200 $tables{ $table_name }{'table_name'} = $table_name;
202 $tables{ $table_name }{'temporary'} = $item[2][0];
204 if ( @table_comments ) {
205 $tables{ $table_name }{'comments'} = [ @table_comments ];
206 @table_comments = ();
210 for my $definition ( @{ $item[6] } ) {
211 if ( $definition->{'supertype'} eq 'field' ) {
212 my $field_name = $definition->{'name'};
213 $tables{ $table_name }{'fields'}{ $field_name } =
214 { %$definition, order => $field_order++ };
216 for my $constraint ( @{ $definition->{'constraints'} || [] } ) {
217 $constraint->{'fields'} = [ $field_name ];
218 push @{ $tables{ $table_name }{'constraints'} },
222 elsif ( $definition->{'supertype'} eq 'constraint' ) {
223 push @{ $tables{ $table_name }{'constraints'} }, $definition;
225 elsif ( $definition->{'supertype'} eq 'index' ) {
226 push @{ $tables{ $table_name }{'indices'} }, $definition;
230 for my $option ( @{ $item[8] } ) {
231 $tables{ $table_name }{'table_options(s?)'}{ $option->{'type'} } =
238 create : CREATE unique(?) /(index|key)/i index_name /on/i table_id using_method(?) '(' field_name(s /,/) ')' where_predicate(?) ';'
240 my $table_info = $item{'table_id'};
241 my $schema_name = $table_info->{'schema_name'};
242 my $table_name = $table_info->{'table_name'};
243 push @{ $tables{ $table_name }{'indices'} },
245 name => $item{'index_name'},
246 supertype => $item{'unique'}[0] ? 'constraint' : 'index',
247 type => $item{'unique'}[0] ? 'unique' : 'normal',
249 method => $item{'using_method'}[0],
254 create : CREATE or_replace(?) temporary(?) VIEW view_id view_fields(?) /AS/i view_target ';'
257 schema_name => $item{view_id}{schema_name},
258 view_name => $item{view_id}{view_name},
259 sql => $item{view_target},
261 is_temporary => $item[3][0],
266 # Create anything else (e.g., domain, etc.)
268 create : CREATE WORD /[^;]+/ ';'
269 { @table_comments = (); }
271 using_method : /using/i WORD { $item[2] }
273 where_predicate : /where/i /[^;]+/
275 create_definition : field
279 comment : /^\s*(?:#|-{2})(.*)\n/
281 my $comment = $item[1];
282 $comment =~ s/^\s*(#|-*)\s*//;
283 $comment =~ s/\s*$//;
285 push @table_comments, $comment;
288 comment_on_table : /comment/i /on/i /table/i table_id /is/i comment_phrase ';'
290 my $table_info = $item{'table_id'};
291 my $schema_name = $table_info->{'schema_name'};
292 my $table_name = $table_info->{'table_name'};
293 push @{ $tables{ $table_name }{'comments'} }, $item{'comment_phrase'};
296 comment_on_column : /comment/i /on/i /column/i column_name /is/i comment_phrase ';'
298 my $table_name = $item[4]->{'table'};
299 my $field_name = $item[4]->{'field'};
300 if ($tables{ $table_name }{'fields'}{ $field_name } ) {
301 push @{ $tables{ $table_name }{'fields'}{ $field_name }{'comments'} },
302 $item{'comment_phrase'};
305 die "No such column as $table_name.$field_name";
309 comment_on_other : /comment/i /on/i /\w+/ /\w+/ /is/i comment_phrase ';'
311 push(@table_comments, $item{'comment_phrase'});
314 # [added by cjm 20041019]
315 # [TODO: other comment-on types]
316 # for now we just have a general mechanism for handling other
317 # kinds of comments than table/column; I'm not sure of the best
318 # way to incorporate these into the datamodel
320 # this is the exhaustive list of types of comment:
321 #COMMENT ON DATABASE my_database IS 'Development Database';
322 #COMMENT ON INDEX my_index IS 'Enforces uniqueness on employee id';
323 #COMMENT ON RULE my_rule IS 'Logs UPDATES of employee records';
324 #COMMENT ON SEQUENCE my_sequence IS 'Used to generate primary keys';
325 #COMMENT ON TABLE my_table IS 'Employee Information';
326 #COMMENT ON TYPE my_type IS 'Complex Number support';
327 #COMMENT ON VIEW my_view IS 'View of departmental costs';
328 #COMMENT ON COLUMN my_table.my_field IS 'Employee ID number';
329 #COMMENT ON TRIGGER my_trigger ON my_table IS 'Used for R.I.';
331 # this is tested by test 08
333 column_name : NAME '.' NAME
334 { $return = { table => $item[1], field => $item[3] } }
336 comment_phrase : /null/i
339 comment_phrase : /'/ comment_phrase_unquoted(s) /'/
340 { my $phrase = join(' ', @{ $item[2] });
343 # [cjm TODO: double-single quotes in a comment_phrase]
344 comment_phrase_unquoted : /[^\']*/
345 { $return = $item[1] }
348 xxxcomment_phrase : /'.*?'|NULL/
350 my $val = $item[1] || '';
355 field : field_comment(s?) field_name data_type field_meta(s?) field_comment(s?)
357 my ( $default, @constraints, $is_pk );
359 for my $meta ( @{ $item[4] } ) {
360 if ( $meta->{'type'} eq 'default' ) {
364 elsif ( $meta->{'type'} eq 'not_null' ) {
367 elsif ( $meta->{'type'} eq 'primary_key' ) {
371 push @constraints, $meta if $meta->{'supertype'} eq 'constraint';
374 my @comments = ( @{ $item[1] }, @{ $item[5] } );
377 supertype => 'field',
378 name => $item{'field_name'},
379 data_type => $item{'data_type'}{'type'},
380 size => $item{'data_type'}{'size'},
381 is_nullable => $is_nullable,
382 default => $default->{'value'},
383 constraints => [ @constraints ],
384 comments => [ @comments ],
385 is_primary_key => $is_pk || 0,
386 is_auto_increment => $item{'data_type'}{'is_auto_increment'},
391 field_comment : /^\s*(?:#|-{2})(.*)\n/
393 my $comment = $item[1];
394 $comment =~ s/^\s*(#|-*)\s*//;
395 $comment =~ s/\s*$//;
399 field_meta : default_val
402 view_fields : '(' field_name(s /,/) ')'
403 { $return = join (',', @{$item[2]} ) }
405 column_constraint : constraint_name(?) column_constraint_type deferrable(?) deferred(?)
407 my $desc = $item{'column_constraint_type'};
408 my $type = $desc->{'type'};
409 my $fields = $desc->{'fields'} || [];
410 my $expression = $desc->{'expression'} || '';
413 supertype => 'constraint',
414 name => $item{'constraint_name'}[0] || '',
416 expression => $type eq 'check' ? $expression : '',
417 deferrable => $item{'deferrable'},
418 deferred => $item{'deferred'},
419 reference_table => $desc->{'reference_table'},
420 reference_fields => $desc->{'reference_fields'},
421 match_type => $desc->{'match_type'},
422 on_delete => $desc->{'on_delete'} || $desc->{'on_delete_do'},
423 on_update => $desc->{'on_update'} || $desc->{'on_update_do'},
427 constraint_name : /constraint/i name_with_opt_quotes { $item[2] }
429 column_constraint_type : /not null/i { $return = { type => 'not_null' } }
432 { $return = { type => 'null' } }
435 { $return = { type => 'unique' } }
438 { $return = { type => 'primary_key' } }
440 /check/i '(' /[^)]+/ ')'
441 { $return = { type => 'check', expression => $item[3] } }
443 /references/i table_id parens_word_list(?) match_type(?) key_action(s?)
445 my $table_info = $item{'table_id'};
446 my $schema_name = $table_info->{'schema_name'};
447 my $table_name = $table_info->{'table_name'};
448 my ( $on_delete, $on_update );
449 for my $action ( @{ $item[5] || [] } ) {
450 $on_delete = $action->{'action'} if $action->{'type'} eq 'delete';
451 $on_update = $action->{'action'} if $action->{'type'} eq 'update';
455 type => 'foreign_key',
456 reference_table => $table_name,
457 reference_fields => $item[3][0],
458 match_type => $item[4][0],
459 on_delete => $on_delete,
460 on_update => $on_update,
464 table_id : schema_qualification(?) name_with_opt_quotes {
465 $return = { schema_name => $item[1][0], table_name => $item[2] }
468 view_id : schema_qualification(?) name_with_opt_quotes {
469 $return = { schema_name => $item[1][0], view_name => $item[2] }
472 view_target : /select|with/i /[^;]+/ {
473 $return = "$item[1] $item[2]";
476 # SELECT views _may_ support outer parens, and we used to produce
477 # such sql, although non-standard. Use ugly lookeahead to parse
478 view_target : '(' /select/i / [^;]+ (?= \) ) /x ')' {
479 $return = "$item[2] $item[3]"
484 schema_qualification : name_with_opt_quotes '.'
486 schema_name : name_with_opt_quotes
488 field_name : name_with_opt_quotes
490 name_with_opt_quotes : double_quote(?) NAME double_quote(?) { $item[2] }
494 index_name : name_with_opt_quotes
496 data_type : pg_data_type parens_value_list(?)
498 my $data_type = $item[1];
501 # We can deduce some sizes from the data type's name.
503 if ( my $size = $item[2][0] ) {
504 $data_type->{'size'} = $size;
507 $return = $data_type;
529 $return = { type => 'interval' };
532 /(integer|int4?)/i # interval must come before this
548 /(double precision|float8?)/i
556 /(bigserial|serial8)/i
561 is_auto_increment => 1,
570 is_auto_increment => 1,
574 /(bit varying|varbit)/i
576 $return = { type => 'varbit' };
581 $return = { type => 'varchar' };
586 $return = { type => 'char' };
591 $return = { type => 'boolean' };
596 $return = { type => 'bytea' };
599 /(timestamptz|timestamp)(?:\(\d\))?( with(?:out)? time zone)?/i
601 $return = { type => 'timestamp' . ($2||'') };
612 /(bit|box|cidr|circle|date|inet|line|lseg|macaddr|money|numeric|decimal|path|point|polygon|timetz|time|varchar)/i
614 $return = { type => $item[1] };
617 parens_value_list : '(' VALUE(s /,/) ')'
621 parens_word_list : '(' name_with_opt_quotes(s /,/) ')'
624 field_size : '(' num_range ')' { $item{'num_range'} }
626 num_range : DIGITS ',' DIGITS
627 { $return = $item[1].','.$item[3] }
629 { $return = $item[1] }
631 table_constraint : comment(s?) constraint_name(?) table_constraint_type deferrable(?) deferred(?) comment(s?)
633 my $desc = $item{'table_constraint_type'};
634 my $type = $desc->{'type'};
635 my $fields = $desc->{'fields'};
636 my $expression = $desc->{'expression'};
637 my @comments = ( @{ $item[1] }, @{ $item[-1] } );
640 name => $item[2][0] || '',
641 supertype => 'constraint',
643 fields => $type ne 'check' ? $fields : [],
644 expression => $type eq 'check' ? $expression : '',
645 deferrable => $item{'deferrable'},
646 deferred => $item{'deferred'},
647 reference_table => $desc->{'reference_table'},
648 reference_fields => $desc->{'reference_fields'},
649 match_type => $desc->{'match_type'},
650 on_delete => $desc->{'on_delete'} || $desc->{'on_delete_do'},
651 on_update => $desc->{'on_update'} || $desc->{'on_update_do'},
652 comments => [ @comments ],
656 table_constraint_type : /primary key/i '(' name_with_opt_quotes(s /,/) ')'
659 type => 'primary_key',
664 /unique/i '(' name_with_opt_quotes(s /,/) ')'
672 /check/i '(' /[^)]+/ ')'
676 expression => $item[3],
680 /foreign key/i '(' name_with_opt_quotes(s /,/) ')' /references/i table_id parens_word_list(?) match_type(?) key_action(s?)
682 my ( $on_delete, $on_update );
683 for my $action ( @{ $item[9] || [] } ) {
684 $on_delete = $action->{'action'} if $action->{'type'} eq 'delete';
685 $on_update = $action->{'action'} if $action->{'type'} eq 'update';
689 supertype => 'constraint',
690 type => 'foreign_key',
692 reference_table => $item[6]->{'table_name'},
693 reference_fields => $item[7][0],
694 match_type => $item[8][0],
695 on_delete => $on_delete || '',
696 on_update => $on_update || '',
700 deferrable : not(?) /deferrable/i
702 $return = ( $item[1] =~ /not/i ) ? 0 : 1;
705 deferred : /initially/i /(deferred|immediate)/i { $item[2] }
707 match_type : /match/i /partial|full|simple/i { $item[2] }
709 key_action : key_delete
713 key_delete : /on delete/i key_mutation
721 key_update : /on update/i key_mutation
729 key_mutation : /no action/i { $return = 'no_action' }
731 /restrict/i { $return = 'restrict' }
733 /cascade/i { $return = 'cascade' }
735 /set null/i { $return = 'set null' }
737 /set default/i { $return = 'set default' }
739 alter : alter_table table_id add_column field ';'
741 my $field_def = $item[4];
742 $tables{ $item[2]->{'table_name'} }{'fields'}{ $field_def->{'name'} } = {
743 %$field_def, order => $field_order++
748 alter : alter_table table_id ADD table_constraint ';'
750 my $table_name = $item[2]->{'table_name'};
751 my $constraint = $item[4];
752 push @{ $tables{ $table_name }{'constraints'} }, $constraint;
756 alter : alter_table table_id drop_column NAME restrict_or_cascade(?) ';'
758 $tables{ $item[2]->{'table_name'} }{'fields'}{ $item[4] }{'drop'} = 1;
762 alter : alter_table table_id alter_column NAME alter_default_val ';'
764 $tables{ $item[2]->{'table_name'} }{'fields'}{ $item[4] }{'default'} =
770 # These will just parse for now but won't affect the structure. - ky
772 alter : alter_table table_id /rename/i /to/i NAME ';'
775 alter : alter_table table_id alter_column NAME SET /statistics/i INTEGER ';'
778 alter : alter_table table_id alter_column NAME SET /storage/i storage_type ';'
781 alter : alter_table table_id rename_column NAME /to/i NAME ';'
784 alter : alter_table table_id DROP /constraint/i NAME restrict_or_cascade ';'
787 alter : alter_table table_id /owner/i /to/i NAME ';'
790 alter : alter_sequence NAME /owned/i /by/i column_name ';'
793 storage_type : /(plain|external|extended|main)/i
795 temporary : /temp(orary)?\\b/i
800 or_replace : /or replace/i
802 alter_default_val : SET default_val
804 $return = { value => $item[2]->{'value'} }
808 $return = { value => undef }
812 # This is a little tricky to get right, at least WRT to making the
813 # tests pass. The problem is that the constraints are stored just as
814 # a list (no name access), and the tests expect the constraints in a
815 # particular order. I'm going to leave the rule but disable the code
818 alter : alter_table table_id alter_column NAME alter_nullable ';'
820 # my $table_name = $item[2]->{'table_name'};
821 # my $field_name = $item[4];
822 # my $is_nullable = $item[5]->{'is_nullable'};
824 # $tables{ $table_name }{'fields'}{ $field_name }{'is_nullable'} =
827 # if ( $is_nullable ) {
829 # push @{ $tables{ $table_name }{'constraints'} }, {
830 # type => 'not_null',
831 # fields => [ $field_name ],
836 # 0 .. $#{ $tables{ $table_name }{'constraints'} || [] }
838 # my $c = $tables{ $table_name }{'constraints'}[ $i ] or next;
839 # my $fields = join( '', @{ $c->{'fields'} || [] } ) or next;
840 # if ( $c->{'type'} eq 'not_null' && $fields eq $field_name ) {
841 # delete $tables{ $table_name }{'constraints'}[ $i ];
850 alter_nullable : SET not_null
852 $return = { is_nullable => 0 }
856 $return = { is_nullable => 1 }
859 not_null : /not/i /null/i
863 add_column : ADD COLUMN(?)
865 alter_table : ALTER TABLE ONLY(?)
867 alter_sequence : ALTER SEQUENCE
869 drop_column : DROP COLUMN(?)
871 alter_column : ALTER COLUMN(?)
873 rename_column : /rename/i COLUMN(?)
875 restrict_or_cascade : /restrict/i |
878 # Handle functions that can be called
879 select : SELECT select_function ';'
882 # Read the setval function but don't do anything with it because this parser
883 # isn't handling sequences
884 select_function : schema_qualification(?) /setval/i '(' VALUE /,/ VALUE /,/ /(true|false)/i ')'
887 # Skipping all COPY commands
888 copy : COPY WORD /[^;]+/ ';' { 1 }
891 # The "\." allows reading in from STDIN but this isn't needed for schema
892 # creation, so it is skipped.
897 # End basically useless stuff. - ky
900 create_table : CREATE TABLE
902 create_index : CREATE /index/i
904 default_val : DEFAULT /(\d+|'[^']*'|\w+\(.*\))|\w+/
906 my $val = defined $item[2] ? $item[2] : '';
909 supertype => 'constraint',
917 supertype => 'constraint',
923 name_with_opt_paren : NAME parens_value_list(s?)
924 { $item[2][0] ? "$item[1]($item[2][0][0])" : $item[1] }
926 unique : /unique/i { 1 }
928 key : /key/i | /index/i
930 table_option : /inherits/i '(' name_with_opt_quotes(s /,/) ')'
932 $return = { type => 'inherits', table_name => $item[3] }
937 $return = { type => $item[1] =~ /out/i ? 'without_oids' : 'with_oids' }
960 SEMICOLON : /\s*;\n?/
962 SEQUENCE : /sequence/i
985 VALUE : /[-+]?\.?\d+(?:[eE]\d+)?/
987 | /'.*?'/ # XXX doesn't handle embedded quotes
995 my ( $translator, $data ) = @_;
996 my $parser = Parse::RecDescent->new($GRAMMAR);
998 $::RD_TRACE = $translator->trace ? 1 : undef;
999 $DEBUG = $translator->debug;
1001 unless (defined $parser) {
1002 return $translator->error("Error instantiating Parse::RecDescent ".
1003 "instance: Bad grammer");
1006 my $result = $parser->startrule($data);
1007 die "Parse failed.\n" unless defined $result;
1008 warn Dumper($result) if $DEBUG;
1010 my $schema = $translator->schema;
1012 ( $result->{tables}{ $a }{'order'} || 0 ) <=> ( $result->{tables}{ $b }{'order'} || 0 )
1013 } keys %{ $result->{tables} };
1015 for my $table_name ( @tables ) {
1016 my $tdata = $result->{tables}{ $table_name };
1017 my $table = $schema->add_table(
1018 #schema => $tdata->{'schema_name'},
1019 name => $tdata->{'table_name'},
1020 ) or die "Couldn't create table '$table_name': " . $schema->error;
1022 $table->extra(temporary => 1) if $tdata->{'temporary'};
1024 $table->comments( $tdata->{'comments'} );
1027 $tdata->{'fields'}{ $a }{'order'}
1029 $tdata->{'fields'}{ $b }{'order'}
1030 } keys %{ $tdata->{'fields'} };
1032 for my $fname ( @fields ) {
1033 my $fdata = $tdata->{'fields'}{ $fname };
1034 next if $fdata->{'drop'};
1035 my $field = $table->add_field(
1036 name => $fdata->{'name'},
1037 data_type => $fdata->{'data_type'},
1038 size => $fdata->{'size'},
1039 default_value => $fdata->{'default'},
1040 is_auto_increment => $fdata->{'is_auto_increment'},
1041 is_nullable => $fdata->{'is_nullable'},
1042 comments => $fdata->{'comments'},
1043 ) or die $table->error;
1045 $table->primary_key( $field->name ) if $fdata->{'is_primary_key'};
1047 for my $cdata ( @{ $fdata->{'constraints'} } ) {
1048 next unless $cdata->{'type'} eq 'foreign_key';
1049 $cdata->{'fields'} ||= [ $field->name ];
1050 push @{ $tdata->{'constraints'} }, $cdata;
1054 for my $idata ( @{ $tdata->{'indices'} || [] } ) {
1055 my $index = $table->add_index(
1056 name => $idata->{'name'},
1057 type => uc $idata->{'type'},
1058 fields => $idata->{'fields'},
1059 ) or die $table->error . ' ' . $table->name;
1062 for my $cdata ( @{ $tdata->{'constraints'} || [] } ) {
1063 my $constraint = $table->add_constraint(
1064 name => $cdata->{'name'},
1065 type => $cdata->{'type'},
1066 fields => $cdata->{'fields'},
1067 reference_table => $cdata->{'reference_table'},
1068 reference_fields => $cdata->{'reference_fields'},
1069 match_type => $cdata->{'match_type'} || '',
1070 on_delete => $cdata->{'on_delete'} || $cdata->{'on_delete_do'},
1071 on_update => $cdata->{'on_update'} || $cdata->{'on_update_do'},
1072 expression => $cdata->{'expression'},
1073 ) or die "Can't add constraint of type '" .
1074 $cdata->{'type'} . "' to table '" . $table->name .
1075 "': " . $table->error;
1079 for my $vinfo (@{$result->{views}}) {
1080 my $sql = $vinfo->{sql};
1081 $sql =~ s/\A\s+|\s+\z//g;
1082 my $view = $schema->add_view (
1083 name => $vinfo->{view_name},
1085 fields => $vinfo->{fields},
1088 $view->extra ( temporary => 1 ) if $vinfo->{is_temporary};
1096 # -------------------------------------------------------------------
1097 # Rescue the drowning and tie your shoestrings.
1098 # Henry David Thoreau
1099 # -------------------------------------------------------------------
1105 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>,
1106 Allen Day E<lt>allenday@ucla.eduE<gt>.
1110 perl(1), Parse::RecDescent.