Force everything to 1.99, hopefully will work
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Parser / PostgreSQL.pm
1 package SQL::Translator::Parser::PostgreSQL;
2
3 # -------------------------------------------------------------------
4 # $Id: PostgreSQL.pm 1440 2009-01-17 16:31:57Z jawnsy $
5 # -------------------------------------------------------------------
6 # Copyright (C) 2002-2009 SQLFairy Authors
7 #
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.
11 #
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.
16 #
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
20 # 02111-1307  USA
21 # -------------------------------------------------------------------
22
23 =head1 NAME
24
25 SQL::Translator::Parser::PostgreSQL - parser for PostgreSQL
26
27 =head1 SYNOPSIS
28
29   use SQL::Translator;
30   use SQL::Translator::Parser::PostgreSQL;
31
32   my $translator = SQL::Translator->new;
33   $translator->parser("SQL::Translator::Parser::PostgreSQL");
34
35 =head1 DESCRIPTION
36
37 The grammar was started from the MySQL parsers.  Here is the description 
38 from PostgreSQL:
39
40 Table:
41 (http://www.postgresql.org/docs/view.php?version=7.3&idoc=1&file=sql-createtable.html)
42
43   CREATE [ [ LOCAL ] { TEMPORARY | TEMP } ] TABLE table_name (
44       { column_name data_type [ DEFAULT default_expr ] 
45          [ column_constraint [, ... ] ]
46       | table_constraint }  [, ... ]
47   )
48   [ INHERITS ( parent_table [, ... ] ) ]
49   [ WITH OIDS | WITHOUT OIDS ]
50   
51   where column_constraint is:
52   
53   [ CONSTRAINT constraint_name ]
54   { NOT NULL | NULL | UNIQUE | PRIMARY KEY |
55     CHECK (expression) |
56     REFERENCES reftable [ ( refcolumn ) ] [ MATCH FULL | MATCH PARTIAL ]
57       [ ON DELETE action ] [ ON UPDATE action ] }
58   [ DEFERRABLE | NOT DEFERRABLE ] 
59   [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]
60   
61   and table_constraint is:
62   
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 ]
73
74 Index:
75 (http://www.postgresql.org/docs/view.php?version=7.3&idoc=1&file=sql-createindex.html)
76
77   CREATE [ UNIQUE ] INDEX index_name ON table
78       [ USING acc_method ] ( column [ ops_name ] [, ...] )
79       [ WHERE predicate ]
80   CREATE [ UNIQUE ] INDEX index_name ON table
81       [ USING acc_method ] ( func_name( column [, ... ]) [ ops_name ] )
82       [ WHERE predicate ]
83
84 Alter table:
85
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
94   ALTER TABLE table
95       RENAME TO new_table
96   ALTER TABLE table
97       ADD table_constraint_definition
98   ALTER TABLE [ ONLY ] table 
99           DROP CONSTRAINT constraint { RESTRICT | CASCADE }
100   ALTER TABLE table
101           OWNER TO new_owner 
102
103 View table:
104
105     CREATE [ OR REPLACE ] VIEW view [ ( column name list ) ] AS SELECT query
106
107 =cut
108
109 use strict;
110 use vars qw[ $DEBUG $VERSION $GRAMMAR @EXPORT_OK ];
111 $VERSION = '1.99';
112 $DEBUG   = 0 unless defined $DEBUG;
113
114 use Data::Dumper;
115 use Parse::RecDescent;
116 use Exporter;
117 use base qw(Exporter);
118
119 @EXPORT_OK = qw(parse);
120
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.
125
126 my $parser; # should we do this?  There's no programmic way to 
127             # change the grammar, so I think this is safe.
128
129 $GRAMMAR = q!
130
131 { my ( %tables, $table_order, $field_order, @table_comments) }
132
133 #
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,
137 # failed. -ky
138 #
139 startrule : statement(s) eofile { \%tables }
140
141 eofile : /^\Z/
142    
143
144 statement : create
145   | comment_on_table
146   | comment_on_column
147   | comment_on_other
148   | comment
149   | alter
150   | grant
151   | revoke
152   | drop
153   | insert
154   | connect
155   | update
156   | set
157   | select
158   | copy
159   | readin_symbol
160   | <error>
161
162 connect : /^\s*\\\connect.*\n/
163
164 set : /set/i /[^;]*/ ';'
165
166 revoke : /revoke/i WORD(s /,/) /on/i TABLE(?) table_name /from/i name_with_opt_quotes(s /,/) ';'
167     {
168         my $table_info  = $item{'table_name'};
169         my $schema_name = $table_info->{'schema_name'};
170         my $table_name  = $table_info->{'table_name'};
171         push @{ $tables{ $table_name }{'permissions'} }, {
172             type       => 'revoke',
173             actions    => $item[2],
174             users      => $item[7],
175         }
176     }
177
178 revoke : /revoke/i WORD(s /,/) /on/i SCHEMA(?) schema_name /from/i name_with_opt_quotes(s /,/) ';'
179     { 1 }
180
181 grant : /grant/i WORD(s /,/) /on/i TABLE(?) table_name /to/i name_with_opt_quotes(s /,/) ';'
182     {
183         my $table_info  = $item{'table_name'};
184         my $schema_name = $table_info->{'schema_name'};
185         my $table_name  = $table_info->{'table_name'};
186         push @{ $tables{ $table_name }{'permissions'} }, {
187             type       => 'grant',
188             actions    => $item[2],
189             users      => $item[7],
190         }
191     }
192
193 grant : /grant/i WORD(s /,/) /on/i SCHEMA(?) schema_name /to/i name_with_opt_quotes(s /,/) ';'
194     { 1 }
195
196 drop : /drop/i /[^;]*/ ';'
197
198 string :
199    /'(\\.|''|[^\\\'])*'/ 
200
201 nonstring : /[^;\'"]+/
202
203 statement_body : (string | nonstring)(s?)
204
205 insert : /insert/i statement_body ';'
206
207 update : /update/i statement_body ';'
208
209 #
210 # Create table.
211 #
212 create : CREATE temporary_table(?) TABLE table_name '(' create_definition(s? /,/) ')' table_option(s?) ';'
213     {
214         my $table_info  = $item{'table_name'};
215         my $schema_name = $table_info->{'schema_name'};
216         my $table_name  = $table_info->{'table_name'};
217         $tables{ $table_name }{'order'}       = ++$table_order;
218         $tables{ $table_name }{'schema_name'} = $schema_name;
219         $tables{ $table_name }{'table_name'}  = $table_name;
220
221         $tables{ $table_name }{'temporary'} = $item[2][0]; 
222
223         if ( @table_comments ) {
224             $tables{ $table_name }{'comments'} = [ @table_comments ];
225             @table_comments = ();
226         }
227
228         my @constraints;
229         for my $definition ( @{ $item[6] } ) {
230             if ( $definition->{'supertype'} eq 'field' ) {
231                 my $field_name = $definition->{'name'};
232                 $tables{ $table_name }{'fields'}{ $field_name } = 
233                     { %$definition, order => $field_order++ };
234                                 
235                 for my $constraint ( @{ $definition->{'constraints'} || [] } ) {
236                     $constraint->{'fields'} = [ $field_name ];
237                     push @{ $tables{ $table_name }{'constraints'} },
238                         $constraint;
239                 }
240             }
241             elsif ( $definition->{'supertype'} eq 'constraint' ) {
242                 push @{ $tables{ $table_name }{'constraints'} }, $definition;
243             }
244             elsif ( $definition->{'supertype'} eq 'index' ) {
245                 push @{ $tables{ $table_name }{'indices'} }, $definition;
246             }
247         }
248
249         for my $option ( @{ $item[8] } ) {
250             $tables{ $table_name }{'table_options(s?)'}{ $option->{'type'} } = 
251                 $option;
252         }
253
254         1;
255     }
256
257 create : CREATE unique(?) /(index|key)/i index_name /on/i table_name using_method(?) '(' field_name(s /,/) ')' where_predicate(?) ';'
258     {
259         my $table_info  = $item{'table_name'};
260         my $schema_name = $table_info->{'schema_name'};
261         my $table_name  = $table_info->{'table_name'};
262         push @{ $tables{ $table_name }{'indices'} },
263             {
264                 name      => $item{'index_name'},
265                 supertype => $item{'unique'}[0] ? 'constraint' : 'index',
266                 type      => $item{'unique'}[0] ? 'unique'     : 'normal',
267                 fields    => $item[9],
268                 method    => $item{'using_method'}[0],
269             }
270         ;
271
272     }
273
274 #
275 # Create anything else (e.g., domain, etc.)
276 #
277 create : CREATE WORD /[^;]+/ ';'
278     { @table_comments = (); }
279
280 using_method : /using/i WORD { $item[2] }
281
282 where_predicate : /where/i /[^;]+/
283
284 create_definition : field
285     | table_constraint
286     | <error>
287
288 comment : /^\s*(?:#|-{2})(.*)\n/ 
289     { 
290         my $comment =  $item[1];
291         $comment    =~ s/^\s*(#|-*)\s*//;
292         $comment    =~ s/\s*$//;
293         $return     = $comment;
294         push @table_comments, $comment;
295     }
296
297 comment_on_table : /comment/i /on/i /table/i table_name /is/i comment_phrase ';'
298     {
299         my $table_info  = $item{'table_name'};
300         my $schema_name = $table_info->{'schema_name'};
301         my $table_name  = $table_info->{'table_name'};
302         push @{ $tables{ $table_name }{'comments'} }, $item{'comment_phrase'};
303     }
304
305 comment_on_column : /comment/i /on/i /column/i column_name /is/i comment_phrase ';'
306     {
307         my $table_name = $item[4]->{'table'};
308         my $field_name = $item[4]->{'field'};
309         if ($tables{ $table_name }{'fields'}{ $field_name } ) {
310           push @{ $tables{ $table_name }{'fields'}{ $field_name }{'comments'} }, 
311               $item{'comment_phrase'};
312         }
313         else {
314            die "No such column as $table_name.$field_name";
315         }
316     }
317
318 comment_on_other : /comment/i /on/i /\w+/ /\w+/ /is/i comment_phrase ';'
319     {
320         push(@table_comments, $item{'comment_phrase'});
321     }
322
323 # [added by cjm 20041019]
324 # [TODO: other comment-on types]
325 # for now we just have a general mechanism for handling other
326 # kinds of comments than table/column; I'm not sure of the best
327 # way to incorporate these into the datamodel
328 #
329 # this is the exhaustive list of types of comment:
330 #COMMENT ON DATABASE my_database IS 'Development Database';
331 #COMMENT ON INDEX my_index IS 'Enforces uniqueness on employee id';
332 #COMMENT ON RULE my_rule IS 'Logs UPDATES of employee records';
333 #COMMENT ON SEQUENCE my_sequence IS 'Used to generate primary keys';
334 #COMMENT ON TABLE my_table IS 'Employee Information';
335 #COMMENT ON TYPE my_type IS 'Complex Number support';
336 #COMMENT ON VIEW my_view IS 'View of departmental costs';
337 #COMMENT ON COLUMN my_table.my_field IS 'Employee ID number';
338 #COMMENT ON TRIGGER my_trigger ON my_table IS 'Used for R.I.';
339 #
340 # this is tested by test 08
341
342 column_name : NAME '.' NAME
343     { $return = { table => $item[1], field => $item[3] } }
344
345 comment_phrase : /null/i
346     { $return = 'NULL' }
347
348 comment_phrase : /'/ comment_phrase_unquoted(s) /'/
349     { my $phrase = join(' ', @{ $item[2] });
350       $return = $phrase}
351
352 # [cjm TODO: double-single quotes in a comment_phrase]
353 comment_phrase_unquoted : /[^\']*/
354     { $return = $item[1] }
355
356
357 xxxcomment_phrase : /'.*?'|NULL/ 
358     { 
359         my $val = $item[1] || '';
360         $val =~ s/^'|'$//g;
361         $return = $val;
362     }
363
364 field : field_comment(s?) field_name data_type field_meta(s?) field_comment(s?)
365     {
366         my ( $default, @constraints, $is_pk );
367         my $is_nullable = 1;
368         for my $meta ( @{ $item[4] } ) {
369             if ( $meta->{'type'} eq 'default' ) {
370                 $default = $meta;
371                 next;
372             }
373             elsif ( $meta->{'type'} eq 'not_null' ) {
374                 $is_nullable = 0;
375             }
376             elsif ( $meta->{'type'} eq 'primary_key' ) {
377                 $is_pk = 1;
378             }
379
380             push @constraints, $meta if $meta->{'supertype'} eq 'constraint';
381         }
382
383         my @comments = ( @{ $item[1] }, @{ $item[5] } );
384
385         $return = {
386             supertype         => 'field',
387             name              => $item{'field_name'}, 
388             data_type         => $item{'data_type'}{'type'},
389             size              => $item{'data_type'}{'size'},
390             is_nullable       => $is_nullable,
391             default           => $default->{'value'},
392             constraints       => [ @constraints ],
393             comments          => [ @comments ],
394             is_primary_key    => $is_pk || 0,
395             is_auto_increment => $item{'data_type'}{'is_auto_increment'},
396         } 
397     }
398     | <error>
399
400 field_comment : /^\s*(?:#|-{2})(.*)\n/ 
401     { 
402         my $comment =  $item[1];
403         $comment    =~ s/^\s*(#|-*)\s*//;
404         $comment    =~ s/\s*$//;
405         $return     = $comment;
406     }
407
408 field_meta : default_val
409     | column_constraint
410
411 column_constraint : constraint_name(?) column_constraint_type deferrable(?) deferred(?)
412     {
413         my $desc       = $item{'column_constraint_type'};
414         my $type       = $desc->{'type'};
415         my $fields     = $desc->{'fields'}     || [];
416         my $expression = $desc->{'expression'} || '';
417
418         $return              =  {
419             supertype        => 'constraint',
420             name             => $item{'constraint_name'}[0] || '',
421             type             => $type,
422             expression       => $type eq 'check' ? $expression : '',
423             deferrable       => $item{'deferrable'},
424             deferred         => $item{'deferred'},
425             reference_table  => $desc->{'reference_table'},
426             reference_fields => $desc->{'reference_fields'},
427             match_type       => $desc->{'match_type'},
428             on_delete        => $desc->{'on_delete'} || $desc->{'on_delete_do'},
429             on_update        => $desc->{'on_update'} || $desc->{'on_update_do'},
430         } 
431     }
432
433 constraint_name : /constraint/i name_with_opt_quotes { $item[2] }
434
435 column_constraint_type : /not null/i { $return = { type => 'not_null' } }
436     |
437     /null/i
438         { $return = { type => 'null' } }
439     |
440     /unique/i
441         { $return = { type => 'unique' } }
442     |
443     /primary key/i 
444         { $return = { type => 'primary_key' } }
445     |
446     /check/i '(' /[^)]+/ ')' 
447         { $return = { type => 'check', expression => $item[3] } }
448     |
449     /references/i table_name parens_word_list(?) match_type(?) key_action(s?)
450     {
451         my $table_info  = $item{'table_name'};
452         my $schema_name = $table_info->{'schema_name'};
453         my $table_name  = $table_info->{'table_name'};
454         my ( $on_delete, $on_update );
455         for my $action ( @{ $item[5] || [] } ) {
456             $on_delete = $action->{'action'} if $action->{'type'} eq 'delete';
457             $on_update = $action->{'action'} if $action->{'type'} eq 'update';
458         }
459
460         $return              =  {
461             type             => 'foreign_key',
462             reference_table  => $table_name,
463             reference_fields => $item[3][0],
464             match_type       => $item[4][0],
465             on_delete        => $on_delete,
466             on_update        => $on_update,
467         }
468     }
469
470 table_name : schema_qualification(?) name_with_opt_quotes {
471     $return = { schema_name => $item[1], table_name => $item[2] }
472 }
473
474   schema_qualification : name_with_opt_quotes '.'
475
476 schema_name : name_with_opt_quotes
477
478 field_name : name_with_opt_quotes
479
480 name_with_opt_quotes : double_quote(?) NAME double_quote(?) { $item[2] }
481
482 double_quote: /"/
483
484 index_name : WORD
485
486 data_type : pg_data_type parens_value_list(?)
487     { 
488         my $data_type = $item[1];
489
490         #
491         # We can deduce some sizes from the data type's name.
492         #
493         if ( my $size = $item[2][0] ) {
494             $data_type->{'size'} = $size;
495         }
496
497         $return  = $data_type;
498     }
499
500 pg_data_type :
501     /(bigint|int8)/i
502         { 
503             $return = { 
504                 type => 'integer',
505                 size => 20,
506             };
507         }
508     |
509     /(smallint|int2)/i
510         { 
511             $return = {
512                 type => 'integer', 
513                 size => 5,
514             };
515         }
516     |
517     /interval/i
518         {
519             $return = { type => 'interval' };
520         }
521     |
522     /(integer|int4?)/i # interval must come before this
523         { 
524             $return = {
525                 type => 'integer', 
526                 size => 10,
527             };
528         }
529     |    
530     /(real|float4)/i
531         { 
532             $return = {
533                 type => 'real', 
534                 size => 10,
535             };
536         }
537     |
538     /(double precision|float8?)/i
539         { 
540             $return = {
541                 type => 'float', 
542                 size => 20,
543             }; 
544         }
545     |
546     /(bigserial|serial8)/i
547         { 
548             $return = { 
549                 type              => 'integer', 
550                 size              => 20, 
551                 is_auto_increment => 1,
552             };
553         }
554     |
555     /serial4?/i
556         { 
557             $return = { 
558                 type              => 'integer',
559                 size              => 11, 
560                 is_auto_increment => 1,
561             };
562         }
563     |
564     /(bit varying|varbit)/i
565         { 
566             $return = { type => 'varbit' };
567         }
568     |
569     /character varying/i
570         { 
571             $return = { type => 'varchar' };
572         }
573     |
574     /char(acter)?/i
575         { 
576             $return = { type => 'char' };
577         }
578     |
579     /bool(ean)?/i
580         { 
581             $return = { type => 'boolean' };
582         }
583     |
584     /bytea/i
585         { 
586             $return = { type => 'bytea' };
587         }
588     |
589     /(timestamptz|timestamp)(?:\(\d\))?( with(out)? time zone)?/i
590         { 
591             $return = { type => 'timestamp' };
592         }
593     |
594     /text/i
595         { 
596             $return = { 
597                 type => 'text',
598                 size => 64_000,
599             };
600         }
601     |
602     /(bit|box|cidr|circle|date|inet|line|lseg|macaddr|money|numeric|decimal|path|point|polygon|timetz|time|varchar)/i
603         { 
604             $return = { type => $item[1] };
605         }
606
607 parens_value_list : '(' VALUE(s /,/) ')'
608     { $item[2] }
609
610
611 parens_word_list : '(' name_with_opt_quotes(s /,/) ')'
612     { $item[2] }
613
614 field_size : '(' num_range ')' { $item{'num_range'} }
615
616 num_range : DIGITS ',' DIGITS
617     { $return = $item[1].','.$item[3] }
618     | DIGITS
619     { $return = $item[1] }
620
621 table_constraint : comment(s?) constraint_name(?) table_constraint_type deferrable(?) deferred(?) comment(s?)
622     {
623         my $desc       = $item{'table_constraint_type'};
624         my $type       = $desc->{'type'};
625         my $fields     = $desc->{'fields'};
626         my $expression = $desc->{'expression'};
627         my @comments   = ( @{ $item[1] }, @{ $item[-1] } );
628
629         $return              =  {
630             name             => $item{'constraint_name'}[0] || '',
631             supertype        => 'constraint',
632             type             => $type,
633             fields           => $type ne 'check' ? $fields : [],
634             expression       => $type eq 'check' ? $expression : '',
635             deferrable       => $item{'deferrable'},
636             deferred         => $item{'deferred'},
637             reference_table  => $desc->{'reference_table'},
638             reference_fields => $desc->{'reference_fields'},
639             match_type       => $desc->{'match_type'}[0],
640             on_delete        => $desc->{'on_delete'} || $desc->{'on_delete_do'},
641             on_update        => $desc->{'on_update'} || $desc->{'on_update_do'},
642             comments         => [ @comments ],
643         } 
644     }
645
646 table_constraint_type : /primary key/i '(' name_with_opt_quotes(s /,/) ')' 
647     { 
648         $return = {
649             type   => 'primary_key',
650             fields => $item[3],
651         }
652     }
653     |
654     /unique/i '(' name_with_opt_quotes(s /,/) ')' 
655     { 
656         $return    =  {
657             type   => 'unique',
658             fields => $item[3],
659         }
660     }
661     |
662     /check/i '(' /[^)]+/ ')' 
663     {
664         $return        =  {
665             type       => 'check',
666             expression => $item[3],
667         }
668     }
669     |
670     /foreign key/i '(' name_with_opt_quotes(s /,/) ')' /references/i table_name parens_word_list(?) match_type(?) key_action(s?)
671     {
672         my ( $on_delete, $on_update );
673         for my $action ( @{ $item[9] || [] } ) {
674             $on_delete = $action->{'action'} if $action->{'type'} eq 'delete';
675             $on_update = $action->{'action'} if $action->{'type'} eq 'update';
676         }
677         
678         $return              =  {
679             supertype        => 'constraint',
680             type             => 'foreign_key',
681             fields           => $item[3],
682             reference_table  => $item[6]->{'table_name'},
683             reference_fields => $item[7][0],
684             match_type       => $item[8][0],
685             on_delete     => $on_delete || '',
686             on_update     => $on_update || '',
687         }
688     }
689
690 deferrable : not(?) /deferrable/i 
691     { 
692         $return = ( $item[1] =~ /not/i ) ? 0 : 1;
693     }
694
695 deferred : /initially/i /(deferred|immediate)/i { $item[2] }
696
697 match_type : /match full/i { 'match_full' }
698     |
699     /match partial/i { 'match_partial' }
700
701 key_action : key_delete 
702     |
703     key_update
704
705 key_delete : /on delete/i key_mutation
706     { 
707         $return = { 
708             type   => 'delete',
709             action => $item[2],
710         };
711     }
712
713 key_update : /on update/i key_mutation
714     { 
715         $return = { 
716             type   => 'update',
717             action => $item[2],
718         };
719     }
720
721 key_mutation : /no action/i { $return = 'no_action' }
722     |
723     /restrict/i { $return = 'restrict' }
724     |
725     /cascade/i { $return = 'cascade' }
726     |
727     /set null/i { $return = 'set null' }
728     |
729     /set default/i { $return = 'set default' }
730
731 alter : alter_table table_name add_column field ';' 
732     { 
733         my $field_def = $item[4];
734         $tables{ $item[2]->{'table_name'} }{'fields'}{ $field_def->{'name'} } = {
735             %$field_def, order => $field_order++
736         };
737         1;
738     }
739
740 alter : alter_table table_name ADD table_constraint ';' 
741     { 
742         my $table_name = $item[2]->{'table_name'};
743         my $constraint = $item[4];
744         push @{ $tables{ $table_name }{'constraints'} }, $constraint;
745         1;
746     }
747
748 alter : alter_table table_name drop_column NAME restrict_or_cascade(?) ';' 
749     {
750         $tables{ $item[2]->{'table_name'} }{'fields'}{ $item[4] }{'drop'} = 1;
751         1;
752     }
753
754 alter : alter_table table_name alter_column NAME alter_default_val ';' 
755     {
756         $tables{ $item[2]->{'table_name'} }{'fields'}{ $item[4] }{'default'} = 
757             $item[5]->{'value'};
758         1;
759     }
760
761 #
762 # These will just parse for now but won't affect the structure. - ky
763 #
764 alter : alter_table table_name /rename/i /to/i NAME ';'
765     { 1 }
766
767 alter : alter_table table_name alter_column NAME SET /statistics/i INTEGER ';' 
768     { 1 }
769
770 alter : alter_table table_name alter_column NAME SET /storage/i storage_type ';'
771     { 1 }
772
773 alter : alter_table table_name rename_column NAME /to/i NAME ';'
774     { 1 }
775
776 alter : alter_table table_name DROP /constraint/i NAME restrict_or_cascade ';'
777     { 1 }
778
779 alter : alter_table table_name /owner/i /to/i NAME ';'
780     { 1 }
781
782 alter : alter_sequence NAME /owned/i /by/i column_name ';'
783     { 1 }
784
785 storage_type : /(plain|external|extended|main)/i
786
787 temporary: /temp(orary)?\\b/i
788
789 temporary_table: temporary
790     {
791         1;
792     }
793
794 alter_default_val : SET default_val 
795     { 
796         $return = { value => $item[2]->{'value'} } 
797     }
798     | DROP DEFAULT 
799     { 
800         $return = { value => undef } 
801     } 
802
803 #
804 # This is a little tricky to get right, at least WRT to making the 
805 # tests pass.  The problem is that the constraints are stored just as
806 # a list (no name access), and the tests expect the constraints in a
807 # particular order.  I'm going to leave the rule but disable the code 
808 # for now. - ky
809 #
810 alter : alter_table table_name alter_column NAME alter_nullable ';'
811     {
812 #        my $table_name  = $item[2]->{'table_name'};
813 #        my $field_name  = $item[4];
814 #        my $is_nullable = $item[5]->{'is_nullable'};
815 #
816 #        $tables{ $table_name }{'fields'}{ $field_name }{'is_nullable'} = 
817 #            $is_nullable;
818 #
819 #        if ( $is_nullable ) {
820 #            1;
821 #            push @{ $tables{ $table_name }{'constraints'} }, {
822 #                type   => 'not_null',
823 #                fields => [ $field_name ],
824 #            };
825 #        }
826 #        else {
827 #            for my $i ( 
828 #                0 .. $#{ $tables{ $table_name }{'constraints'} || [] } 
829 #            ) {
830 #                my $c = $tables{ $table_name }{'constraints'}[ $i ] or next;
831 #                my $fields = join( '', @{ $c->{'fields'} || [] } ) or next;
832 #                if ( $c->{'type'} eq 'not_null' && $fields eq $field_name ) {
833 #                    delete $tables{ $table_name }{'constraints'}[ $i ];
834 #                    last;
835 #                }
836 #            }
837 #        }
838
839         1;
840     }
841
842 alter_nullable : SET not_null 
843     { 
844         $return = { is_nullable => 0 } 
845     }
846     | DROP not_null
847     { 
848         $return = { is_nullable => 1 } 
849     }
850
851 not_null : /not/i /null/i
852
853 not : /not/i
854
855 add_column : ADD COLUMN(?)
856
857 alter_table : ALTER TABLE ONLY(?)
858
859 alter_sequence : ALTER SEQUENCE 
860
861 drop_column : DROP COLUMN(?)
862
863 alter_column : ALTER COLUMN(?)
864
865 rename_column : /rename/i COLUMN(?)
866
867 restrict_or_cascade : /restrict/i | 
868     /cascade/i
869
870 # Handle functions that can be called
871 select : SELECT select_function ';' 
872     { 1 }
873
874 # Read the setval function but don't do anything with it because this parser
875 # isn't handling sequences
876 select_function : schema_qualification(?) /setval/i '(' VALUE /,/ VALUE /,/ /(true|false)/i ')' 
877     { 1 }
878
879 # Skipping all COPY commands
880 copy : COPY WORD /[^;]+/ ';' { 1 }
881     { 1 }
882
883 # The "\." allows reading in from STDIN but this isn't needed for schema
884 # creation, so it is skipped.
885 readin_symbol : '\.'
886     {1}
887
888 #
889 # End basically useless stuff. - ky
890 #
891
892 create_table : CREATE TABLE
893
894 create_index : CREATE /index/i
895
896 default_val  : DEFAULT /(\d+|'[^']*'|\w+\(.*\))|\w+/
897     { 
898         my $val =  defined $item[2] ? $item[2] : '';
899         $val    =~ s/^'|'$//g; 
900         $return =  {
901             supertype => 'constraint',
902             type      => 'default',
903             value     => $val,
904         }
905     }
906     | /null/i
907     { 
908         $return =  {
909             supertype => 'constraint',
910             type      => 'default',
911             value     => 'NULL',
912         }
913     }
914
915 name_with_opt_paren : NAME parens_value_list(s?)
916     { $item[2][0] ? "$item[1]($item[2][0][0])" : $item[1] }
917
918 unique : /unique/i { 1 }
919
920 key : /key/i | /index/i
921
922 table_option : /inherits/i '(' name_with_opt_quotes(s /,/) ')'
923     { 
924         $return = { type => 'inherits', table_name => $item[3] }
925     }
926     |
927     /with(out)? oids/i
928     {
929         $return = { type => $item[1] =~ /out/i ? 'without_oids' : 'with_oids' }
930     }
931
932 ADD : /add/i
933
934 ALTER : /alter/i
935
936 CREATE : /create/i
937
938 ONLY : /only/i
939
940 DEFAULT : /default/i
941
942 DROP : /drop/i
943
944 COLUMN : /column/i
945
946 TABLE : /table/i
947
948 SCHEMA : /schema/i
949
950 SEMICOLON : /\s*;\n?/
951
952 SEQUENCE : /sequence/i
953
954 SELECT : /select/i
955
956 COPY : /copy/i
957
958 INTEGER : /\d+/
959
960 WORD : /\w+/
961
962 DIGITS : /\d+/
963
964 COMMA : ','
965
966 SET : /set/i
967
968 NAME    : "`" /\w+/ "`"
969     { $item[2] }
970     | /\w+/
971     { $item[1] }
972     | /[\$\w]+/
973     { $item[1] }
974
975 VALUE   : /[-+]?\.?\d+(?:[eE]\d+)?/
976     { $item[1] }
977     | /'.*?'/   # XXX doesn't handle embedded quotes
978     { $item[1] }
979     | /null/i
980     { 'NULL' }
981
982 !;
983
984 # -------------------------------------------------------------------
985 sub parse {
986     my ( $translator, $data ) = @_;
987     $parser ||= Parse::RecDescent->new($GRAMMAR);
988
989     $::RD_TRACE  = $translator->trace ? 1 : undef;
990     $DEBUG       = $translator->debug;
991
992     unless (defined $parser) {
993         return $translator->error("Error instantiating Parse::RecDescent ".
994             "instance: Bad grammer");
995     }
996
997     my $result = $parser->startrule($data);
998     die "Parse failed.\n" unless defined $result;
999     warn Dumper($result) if $DEBUG;
1000
1001     my $schema = $translator->schema;
1002     my @tables = sort { 
1003         ( $result->{ $a }{'order'} || 0 ) <=> ( $result->{ $b }{'order'} || 0 )
1004     } keys %{ $result };
1005
1006     for my $table_name ( @tables ) {
1007         my $tdata =  $result->{ $table_name };
1008         my $table =  $schema->add_table( 
1009             #schema => $tdata->{'schema_name'},
1010             name   => $tdata->{'table_name'},
1011         ) or die "Couldn't create table '$table_name': " . $schema->error;
1012
1013         $table->extra(temporary => 1) if $tdata->{'temporary'};
1014
1015         $table->comments( $tdata->{'comments'} );
1016
1017         my @fields = sort { 
1018             $tdata->{'fields'}{ $a }{'order'} 
1019             <=>
1020             $tdata->{'fields'}{ $b }{'order'}
1021         } keys %{ $tdata->{'fields'} };
1022
1023         for my $fname ( @fields ) {
1024             my $fdata = $tdata->{'fields'}{ $fname };
1025             next if $fdata->{'drop'};
1026             my $field = $table->add_field(
1027                 name              => $fdata->{'name'},
1028                 data_type         => $fdata->{'data_type'},
1029                 size              => $fdata->{'size'},
1030                 default_value     => $fdata->{'default'},
1031                 is_auto_increment => $fdata->{'is_auto_increment'},
1032                 is_nullable       => $fdata->{'is_nullable'},
1033                 comments          => $fdata->{'comments'},
1034             ) or die $table->error;
1035
1036             $table->primary_key( $field->name ) if $fdata->{'is_primary_key'};
1037
1038             for my $cdata ( @{ $fdata->{'constraints'} } ) {
1039                 next unless $cdata->{'type'} eq 'foreign_key';
1040                 $cdata->{'fields'} ||= [ $field->name ];
1041                 push @{ $tdata->{'constraints'} }, $cdata;
1042             }
1043         }
1044
1045         for my $idata ( @{ $tdata->{'indices'} || [] } ) {
1046             my $index  =  $table->add_index(
1047                 name   => $idata->{'name'},
1048                 type   => uc $idata->{'type'},
1049                 fields => $idata->{'fields'},
1050             ) or die $table->error . ' ' . $table->name;
1051         }
1052
1053         for my $cdata ( @{ $tdata->{'constraints'} || [] } ) {
1054             my $constraint       =  $table->add_constraint(
1055                 name             => $cdata->{'name'},
1056                 type             => $cdata->{'type'},
1057                 fields           => $cdata->{'fields'},
1058                 reference_table  => $cdata->{'reference_table'},
1059                 reference_fields => $cdata->{'reference_fields'},
1060                 match_type       => $cdata->{'match_type'} || '',
1061                 on_delete        => $cdata->{'on_delete'} || $cdata->{'on_delete_do'},
1062                 on_update        => $cdata->{'on_update'} || $cdata->{'on_update_do'},
1063                 expression       => $cdata->{'expression'},
1064             ) or die "Can't add constraint of type '" .
1065                 $cdata->{'type'} .  "' to table '" . $table->name . 
1066                 "': " . $table->error;
1067         }
1068     }
1069
1070     return 1;
1071 }
1072
1073 1;
1074
1075 # -------------------------------------------------------------------
1076 # Rescue the drowning and tie your shoestrings.
1077 # Henry David Thoreau 
1078 # -------------------------------------------------------------------
1079
1080 =pod
1081
1082 =head1 AUTHORS
1083
1084 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>,
1085 Allen Day E<lt>allenday@ucla.eduE<gt>.
1086
1087 =head1 SEE ALSO
1088
1089 perl(1), Parse::RecDescent.
1090
1091 =cut