1 package SQL::Translator::Parser::PostgreSQL;
3 # -------------------------------------------------------------------
4 # $Id: PostgreSQL.pm,v 1.43 2004-10-23 20:18:44 cmungall Exp $
5 # -------------------------------------------------------------------
6 # Copyright (C) 2002-4 SQLFairy Authors
8 # This program is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License as
10 # published by the Free Software Foundation; version 2.
12 # This program is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 # -------------------------------------------------------------------
25 SQL::Translator::Parser::PostgreSQL - parser for PostgreSQL
30 use SQL::Translator::Parser::PostgreSQL;
32 my $translator = SQL::Translator->new;
33 $translator->parser("SQL::Translator::Parser::PostgreSQL");
37 The grammar was started from the MySQL parsers. Here is the description
41 (http://www.postgresql.org/docs/view.php?version=7.3&idoc=1&file=sql-createtable.html)
43 CREATE [ [ LOCAL ] { TEMPORARY | TEMP } ] TABLE table_name (
44 { column_name data_type [ DEFAULT default_expr ]
45 [ column_constraint [, ... ] ]
46 | table_constraint } [, ... ]
48 [ INHERITS ( parent_table [, ... ] ) ]
49 [ WITH OIDS | WITHOUT OIDS ]
51 where column_constraint is:
53 [ CONSTRAINT constraint_name ]
54 { NOT NULL | NULL | UNIQUE | PRIMARY KEY |
56 REFERENCES reftable [ ( refcolumn ) ] [ MATCH FULL | MATCH PARTIAL ]
57 [ ON DELETE action ] [ ON UPDATE action ] }
58 [ DEFERRABLE | NOT DEFERRABLE ]
59 [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]
61 and table_constraint is:
63 [ CONSTRAINT constraint_name ]
64 { UNIQUE ( column_name [, ... ] ) |
65 PRIMARY KEY ( column_name [, ... ] ) |
66 CHECK ( expression ) |
67 FOREIGN KEY ( column_name [, ... ] )
68 REFERENCES reftable [ ( refcolumn [, ... ] ) ]
69 [ MATCH FULL | MATCH PARTIAL ]
70 [ ON DELETE action ] [ ON UPDATE action ] }
71 [ DEFERRABLE | NOT DEFERRABLE ]
72 [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]
75 (http://www.postgresql.org/docs/view.php?version=7.3&idoc=1&file=sql-createindex.html)
77 CREATE [ UNIQUE ] INDEX index_name ON table
78 [ USING acc_method ] ( column [ ops_name ] [, ...] )
80 CREATE [ UNIQUE ] INDEX index_name ON table
81 [ USING acc_method ] ( func_name( column [, ... ]) [ ops_name ] )
86 ALTER TABLE [ ONLY ] table [ * ]
87 ADD [ COLUMN ] column type [ column_constraint [ ... ] ]
88 ALTER TABLE [ ONLY ] table [ * ]
89 ALTER [ COLUMN ] column { SET DEFAULT value | DROP DEFAULT }
90 ALTER TABLE [ ONLY ] table [ * ]
91 ALTER [ COLUMN ] column SET STATISTICS integer
92 ALTER TABLE [ ONLY ] table [ * ]
93 RENAME [ COLUMN ] column TO newcolumn
97 ADD table_constraint_definition
98 ALTER TABLE [ ONLY ] table
99 DROP CONSTRAINT constraint { RESTRICT | CASCADE }
105 CREATE [ OR REPLACE ] VIEW view [ ( column name list ) ] AS SELECT query
110 use vars qw[ $DEBUG $VERSION $GRAMMAR @EXPORT_OK ];
111 $VERSION = sprintf "%d.%02d", q$Revision: 1.43 $ =~ /(\d+)\.(\d+)/;
112 $DEBUG = 0 unless defined $DEBUG;
115 use Parse::RecDescent;
117 use base qw(Exporter);
119 @EXPORT_OK = qw(parse);
121 # Enable warnings within the Parse::RecDescent module.
122 $::RD_ERRORS = 1; # Make sure the parser dies when it encounters an error
123 $::RD_WARN = 1; # Enable warnings. This will warn on unused rules &c.
124 $::RD_HINT = 1; # Give out hints to help fix problems.
126 my $parser; # should we do this? There's no programmic way to
127 # change the grammar, so I think this is safe.
131 { my ( %tables, $table_order, $field_order, @table_comments) }
134 # The "eofile" rule makes the parser fail if any "statement" rule
135 # fails. Otherwise, the first successful match by a "statement"
136 # won't cause the failure needed to know that the parse, as a whole,
139 startrule : statement(s) eofile { \%tables }
159 connect : /^\s*\\\connect.*\n/
161 set : /set/i /[^;]*/ ';'
163 revoke : /revoke/i WORD(s /,/) /on/i TABLE(?) table_name /from/i name_with_opt_quotes(s /,/) ';'
165 my $table_name = $item{'table_name'};
166 push @{ $tables{ $table_name }{'permissions'} }, {
173 grant : /grant/i WORD(s /,/) /on/i TABLE(?) table_name /to/i name_with_opt_quotes(s /,/) ';'
175 my $table_name = $item{'table_name'};
176 push @{ $tables{ $table_name }{'permissions'} }, {
183 drop : /drop/i /[^;]*/ ';'
185 insert : /insert/i /[^;]*/ ';'
187 update : /update/i /[^;]*/ ';'
192 create : create_table table_name '(' create_definition(s /,/) ')' table_option(s?) ';'
194 my $table_name = $item{'table_name'};
195 $tables{ $table_name }{'order'} = ++$table_order;
196 $tables{ $table_name }{'table_name'} = $table_name;
198 if ( @table_comments ) {
199 $tables{ $table_name }{'comments'} = [ @table_comments ];
200 @table_comments = ();
204 for my $definition ( @{ $item[4] } ) {
205 if ( $definition->{'supertype'} eq 'field' ) {
206 my $field_name = $definition->{'name'};
207 $tables{ $table_name }{'fields'}{ $field_name } =
208 { %$definition, order => $field_order++ };
210 for my $constraint ( @{ $definition->{'constraints'} || [] } ) {
211 $constraint->{'fields'} = [ $field_name ];
212 push @{ $tables{ $table_name }{'constraints'} },
216 elsif ( $definition->{'supertype'} eq 'constraint' ) {
217 push @{ $tables{ $table_name }{'constraints'} }, $definition;
219 elsif ( $definition->{'supertype'} eq 'index' ) {
220 push @{ $tables{ $table_name }{'indices'} }, $definition;
224 for my $option ( @{ $item[6] } ) {
225 $tables{ $table_name }{'table_options(s?)'}{ $option->{'type'} } =
232 create : CREATE unique(?) /(index|key)/i index_name /on/i table_name using_method(?) '(' field_name(s /,/) ')' where_predicate(?) ';'
234 push @{ $tables{ $item{'table_name'} }{'indices'} },
236 name => $item{'index_name'},
237 supertype => $item{'unique'}[0] ? 'constraint' : 'index',
238 type => $item{'unique'}[0] ? 'unique' : 'normal',
240 method => $item{'using_method'}[0],
247 # Create anything else (e.g., domain, etc.)
249 create : CREATE WORD /[^;]+/ ';'
250 { @table_comments = (); }
252 using_method : /using/i WORD { $item[2] }
254 where_predicate : /where/i /[^;]+/
256 create_definition : field
260 comment : /^\s*(?:#|-{2})(.*)\n/
262 my $comment = $item[1];
263 $comment =~ s/^\s*(#|-*)\s*//;
264 $comment =~ s/\s*$//;
266 push @table_comments, $comment;
269 comment_on_table : /comment/i /on/i /table/i table_name /is/i comment_phrase ';'
271 push @{ $tables{ $item{'table_name'} }{'comments'} }, $item{'comment_phrase'};
274 comment_on_column : /comment/i /on/i /column/i column_name /is/i comment_phrase ';'
276 my $table_name = $item[4]->{'table'};
277 my $field_name = $item[4]->{'field'};
278 if ($tables{ $table_name }{'fields'}{ $field_name } ) {
279 push @{ $tables{ $table_name }{'fields'}{ $field_name }{'comments'} },
280 $item{'comment_phrase'};
283 die "No such column as $table_name.$field_name";
287 comment_on_other : /comment/i /on/i /\w+/ /\w+/ /is/i comment_phrase ';'
289 push(@table_comments, $item{'comment_phrase'});
292 # [added by cjm 20041019]
293 # [TODO: other comment-on types]
294 # for now we just have a general mechanism for handling other
295 # kinds of comments than table/column; I'm not sure of the best
296 # way to incorporate these into the datamodel
298 # this is the exhaustive list of types of comment:
299 #COMMENT ON DATABASE my_database IS 'Development Database';
300 #COMMENT ON INDEX my_index IS 'Enforces uniqueness on employee id';
301 #COMMENT ON RULE my_rule IS 'Logs UPDATES of employee records';
302 #COMMENT ON SEQUENCE my_sequence IS 'Used to generate primary keys';
303 #COMMENT ON TABLE my_table IS 'Employee Information';
304 #COMMENT ON TYPE my_type IS 'Complex Number support';
305 #COMMENT ON VIEW my_view IS 'View of departmental costs';
306 #COMMENT ON COLUMN my_table.my_field IS 'Employee ID number';
307 #COMMENT ON TRIGGER my_trigger ON my_table IS 'Used for R.I.';
309 # this is tested by test 08
311 column_name : NAME '.' NAME
312 { $return = { table => $item[1], field => $item[3] } }
314 comment_phrase : /null/i
317 comment_phrase : /'/ comment_phrase_unquoted(s) /'/
318 { my $phrase = join(' ', @{ $item[2] });
321 # [cjm TODO: double-single quotes in a comment_phrase]
322 comment_phrase_unquoted : /[^\']*/
323 { $return = $item[1] }
326 xxxcomment_phrase : /'.*?'|NULL/
328 my $val = $item[1] || '';
333 field : field_comment(s?) field_name data_type field_meta(s?) field_comment(s?)
335 my ( $default, @constraints, $is_pk );
337 for my $meta ( @{ $item[4] } ) {
338 if ( $meta->{'type'} eq 'default' ) {
342 elsif ( $meta->{'type'} eq 'not_null' ) {
345 elsif ( $meta->{'type'} eq 'primary_key' ) {
349 push @constraints, $meta if $meta->{'supertype'} eq 'constraint';
352 my @comments = ( @{ $item[1] }, @{ $item[5] } );
355 supertype => 'field',
356 name => $item{'field_name'},
357 data_type => $item{'data_type'}{'type'},
358 size => $item{'data_type'}{'size'},
359 is_nullable => $is_nullable,
360 default => $default->{'value'},
361 constraints => [ @constraints ],
362 comments => [ @comments ],
363 is_primary_key => $is_pk || 0,
364 is_auto_increment => $item{'data_type'}{'is_auto_increment'},
369 field_comment : /^\s*(?:#|-{2})(.*)\n/
371 my $comment = $item[1];
372 $comment =~ s/^\s*(#|-*)\s*//;
373 $comment =~ s/\s*$//;
377 field_meta : default_val
380 column_constraint : constraint_name(?) column_constraint_type deferrable(?) deferred(?)
382 my $desc = $item{'column_constraint_type'};
383 my $type = $desc->{'type'};
384 my $fields = $desc->{'fields'} || [];
385 my $expression = $desc->{'expression'} || '';
388 supertype => 'constraint',
389 name => $item{'constraint_name'}[0] || '',
391 expression => $type eq 'check' ? $expression : '',
392 deferrable => $item{'deferrable'},
393 deferred => $item{'deferred'},
394 reference_table => $desc->{'reference_table'},
395 reference_fields => $desc->{'reference_fields'},
396 match_type => $desc->{'match_type'},
397 on_delete_do => $desc->{'on_delete_do'},
398 on_update_do => $desc->{'on_update_do'},
402 constraint_name : /constraint/i name_with_opt_quotes { $item[2] }
404 column_constraint_type : /not null/i { $return = { type => 'not_null' } }
407 { $return = { type => 'null' } }
410 { $return = { type => 'unique' } }
413 { $return = { type => 'primary_key' } }
415 /check/i '(' /[^)]+/ ')'
416 { $return = { type => 'check', expression => $item[3] } }
418 /references/i table_name parens_word_list(?) match_type(?) key_action(s?)
420 my ( $on_delete, $on_update );
421 for my $action ( @{ $item[5] || [] } ) {
422 $on_delete = $action->{'action'} if $action->{'type'} eq 'delete';
423 $on_update = $action->{'action'} if $action->{'type'} eq 'update';
427 type => 'foreign_key',
428 reference_table => $item[2],
429 reference_fields => $item[3][0],
430 match_type => $item[4][0],
431 on_delete_do => $on_delete,
432 on_update_do => $on_update,
436 table_name : name_with_opt_quotes
438 field_name : name_with_opt_quotes
440 name_with_opt_quotes : double_quote(?) NAME double_quote(?) { $item[2] }
446 data_type : pg_data_type parens_value_list(?)
448 my $data_type = $item[1];
451 # We can deduce some sizes from the data type's name.
453 if ( my $size = $item[2][0] ) {
454 $data_type->{'size'} = $size;
457 $return = $data_type;
479 $return = { type => 'interval' };
482 /(integer|int4?)/i # interval must come before this
498 /(double precision|float8?)/i
506 /(bigserial|serial8)/i
511 is_auto_increment => 1,
520 is_auto_increment => 1,
524 /(bit varying|varbit)/i
526 $return = { type => 'varbit' };
531 $return = { type => 'varchar' };
536 $return = { type => 'char' };
541 $return = { type => 'boolean' };
546 $return = { type => 'bytea' };
549 /(timestamptz|timestamp)( with time zone)?/i
551 $return = { type => 'timestamp' };
554 /(timestamptz|timestamp)( without time zone)?/i
556 $return = { type => 'timestamp' };
567 /(bit|box|cidr|circle|date|inet|line|lseg|macaddr|money|numeric|decimal|path|point|polygon|timetz|time|varchar)/i
569 $return = { type => $item[1] };
572 parens_value_list : '(' VALUE(s /,/) ')'
575 parens_word_list : '(' WORD(s /,/) ')'
578 field_size : '(' num_range ')' { $item{'num_range'} }
580 num_range : DIGITS ',' DIGITS
581 { $return = $item[1].','.$item[3] }
583 { $return = $item[1] }
585 table_constraint : comment(s?) constraint_name(?) table_constraint_type deferrable(?) deferred(?) comment(s?)
587 my $desc = $item{'table_constraint_type'};
588 my $type = $desc->{'type'};
589 my $fields = $desc->{'fields'};
590 my $expression = $desc->{'expression'};
591 my @comments = ( @{ $item[1] }, @{ $item[-1] } );
594 name => $item{'constraint_name'}[0] || '',
595 supertype => 'constraint',
597 fields => $type ne 'check' ? $fields : [],
598 expression => $type eq 'check' ? $expression : '',
599 deferrable => $item{'deferrable'},
600 deferred => $item{'deferred'},
601 reference_table => $desc->{'reference_table'},
602 reference_fields => $desc->{'reference_fields'},
603 match_type => $desc->{'match_type'}[0],
604 on_delete_do => $desc->{'on_delete_do'},
605 on_update_do => $desc->{'on_update_do'},
606 comments => [ @comments ],
610 table_constraint_type : /primary key/i '(' name_with_opt_quotes(s /,/) ')'
613 type => 'primary_key',
618 /unique/i '(' name_with_opt_quotes(s /,/) ')'
626 /check/i '(' /[^)]+/ ')'
630 expression => $item[3],
634 /foreign key/i '(' name_with_opt_quotes(s /,/) ')' /references/i table_name parens_word_list(?) match_type(?) key_action(s?)
636 my ( $on_delete, $on_update );
637 for my $action ( @{ $item[9] || [] } ) {
638 $on_delete = $action->{'action'} if $action->{'type'} eq 'delete';
639 $on_update = $action->{'action'} if $action->{'type'} eq 'update';
643 supertype => 'constraint',
644 type => 'foreign_key',
646 reference_table => $item[6],
647 reference_fields => $item[7][0],
648 match_type => $item[8][0],
649 on_delete_do => $on_delete || '',
650 on_update_do => $on_update || '',
654 deferrable : /not/i /deferrable/i
656 $return = ( $item[1] =~ /not/i ) ? 0 : 1;
659 deferred : /initially/i /(deferred|immediate)/i { $item[2] }
661 match_type : /match full/i { 'match_full' }
663 /match partial/i { 'match_partial' }
665 key_action : key_delete
669 key_delete : /on delete/i key_mutation
677 key_update : /on update/i key_mutation
685 key_mutation : /no action/i { $return = 'no_action' }
687 /restrict/i { $return = 'restrict' }
689 /cascade/i { $return = 'cascade' }
691 /set null/i { $return = 'set null' }
693 /set default/i { $return = 'set default' }
695 alter : alter_table table_name add_column field ';'
697 my $field_def = $item[4];
698 $tables{ $item[2] }{'fields'}{ $field_def->{'name'} } = {
699 %$field_def, order => $field_order++
704 alter : alter_table table_name ADD table_constraint ';'
706 my $table_name = $item[2];
707 my $constraint = $item[4];
708 push @{ $tables{ $table_name }{'constraints'} }, $constraint;
712 alter : alter_table table_name drop_column NAME restrict_or_cascade(?) ';'
714 $tables{ $item[2] }{'fields'}{ $item[4] }{'drop'} = 1;
718 alter : alter_table table_name alter_column NAME alter_default_val ';'
720 $tables{ $item[2] }{'fields'}{ $item[4] }{'default'} =
726 # These will just parse for now but won't affect the structure. - ky
728 alter : alter_table table_name /rename/i /to/i NAME ';'
731 alter : alter_table table_name alter_column NAME SET /statistics/i INTEGER ';'
734 alter : alter_table table_name alter_column NAME SET /storage/i storage_type ';'
737 alter : alter_table table_name rename_column NAME /to/i NAME ';'
740 alter : alter_table table_name DROP /constraint/i NAME restrict_or_cascade ';'
743 alter : alter_table table_name /owner/i /to/i NAME ';'
746 storage_type : /(plain|external|extended|main)/i
748 alter_default_val : SET default_val
750 $return = { value => $item[2]->{'value'} }
754 $return = { value => undef }
758 # This is a little tricky to get right, at least WRT to making the
759 # tests pass. The problem is that the constraints are stored just as
760 # a list (no name access), and the tests expect the constraints in a
761 # particular order. I'm going to leave the rule but disable the code
764 alter : alter_table table_name alter_column NAME alter_nullable ';'
766 # my $table_name = $item[2];
767 # my $field_name = $item[4];
768 # my $is_nullable = $item[5]->{'is_nullable'};
770 # $tables{ $table_name }{'fields'}{ $field_name }{'is_nullable'} =
773 # if ( $is_nullable ) {
775 # push @{ $tables{ $table_name }{'constraints'} }, {
776 # type => 'not_null',
777 # fields => [ $field_name ],
782 # 0 .. $#{ $tables{ $table_name }{'constraints'} || [] }
784 # my $c = $tables{ $table_name }{'constraints'}[ $i ] or next;
785 # my $fields = join( '', @{ $c->{'fields'} || [] } ) or next;
786 # if ( $c->{'type'} eq 'not_null' && $fields eq $field_name ) {
787 # delete $tables{ $table_name }{'constraints'}[ $i ];
796 alter_nullable : SET not_null
798 $return = { is_nullable => 0 }
802 $return = { is_nullable => 1 }
805 not_null : /not/i /null/i
807 add_column : ADD COLUMN(?)
809 alter_table : ALTER TABLE ONLY(?)
811 drop_column : DROP COLUMN(?)
813 alter_column : ALTER COLUMN(?)
815 rename_column : /rename/i COLUMN(?)
817 restrict_or_cascade : /restrict/i |
821 # End basically useless stuff. - ky
824 create_table : CREATE TABLE
826 create_index : CREATE /index/i
828 default_val : DEFAULT /(\d+|'[^']*'|\w+\(.*?\))|\w+/
830 my $val = defined $item[2] ? $item[2] : '';
833 supertype => 'constraint',
841 supertype => 'constraint',
847 name_with_opt_paren : NAME parens_value_list(s?)
848 { $item[2][0] ? "$item[1]($item[2][0][0])" : $item[1] }
850 unique : /unique/i { 1 }
852 key : /key/i | /index/i
854 table_option : /inherits/i '(' name_with_opt_quotes(s /,/) ')'
856 $return = { type => 'inherits', table_name => $item[3] }
861 $return = { type => $item[1] =~ /out/i ? 'without_oids' : 'with_oids' }
880 SEMICOLON : /\s*;\n?/
899 VALUE : /[-+]?\.?\d+(?:[eE]\d+)?/
901 | /'.*?'/ # XXX doesn't handle embedded quotes
908 # -------------------------------------------------------------------
910 my ( $translator, $data ) = @_;
911 $parser ||= Parse::RecDescent->new($GRAMMAR);
913 $::RD_TRACE = $translator->trace ? 1 : undef;
914 $DEBUG = $translator->debug;
916 unless (defined $parser) {
917 return $translator->error("Error instantiating Parse::RecDescent ".
918 "instance: Bad grammer");
921 my $result = $parser->startrule($data);
922 die "Parse failed.\n" unless defined $result;
923 warn Dumper($result) if $DEBUG;
925 my $schema = $translator->schema;
927 ( $result->{ $a }{'order'} || 0 ) <=> ( $result->{ $b }{'order'} || 0 )
930 for my $table_name ( @tables ) {
931 my $tdata = $result->{ $table_name };
932 my $table = $schema->add_table(
933 name => $tdata->{'table_name'},
934 ) or die "Couldn't create table '$table_name': " . $schema->error;
936 $table->comments( $tdata->{'comments'} );
939 $tdata->{'fields'}{ $a }{'order'}
941 $tdata->{'fields'}{ $b }{'order'}
942 } keys %{ $tdata->{'fields'} };
944 for my $fname ( @fields ) {
945 my $fdata = $tdata->{'fields'}{ $fname };
946 next if $fdata->{'drop'};
947 my $field = $table->add_field(
948 name => $fdata->{'name'},
949 data_type => $fdata->{'data_type'},
950 size => $fdata->{'size'},
951 default_value => $fdata->{'default'},
952 is_auto_increment => $fdata->{'is_auto_increment'},
953 is_nullable => $fdata->{'is_nullable'},
954 comments => $fdata->{'comments'},
955 ) or die $table->error;
957 $table->primary_key( $field->name ) if $fdata->{'is_primary_key'};
959 for my $cdata ( @{ $fdata->{'constraints'} } ) {
960 next unless $cdata->{'type'} eq 'foreign_key';
961 $cdata->{'fields'} ||= [ $field->name ];
962 push @{ $tdata->{'constraints'} }, $cdata;
966 for my $idata ( @{ $tdata->{'indices'} || [] } ) {
967 my $index = $table->add_index(
968 name => $idata->{'name'},
969 type => uc $idata->{'type'},
970 fields => $idata->{'fields'},
971 ) or die $table->error;
974 for my $cdata ( @{ $tdata->{'constraints'} || [] } ) {
975 my $constraint = $table->add_constraint(
976 name => $cdata->{'name'},
977 type => $cdata->{'type'},
978 fields => $cdata->{'fields'},
979 reference_table => $cdata->{'reference_table'},
980 reference_fields => $cdata->{'reference_fields'},
981 match_type => $cdata->{'match_type'} || '',
982 on_delete => $cdata->{'on_delete_do'},
983 on_update => $cdata->{'on_update_do'},
984 expression => $cdata->{'expression'},
985 ) or die "Can't add constraint of type '" .
986 $cdata->{'type'} . "' to table '" . $table->name .
987 "': " . $table->error;
996 # -------------------------------------------------------------------
997 # Rescue the drowning and tie your shoestrings.
998 # Henry David Thoreau
999 # -------------------------------------------------------------------
1005 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>,
1006 Allen Day E<lt>allenday@ucla.eduE<gt>.
1010 perl(1), Parse::RecDescent.