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 our ( $DEBUG, $GRAMMAR, @EXPORT_OK );
92 our $VERSION = '1.59';
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, @triggers, $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 {
121 triggers => \@triggers,
147 commit : /commit/i ';'
149 connect : /^\s*\\\connect.*\n/
151 set : /set/i /[^;]*/ ';'
153 revoke : /revoke/i WORD(s /,/) /on/i TABLE(?) table_id /from/i name_with_opt_quotes(s /,/) ';'
155 my $table_info = $item{'table_id'};
156 my $schema_name = $table_info->{'schema_name'};
157 my $table_name = $table_info->{'table_name'};
158 push @{ $tables{ $table_name }{'permissions'} }, {
165 revoke : /revoke/i WORD(s /,/) /on/i SCHEMA(?) schema_name /from/i name_with_opt_quotes(s /,/) ';'
168 grant : /grant/i WORD(s /,/) /on/i TABLE(?) table_id /to/i name_with_opt_quotes(s /,/) ';'
170 my $table_info = $item{'table_id'};
171 my $schema_name = $table_info->{'schema_name'};
172 my $table_name = $table_info->{'table_name'};
173 push @{ $tables{ $table_name }{'permissions'} }, {
180 grant : /grant/i WORD(s /,/) /on/i SCHEMA(?) schema_name /to/i name_with_opt_quotes(s /,/) ';'
183 drop : /drop/i /[^;]*/ ';'
186 /'(\\.|''|[^\\\'])*'/
188 nonstring : /[^;\'"]+/
190 statement_body : string | nonstring
192 insert : /insert/i statement_body(s?) ';'
194 update : /update/i statement_body(s?) ';'
199 create : CREATE temporary(?) TABLE table_id '(' create_definition(s? /,/) ')' table_option(s?) ';'
201 my $table_info = $item{'table_id'};
202 my $schema_name = $table_info->{'schema_name'};
203 my $table_name = $table_info->{'table_name'};
204 $tables{ $table_name }{'order'} = ++$table_order;
205 $tables{ $table_name }{'schema_name'} = $schema_name;
206 $tables{ $table_name }{'table_name'} = $table_name;
208 $tables{ $table_name }{'temporary'} = $item[2][0];
210 if ( @table_comments ) {
211 $tables{ $table_name }{'comments'} = [ @table_comments ];
212 @table_comments = ();
216 for my $definition ( @{ $item[6] } ) {
217 if ( $definition->{'supertype'} eq 'field' ) {
218 my $field_name = $definition->{'name'};
219 $tables{ $table_name }{'fields'}{ $field_name } =
220 { %$definition, order => $field_order++ };
222 for my $constraint ( @{ $definition->{'constraints'} || [] } ) {
223 $constraint->{'fields'} = [ $field_name ];
224 push @{ $tables{ $table_name }{'constraints'} },
228 elsif ( $definition->{'supertype'} eq 'constraint' ) {
229 push @{ $tables{ $table_name }{'constraints'} }, $definition;
231 elsif ( $definition->{'supertype'} eq 'index' ) {
232 push @{ $tables{ $table_name }{'indices'} }, $definition;
236 for my $option ( @{ $item[8] } ) {
237 $tables{ $table_name }{'table_options(s?)'}{ $option->{'type'} } =
244 create : CREATE unique(?) /(index|key)/i index_name /on/i table_id using_method(?) '(' field_name(s /,/) ')' where_predicate(?) ';'
246 my $table_info = $item{'table_id'};
247 my $schema_name = $table_info->{'schema_name'};
248 my $table_name = $table_info->{'table_name'};
249 push @{ $tables{ $table_name }{'indices'} },
251 name => $item{'index_name'},
252 supertype => $item{'unique'}[0] ? 'constraint' : 'index',
253 type => $item{'unique'}[0] ? 'unique' : 'normal',
255 method => $item{'using_method'}[0],
260 create : CREATE or_replace(?) temporary(?) VIEW view_id view_fields(?) /AS/i view_target ';'
263 schema_name => $item{view_id}{schema_name},
264 view_name => $item{view_id}{view_name},
265 sql => $item{view_target},
267 is_temporary => $item[3][0],
271 trigger_name : name_with_opt_quotes
273 trigger_scope : /FOR/i /EACH/i /(ROW|STATEMENT)/i { $return = lc $1 }
275 before_or_after : /(before|after)/i { $return = lc $1 }
277 trigger_action : /.+/
279 database_event : /insert|update|delete/i
280 database_events : database_event(s /OR/)
282 create : CREATE /TRIGGER/i trigger_name before_or_after database_events /ON/i table_id trigger_scope(?) trigger_action
284 # Hack to pass roundtrip tests which have trigger statements terminated by double semicolon
285 # and expect the returned data to have the same
286 my $action = $item{trigger_action};
290 name => $item{trigger_name},
291 perform_action_when => $item{before_or_after},
292 database_events => $item{database_events},
293 on_table => $item{table_id}{table_name},
294 scope => $item{'trigger_scope(?)'}[0],
300 # Create anything else (e.g., domain, etc.)
302 create : CREATE WORD /[^;]+/ ';'
303 { @table_comments = (); }
305 using_method : /using/i WORD { $item[2] }
307 where_predicate : /where/i /[^;]+/
309 create_definition : field
313 comment : /^\s*(?:#|-{2})(.*)\n/
315 my $comment = $item[1];
316 $comment =~ s/^\s*(#|-*)\s*//;
317 $comment =~ s/\s*$//;
319 push @table_comments, $comment;
322 comment_on_table : /comment/i /on/i /table/i table_id /is/i comment_phrase ';'
324 my $table_info = $item{'table_id'};
325 my $schema_name = $table_info->{'schema_name'};
326 my $table_name = $table_info->{'table_name'};
327 push @{ $tables{ $table_name }{'comments'} }, $item{'comment_phrase'};
330 comment_on_column : /comment/i /on/i /column/i column_name /is/i comment_phrase ';'
332 my $table_name = $item[4]->{'table'};
333 my $field_name = $item[4]->{'field'};
334 if ($tables{ $table_name }{'fields'}{ $field_name } ) {
335 push @{ $tables{ $table_name }{'fields'}{ $field_name }{'comments'} },
336 $item{'comment_phrase'};
339 die "No such column as $table_name.$field_name";
343 comment_on_other : /comment/i /on/i /\w+/ /\w+/ /is/i comment_phrase ';'
345 push(@table_comments, $item{'comment_phrase'});
348 # [added by cjm 20041019]
349 # [TODO: other comment-on types]
350 # for now we just have a general mechanism for handling other
351 # kinds of comments than table/column; I'm not sure of the best
352 # way to incorporate these into the datamodel
354 # this is the exhaustive list of types of comment:
355 #COMMENT ON DATABASE my_database IS 'Development Database';
356 #COMMENT ON INDEX my_index IS 'Enforces uniqueness on employee id';
357 #COMMENT ON RULE my_rule IS 'Logs UPDATES of employee records';
358 #COMMENT ON SEQUENCE my_sequence IS 'Used to generate primary keys';
359 #COMMENT ON TABLE my_table IS 'Employee Information';
360 #COMMENT ON TYPE my_type IS 'Complex Number support';
361 #COMMENT ON VIEW my_view IS 'View of departmental costs';
362 #COMMENT ON COLUMN my_table.my_field IS 'Employee ID number';
363 #COMMENT ON TRIGGER my_trigger ON my_table IS 'Used for R.I.';
365 # this is tested by test 08
367 column_name : NAME '.' NAME
368 { $return = { table => $item[1], field => $item[3] } }
370 comment_phrase : /null/i
373 comment_phrase : /'/ comment_phrase_unquoted(s) /'/
374 { my $phrase = join(' ', @{ $item[2] });
377 # [cjm TODO: double-single quotes in a comment_phrase]
378 comment_phrase_unquoted : /[^\']*/
379 { $return = $item[1] }
382 xxxcomment_phrase : /'.*?'|NULL/
384 my $val = $item[1] || '';
389 field : field_comment(s?) field_name data_type field_meta(s?) field_comment(s?)
391 my ( $default, @constraints, $is_pk );
393 for my $meta ( @{ $item[4] } ) {
394 if ( $meta->{'type'} eq 'default' ) {
398 elsif ( $meta->{'type'} eq 'not_null' ) {
401 elsif ( $meta->{'type'} eq 'primary_key' ) {
405 push @constraints, $meta if $meta->{'supertype'} eq 'constraint';
408 my @comments = ( @{ $item[1] }, @{ $item[5] } );
411 supertype => 'field',
412 name => $item{'field_name'},
413 data_type => $item{'data_type'}{'type'},
414 size => $item{'data_type'}{'size'},
415 is_nullable => $is_nullable,
416 default => $default->{'value'},
417 constraints => [ @constraints ],
418 comments => [ @comments ],
419 is_primary_key => $is_pk || 0,
420 is_auto_increment => $item{'data_type'}{'is_auto_increment'},
425 field_comment : /^\s*(?:#|-{2})(.*)\n/
427 my $comment = $item[1];
428 $comment =~ s/^\s*(#|-*)\s*//;
429 $comment =~ s/\s*$//;
433 field_meta : default_val
436 view_fields : '(' field_name(s /,/) ')'
437 { $return = join (',', @{$item[2]} ) }
439 column_constraint : constraint_name(?) column_constraint_type deferrable(?) deferred(?)
441 my $desc = $item{'column_constraint_type'};
442 my $type = $desc->{'type'};
443 my $fields = $desc->{'fields'} || [];
444 my $expression = $desc->{'expression'} || '';
447 supertype => 'constraint',
448 name => $item{'constraint_name'}[0] || '',
450 expression => $type eq 'check' ? $expression : '',
451 deferrable => $item{'deferrable'},
452 deferred => $item{'deferred'},
453 reference_table => $desc->{'reference_table'},
454 reference_fields => $desc->{'reference_fields'},
455 match_type => $desc->{'match_type'},
456 on_delete => $desc->{'on_delete'} || $desc->{'on_delete_do'},
457 on_update => $desc->{'on_update'} || $desc->{'on_update_do'},
461 constraint_name : /constraint/i name_with_opt_quotes { $item[2] }
463 column_constraint_type : /not null/i { $return = { type => 'not_null' } }
466 { $return = { type => 'null' } }
469 { $return = { type => 'unique' } }
472 { $return = { type => 'primary_key' } }
474 /check/i '(' /[^)]+/ ')'
475 { $return = { type => 'check', expression => $item[3] } }
477 /references/i table_id parens_word_list(?) match_type(?) key_action(s?)
479 my $table_info = $item{'table_id'};
480 my $schema_name = $table_info->{'schema_name'};
481 my $table_name = $table_info->{'table_name'};
482 my ( $on_delete, $on_update );
483 for my $action ( @{ $item[5] || [] } ) {
484 $on_delete = $action->{'action'} if $action->{'type'} eq 'delete';
485 $on_update = $action->{'action'} if $action->{'type'} eq 'update';
489 type => 'foreign_key',
490 reference_table => $table_name,
491 reference_fields => $item[3][0],
492 match_type => $item[4][0],
493 on_delete => $on_delete,
494 on_update => $on_update,
498 table_id : schema_qualification(?) name_with_opt_quotes {
499 $return = { schema_name => $item[1][0], table_name => $item[2] }
502 view_id : schema_qualification(?) name_with_opt_quotes {
503 $return = { schema_name => $item[1][0], view_name => $item[2] }
506 view_target : /select|with/i /[^;]+/ {
507 $return = "$item[1] $item[2]";
510 # SELECT views _may_ support outer parens, and we used to produce
511 # such sql, although non-standard. Use ugly lookeahead to parse
512 view_target : '(' /select/i / [^;]+ (?= \) ) /x ')' {
513 $return = "$item[2] $item[3]"
518 schema_qualification : name_with_opt_quotes '.'
520 schema_name : name_with_opt_quotes
522 field_name : name_with_opt_quotes
524 name_with_opt_quotes : double_quote(?) NAME double_quote(?) { $item[2] }
528 index_name : name_with_opt_quotes
531 data_type : pg_data_type parens_value_list(?)
533 my $data_type = $item[1];
536 # We can deduce some sizes from the data type's name.
538 if ( my $size = $item[2][0] ) {
539 $data_type->{'size'} = $size;
542 $return = $data_type;
564 $return = { type => 'interval' };
567 /(integer|int4?)/i # interval must come before this
583 /(double precision|float8?)/i
591 /(bigserial|serial8)/i
596 is_auto_increment => 1,
605 is_auto_increment => 1,
609 /(bit varying|varbit)/i
611 $return = { type => 'varbit' };
616 $return = { type => 'varchar' };
621 $return = { type => 'char' };
626 $return = { type => 'boolean' };
631 $return = { type => 'bytea' };
634 /(timestamptz|timestamp)(?:\(\d\))?( with(?:out)? time zone)?/i
636 $return = { type => 'timestamp' . ($2||'') };
647 /(bit|box|cidr|circle|date|inet|line|lseg|macaddr|money|numeric|decimal|path|point|polygon|timetz|time|varchar)/i
649 $return = { type => $item[1] };
652 parens_value_list : '(' VALUE(s /,/) ')'
656 parens_word_list : '(' name_with_opt_quotes(s /,/) ')'
659 field_size : '(' num_range ')' { $item{'num_range'} }
661 num_range : DIGITS ',' DIGITS
662 { $return = $item[1].','.$item[3] }
664 { $return = $item[1] }
666 table_constraint : comment(s?) constraint_name(?) table_constraint_type deferrable(?) deferred(?) comment(s?)
668 my $desc = $item{'table_constraint_type'};
669 my $type = $desc->{'type'};
670 my $fields = $desc->{'fields'};
671 my $expression = $desc->{'expression'};
672 my @comments = ( @{ $item[1] }, @{ $item[-1] } );
675 name => $item[2][0] || '',
676 supertype => 'constraint',
678 fields => $type ne 'check' ? $fields : [],
679 expression => $type eq 'check' ? $expression : '',
680 deferrable => $item{'deferrable'},
681 deferred => $item{'deferred'},
682 reference_table => $desc->{'reference_table'},
683 reference_fields => $desc->{'reference_fields'},
684 match_type => $desc->{'match_type'},
685 on_delete => $desc->{'on_delete'} || $desc->{'on_delete_do'},
686 on_update => $desc->{'on_update'} || $desc->{'on_update_do'},
687 comments => [ @comments ],
691 table_constraint_type : /primary key/i '(' name_with_opt_quotes(s /,/) ')'
694 type => 'primary_key',
699 /unique/i '(' name_with_opt_quotes(s /,/) ')'
707 /check/i '(' /[^)]+/ ')'
711 expression => $item[3],
715 /foreign key/i '(' name_with_opt_quotes(s /,/) ')' /references/i table_id parens_word_list(?) match_type(?) key_action(s?)
717 my ( $on_delete, $on_update );
718 for my $action ( @{ $item[9] || [] } ) {
719 $on_delete = $action->{'action'} if $action->{'type'} eq 'delete';
720 $on_update = $action->{'action'} if $action->{'type'} eq 'update';
724 supertype => 'constraint',
725 type => 'foreign_key',
727 reference_table => $item[6]->{'table_name'},
728 reference_fields => $item[7][0],
729 match_type => $item[8][0],
730 on_delete => $on_delete || '',
731 on_update => $on_update || '',
735 deferrable : not(?) /deferrable/i
737 $return = ( $item[1] =~ /not/i ) ? 0 : 1;
740 deferred : /initially/i /(deferred|immediate)/i { $item[2] }
742 match_type : /match/i /partial|full|simple/i { $item[2] }
744 key_action : key_delete
748 key_delete : /on delete/i key_mutation
756 key_update : /on update/i key_mutation
764 key_mutation : /no action/i { $return = 'no_action' }
766 /restrict/i { $return = 'restrict' }
768 /cascade/i { $return = 'cascade' }
770 /set null/i { $return = 'set null' }
772 /set default/i { $return = 'set default' }
774 alter : alter_table table_id add_column field ';'
776 my $field_def = $item[4];
777 $tables{ $item[2]->{'table_name'} }{'fields'}{ $field_def->{'name'} } = {
778 %$field_def, order => $field_order++
783 alter : alter_table table_id ADD table_constraint ';'
785 my $table_name = $item[2]->{'table_name'};
786 my $constraint = $item[4];
787 push @{ $tables{ $table_name }{'constraints'} }, $constraint;
791 alter : alter_table table_id drop_column NAME restrict_or_cascade(?) ';'
793 $tables{ $item[2]->{'table_name'} }{'fields'}{ $item[4] }{'drop'} = 1;
797 alter : alter_table table_id alter_column NAME alter_default_val ';'
799 $tables{ $item[2]->{'table_name'} }{'fields'}{ $item[4] }{'default'} =
805 # These will just parse for now but won't affect the structure. - ky
807 alter : alter_table table_id /rename/i /to/i NAME ';'
810 alter : alter_table table_id alter_column NAME SET /statistics/i INTEGER ';'
813 alter : alter_table table_id alter_column NAME SET /storage/i storage_type ';'
816 alter : alter_table table_id rename_column NAME /to/i NAME ';'
819 alter : alter_table table_id DROP /constraint/i NAME restrict_or_cascade ';'
822 alter : alter_table table_id /owner/i /to/i NAME ';'
825 alter : alter_sequence NAME /owned/i /by/i column_name ';'
828 storage_type : /(plain|external|extended|main)/i
830 temporary : /temp(orary)?\\b/i
835 or_replace : /or replace/i
837 alter_default_val : SET default_val
839 $return = { value => $item[2]->{'value'} }
843 $return = { value => undef }
847 # This is a little tricky to get right, at least WRT to making the
848 # tests pass. The problem is that the constraints are stored just as
849 # a list (no name access), and the tests expect the constraints in a
850 # particular order. I'm going to leave the rule but disable the code
853 alter : alter_table table_id alter_column NAME alter_nullable ';'
855 # my $table_name = $item[2]->{'table_name'};
856 # my $field_name = $item[4];
857 # my $is_nullable = $item[5]->{'is_nullable'};
859 # $tables{ $table_name }{'fields'}{ $field_name }{'is_nullable'} =
862 # if ( $is_nullable ) {
864 # push @{ $tables{ $table_name }{'constraints'} }, {
865 # type => 'not_null',
866 # fields => [ $field_name ],
871 # 0 .. $#{ $tables{ $table_name }{'constraints'} || [] }
873 # my $c = $tables{ $table_name }{'constraints'}[ $i ] or next;
874 # my $fields = join( '', @{ $c->{'fields'} || [] } ) or next;
875 # if ( $c->{'type'} eq 'not_null' && $fields eq $field_name ) {
876 # delete $tables{ $table_name }{'constraints'}[ $i ];
885 alter_nullable : SET not_null
887 $return = { is_nullable => 0 }
891 $return = { is_nullable => 1 }
894 not_null : /not/i /null/i
898 add_column : ADD COLUMN(?)
900 alter_table : ALTER TABLE ONLY(?)
902 alter_sequence : ALTER SEQUENCE
904 drop_column : DROP COLUMN(?)
906 alter_column : ALTER COLUMN(?)
908 rename_column : /rename/i COLUMN(?)
910 restrict_or_cascade : /restrict/i |
913 # Handle functions that can be called
914 select : SELECT select_function ';'
917 # Read the setval function but don't do anything with it because this parser
918 # isn't handling sequences
919 select_function : schema_qualification(?) /setval/i '(' VALUE /,/ VALUE /,/ /(true|false)/i ')'
922 # Skipping all COPY commands
923 copy : COPY WORD /[^;]+/ ';' { 1 }
926 # The "\." allows reading in from STDIN but this isn't needed for schema
927 # creation, so it is skipped.
932 # End basically useless stuff. - ky
935 create_table : CREATE TABLE
937 create_index : CREATE /index/i
939 default_val : DEFAULT /(\d+|'[^']*'|\w+\(.*\))|\w+/
941 my $val = defined $item[2] ? $item[2] : '';
944 supertype => 'constraint',
952 supertype => 'constraint',
958 name_with_opt_paren : NAME parens_value_list(s?)
959 { $item[2][0] ? "$item[1]($item[2][0][0])" : $item[1] }
961 unique : /unique/i { 1 }
963 key : /key/i | /index/i
965 table_option : /inherits/i '(' name_with_opt_quotes(s /,/) ')'
967 $return = { type => 'inherits', table_name => $item[3] }
972 $return = { type => $item[1] =~ /out/i ? 'without_oids' : 'with_oids' }
995 SEMICOLON : /\s*;\n?/
997 SEQUENCE : /sequence/i
1013 NAME : "`" /\w+/ "`"
1020 VALUE : /[-+]?\.?\d+(?:[eE]\d+)?/
1022 | /'.*?'/ # XXX doesn't handle embedded quotes
1030 my ( $translator, $data ) = @_;
1031 my $parser = Parse::RecDescent->new($GRAMMAR);
1033 $::RD_TRACE = $translator->trace ? 1 : undef;
1034 $DEBUG = $translator->debug;
1036 unless (defined $parser) {
1037 return $translator->error("Error instantiating Parse::RecDescent ".
1038 "instance: Bad grammer");
1041 my $result = $parser->startrule($data);
1042 die "Parse failed.\n" unless defined $result;
1043 warn Dumper($result) if $DEBUG;
1045 my $schema = $translator->schema;
1047 ( $result->{tables}{ $a }{'order'} || 0 ) <=> ( $result->{tables}{ $b }{'order'} || 0 )
1048 } keys %{ $result->{tables} };
1050 for my $table_name ( @tables ) {
1051 my $tdata = $result->{tables}{ $table_name };
1052 my $table = $schema->add_table(
1053 #schema => $tdata->{'schema_name'},
1054 name => $tdata->{'table_name'},
1055 ) or die "Couldn't create table '$table_name': " . $schema->error;
1057 $table->extra(temporary => 1) if $tdata->{'temporary'};
1059 $table->comments( $tdata->{'comments'} );
1062 $tdata->{'fields'}{ $a }{'order'}
1064 $tdata->{'fields'}{ $b }{'order'}
1065 } keys %{ $tdata->{'fields'} };
1067 for my $fname ( @fields ) {
1068 my $fdata = $tdata->{'fields'}{ $fname };
1069 next if $fdata->{'drop'};
1070 my $field = $table->add_field(
1071 name => $fdata->{'name'},
1072 data_type => $fdata->{'data_type'},
1073 size => $fdata->{'size'},
1074 default_value => $fdata->{'default'},
1075 is_auto_increment => $fdata->{'is_auto_increment'},
1076 is_nullable => $fdata->{'is_nullable'},
1077 comments => $fdata->{'comments'},
1078 ) or die $table->error;
1080 $table->primary_key( $field->name ) if $fdata->{'is_primary_key'};
1082 for my $cdata ( @{ $fdata->{'constraints'} } ) {
1083 next unless $cdata->{'type'} eq 'foreign_key';
1084 $cdata->{'fields'} ||= [ $field->name ];
1085 push @{ $tdata->{'constraints'} }, $cdata;
1089 for my $idata ( @{ $tdata->{'indices'} || [] } ) {
1090 my $index = $table->add_index(
1091 name => $idata->{'name'},
1092 type => uc $idata->{'type'},
1093 fields => $idata->{'fields'},
1094 ) or die $table->error . ' ' . $table->name;
1097 for my $cdata ( @{ $tdata->{'constraints'} || [] } ) {
1098 my $constraint = $table->add_constraint(
1099 name => $cdata->{'name'},
1100 type => $cdata->{'type'},
1101 fields => $cdata->{'fields'},
1102 reference_table => $cdata->{'reference_table'},
1103 reference_fields => $cdata->{'reference_fields'},
1104 match_type => $cdata->{'match_type'} || '',
1105 on_delete => $cdata->{'on_delete'} || $cdata->{'on_delete_do'},
1106 on_update => $cdata->{'on_update'} || $cdata->{'on_update_do'},
1107 expression => $cdata->{'expression'},
1108 ) or die "Can't add constraint of type '" .
1109 $cdata->{'type'} . "' to table '" . $table->name .
1110 "': " . $table->error;
1114 for my $vinfo (@{$result->{views}}) {
1115 my $sql = $vinfo->{sql};
1116 $sql =~ s/\A\s+|\s+\z//g;
1117 my $view = $schema->add_view (
1118 name => $vinfo->{view_name},
1120 fields => $vinfo->{fields},
1123 $view->extra ( temporary => 1 ) if $vinfo->{is_temporary};
1126 for my $trigger (@{ $result->{triggers} }) {
1127 $schema->add_trigger( %$trigger );
1135 # -------------------------------------------------------------------
1136 # Rescue the drowning and tie your shoestrings.
1137 # Henry David Thoreau
1138 # -------------------------------------------------------------------
1144 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>,
1145 Allen Day E<lt>allenday@ucla.eduE<gt>.
1149 perl(1), Parse::RecDescent.