1 package SQL::Translator::Parser::PostgreSQL;
3 # -------------------------------------------------------------------
4 # Copyright (C) 2002-2009 SQLFairy Authors
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License as
8 # published by the Free Software Foundation; version 2.
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 # -------------------------------------------------------------------
23 SQL::Translator::Parser::PostgreSQL - parser for PostgreSQL
28 use SQL::Translator::Parser::PostgreSQL;
30 my $translator = SQL::Translator->new;
31 $translator->parser("SQL::Translator::Parser::PostgreSQL");
35 The grammar was started from the MySQL parsers. Here is the description
39 (http://www.postgresql.org/docs/view.php?version=7.3&idoc=1&file=sql-createtable.html)
41 CREATE [ [ LOCAL ] { TEMPORARY | TEMP } ] TABLE table_name (
42 { column_name data_type [ DEFAULT default_expr ]
43 [ column_constraint [, ... ] ]
44 | table_constraint } [, ... ]
46 [ INHERITS ( parent_table [, ... ] ) ]
47 [ WITH OIDS | WITHOUT OIDS ]
49 where column_constraint is:
51 [ CONSTRAINT constraint_name ]
52 { NOT NULL | NULL | UNIQUE | PRIMARY KEY |
54 REFERENCES reftable [ ( refcolumn ) ] [ MATCH FULL | MATCH PARTIAL ]
55 [ ON DELETE action ] [ ON UPDATE action ] }
56 [ DEFERRABLE | NOT DEFERRABLE ]
57 [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]
59 and table_constraint is:
61 [ CONSTRAINT constraint_name ]
62 { UNIQUE ( column_name [, ... ] ) |
63 PRIMARY KEY ( column_name [, ... ] ) |
64 CHECK ( expression ) |
65 FOREIGN KEY ( column_name [, ... ] )
66 REFERENCES reftable [ ( refcolumn [, ... ] ) ]
67 [ MATCH FULL | MATCH PARTIAL ]
68 [ ON DELETE action ] [ ON UPDATE action ] }
69 [ DEFERRABLE | NOT DEFERRABLE ]
70 [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]
73 (http://www.postgresql.org/docs/view.php?version=7.3&idoc=1&file=sql-createindex.html)
75 CREATE [ UNIQUE ] INDEX index_name ON table
76 [ USING acc_method ] ( column [ ops_name ] [, ...] )
78 CREATE [ UNIQUE ] INDEX index_name ON table
79 [ USING acc_method ] ( func_name( column [, ... ]) [ ops_name ] )
84 ALTER TABLE [ ONLY ] table [ * ]
85 ADD [ COLUMN ] column type [ column_constraint [ ... ] ]
86 ALTER TABLE [ ONLY ] table [ * ]
87 ALTER [ COLUMN ] column { SET DEFAULT value | DROP DEFAULT }
88 ALTER TABLE [ ONLY ] table [ * ]
89 ALTER [ COLUMN ] column SET STATISTICS integer
90 ALTER TABLE [ ONLY ] table [ * ]
91 RENAME [ COLUMN ] column TO newcolumn
95 ADD table_constraint_definition
96 ALTER TABLE [ ONLY ] table
97 DROP CONSTRAINT constraint { RESTRICT | CASCADE }
103 CREATE [ OR REPLACE ] VIEW view [ ( column name list ) ] AS SELECT query
108 use vars qw[ $DEBUG $VERSION $GRAMMAR @EXPORT_OK ];
110 $DEBUG = 0 unless defined $DEBUG;
113 use Parse::RecDescent;
115 use base qw(Exporter);
117 @EXPORT_OK = qw(parse);
119 # Enable warnings within the Parse::RecDescent module.
120 $::RD_ERRORS = 1; # Make sure the parser dies when it encounters an error
121 $::RD_WARN = 1; # Enable warnings. This will warn on unused rules &c.
122 $::RD_HINT = 1; # Give out hints to help fix problems.
126 { my ( %tables, @views, $table_order, $field_order, @table_comments) }
129 # The "eofile" rule makes the parser fail if any "statement" rule
130 # fails. Otherwise, the first successful match by a "statement"
131 # won't cause the failure needed to know that the parse, as a whole,
134 startrule : statement(s) eofile { { tables => \%tables, views => \@views } }
157 connect : /^\s*\\\connect.*\n/
159 set : /set/i /[^;]*/ ';'
161 revoke : /revoke/i WORD(s /,/) /on/i TABLE(?) table_id /from/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 revoke : /revoke/i WORD(s /,/) /on/i SCHEMA(?) schema_name /from/i name_with_opt_quotes(s /,/) ';'
176 grant : /grant/i WORD(s /,/) /on/i TABLE(?) table_id /to/i name_with_opt_quotes(s /,/) ';'
178 my $table_info = $item{'table_id'};
179 my $schema_name = $table_info->{'schema_name'};
180 my $table_name = $table_info->{'table_name'};
181 push @{ $tables{ $table_name }{'permissions'} }, {
188 grant : /grant/i WORD(s /,/) /on/i SCHEMA(?) schema_name /to/i name_with_opt_quotes(s /,/) ';'
191 drop : /drop/i /[^;]*/ ';'
194 /'(\\.|''|[^\\\'])*'/
196 nonstring : /[^;\'"]+/
198 statement_body : string | nonstring
200 insert : /insert/i statement_body(s?) ';'
202 update : /update/i statement_body(s?) ';'
207 create : CREATE temporary(?) TABLE table_id '(' create_definition(s? /,/) ')' table_option(s?) ';'
209 my $table_info = $item{'table_id'};
210 my $schema_name = $table_info->{'schema_name'};
211 my $table_name = $table_info->{'table_name'};
212 $tables{ $table_name }{'order'} = ++$table_order;
213 $tables{ $table_name }{'schema_name'} = $schema_name;
214 $tables{ $table_name }{'table_name'} = $table_name;
216 $tables{ $table_name }{'temporary'} = $item[2][0];
218 if ( @table_comments ) {
219 $tables{ $table_name }{'comments'} = [ @table_comments ];
220 @table_comments = ();
224 for my $definition ( @{ $item[6] } ) {
225 if ( $definition->{'supertype'} eq 'field' ) {
226 my $field_name = $definition->{'name'};
227 $tables{ $table_name }{'fields'}{ $field_name } =
228 { %$definition, order => $field_order++ };
230 for my $constraint ( @{ $definition->{'constraints'} || [] } ) {
231 $constraint->{'fields'} = [ $field_name ];
232 push @{ $tables{ $table_name }{'constraints'} },
236 elsif ( $definition->{'supertype'} eq 'constraint' ) {
237 push @{ $tables{ $table_name }{'constraints'} }, $definition;
239 elsif ( $definition->{'supertype'} eq 'index' ) {
240 push @{ $tables{ $table_name }{'indices'} }, $definition;
244 for my $option ( @{ $item[8] } ) {
245 $tables{ $table_name }{'table_options(s?)'}{ $option->{'type'} } =
252 create : CREATE unique(?) /(index|key)/i index_name /on/i table_id using_method(?) '(' field_name(s /,/) ')' where_predicate(?) ';'
254 my $table_info = $item{'table_id'};
255 my $schema_name = $table_info->{'schema_name'};
256 my $table_name = $table_info->{'table_name'};
257 push @{ $tables{ $table_name }{'indices'} },
259 name => $item{'index_name'},
260 supertype => $item{'unique'}[0] ? 'constraint' : 'index',
261 type => $item{'unique'}[0] ? 'unique' : 'normal',
263 method => $item{'using_method'}[0],
268 create : CREATE or_replace(?) temporary(?) VIEW view_id view_fields(?) /AS/i view_target ';'
271 schema_name => $item{view_id}{schema_name},
272 view_name => $item{view_id}{view_name},
273 sql => $item{view_target},
275 is_temporary => $item[3][0],
280 # Create anything else (e.g., domain, etc.)
282 create : CREATE WORD /[^;]+/ ';'
283 { @table_comments = (); }
285 using_method : /using/i WORD { $item[2] }
287 where_predicate : /where/i /[^;]+/
289 create_definition : field
293 comment : /^\s*(?:#|-{2})(.*)\n/
295 my $comment = $item[1];
296 $comment =~ s/^\s*(#|-*)\s*//;
297 $comment =~ s/\s*$//;
299 push @table_comments, $comment;
302 comment_on_table : /comment/i /on/i /table/i table_id /is/i comment_phrase ';'
304 my $table_info = $item{'table_id'};
305 my $schema_name = $table_info->{'schema_name'};
306 my $table_name = $table_info->{'table_name'};
307 push @{ $tables{ $table_name }{'comments'} }, $item{'comment_phrase'};
310 comment_on_column : /comment/i /on/i /column/i column_name /is/i comment_phrase ';'
312 my $table_name = $item[4]->{'table'};
313 my $field_name = $item[4]->{'field'};
314 if ($tables{ $table_name }{'fields'}{ $field_name } ) {
315 push @{ $tables{ $table_name }{'fields'}{ $field_name }{'comments'} },
316 $item{'comment_phrase'};
319 die "No such column as $table_name.$field_name";
323 comment_on_other : /comment/i /on/i /\w+/ /\w+/ /is/i comment_phrase ';'
325 push(@table_comments, $item{'comment_phrase'});
328 # [added by cjm 20041019]
329 # [TODO: other comment-on types]
330 # for now we just have a general mechanism for handling other
331 # kinds of comments than table/column; I'm not sure of the best
332 # way to incorporate these into the datamodel
334 # this is the exhaustive list of types of comment:
335 #COMMENT ON DATABASE my_database IS 'Development Database';
336 #COMMENT ON INDEX my_index IS 'Enforces uniqueness on employee id';
337 #COMMENT ON RULE my_rule IS 'Logs UPDATES of employee records';
338 #COMMENT ON SEQUENCE my_sequence IS 'Used to generate primary keys';
339 #COMMENT ON TABLE my_table IS 'Employee Information';
340 #COMMENT ON TYPE my_type IS 'Complex Number support';
341 #COMMENT ON VIEW my_view IS 'View of departmental costs';
342 #COMMENT ON COLUMN my_table.my_field IS 'Employee ID number';
343 #COMMENT ON TRIGGER my_trigger ON my_table IS 'Used for R.I.';
345 # this is tested by test 08
347 column_name : NAME '.' NAME
348 { $return = { table => $item[1], field => $item[3] } }
350 comment_phrase : /null/i
353 comment_phrase : /'/ comment_phrase_unquoted(s) /'/
354 { my $phrase = join(' ', @{ $item[2] });
357 # [cjm TODO: double-single quotes in a comment_phrase]
358 comment_phrase_unquoted : /[^\']*/
359 { $return = $item[1] }
362 xxxcomment_phrase : /'.*?'|NULL/
364 my $val = $item[1] || '';
369 field : field_comment(s?) field_name data_type field_meta(s?) field_comment(s?)
371 my ( $default, @constraints, $is_pk );
373 for my $meta ( @{ $item[4] } ) {
374 if ( $meta->{'type'} eq 'default' ) {
378 elsif ( $meta->{'type'} eq 'not_null' ) {
381 elsif ( $meta->{'type'} eq 'primary_key' ) {
385 push @constraints, $meta if $meta->{'supertype'} eq 'constraint';
388 my @comments = ( @{ $item[1] }, @{ $item[5] } );
391 supertype => 'field',
392 name => $item{'field_name'},
393 data_type => $item{'data_type'}{'type'},
394 size => $item{'data_type'}{'size'},
395 is_nullable => $is_nullable,
396 default => $default->{'value'},
397 constraints => [ @constraints ],
398 comments => [ @comments ],
399 is_primary_key => $is_pk || 0,
400 is_auto_increment => $item{'data_type'}{'is_auto_increment'},
405 field_comment : /^\s*(?:#|-{2})(.*)\n/
407 my $comment = $item[1];
408 $comment =~ s/^\s*(#|-*)\s*//;
409 $comment =~ s/\s*$//;
413 field_meta : default_val
416 view_fields : '(' field_name(s /,/) ')'
417 { $return = join (',', @{$item[2]} ) }
419 column_constraint : constraint_name(?) column_constraint_type deferrable(?) deferred(?)
421 my $desc = $item{'column_constraint_type'};
422 my $type = $desc->{'type'};
423 my $fields = $desc->{'fields'} || [];
424 my $expression = $desc->{'expression'} || '';
427 supertype => 'constraint',
428 name => $item{'constraint_name'}[0] || '',
430 expression => $type eq 'check' ? $expression : '',
431 deferrable => $item{'deferrable'},
432 deferred => $item{'deferred'},
433 reference_table => $desc->{'reference_table'},
434 reference_fields => $desc->{'reference_fields'},
435 match_type => $desc->{'match_type'},
436 on_delete => $desc->{'on_delete'} || $desc->{'on_delete_do'},
437 on_update => $desc->{'on_update'} || $desc->{'on_update_do'},
441 constraint_name : /constraint/i name_with_opt_quotes { $item[2] }
443 column_constraint_type : /not null/i { $return = { type => 'not_null' } }
446 { $return = { type => 'null' } }
449 { $return = { type => 'unique' } }
452 { $return = { type => 'primary_key' } }
454 /check/i '(' /[^)]+/ ')'
455 { $return = { type => 'check', expression => $item[3] } }
457 /references/i table_id parens_word_list(?) match_type(?) key_action(s?)
459 my $table_info = $item{'table_id'};
460 my $schema_name = $table_info->{'schema_name'};
461 my $table_name = $table_info->{'table_name'};
462 my ( $on_delete, $on_update );
463 for my $action ( @{ $item[5] || [] } ) {
464 $on_delete = $action->{'action'} if $action->{'type'} eq 'delete';
465 $on_update = $action->{'action'} if $action->{'type'} eq 'update';
469 type => 'foreign_key',
470 reference_table => $table_name,
471 reference_fields => $item[3][0],
472 match_type => $item[4][0],
473 on_delete => $on_delete,
474 on_update => $on_update,
478 table_id : schema_qualification(?) name_with_opt_quotes {
479 $return = { schema_name => $item[1][0], table_name => $item[2] }
482 view_id : schema_qualification(?) name_with_opt_quotes {
483 $return = { schema_name => $item[1][0], view_name => $item[2] }
486 view_target : /select|with/i /[^;]+/ {
487 $return = "$item[1] $item[2]";
490 # SELECT views _may_ support outer parens, and we used to produce
491 # such sql, although non-standard. Use ugly lookeahead to parse
492 view_target : '(' /select/i / [^;]+ (?= \) ) /x ')' {
493 $return = "$item[2] $item[3]"
498 schema_qualification : name_with_opt_quotes '.'
500 schema_name : name_with_opt_quotes
502 field_name : name_with_opt_quotes
504 name_with_opt_quotes : double_quote(?) NAME double_quote(?) { $item[2] }
508 index_name : name_with_opt_quotes
510 data_type : pg_data_type parens_value_list(?)
512 my $data_type = $item[1];
515 # We can deduce some sizes from the data type's name.
517 if ( my $size = $item[2][0] ) {
518 $data_type->{'size'} = $size;
521 $return = $data_type;
543 $return = { type => 'interval' };
546 /(integer|int4?)/i # interval must come before this
562 /(double precision|float8?)/i
570 /(bigserial|serial8)/i
575 is_auto_increment => 1,
584 is_auto_increment => 1,
588 /(bit varying|varbit)/i
590 $return = { type => 'varbit' };
595 $return = { type => 'varchar' };
600 $return = { type => 'char' };
605 $return = { type => 'boolean' };
610 $return = { type => 'bytea' };
613 /(timestamptz|timestamp)(?:\(\d\))?( with(?:out)? time zone)?/i
615 $return = { type => 'timestamp' . ($2||'') };
626 /(bit|box|cidr|circle|date|inet|line|lseg|macaddr|money|numeric|decimal|path|point|polygon|timetz|time|varchar)/i
628 $return = { type => $item[1] };
631 parens_value_list : '(' VALUE(s /,/) ')'
635 parens_word_list : '(' name_with_opt_quotes(s /,/) ')'
638 field_size : '(' num_range ')' { $item{'num_range'} }
640 num_range : DIGITS ',' DIGITS
641 { $return = $item[1].','.$item[3] }
643 { $return = $item[1] }
645 table_constraint : comment(s?) constraint_name(?) table_constraint_type deferrable(?) deferred(?) comment(s?)
647 my $desc = $item{'table_constraint_type'};
648 my $type = $desc->{'type'};
649 my $fields = $desc->{'fields'};
650 my $expression = $desc->{'expression'};
651 my @comments = ( @{ $item[1] }, @{ $item[-1] } );
654 name => $item[2][0] || '',
655 supertype => 'constraint',
657 fields => $type ne 'check' ? $fields : [],
658 expression => $type eq 'check' ? $expression : '',
659 deferrable => $item{'deferrable'},
660 deferred => $item{'deferred'},
661 reference_table => $desc->{'reference_table'},
662 reference_fields => $desc->{'reference_fields'},
663 match_type => $desc->{'match_type'},
664 on_delete => $desc->{'on_delete'} || $desc->{'on_delete_do'},
665 on_update => $desc->{'on_update'} || $desc->{'on_update_do'},
666 comments => [ @comments ],
670 table_constraint_type : /primary key/i '(' name_with_opt_quotes(s /,/) ')'
673 type => 'primary_key',
678 /unique/i '(' name_with_opt_quotes(s /,/) ')'
686 /check/i '(' /[^)]+/ ')'
690 expression => $item[3],
694 /foreign key/i '(' name_with_opt_quotes(s /,/) ')' /references/i table_id parens_word_list(?) match_type(?) key_action(s?)
696 my ( $on_delete, $on_update );
697 for my $action ( @{ $item[9] || [] } ) {
698 $on_delete = $action->{'action'} if $action->{'type'} eq 'delete';
699 $on_update = $action->{'action'} if $action->{'type'} eq 'update';
703 supertype => 'constraint',
704 type => 'foreign_key',
706 reference_table => $item[6]->{'table_name'},
707 reference_fields => $item[7][0],
708 match_type => $item[8][0],
709 on_delete => $on_delete || '',
710 on_update => $on_update || '',
714 deferrable : not(?) /deferrable/i
716 $return = ( $item[1] =~ /not/i ) ? 0 : 1;
719 deferred : /initially/i /(deferred|immediate)/i { $item[2] }
721 match_type : /match/i /partial|full|simple/i { $item[2] }
723 key_action : key_delete
727 key_delete : /on delete/i key_mutation
735 key_update : /on update/i key_mutation
743 key_mutation : /no action/i { $return = 'no_action' }
745 /restrict/i { $return = 'restrict' }
747 /cascade/i { $return = 'cascade' }
749 /set null/i { $return = 'set null' }
751 /set default/i { $return = 'set default' }
753 alter : alter_table table_id add_column field ';'
755 my $field_def = $item[4];
756 $tables{ $item[2]->{'table_name'} }{'fields'}{ $field_def->{'name'} } = {
757 %$field_def, order => $field_order++
762 alter : alter_table table_id ADD table_constraint ';'
764 my $table_name = $item[2]->{'table_name'};
765 my $constraint = $item[4];
766 push @{ $tables{ $table_name }{'constraints'} }, $constraint;
770 alter : alter_table table_id drop_column NAME restrict_or_cascade(?) ';'
772 $tables{ $item[2]->{'table_name'} }{'fields'}{ $item[4] }{'drop'} = 1;
776 alter : alter_table table_id alter_column NAME alter_default_val ';'
778 $tables{ $item[2]->{'table_name'} }{'fields'}{ $item[4] }{'default'} =
784 # These will just parse for now but won't affect the structure. - ky
786 alter : alter_table table_id /rename/i /to/i NAME ';'
789 alter : alter_table table_id alter_column NAME SET /statistics/i INTEGER ';'
792 alter : alter_table table_id alter_column NAME SET /storage/i storage_type ';'
795 alter : alter_table table_id rename_column NAME /to/i NAME ';'
798 alter : alter_table table_id DROP /constraint/i NAME restrict_or_cascade ';'
801 alter : alter_table table_id /owner/i /to/i NAME ';'
804 alter : alter_sequence NAME /owned/i /by/i column_name ';'
807 storage_type : /(plain|external|extended|main)/i
809 temporary : /temp(orary)?\\b/i
814 or_replace : /or replace/i
816 alter_default_val : SET default_val
818 $return = { value => $item[2]->{'value'} }
822 $return = { value => undef }
826 # This is a little tricky to get right, at least WRT to making the
827 # tests pass. The problem is that the constraints are stored just as
828 # a list (no name access), and the tests expect the constraints in a
829 # particular order. I'm going to leave the rule but disable the code
832 alter : alter_table table_id alter_column NAME alter_nullable ';'
834 # my $table_name = $item[2]->{'table_name'};
835 # my $field_name = $item[4];
836 # my $is_nullable = $item[5]->{'is_nullable'};
838 # $tables{ $table_name }{'fields'}{ $field_name }{'is_nullable'} =
841 # if ( $is_nullable ) {
843 # push @{ $tables{ $table_name }{'constraints'} }, {
844 # type => 'not_null',
845 # fields => [ $field_name ],
850 # 0 .. $#{ $tables{ $table_name }{'constraints'} || [] }
852 # my $c = $tables{ $table_name }{'constraints'}[ $i ] or next;
853 # my $fields = join( '', @{ $c->{'fields'} || [] } ) or next;
854 # if ( $c->{'type'} eq 'not_null' && $fields eq $field_name ) {
855 # delete $tables{ $table_name }{'constraints'}[ $i ];
864 alter_nullable : SET not_null
866 $return = { is_nullable => 0 }
870 $return = { is_nullable => 1 }
873 not_null : /not/i /null/i
877 add_column : ADD COLUMN(?)
879 alter_table : ALTER TABLE ONLY(?)
881 alter_sequence : ALTER SEQUENCE
883 drop_column : DROP COLUMN(?)
885 alter_column : ALTER COLUMN(?)
887 rename_column : /rename/i COLUMN(?)
889 restrict_or_cascade : /restrict/i |
892 # Handle functions that can be called
893 select : SELECT select_function ';'
896 # Read the setval function but don't do anything with it because this parser
897 # isn't handling sequences
898 select_function : schema_qualification(?) /setval/i '(' VALUE /,/ VALUE /,/ /(true|false)/i ')'
901 # Skipping all COPY commands
902 copy : COPY WORD /[^;]+/ ';' { 1 }
905 # The "\." allows reading in from STDIN but this isn't needed for schema
906 # creation, so it is skipped.
911 # End basically useless stuff. - ky
914 create_table : CREATE TABLE
916 create_index : CREATE /index/i
918 default_val : DEFAULT /(\d+|'[^']*'|\w+\(.*\))|\w+/
920 my $val = defined $item[2] ? $item[2] : '';
923 supertype => 'constraint',
931 supertype => 'constraint',
937 name_with_opt_paren : NAME parens_value_list(s?)
938 { $item[2][0] ? "$item[1]($item[2][0][0])" : $item[1] }
940 unique : /unique/i { 1 }
942 key : /key/i | /index/i
944 table_option : /inherits/i '(' name_with_opt_quotes(s /,/) ')'
946 $return = { type => 'inherits', table_name => $item[3] }
951 $return = { type => $item[1] =~ /out/i ? 'without_oids' : 'with_oids' }
974 SEMICOLON : /\s*;\n?/
976 SEQUENCE : /sequence/i
999 VALUE : /[-+]?\.?\d+(?:[eE]\d+)?/
1001 | /'.*?'/ # XXX doesn't handle embedded quotes
1008 # -------------------------------------------------------------------
1010 my ( $translator, $data ) = @_;
1011 my $parser = Parse::RecDescent->new($GRAMMAR);
1013 $::RD_TRACE = $translator->trace ? 1 : undef;
1014 $DEBUG = $translator->debug;
1016 unless (defined $parser) {
1017 return $translator->error("Error instantiating Parse::RecDescent ".
1018 "instance: Bad grammer");
1021 my $result = $parser->startrule($data);
1022 die "Parse failed.\n" unless defined $result;
1023 warn Dumper($result) if $DEBUG;
1025 my $schema = $translator->schema;
1027 ( $result->{tables}{ $a }{'order'} || 0 ) <=> ( $result->{tables}{ $b }{'order'} || 0 )
1028 } keys %{ $result->{tables} };
1030 for my $table_name ( @tables ) {
1031 my $tdata = $result->{tables}{ $table_name };
1032 my $table = $schema->add_table(
1033 #schema => $tdata->{'schema_name'},
1034 name => $tdata->{'table_name'},
1035 ) or die "Couldn't create table '$table_name': " . $schema->error;
1037 $table->extra(temporary => 1) if $tdata->{'temporary'};
1039 $table->comments( $tdata->{'comments'} );
1042 $tdata->{'fields'}{ $a }{'order'}
1044 $tdata->{'fields'}{ $b }{'order'}
1045 } keys %{ $tdata->{'fields'} };
1047 for my $fname ( @fields ) {
1048 my $fdata = $tdata->{'fields'}{ $fname };
1049 next if $fdata->{'drop'};
1050 my $field = $table->add_field(
1051 name => $fdata->{'name'},
1052 data_type => $fdata->{'data_type'},
1053 size => $fdata->{'size'},
1054 default_value => $fdata->{'default'},
1055 is_auto_increment => $fdata->{'is_auto_increment'},
1056 is_nullable => $fdata->{'is_nullable'},
1057 comments => $fdata->{'comments'},
1058 ) or die $table->error;
1060 $table->primary_key( $field->name ) if $fdata->{'is_primary_key'};
1062 for my $cdata ( @{ $fdata->{'constraints'} } ) {
1063 next unless $cdata->{'type'} eq 'foreign_key';
1064 $cdata->{'fields'} ||= [ $field->name ];
1065 push @{ $tdata->{'constraints'} }, $cdata;
1069 for my $idata ( @{ $tdata->{'indices'} || [] } ) {
1070 my $index = $table->add_index(
1071 name => $idata->{'name'},
1072 type => uc $idata->{'type'},
1073 fields => $idata->{'fields'},
1074 ) or die $table->error . ' ' . $table->name;
1077 for my $cdata ( @{ $tdata->{'constraints'} || [] } ) {
1078 my $constraint = $table->add_constraint(
1079 name => $cdata->{'name'},
1080 type => $cdata->{'type'},
1081 fields => $cdata->{'fields'},
1082 reference_table => $cdata->{'reference_table'},
1083 reference_fields => $cdata->{'reference_fields'},
1084 match_type => $cdata->{'match_type'} || '',
1085 on_delete => $cdata->{'on_delete'} || $cdata->{'on_delete_do'},
1086 on_update => $cdata->{'on_update'} || $cdata->{'on_update_do'},
1087 expression => $cdata->{'expression'},
1088 ) or die "Can't add constraint of type '" .
1089 $cdata->{'type'} . "' to table '" . $table->name .
1090 "': " . $table->error;
1094 for my $vinfo (@{$result->{views}}) {
1095 my $sql = $vinfo->{sql};
1096 $sql =~ s/\A\s+|\s+\z//g;
1097 my $view = $schema->add_view (
1098 name => $vinfo->{view_name},
1100 fields => $vinfo->{fields},
1103 $view->extra ( temporary => 1 ) if $vinfo->{is_temporary};
1111 # -------------------------------------------------------------------
1112 # Rescue the drowning and tie your shoestrings.
1113 # Henry David Thoreau
1114 # -------------------------------------------------------------------
1120 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>,
1121 Allen Day E<lt>allenday@ucla.eduE<gt>.
1125 perl(1), Parse::RecDescent.