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.60';
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],
251 where => $item{'where_predicate(?)'}[0],
256 create : CREATE or_replace(?) temporary(?) VIEW view_id view_fields(?) /AS/i view_target ';'
259 schema_name => $item{view_id}{schema_name},
260 view_name => $item{view_id}{view_name},
261 sql => $item{view_target},
263 is_temporary => $item[3][0],
269 trigger_scope : /FOR/i /EACH/i /(ROW|STATEMENT)/i { $return = lc $1 }
271 before_or_after : /(before|after)/i { $return = lc $1 }
273 trigger_action : /.+/
275 database_event : /insert|update|delete/i
276 database_events : database_event(s /OR/)
278 create : CREATE /TRIGGER/i trigger_name before_or_after database_events /ON/i table_id trigger_scope(?) trigger_action
280 # Hack to pass roundtrip tests which have trigger statements terminated by double semicolon
281 # and expect the returned data to have the same
282 my $action = $item{trigger_action};
286 name => $item{trigger_name},
287 perform_action_when => $item{before_or_after},
288 database_events => $item{database_events},
289 on_table => $item{table_id}{table_name},
290 scope => $item{'trigger_scope(?)'}[0],
296 # Create anything else (e.g., domain, etc.)
298 create : CREATE WORD /[^;]+/ ';'
299 { @table_comments = (); }
301 using_method : /using/i WORD { $item[2] }
303 where_predicate : /where/i /[^;]+/
305 create_definition : field
309 comment : /^\s*(?:#|-{2})(.*)\n/
311 my $comment = $item[1];
312 $comment =~ s/^\s*(#|-*)\s*//;
313 $comment =~ s/\s*$//;
315 push @table_comments, $comment;
318 comment_on_table : /comment/i /on/i /table/i table_id /is/i comment_phrase ';'
320 my $table_info = $item{'table_id'};
321 my $schema_name = $table_info->{'schema_name'};
322 my $table_name = $table_info->{'table_name'};
323 push @{ $tables{ $table_name }{'comments'} }, $item{'comment_phrase'};
326 comment_on_column : /comment/i /on/i /column/i column_name /is/i comment_phrase ';'
328 my $table_name = $item[4]->{'table'};
329 my $field_name = $item[4]->{'field'};
330 if ($tables{ $table_name }{'fields'}{ $field_name } ) {
331 push @{ $tables{ $table_name }{'fields'}{ $field_name }{'comments'} },
332 $item{'comment_phrase'};
335 die "No such column as $table_name.$field_name";
339 comment_on_other : /comment/i /on/i /\w+/ /\w+/ /is/i comment_phrase ';'
341 push(@table_comments, $item{'comment_phrase'});
344 # [added by cjm 20041019]
345 # [TODO: other comment-on types]
346 # for now we just have a general mechanism for handling other
347 # kinds of comments than table/column; I'm not sure of the best
348 # way to incorporate these into the datamodel
350 # this is the exhaustive list of types of comment:
351 #COMMENT ON DATABASE my_database IS 'Development Database';
352 #COMMENT ON INDEX my_index IS 'Enforces uniqueness on employee id';
353 #COMMENT ON RULE my_rule IS 'Logs UPDATES of employee records';
354 #COMMENT ON SEQUENCE my_sequence IS 'Used to generate primary keys';
355 #COMMENT ON TABLE my_table IS 'Employee Information';
356 #COMMENT ON TYPE my_type IS 'Complex Number support';
357 #COMMENT ON VIEW my_view IS 'View of departmental costs';
358 #COMMENT ON COLUMN my_table.my_field IS 'Employee ID number';
359 #COMMENT ON TRIGGER my_trigger ON my_table IS 'Used for R.I.';
361 # this is tested by test 08
363 column_name : NAME '.' NAME
364 { $return = { table => $item[1], field => $item[3] } }
366 comment_phrase : /null/i
370 field : field_comment(s?) field_name data_type field_meta(s?) field_comment(s?)
372 my ( $default, @constraints, $is_pk );
374 for my $meta ( @{ $item[4] } ) {
375 if ( $meta->{'type'} eq 'default' ) {
379 elsif ( $meta->{'type'} eq 'not_null' ) {
382 elsif ( $meta->{'type'} eq 'primary_key' ) {
386 push @constraints, $meta if $meta->{'supertype'} eq 'constraint';
389 my @comments = ( @{ $item[1] }, @{ $item[5] } );
392 supertype => 'field',
393 name => $item{'field_name'},
394 data_type => $item{'data_type'}{'type'},
395 size => $item{'data_type'}{'size'},
396 is_nullable => $is_nullable,
397 default => $default->{'value'},
398 constraints => [ @constraints ],
399 comments => [ @comments ],
400 is_primary_key => $is_pk || 0,
401 is_auto_increment => $item{'data_type'}{'is_auto_increment'},
406 field_comment : /^\s*(?:#|-{2})(.*)\n/
408 my $comment = $item[1];
409 $comment =~ s/^\s*(#|-*)\s*//;
410 $comment =~ s/\s*$//;
414 field_meta : default_val
417 view_fields : '(' field_name(s /,/) ')'
418 { $return = join (',', @{$item[2]} ) }
420 column_constraint : constraint_name(?) column_constraint_type deferrable(?) deferred(?)
422 my $desc = $item{'column_constraint_type'};
423 my $type = $desc->{'type'};
424 my $fields = $desc->{'fields'} || [];
425 my $expression = $desc->{'expression'} || '';
428 supertype => 'constraint',
429 name => $item{'constraint_name'}[0] || '',
431 expression => $type eq 'check' ? $expression : '',
432 deferrable => $item{'deferrable'},
433 deferred => $item{'deferred'},
434 reference_table => $desc->{'reference_table'},
435 reference_fields => $desc->{'reference_fields'},
436 match_type => $desc->{'match_type'},
437 on_delete => $desc->{'on_delete'} || $desc->{'on_delete_do'},
438 on_update => $desc->{'on_update'} || $desc->{'on_update_do'},
442 constraint_name : /constraint/i NAME { $item[2] }
444 column_constraint_type : /not null/i { $return = { type => 'not_null' } }
447 { $return = { type => 'null' } }
450 { $return = { type => 'unique' } }
453 { $return = { type => 'primary_key' } }
455 /check/i '(' /[^)]+/ ')'
456 { $return = { type => 'check', expression => $item[3] } }
458 /references/i table_id parens_word_list(?) match_type(?) key_action(s?)
460 my $table_info = $item{'table_id'};
461 my $schema_name = $table_info->{'schema_name'};
462 my $table_name = $table_info->{'table_name'};
463 my ( $on_delete, $on_update );
464 for my $action ( @{ $item[5] || [] } ) {
465 $on_delete = $action->{'action'} if $action->{'type'} eq 'delete';
466 $on_update = $action->{'action'} if $action->{'type'} eq 'update';
470 type => 'foreign_key',
471 reference_table => $table_name,
472 reference_fields => $item[3][0],
473 match_type => $item[4][0],
474 on_delete => $on_delete,
475 on_update => $on_update,
479 table_id : schema_qualification(?) NAME {
480 $return = { schema_name => $item[1][0], table_name => $item[2] }
483 view_id : schema_qualification(?) NAME {
484 $return = { schema_name => $item[1][0], view_name => $item[2] }
487 view_target : /select|with/i /[^;]+/ {
488 $return = "$item[1] $item[2]";
491 # SELECT views _may_ support outer parens, and we used to produce
492 # such sql, although non-standard. Use ugly lookeahead to parse
493 view_target : '(' /select/i / [^;]+ (?= \) ) /x ')' {
494 $return = "$item[2] $item[3]"
499 schema_qualification : NAME '.'
509 array_indicator : '[' ']'
510 { $return = $item[1].$item[2] }
512 data_type : pg_data_type parens_value_list(?) array_indicator(?)
514 my $data_type = $item[1];
516 $data_type->{type} .= $item[3][0] if $item[3][0];
519 # We can deduce some sizes from the data type's name.
521 if ( my @size = @{$item[2]} ) {
522 $data_type->{'size'} = (@size == 1 ? $size[0] : \@size);
525 $return = $data_type;
547 $return = { type => 'interval' };
550 /(integer|int4?)/i # interval must come before this
566 /(double precision|float8?)/i
574 /(bigserial|serial8)/i
579 is_auto_increment => 1,
588 is_auto_increment => 1,
592 /(bit varying|varbit)/i
594 $return = { type => 'varbit' };
599 $return = { type => 'varchar' };
604 $return = { type => 'char' };
609 $return = { type => 'boolean' };
614 $return = { type => 'bytea' };
617 / ( timestamp (?:tz)? ) (?: \( \d \) )? ( \s with (?:out)? \s time \s zone )? /ix
619 $return = { type => 'timestamp' . ($2||'') };
622 / ( time (?:tz)? ) (?: \( \d \) )? ( \s with (?:out)? \s time \s zone )? /ix
624 $return = { type => 'time' . ($2||'') };
635 /(bit|box|cidr|circle|date|inet|line|lseg|macaddr|money|numeric|decimal|path|point|polygon|varchar|json|hstore|uuid)/i
637 $return = { type => $item[1] };
640 parens_value_list : '(' VALUE(s /,/) ')'
644 parens_word_list : '(' NAME(s /,/) ')'
647 field_size : '(' num_range ')' { $item{'num_range'} }
649 num_range : DIGITS ',' DIGITS
650 { $return = $item[1].','.$item[3] }
652 { $return = $item[1] }
654 table_constraint : comment(s?) constraint_name(?) table_constraint_type deferrable(?) deferred(?) comment(s?)
656 my $desc = $item{'table_constraint_type'};
657 my $type = $desc->{'type'};
658 my $fields = $desc->{'fields'};
659 my $expression = $desc->{'expression'};
660 my @comments = ( @{ $item[1] }, @{ $item[-1] } );
663 name => $item[2][0] || '',
664 supertype => 'constraint',
666 fields => $type ne 'check' ? $fields : [],
667 expression => $type eq 'check' ? $expression : '',
668 deferrable => $item{'deferrable'},
669 deferred => $item{'deferred'},
670 reference_table => $desc->{'reference_table'},
671 reference_fields => $desc->{'reference_fields'},
672 match_type => $desc->{'match_type'},
673 on_delete => $desc->{'on_delete'} || $desc->{'on_delete_do'},
674 on_update => $desc->{'on_update'} || $desc->{'on_update_do'},
675 comments => [ @comments ],
679 table_constraint_type : /primary key/i '(' NAME(s /,/) ')'
682 type => 'primary_key',
687 /unique/i '(' NAME(s /,/) ')'
695 /check/i '(' /[^)]+/ ')'
699 expression => $item[3],
703 /foreign key/i '(' NAME(s /,/) ')' /references/i table_id parens_word_list(?) match_type(?) key_action(s?)
705 my ( $on_delete, $on_update );
706 for my $action ( @{ $item[9] || [] } ) {
707 $on_delete = $action->{'action'} if $action->{'type'} eq 'delete';
708 $on_update = $action->{'action'} if $action->{'type'} eq 'update';
712 supertype => 'constraint',
713 type => 'foreign_key',
715 reference_table => $item[6]->{'table_name'},
716 reference_fields => $item[7][0],
717 match_type => $item[8][0],
718 on_delete => $on_delete || '',
719 on_update => $on_update || '',
723 deferrable : not(?) /deferrable/i
725 $return = ( $item[1] =~ /not/i ) ? 0 : 1;
728 deferred : /initially/i /(deferred|immediate)/i { $item[2] }
730 match_type : /match/i /partial|full|simple/i { $item[2] }
732 key_action : key_delete
736 key_delete : /on delete/i key_mutation
744 key_update : /on update/i key_mutation
752 key_mutation : /no action/i { $return = 'no_action' }
754 /restrict/i { $return = 'restrict' }
756 /cascade/i { $return = 'cascade' }
758 /set null/i { $return = 'set null' }
760 /set default/i { $return = 'set default' }
762 alter : alter_table table_id add_column field ';'
764 my $field_def = $item[4];
765 $tables{ $item[2]->{'table_name'} }{'fields'}{ $field_def->{'name'} } = {
766 %$field_def, order => $field_order++
771 alter : alter_table table_id ADD table_constraint ';'
773 my $table_name = $item[2]->{'table_name'};
774 my $constraint = $item[4];
775 push @{ $tables{ $table_name }{'constraints'} }, $constraint;
779 alter : alter_table table_id drop_column NAME restrict_or_cascade(?) ';'
781 $tables{ $item[2]->{'table_name'} }{'fields'}{ $item[4] }{'drop'} = 1;
785 alter : alter_table table_id alter_column NAME alter_default_val ';'
787 $tables{ $item[2]->{'table_name'} }{'fields'}{ $item[4] }{'default'} =
793 # These will just parse for now but won't affect the structure. - ky
795 alter : alter_table table_id /rename/i /to/i NAME ';'
798 alter : alter_table table_id alter_column NAME SET /statistics/i INTEGER ';'
801 alter : alter_table table_id alter_column NAME SET /storage/i storage_type ';'
804 alter : alter_table table_id rename_column NAME /to/i NAME ';'
807 alter : alter_table table_id DROP /constraint/i NAME restrict_or_cascade ';'
810 alter : alter_table table_id /owner/i /to/i NAME ';'
813 alter : alter_sequence NAME /owned/i /by/i column_name ';'
816 storage_type : /(plain|external|extended|main)/i
818 temporary : /temp(orary)?\b/i
823 or_replace : /or replace/i
825 alter_default_val : SET default_val
827 $return = { value => $item[2]->{'value'} }
831 $return = { value => undef }
835 # This is a little tricky to get right, at least WRT to making the
836 # tests pass. The problem is that the constraints are stored just as
837 # a list (no name access), and the tests expect the constraints in a
838 # particular order. I'm going to leave the rule but disable the code
841 alter : alter_table table_id alter_column NAME alter_nullable ';'
843 # my $table_name = $item[2]->{'table_name'};
844 # my $field_name = $item[4];
845 # my $is_nullable = $item[5]->{'is_nullable'};
847 # $tables{ $table_name }{'fields'}{ $field_name }{'is_nullable'} =
850 # if ( $is_nullable ) {
852 # push @{ $tables{ $table_name }{'constraints'} }, {
853 # type => 'not_null',
854 # fields => [ $field_name ],
859 # 0 .. $#{ $tables{ $table_name }{'constraints'} || [] }
861 # my $c = $tables{ $table_name }{'constraints'}[ $i ] or next;
862 # my $fields = join( '', @{ $c->{'fields'} || [] } ) or next;
863 # if ( $c->{'type'} eq 'not_null' && $fields eq $field_name ) {
864 # delete $tables{ $table_name }{'constraints'}[ $i ];
873 alter_nullable : SET not_null
875 $return = { is_nullable => 0 }
879 $return = { is_nullable => 1 }
882 not_null : /not/i /null/i
886 add_column : ADD COLUMN(?)
888 alter_table : ALTER TABLE ONLY(?)
890 alter_sequence : ALTER SEQUENCE
892 drop_column : DROP COLUMN(?)
894 alter_column : ALTER COLUMN(?)
896 rename_column : /rename/i COLUMN(?)
898 restrict_or_cascade : /restrict/i |
901 # Handle functions that can be called
902 select : SELECT select_function ';'
905 # Read the setval function but don't do anything with it because this parser
906 # isn't handling sequences
907 select_function : schema_qualification(?) /setval/i '(' VALUE /,/ VALUE /,/ /(true|false)/i ')'
910 # Skipping all COPY commands
911 copy : COPY WORD /[^;]+/ ';' { 1 }
914 # The "\." allows reading in from STDIN but this isn't needed for schema
915 # creation, so it is skipped.
920 # End basically useless stuff. - ky
923 create_table : CREATE TABLE
925 create_index : CREATE /index/i
927 default_val : DEFAULT DEFAULT_VALUE ( '::' data_type )(?)
930 $val =~ s/^\((\d+)\)\z/$1/; # for example (0)::smallint
932 supertype => 'constraint',
940 supertype => 'constraint',
946 DEFAULT_VALUE : VALUE
951 name_with_opt_paren : NAME parens_value_list(s?)
952 { $item[2][0] ? "$item[1]($item[2][0][0])" : $item[1] }
954 unique : /unique/i { 1 }
956 key : /key/i | /index/i
958 table_option : /inherits/i '(' NAME(s /,/) ')'
960 $return = { type => 'inherits', table_name => $item[3] }
965 $return = { type => $item[1] =~ /out/i ? 'without_oids' : 'with_oids' }
988 SEMICOLON : /\s*;\n?/
990 SEQUENCE : /sequence/i
1009 DQSTRING : '"' <skip: ''> /((?:[^"]|"")+)/ '"'
1010 { ($return = $item[3]) =~ s/""/"/g; }
1012 SQSTRING : "'" <skip: ''> /((?:[^']|'')*)/ "'"
1013 { ($return = $item[3]) =~ s/''/'/g }
1015 DOLLARSTRING : /\$[^\$]*\$/ <skip: ''> /.*?(?=\Q$item[1]\E)/s "$item[1]"
1016 { $return = $item[3]; }
1018 VALUE : /[-+]?\d*\.?\d+(?:[eE]\d+)?/
1027 my ( $translator, $data ) = @_;
1029 # Enable warnings within the Parse::RecDescent module.
1030 local $::RD_ERRORS = 1 unless defined $::RD_ERRORS; # Make sure the parser dies when it encounters an error
1031 local $::RD_WARN = 1 unless defined $::RD_WARN; # Enable warnings. This will warn on unused rules &c.
1032 local $::RD_HINT = 1 unless defined $::RD_HINT; # Give out hints to help fix problems.
1034 local $::RD_TRACE = $translator->trace ? 1 : undef;
1035 local $DEBUG = $translator->debug;
1037 my $parser = ddl_parser_instance('PostgreSQL');
1039 my $result = $parser->startrule($data);
1040 die "Parse failed.\n" unless defined $result;
1041 warn Dumper($result) if $DEBUG;
1043 my $schema = $translator->schema;
1045 ( $result->{tables}{ $a }{'order'} || 0 ) <=> ( $result->{tables}{ $b }{'order'} || 0 )
1046 } keys %{ $result->{tables} };
1048 for my $table_name ( @tables ) {
1049 my $tdata = $result->{tables}{ $table_name };
1050 my $table = $schema->add_table(
1051 #schema => $tdata->{'schema_name'},
1052 name => $tdata->{'table_name'},
1053 ) or die "Couldn't create table '$table_name': " . $schema->error;
1055 $table->extra(temporary => 1) if $tdata->{'temporary'};
1057 $table->comments( $tdata->{'comments'} );
1060 $tdata->{'fields'}{ $a }{'order'}
1062 $tdata->{'fields'}{ $b }{'order'}
1063 } keys %{ $tdata->{'fields'} };
1065 for my $fname ( @fields ) {
1066 my $fdata = $tdata->{'fields'}{ $fname };
1067 next if $fdata->{'drop'};
1068 my $field = $table->add_field(
1069 name => $fdata->{'name'},
1070 data_type => $fdata->{'data_type'},
1071 size => $fdata->{'size'},
1072 default_value => $fdata->{'default'},
1073 is_auto_increment => $fdata->{'is_auto_increment'},
1074 is_nullable => $fdata->{'is_nullable'},
1075 comments => $fdata->{'comments'},
1076 ) or die $table->error;
1078 $table->primary_key( $field->name ) if $fdata->{'is_primary_key'};
1080 for my $cdata ( @{ $fdata->{'constraints'} } ) {
1081 next unless $cdata->{'type'} eq 'foreign_key';
1082 $cdata->{'fields'} ||= [ $field->name ];
1083 push @{ $tdata->{'constraints'} }, $cdata;
1087 for my $idata ( @{ $tdata->{'indices'} || [] } ) {
1089 push @options, { using => $idata->{'method'} } if $idata->{method};
1090 push @options, { where => $idata->{'where'} } if $idata->{where};
1091 my $index = $table->add_index(
1092 name => $idata->{'name'},
1093 type => uc $idata->{'type'},
1094 fields => $idata->{'fields'},
1095 options => \@options
1096 ) or die $table->error . ' ' . $table->name;
1099 for my $cdata ( @{ $tdata->{'constraints'} || [] } ) {
1100 my $constraint = $table->add_constraint(
1101 name => $cdata->{'name'},
1102 type => $cdata->{'type'},
1103 fields => $cdata->{'fields'},
1104 reference_table => $cdata->{'reference_table'},
1105 reference_fields => $cdata->{'reference_fields'},
1106 match_type => $cdata->{'match_type'} || '',
1107 on_delete => $cdata->{'on_delete'} || $cdata->{'on_delete_do'},
1108 on_update => $cdata->{'on_update'} || $cdata->{'on_update_do'},
1109 expression => $cdata->{'expression'},
1110 ) or die "Can't add constraint of type '" .
1111 $cdata->{'type'} . "' to table '" . $table->name .
1112 "': " . $table->error;
1116 for my $vinfo (@{$result->{views}}) {
1117 my $sql = $vinfo->{sql};
1118 $sql =~ s/\A\s+|\s+\z//g;
1119 my $view = $schema->add_view (
1120 name => $vinfo->{view_name},
1122 fields => $vinfo->{fields},
1125 $view->extra ( temporary => 1 ) if $vinfo->{is_temporary};
1128 for my $trigger (@{ $result->{triggers} }) {
1129 $schema->add_trigger( %$trigger );
1137 # -------------------------------------------------------------------
1138 # Rescue the drowning and tie your shoestrings.
1139 # Henry David Thoreau
1140 # -------------------------------------------------------------------
1146 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>,
1147 Allen Day E<lt>allenday@ucla.eduE<gt>.
1151 perl(1), Parse::RecDescent.