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
90 use vars qw[ $DEBUG $VERSION $GRAMMAR @EXPORT_OK ];
92 $DEBUG = 0 unless defined $DEBUG;
95 use Parse::RecDescent;
97 use base qw(Exporter);
99 @EXPORT_OK = qw(parse);
101 # Enable warnings within the Parse::RecDescent module.
102 $::RD_ERRORS = 1; # Make sure the parser dies when it encounters an error
103 $::RD_WARN = 1; # Enable warnings. This will warn on unused rules &c.
104 $::RD_HINT = 1; # Give out hints to help fix problems.
108 { my ( %tables, @views, $table_order, $field_order, @table_comments) }
111 # The "eofile" rule makes the parser fail if any "statement" rule
112 # fails. Otherwise, the first successful match by a "statement"
113 # won't cause the failure needed to know that the parse, as a whole,
116 startrule : statement(s) eofile { { tables => \%tables, views => \@views } }
140 commit : /commit/i ';'
142 connect : /^\s*\\\connect.*\n/
144 set : /set/i /[^;]*/ ';'
146 revoke : /revoke/i WORD(s /,/) /on/i TABLE(?) table_id /from/i name_with_opt_quotes(s /,/) ';'
148 my $table_info = $item{'table_id'};
149 my $schema_name = $table_info->{'schema_name'};
150 my $table_name = $table_info->{'table_name'};
151 push @{ $tables{ $table_name }{'permissions'} }, {
158 revoke : /revoke/i WORD(s /,/) /on/i SCHEMA(?) schema_name /from/i name_with_opt_quotes(s /,/) ';'
161 grant : /grant/i WORD(s /,/) /on/i TABLE(?) table_id /to/i name_with_opt_quotes(s /,/) ';'
163 my $table_info = $item{'table_id'};
164 my $schema_name = $table_info->{'schema_name'};
165 my $table_name = $table_info->{'table_name'};
166 push @{ $tables{ $table_name }{'permissions'} }, {
173 grant : /grant/i WORD(s /,/) /on/i SCHEMA(?) schema_name /to/i name_with_opt_quotes(s /,/) ';'
176 drop : /drop/i /[^;]*/ ';'
179 /'(\\.|''|[^\\\'])*'/
181 nonstring : /[^;\'"]+/
183 statement_body : string | nonstring
185 insert : /insert/i statement_body(s?) ';'
187 update : /update/i statement_body(s?) ';'
192 create : CREATE temporary(?) TABLE table_id '(' create_definition(s? /,/) ')' table_option(s?) ';'
194 my $table_info = $item{'table_id'};
195 my $schema_name = $table_info->{'schema_name'};
196 my $table_name = $table_info->{'table_name'};
197 $tables{ $table_name }{'order'} = ++$table_order;
198 $tables{ $table_name }{'schema_name'} = $schema_name;
199 $tables{ $table_name }{'table_name'} = $table_name;
201 $tables{ $table_name }{'temporary'} = $item[2][0];
203 if ( @table_comments ) {
204 $tables{ $table_name }{'comments'} = [ @table_comments ];
205 @table_comments = ();
209 for my $definition ( @{ $item[6] } ) {
210 if ( $definition->{'supertype'} eq 'field' ) {
211 my $field_name = $definition->{'name'};
212 $tables{ $table_name }{'fields'}{ $field_name } =
213 { %$definition, order => $field_order++ };
215 for my $constraint ( @{ $definition->{'constraints'} || [] } ) {
216 $constraint->{'fields'} = [ $field_name ];
217 push @{ $tables{ $table_name }{'constraints'} },
221 elsif ( $definition->{'supertype'} eq 'constraint' ) {
222 push @{ $tables{ $table_name }{'constraints'} }, $definition;
224 elsif ( $definition->{'supertype'} eq 'index' ) {
225 push @{ $tables{ $table_name }{'indices'} }, $definition;
229 for my $option ( @{ $item[8] } ) {
230 $tables{ $table_name }{'table_options(s?)'}{ $option->{'type'} } =
237 create : CREATE unique(?) /(index|key)/i index_name /on/i table_id using_method(?) '(' field_name(s /,/) ')' where_predicate(?) ';'
239 my $table_info = $item{'table_id'};
240 my $schema_name = $table_info->{'schema_name'};
241 my $table_name = $table_info->{'table_name'};
242 push @{ $tables{ $table_name }{'indices'} },
244 name => $item{'index_name'},
245 supertype => $item{'unique'}[0] ? 'constraint' : 'index',
246 type => $item{'unique'}[0] ? 'unique' : 'normal',
248 method => $item{'using_method'}[0],
253 create : CREATE or_replace(?) temporary(?) VIEW view_id view_fields(?) /AS/i view_target ';'
256 schema_name => $item{view_id}{schema_name},
257 view_name => $item{view_id}{view_name},
258 sql => $item{view_target},
260 is_temporary => $item[3][0],
265 # Create anything else (e.g., domain, etc.)
267 create : CREATE WORD /[^;]+/ ';'
268 { @table_comments = (); }
270 using_method : /using/i WORD { $item[2] }
272 where_predicate : /where/i /[^;]+/
274 create_definition : field
278 comment : /^\s*(?:#|-{2})(.*)\n/
280 my $comment = $item[1];
281 $comment =~ s/^\s*(#|-*)\s*//;
282 $comment =~ s/\s*$//;
284 push @table_comments, $comment;
287 comment_on_table : /comment/i /on/i /table/i table_id /is/i comment_phrase ';'
289 my $table_info = $item{'table_id'};
290 my $schema_name = $table_info->{'schema_name'};
291 my $table_name = $table_info->{'table_name'};
292 push @{ $tables{ $table_name }{'comments'} }, $item{'comment_phrase'};
295 comment_on_column : /comment/i /on/i /column/i column_name /is/i comment_phrase ';'
297 my $table_name = $item[4]->{'table'};
298 my $field_name = $item[4]->{'field'};
299 if ($tables{ $table_name }{'fields'}{ $field_name } ) {
300 push @{ $tables{ $table_name }{'fields'}{ $field_name }{'comments'} },
301 $item{'comment_phrase'};
304 die "No such column as $table_name.$field_name";
308 comment_on_other : /comment/i /on/i /\w+/ /\w+/ /is/i comment_phrase ';'
310 push(@table_comments, $item{'comment_phrase'});
313 # [added by cjm 20041019]
314 # [TODO: other comment-on types]
315 # for now we just have a general mechanism for handling other
316 # kinds of comments than table/column; I'm not sure of the best
317 # way to incorporate these into the datamodel
319 # this is the exhaustive list of types of comment:
320 #COMMENT ON DATABASE my_database IS 'Development Database';
321 #COMMENT ON INDEX my_index IS 'Enforces uniqueness on employee id';
322 #COMMENT ON RULE my_rule IS 'Logs UPDATES of employee records';
323 #COMMENT ON SEQUENCE my_sequence IS 'Used to generate primary keys';
324 #COMMENT ON TABLE my_table IS 'Employee Information';
325 #COMMENT ON TYPE my_type IS 'Complex Number support';
326 #COMMENT ON VIEW my_view IS 'View of departmental costs';
327 #COMMENT ON COLUMN my_table.my_field IS 'Employee ID number';
328 #COMMENT ON TRIGGER my_trigger ON my_table IS 'Used for R.I.';
330 # this is tested by test 08
332 column_name : NAME '.' NAME
333 { $return = { table => $item[1], field => $item[3] } }
335 comment_phrase : /null/i
338 comment_phrase : /'/ comment_phrase_unquoted(s) /'/
339 { my $phrase = join(' ', @{ $item[2] });
342 # [cjm TODO: double-single quotes in a comment_phrase]
343 comment_phrase_unquoted : /[^\']*/
344 { $return = $item[1] }
347 xxxcomment_phrase : /'.*?'|NULL/
349 my $val = $item[1] || '';
354 field : field_comment(s?) field_name data_type field_meta(s?) field_comment(s?)
356 my ( $default, @constraints, $is_pk );
358 for my $meta ( @{ $item[4] } ) {
359 if ( $meta->{'type'} eq 'default' ) {
363 elsif ( $meta->{'type'} eq 'not_null' ) {
366 elsif ( $meta->{'type'} eq 'primary_key' ) {
370 push @constraints, $meta if $meta->{'supertype'} eq 'constraint';
373 my @comments = ( @{ $item[1] }, @{ $item[5] } );
376 supertype => 'field',
377 name => $item{'field_name'},
378 data_type => $item{'data_type'}{'type'},
379 size => $item{'data_type'}{'size'},
380 is_nullable => $is_nullable,
381 default => $default->{'value'},
382 constraints => [ @constraints ],
383 comments => [ @comments ],
384 is_primary_key => $is_pk || 0,
385 is_auto_increment => $item{'data_type'}{'is_auto_increment'},
390 field_comment : /^\s*(?:#|-{2})(.*)\n/
392 my $comment = $item[1];
393 $comment =~ s/^\s*(#|-*)\s*//;
394 $comment =~ s/\s*$//;
398 field_meta : default_val
401 view_fields : '(' field_name(s /,/) ')'
402 { $return = join (',', @{$item[2]} ) }
404 column_constraint : constraint_name(?) column_constraint_type deferrable(?) deferred(?)
406 my $desc = $item{'column_constraint_type'};
407 my $type = $desc->{'type'};
408 my $fields = $desc->{'fields'} || [];
409 my $expression = $desc->{'expression'} || '';
412 supertype => 'constraint',
413 name => $item{'constraint_name'}[0] || '',
415 expression => $type eq 'check' ? $expression : '',
416 deferrable => $item{'deferrable'},
417 deferred => $item{'deferred'},
418 reference_table => $desc->{'reference_table'},
419 reference_fields => $desc->{'reference_fields'},
420 match_type => $desc->{'match_type'},
421 on_delete => $desc->{'on_delete'} || $desc->{'on_delete_do'},
422 on_update => $desc->{'on_update'} || $desc->{'on_update_do'},
426 constraint_name : /constraint/i name_with_opt_quotes { $item[2] }
428 column_constraint_type : /not null/i { $return = { type => 'not_null' } }
431 { $return = { type => 'null' } }
434 { $return = { type => 'unique' } }
437 { $return = { type => 'primary_key' } }
439 /check/i '(' /[^)]+/ ')'
440 { $return = { type => 'check', expression => $item[3] } }
442 /references/i table_id parens_word_list(?) match_type(?) key_action(s?)
444 my $table_info = $item{'table_id'};
445 my $schema_name = $table_info->{'schema_name'};
446 my $table_name = $table_info->{'table_name'};
447 my ( $on_delete, $on_update );
448 for my $action ( @{ $item[5] || [] } ) {
449 $on_delete = $action->{'action'} if $action->{'type'} eq 'delete';
450 $on_update = $action->{'action'} if $action->{'type'} eq 'update';
454 type => 'foreign_key',
455 reference_table => $table_name,
456 reference_fields => $item[3][0],
457 match_type => $item[4][0],
458 on_delete => $on_delete,
459 on_update => $on_update,
463 table_id : schema_qualification(?) name_with_opt_quotes {
464 $return = { schema_name => $item[1][0], table_name => $item[2] }
467 view_id : schema_qualification(?) name_with_opt_quotes {
468 $return = { schema_name => $item[1][0], view_name => $item[2] }
471 view_target : /select|with/i /[^;]+/ {
472 $return = "$item[1] $item[2]";
475 # SELECT views _may_ support outer parens, and we used to produce
476 # such sql, although non-standard. Use ugly lookeahead to parse
477 view_target : '(' /select/i / [^;]+ (?= \) ) /x ')' {
478 $return = "$item[2] $item[3]"
483 schema_qualification : name_with_opt_quotes '.'
485 schema_name : name_with_opt_quotes
487 field_name : name_with_opt_quotes
489 name_with_opt_quotes : double_quote(?) NAME double_quote(?) { $item[2] }
493 index_name : name_with_opt_quotes
495 data_type : pg_data_type parens_value_list(?)
497 my $data_type = $item[1];
500 # We can deduce some sizes from the data type's name.
502 if ( my $size = $item[2][0] ) {
503 $data_type->{'size'} = $size;
506 $return = $data_type;
528 $return = { type => 'interval' };
531 /(integer|int4?)/i # interval must come before this
547 /(double precision|float8?)/i
555 /(bigserial|serial8)/i
560 is_auto_increment => 1,
569 is_auto_increment => 1,
573 /(bit varying|varbit)/i
575 $return = { type => 'varbit' };
580 $return = { type => 'varchar' };
585 $return = { type => 'char' };
590 $return = { type => 'boolean' };
595 $return = { type => 'bytea' };
598 /(timestamptz|timestamp)(?:\(\d\))?( with(?:out)? time zone)?/i
600 $return = { type => 'timestamp' . ($2||'') };
611 /(bit|box|cidr|circle|date|inet|line|lseg|macaddr|money|numeric|decimal|path|point|polygon|timetz|time|varchar)/i
613 $return = { type => $item[1] };
616 parens_value_list : '(' VALUE(s /,/) ')'
620 parens_word_list : '(' name_with_opt_quotes(s /,/) ')'
623 field_size : '(' num_range ')' { $item{'num_range'} }
625 num_range : DIGITS ',' DIGITS
626 { $return = $item[1].','.$item[3] }
628 { $return = $item[1] }
630 table_constraint : comment(s?) constraint_name(?) table_constraint_type deferrable(?) deferred(?) comment(s?)
632 my $desc = $item{'table_constraint_type'};
633 my $type = $desc->{'type'};
634 my $fields = $desc->{'fields'};
635 my $expression = $desc->{'expression'};
636 my @comments = ( @{ $item[1] }, @{ $item[-1] } );
639 name => $item[2][0] || '',
640 supertype => 'constraint',
642 fields => $type ne 'check' ? $fields : [],
643 expression => $type eq 'check' ? $expression : '',
644 deferrable => $item{'deferrable'},
645 deferred => $item{'deferred'},
646 reference_table => $desc->{'reference_table'},
647 reference_fields => $desc->{'reference_fields'},
648 match_type => $desc->{'match_type'},
649 on_delete => $desc->{'on_delete'} || $desc->{'on_delete_do'},
650 on_update => $desc->{'on_update'} || $desc->{'on_update_do'},
651 comments => [ @comments ],
655 table_constraint_type : /primary key/i '(' name_with_opt_quotes(s /,/) ')'
658 type => 'primary_key',
663 /unique/i '(' name_with_opt_quotes(s /,/) ')'
671 /check/i '(' /[^)]+/ ')'
675 expression => $item[3],
679 /foreign key/i '(' name_with_opt_quotes(s /,/) ')' /references/i table_id parens_word_list(?) match_type(?) key_action(s?)
681 my ( $on_delete, $on_update );
682 for my $action ( @{ $item[9] || [] } ) {
683 $on_delete = $action->{'action'} if $action->{'type'} eq 'delete';
684 $on_update = $action->{'action'} if $action->{'type'} eq 'update';
688 supertype => 'constraint',
689 type => 'foreign_key',
691 reference_table => $item[6]->{'table_name'},
692 reference_fields => $item[7][0],
693 match_type => $item[8][0],
694 on_delete => $on_delete || '',
695 on_update => $on_update || '',
699 deferrable : not(?) /deferrable/i
701 $return = ( $item[1] =~ /not/i ) ? 0 : 1;
704 deferred : /initially/i /(deferred|immediate)/i { $item[2] }
706 match_type : /match/i /partial|full|simple/i { $item[2] }
708 key_action : key_delete
712 key_delete : /on delete/i key_mutation
720 key_update : /on update/i key_mutation
728 key_mutation : /no action/i { $return = 'no_action' }
730 /restrict/i { $return = 'restrict' }
732 /cascade/i { $return = 'cascade' }
734 /set null/i { $return = 'set null' }
736 /set default/i { $return = 'set default' }
738 alter : alter_table table_id add_column field ';'
740 my $field_def = $item[4];
741 $tables{ $item[2]->{'table_name'} }{'fields'}{ $field_def->{'name'} } = {
742 %$field_def, order => $field_order++
747 alter : alter_table table_id ADD table_constraint ';'
749 my $table_name = $item[2]->{'table_name'};
750 my $constraint = $item[4];
751 push @{ $tables{ $table_name }{'constraints'} }, $constraint;
755 alter : alter_table table_id drop_column NAME restrict_or_cascade(?) ';'
757 $tables{ $item[2]->{'table_name'} }{'fields'}{ $item[4] }{'drop'} = 1;
761 alter : alter_table table_id alter_column NAME alter_default_val ';'
763 $tables{ $item[2]->{'table_name'} }{'fields'}{ $item[4] }{'default'} =
769 # These will just parse for now but won't affect the structure. - ky
771 alter : alter_table table_id /rename/i /to/i NAME ';'
774 alter : alter_table table_id alter_column NAME SET /statistics/i INTEGER ';'
777 alter : alter_table table_id alter_column NAME SET /storage/i storage_type ';'
780 alter : alter_table table_id rename_column NAME /to/i NAME ';'
783 alter : alter_table table_id DROP /constraint/i NAME restrict_or_cascade ';'
786 alter : alter_table table_id /owner/i /to/i NAME ';'
789 alter : alter_sequence NAME /owned/i /by/i column_name ';'
792 storage_type : /(plain|external|extended|main)/i
794 temporary : /temp(orary)?\\b/i
799 or_replace : /or replace/i
801 alter_default_val : SET default_val
803 $return = { value => $item[2]->{'value'} }
807 $return = { value => undef }
811 # This is a little tricky to get right, at least WRT to making the
812 # tests pass. The problem is that the constraints are stored just as
813 # a list (no name access), and the tests expect the constraints in a
814 # particular order. I'm going to leave the rule but disable the code
817 alter : alter_table table_id alter_column NAME alter_nullable ';'
819 # my $table_name = $item[2]->{'table_name'};
820 # my $field_name = $item[4];
821 # my $is_nullable = $item[5]->{'is_nullable'};
823 # $tables{ $table_name }{'fields'}{ $field_name }{'is_nullable'} =
826 # if ( $is_nullable ) {
828 # push @{ $tables{ $table_name }{'constraints'} }, {
829 # type => 'not_null',
830 # fields => [ $field_name ],
835 # 0 .. $#{ $tables{ $table_name }{'constraints'} || [] }
837 # my $c = $tables{ $table_name }{'constraints'}[ $i ] or next;
838 # my $fields = join( '', @{ $c->{'fields'} || [] } ) or next;
839 # if ( $c->{'type'} eq 'not_null' && $fields eq $field_name ) {
840 # delete $tables{ $table_name }{'constraints'}[ $i ];
849 alter_nullable : SET not_null
851 $return = { is_nullable => 0 }
855 $return = { is_nullable => 1 }
858 not_null : /not/i /null/i
862 add_column : ADD COLUMN(?)
864 alter_table : ALTER TABLE ONLY(?)
866 alter_sequence : ALTER SEQUENCE
868 drop_column : DROP COLUMN(?)
870 alter_column : ALTER COLUMN(?)
872 rename_column : /rename/i COLUMN(?)
874 restrict_or_cascade : /restrict/i |
877 # Handle functions that can be called
878 select : SELECT select_function ';'
881 # Read the setval function but don't do anything with it because this parser
882 # isn't handling sequences
883 select_function : schema_qualification(?) /setval/i '(' VALUE /,/ VALUE /,/ /(true|false)/i ')'
886 # Skipping all COPY commands
887 copy : COPY WORD /[^;]+/ ';' { 1 }
890 # The "\." allows reading in from STDIN but this isn't needed for schema
891 # creation, so it is skipped.
896 # End basically useless stuff. - ky
899 create_table : CREATE TABLE
901 create_index : CREATE /index/i
903 default_val : DEFAULT /(\d+|'[^']*'|\w+\(.*\))|\w+/
905 my $val = defined $item[2] ? $item[2] : '';
908 supertype => 'constraint',
916 supertype => 'constraint',
922 name_with_opt_paren : NAME parens_value_list(s?)
923 { $item[2][0] ? "$item[1]($item[2][0][0])" : $item[1] }
925 unique : /unique/i { 1 }
927 key : /key/i | /index/i
929 table_option : /inherits/i '(' name_with_opt_quotes(s /,/) ')'
931 $return = { type => 'inherits', table_name => $item[3] }
936 $return = { type => $item[1] =~ /out/i ? 'without_oids' : 'with_oids' }
959 SEMICOLON : /\s*;\n?/
961 SEQUENCE : /sequence/i
984 VALUE : /[-+]?\.?\d+(?:[eE]\d+)?/
986 | /'.*?'/ # XXX doesn't handle embedded quotes
994 my ( $translator, $data ) = @_;
995 my $parser = Parse::RecDescent->new($GRAMMAR);
997 $::RD_TRACE = $translator->trace ? 1 : undef;
998 $DEBUG = $translator->debug;
1000 unless (defined $parser) {
1001 return $translator->error("Error instantiating Parse::RecDescent ".
1002 "instance: Bad grammer");
1005 my $result = $parser->startrule($data);
1006 die "Parse failed.\n" unless defined $result;
1007 warn Dumper($result) if $DEBUG;
1009 my $schema = $translator->schema;
1011 ( $result->{tables}{ $a }{'order'} || 0 ) <=> ( $result->{tables}{ $b }{'order'} || 0 )
1012 } keys %{ $result->{tables} };
1014 for my $table_name ( @tables ) {
1015 my $tdata = $result->{tables}{ $table_name };
1016 my $table = $schema->add_table(
1017 #schema => $tdata->{'schema_name'},
1018 name => $tdata->{'table_name'},
1019 ) or die "Couldn't create table '$table_name': " . $schema->error;
1021 $table->extra(temporary => 1) if $tdata->{'temporary'};
1023 $table->comments( $tdata->{'comments'} );
1026 $tdata->{'fields'}{ $a }{'order'}
1028 $tdata->{'fields'}{ $b }{'order'}
1029 } keys %{ $tdata->{'fields'} };
1031 for my $fname ( @fields ) {
1032 my $fdata = $tdata->{'fields'}{ $fname };
1033 next if $fdata->{'drop'};
1034 my $field = $table->add_field(
1035 name => $fdata->{'name'},
1036 data_type => $fdata->{'data_type'},
1037 size => $fdata->{'size'},
1038 default_value => $fdata->{'default'},
1039 is_auto_increment => $fdata->{'is_auto_increment'},
1040 is_nullable => $fdata->{'is_nullable'},
1041 comments => $fdata->{'comments'},
1042 ) or die $table->error;
1044 $table->primary_key( $field->name ) if $fdata->{'is_primary_key'};
1046 for my $cdata ( @{ $fdata->{'constraints'} } ) {
1047 next unless $cdata->{'type'} eq 'foreign_key';
1048 $cdata->{'fields'} ||= [ $field->name ];
1049 push @{ $tdata->{'constraints'} }, $cdata;
1053 for my $idata ( @{ $tdata->{'indices'} || [] } ) {
1054 my $index = $table->add_index(
1055 name => $idata->{'name'},
1056 type => uc $idata->{'type'},
1057 fields => $idata->{'fields'},
1058 ) or die $table->error . ' ' . $table->name;
1061 for my $cdata ( @{ $tdata->{'constraints'} || [] } ) {
1062 my $constraint = $table->add_constraint(
1063 name => $cdata->{'name'},
1064 type => $cdata->{'type'},
1065 fields => $cdata->{'fields'},
1066 reference_table => $cdata->{'reference_table'},
1067 reference_fields => $cdata->{'reference_fields'},
1068 match_type => $cdata->{'match_type'} || '',
1069 on_delete => $cdata->{'on_delete'} || $cdata->{'on_delete_do'},
1070 on_update => $cdata->{'on_update'} || $cdata->{'on_update_do'},
1071 expression => $cdata->{'expression'},
1072 ) or die "Can't add constraint of type '" .
1073 $cdata->{'type'} . "' to table '" . $table->name .
1074 "': " . $table->error;
1078 for my $vinfo (@{$result->{views}}) {
1079 my $sql = $vinfo->{sql};
1080 $sql =~ s/\A\s+|\s+\z//g;
1081 my $view = $schema->add_view (
1082 name => $vinfo->{view_name},
1084 fields => $vinfo->{fields},
1087 $view->extra ( temporary => 1 ) if $vinfo->{is_temporary};
1095 # -------------------------------------------------------------------
1096 # Rescue the drowning and tie your shoestrings.
1097 # Henry David Thoreau
1098 # -------------------------------------------------------------------
1104 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>,
1105 Allen Day E<lt>allenday@ucla.eduE<gt>.
1109 perl(1), Parse::RecDescent.