1 package SQL::Translator::Parser::PostgreSQL;
3 # -------------------------------------------------------------------
4 # $Id: PostgreSQL.pm,v 1.32 2003-11-17 19:09:33 allenday Exp $
5 # -------------------------------------------------------------------
6 # Copyright (C) 2003 Ken Y. Clark <kclark@cpan.org>,
7 # Allen Day <allenday@users.sourceforge.net>,
8 # darren chamberlain <darren@cpan.org>,
9 # Chris Mungall <cjm@fruitfly.org>
11 # This program is free software; you can redistribute it and/or
12 # modify it under the terms of the GNU General Public License as
13 # published by the Free Software Foundation; version 2.
15 # This program is distributed in the hope that it will be useful, but
16 # WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 # General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, write to the Free Software
22 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
24 # -------------------------------------------------------------------
28 SQL::Translator::Parser::PostgreSQL - parser for PostgreSQL
33 use SQL::Translator::Parser::PostgreSQL;
35 my $translator = SQL::Translator->new;
36 $translator->parser("SQL::Translator::Parser::PostgreSQL");
40 The grammar was started from the MySQL parsers. Here is the description
44 (http://www.postgresql.org/docs/view.php?version=7.3&idoc=1&file=sql-createtable.html)
46 CREATE [ [ LOCAL ] { TEMPORARY | TEMP } ] TABLE table_name (
47 { column_name data_type [ DEFAULT default_expr ]
48 [ column_constraint [, ... ] ]
49 | table_constraint } [, ... ]
51 [ INHERITS ( parent_table [, ... ] ) ]
52 [ WITH OIDS | WITHOUT OIDS ]
54 where column_constraint is:
56 [ CONSTRAINT constraint_name ]
57 { NOT NULL | NULL | UNIQUE | PRIMARY KEY |
59 REFERENCES reftable [ ( refcolumn ) ] [ MATCH FULL | MATCH PARTIAL ]
60 [ ON DELETE action ] [ ON UPDATE action ] }
61 [ DEFERRABLE | NOT DEFERRABLE ]
62 [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]
64 and table_constraint is:
66 [ CONSTRAINT constraint_name ]
67 { UNIQUE ( column_name [, ... ] ) |
68 PRIMARY KEY ( column_name [, ... ] ) |
69 CHECK ( expression ) |
70 FOREIGN KEY ( column_name [, ... ] )
71 REFERENCES reftable [ ( refcolumn [, ... ] ) ]
72 [ MATCH FULL | MATCH PARTIAL ]
73 [ ON DELETE action ] [ ON UPDATE action ] }
74 [ DEFERRABLE | NOT DEFERRABLE ]
75 [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]
78 (http://www.postgresql.org/docs/view.php?version=7.3&idoc=1&file=sql-createindex.html)
80 CREATE [ UNIQUE ] INDEX index_name ON table
81 [ USING acc_method ] ( column [ ops_name ] [, ...] )
83 CREATE [ UNIQUE ] INDEX index_name ON table
84 [ USING acc_method ] ( func_name( column [, ... ]) [ ops_name ] )
89 ALTER TABLE [ ONLY ] table [ * ]
90 ADD [ COLUMN ] column type [ column_constraint [ ... ] ]
91 ALTER TABLE [ ONLY ] table [ * ]
92 ALTER [ COLUMN ] column { SET DEFAULT value | DROP DEFAULT }
93 ALTER TABLE [ ONLY ] table [ * ]
94 ALTER [ COLUMN ] column SET STATISTICS integer
95 ALTER TABLE [ ONLY ] table [ * ]
96 RENAME [ COLUMN ] column TO newcolumn
100 ADD table_constraint_definition
101 ALTER TABLE [ ONLY ] table
102 DROP CONSTRAINT constraint { RESTRICT | CASCADE }
108 CREATE [ OR REPLACE ] VIEW view [ ( column name list ) ] AS SELECT query
113 use vars qw[ $DEBUG $VERSION $GRAMMAR @EXPORT_OK ];
114 $VERSION = sprintf "%d.%02d", q$Revision: 1.32 $ =~ /(\d+)\.(\d+)/;
115 $DEBUG = 0 unless defined $DEBUG;
118 use Parse::RecDescent;
120 use base qw(Exporter);
122 @EXPORT_OK = qw(parse);
124 # Enable warnings within the Parse::RecDescent module.
125 $::RD_ERRORS = 1; # Make sure the parser dies when it encounters an error
126 $::RD_WARN = 1; # Enable warnings. This will warn on unused rules &c.
127 $::RD_HINT = 1; # Give out hints to help fix problems.
129 my $parser; # should we do this? There's no programmic way to
130 # change the grammar, so I think this is safe.
134 { my ( %tables, $table_order, @table_comments) }
137 # The "eofile" rule makes the parser fail if any "statement" rule
138 # fails. Otherwise, the first successful match by a "statement"
139 # won't cause the failure needed to know that the parse, as a whole,
142 startrule : statement(s) eofile { \%tables }
160 connect : /^\s*\\\connect.*\n/
162 set : /set/i /[^;]*/ ';'
164 revoke : /revoke/i WORD(s /,/) /on/i TABLE(?) table_name /from/i name_with_opt_quotes(s /,/) ';'
166 my $table_name = $item{'table_name'};
167 push @{ $tables{ $table_name }{'permissions'} }, {
174 grant : /grant/i WORD(s /,/) /on/i TABLE(?) table_name /to/i name_with_opt_quotes(s /,/) ';'
176 my $table_name = $item{'table_name'};
177 push @{ $tables{ $table_name }{'permissions'} }, {
184 drop : /drop/i /[^;]*/ ';'
186 insert : /insert/i /[^;]*/ ';'
188 update : /update/i /[^;]*/ ';'
193 create : create_table table_name '(' create_definition(s /,/) ')' table_option(s?) ';'
195 my $table_name = $item{'table_name'};
196 $tables{ $table_name }{'order'} = ++$table_order;
197 $tables{ $table_name }{'table_name'} = $table_name;
199 if ( @table_comments ) {
200 $tables{ $table_name }{'comments'} = [ @table_comments ];
201 @table_comments = ();
206 for my $definition ( @{ $item[4] } ) {
207 if ( $definition->{'supertype'} eq 'field' ) {
208 my $field_name = $definition->{'name'};
209 $tables{ $table_name }{'fields'}{ $field_name } =
210 { %$definition, order => $i };
213 for my $constraint ( @{ $definition->{'constraints'} || [] } ) {
214 $constraint->{'fields'} = [ $field_name ];
215 push @{ $tables{ $table_name }{'constraints'} },
219 elsif ( $definition->{'supertype'} eq 'constraint' ) {
220 push @{ $tables{ $table_name }{'constraints'} }, $definition;
222 elsif ( $definition->{'supertype'} eq 'index' ) {
223 push @{ $tables{ $table_name }{'indices'} }, $definition;
227 for my $option ( @{ $item[6] } ) {
228 $tables{ $table_name }{'table_options(s?)'}{ $option->{'type'} } =
235 create : /create/i unique(?) /(index|key)/i index_name /on/i table_name using_method(?) '(' field_name(s /,/) ')' where_predicate(?) ';'
237 push @{ $tables{ $item{'table_name'} }{'indices'} },
239 name => $item{'index_name'},
240 supertype => $item{'unique'}[0] ? 'constraint' : 'index',
241 type => $item{'unique'}[0] ? 'unique' : 'normal',
243 method => $item{'using_method'}[0],
250 # Create anything else (e.g., domain, function, etc.)
252 create : /create/i WORD /[^;]+/ ';'
253 { @table_comments = (); }
255 using_method : /using/i WORD { $item[2] }
257 where_predicate : /where/i /[^;]+/
259 create_definition : field
263 table_comment : comment
265 my $comment = $item[1];
267 push @table_comments, $comment;
270 comment : /^\s*(?:#|-{2}).*\n/
272 comment_on_table : /comment/i /on/i /table/i table_name /is/i comment_phrase ';'
274 push @{ $tables{ $item{'table_name'} }{'comments'} }, $item{'comment_phrase'};
277 comment_on_column : /comment/i /on/i /column/i column_name /is/i comment_phrase ';'
279 my $table_name = $item[4]->{'table'};
280 my $field_name = $item[4]->{'field'};
281 push @{ $tables{ $table_name }{'fields'}{ $field_name }{'comments'} },
282 $item{'comment_phrase'};
285 column_name : NAME '.' NAME
286 { $return = { table => $item[1], field => $item[3] } }
288 comment_phrase : /'.*?'|NULL/
295 field : comment(s?) field_name data_type field_meta(s?) comment(s?)
297 my ( $default, @constraints, $is_pk );
299 for my $meta ( @{ $item[4] } ) {
300 if ( $meta->{'type'} eq 'default' ) {
304 elsif ( $meta->{'type'} eq 'not_null' ) {
308 elsif ( $meta->{'type'} eq 'primary_key' ) {
312 push @constraints, $meta if $meta->{'supertype'} eq 'constraint';
315 my @comments = ( @{ $item[1] }, @{ $item[5] } );
318 supertype => 'field',
319 name => $item{'field_name'},
320 data_type => $item{'data_type'}{'type'},
321 size => $item{'data_type'}{'size'},
323 default => $default->{'value'},
324 constraints => [ @constraints ],
325 comments => [ @comments ],
326 is_primary_key => $is_pk || 0,
327 is_auto_increment => $item{'data_type'}{'is_auto_increment'},
332 field_meta : default_val
335 column_constraint : constraint_name(?) column_constraint_type deferrable(?) deferred(?)
337 my $desc = $item{'column_constraint_type'};
338 my $type = $desc->{'type'};
339 my $fields = $desc->{'fields'} || [];
340 my $expression = $desc->{'expression'} || '';
343 supertype => 'constraint',
344 name => $item{'constraint_name'}[0] || '',
346 expression => $type eq 'check' ? $expression : '',
347 deferrable => $item{'deferrable'},
348 deferred => $item{'deferred'},
349 reference_table => $desc->{'reference_table'},
350 reference_fields => $desc->{'reference_fields'},
351 match_type => $desc->{'match_type'},
352 on_delete_do => $desc->{'on_delete_do'},
353 on_update_do => $desc->{'on_update_do'},
357 constraint_name : /constraint/i name_with_opt_quotes { $item[2] }
359 column_constraint_type : /not null/i { $return = { type => 'not_null' } }
362 { $return = { type => 'null' } }
365 { $return = { type => 'unique' } }
368 { $return = { type => 'primary_key' } }
370 /check/i '(' /[^)]+/ ')'
371 { $return = { type => 'check', expression => $item[3] } }
373 /references/i table_name parens_word_list(?) match_type(?) key_action(s?)
375 my ( $on_delete, $on_update );
376 for my $action ( @{ $item[5] || [] } ) {
377 $on_delete = $action->{'action'} if $action->{'type'} eq 'delete';
378 $on_update = $action->{'action'} if $action->{'type'} eq 'update';
382 type => 'foreign_key',
383 reference_table => $item[2],
384 reference_fields => $item[3][0],
385 match_type => $item[4][0],
386 on_delete_do => $on_delete,
387 on_update_do => $on_update,
391 table_name : name_with_opt_quotes
393 field_name : name_with_opt_quotes
395 name_with_opt_quotes : double_quote(?) NAME double_quote(?) { $item[2] }
401 data_type : pg_data_type parens_value_list(?)
403 my $data_type = $item[1];
406 # We can deduce some sizes from the data type's name.
408 $data_type->{'size'} ||= $item[2][0];
410 $return = $data_type;
430 /(integer|int4?)/i # interval must come before this
446 /(double precision|float8?)/i
454 /(bigserial|serial8)/i
459 is_auto_increment => 1,
468 is_auto_increment => 1,
472 /(bit varying|varbit)/i
474 $return = { type => 'varbit' };
479 $return = { type => 'varchar' };
484 $return = { type => 'char' };
489 $return = { type => 'boolean' };
494 $return = { type => 'bytea' };
497 /(timestamptz|timestamp)/i
499 $return = { type => 'timestamp' };
510 /(bit|box|cidr|circle|date|inet|interval|line|lseg|macaddr|money|numeric|decimal|path|point|polygon|timetz|time|varchar)/i
512 $return = { type => $item[1] };
515 parens_value_list : '(' VALUE(s /,/) ')'
518 parens_word_list : '(' WORD(s /,/) ')'
521 field_size : '(' num_range ')' { $item{'num_range'} }
523 num_range : DIGITS ',' DIGITS
524 { $return = $item[1].','.$item[3] }
526 { $return = $item[1] }
528 table_constraint : comment(s?) constraint_name(?) table_constraint_type deferrable(?) deferred(?) comment(s?)
530 my $desc = $item{'table_constraint_type'};
531 my $type = $desc->{'type'};
532 my $fields = $desc->{'fields'};
533 my $expression = $desc->{'expression'};
534 my @comments = ( @{ $item[1] }, @{ $item[-1] } );
537 name => $item{'constraint_name'}[0] || '',
538 supertype => 'constraint',
540 fields => $type ne 'check' ? $fields : [],
541 expression => $type eq 'check' ? $expression : '',
542 deferrable => $item{'deferrable'},
543 deferred => $item{'deferred'},
544 reference_table => $desc->{'reference_table'},
545 reference_fields => $desc->{'reference_fields'},
546 match_type => $desc->{'match_type'}[0],
547 on_delete_do => $desc->{'on_delete_do'},
548 on_update_do => $desc->{'on_update_do'},
549 comments => [ @comments ],
553 table_constraint_type : /primary key/i '(' name_with_opt_quotes(s /,/) ')'
556 type => 'primary_key',
561 /unique/i '(' name_with_opt_quotes(s /,/) ')'
569 /check/i '(' /[^)]+/ ')'
573 expression => $item[3],
577 /foreign key/i '(' name_with_opt_quotes(s /,/) ')' /references/i table_name parens_word_list(?) match_type(?) key_action(s?)
579 my ( $on_delete, $on_update );
580 for my $action ( @{ $item[9] || [] } ) {
581 $on_delete = $action->{'action'} if $action->{'type'} eq 'delete';
582 $on_update = $action->{'action'} if $action->{'type'} eq 'update';
586 supertype => 'constraint',
587 type => 'foreign_key',
589 reference_table => $item[6],
590 reference_fields => $item[7][0],
591 match_type => $item[8][0],
592 on_delete_do => $on_delete || '',
593 on_update_do => $on_update || '',
597 deferrable : /not/i /deferrable/i
599 $return = ( $item[1] =~ /not/i ) ? 0 : 1;
602 deferred : /initially/i /(deferred|immediate)/i { $item[2] }
604 match_type : /match full/i { 'match_full' }
606 /match partial/i { 'match_partial' }
608 key_action : key_delete
612 key_delete : /on delete/i key_mutation
620 key_update : /on update/i key_mutation
628 key_mutation : /no action/i { $return = 'no_action' }
630 /restrict/i { $return = 'restrict' }
632 /cascade/i { $return = 'cascade' }
634 /set null/i { $return = 'set null' }
636 /set default/i { $return = 'set default' }
638 alter : alter_table table_name /add/i table_constraint ';'
640 my $table_name = $item[2];
641 my $constraint = $item[4];
642 push @{ $tables{ $table_name }{'constraints'} }, $constraint;
645 alter_table : /alter/i /table/i only(?)
649 create_table : /create/i TABLE
651 create_index : /create/i /index/i
653 default_val : /default/i /(\d+|'[^']*'|\w+\(.*?\))|\w+/
655 my $val = defined $item[2] ? $item[2] : '';
658 supertype => 'constraint',
666 supertype => 'constraint',
672 name_with_opt_paren : NAME parens_value_list(s?)
673 { $item[2][0] ? "$item[1]($item[2][0][0])" : $item[1] }
675 unique : /unique/i { 1 }
677 key : /key/i | /index/i
679 table_option : /inherits/i '(' name_with_opt_quotes(s /,/) ')'
681 $return = { type => 'inherits', table_name => $item[3] }
686 $return = { type => $item[1] =~ /out/i ? 'without_oids' : 'with_oids' }
691 SEMICOLON : /\s*;\n?/
706 VALUE : /[-+]?\.?\d+(?:[eE]\d+)?/
708 | /'.*?'/ # XXX doesn't handle embedded quotes
715 # -------------------------------------------------------------------
717 my ( $translator, $data ) = @_;
718 $parser ||= Parse::RecDescent->new($GRAMMAR);
720 $::RD_TRACE = $translator->trace ? 1 : undef;
721 $DEBUG = $translator->debug;
723 unless (defined $parser) {
724 return $translator->error("Error instantiating Parse::RecDescent ".
725 "instance: Bad grammer");
728 my $result = $parser->startrule($data);
729 die "Parse failed.\n" unless defined $result;
730 warn Dumper($result) if $DEBUG;
732 my $schema = $translator->schema;
734 $result->{ $a }->{'order'} <=> $result->{ $b }->{'order'}
737 for my $table_name ( @tables ) {
738 my $tdata = $result->{ $table_name };
739 my $table = $schema->add_table(
740 name => $tdata->{'table_name'},
741 ) or die "Couldn't create table '$table_name': " . $schema->error;
744 $tdata->{'fields'}->{ $a }->{'order'}
746 $tdata->{'fields'}->{ $b }->{'order'}
747 } keys %{ $tdata->{'fields'} };
749 for my $fname ( @fields ) {
750 my $fdata = $tdata->{'fields'}{ $fname };
751 my $field = $table->add_field(
752 name => $fdata->{'name'},
753 data_type => $fdata->{'data_type'},
754 size => $fdata->{'size'},
755 default_value => $fdata->{'default'},
756 is_auto_increment => $fdata->{'is_auto_increment'},
757 is_nullable => $fdata->{'null'},
758 ) or die $table->error;
760 $table->primary_key( $field->name ) if $fdata->{'is_primary_key'};
762 for my $cdata ( @{ $fdata->{'constraints'} } ) {
763 next unless $cdata->{'type'} eq 'foreign_key';
764 $cdata->{'fields'} ||= [ $field->name ];
765 push @{ $tdata->{'constraints'} }, $cdata;
769 for my $idata ( @{ $tdata->{'indices'} || [] } ) {
770 my $index = $table->add_index(
771 name => $idata->{'name'},
772 type => uc $idata->{'type'},
773 fields => $idata->{'fields'},
774 ) or die $table->error;
777 for my $cdata ( @{ $tdata->{'constraints'} || [] } ) {
778 my $constraint = $table->add_constraint(
779 name => $cdata->{'name'},
780 type => $cdata->{'type'},
781 fields => $cdata->{'fields'},
782 reference_table => $cdata->{'reference_table'},
783 reference_fields => $cdata->{'reference_fields'},
784 match_type => $cdata->{'match_type'} || '',
785 on_delete => $cdata->{'on_delete_do'},
786 on_update => $cdata->{'on_update_do'},
787 expression => $cdata->{'expression'},
788 ) or die "Can't add constraint of type '" .
789 $cdata->{'type'} . "' to table '" . $table->name .
790 "': " . $table->error;
799 # -------------------------------------------------------------------
800 # Rescue the drowning and tie your shoestrings.
801 # Henry David Thoreau
802 # -------------------------------------------------------------------
808 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>,
809 Allen Day E<lt>allenday@ucla.eduE<gt>.
813 perl(1), Parse::RecDescent.