Fixing bugs as reported by S. Quinney in RT ticket.
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Parser / PostgreSQL.pm
index ebde999..469c9e9 100644 (file)
@@ -1,7 +1,7 @@
 package SQL::Translator::Parser::PostgreSQL;
 
 # -------------------------------------------------------------------
-# $Id: PostgreSQL.pm,v 1.7 2003-02-25 21:25:14 kycl4rk Exp $
+# $Id: PostgreSQL.pm,v 1.23 2003-08-07 18:15:51 kycl4rk Exp $
 # -------------------------------------------------------------------
 # Copyright (C) 2003 Ken Y. Clark <kclark@cpan.org>,
 #                    Allen Day <allenday@users.sourceforge.net>,
@@ -84,11 +84,34 @@ Index:
       [ USING acc_method ] ( func_name( column [, ... ]) [ ops_name ] )
       [ WHERE predicate ]
 
+Alter table:
+
+  ALTER TABLE [ ONLY ] table [ * ]
+      ADD [ COLUMN ] column type [ column_constraint [ ... ] ]
+  ALTER TABLE [ ONLY ] table [ * ]
+      ALTER [ COLUMN ] column { SET DEFAULT value | DROP DEFAULT }
+  ALTER TABLE [ ONLY ] table [ * ]
+      ALTER [ COLUMN ] column SET STATISTICS integer
+  ALTER TABLE [ ONLY ] table [ * ]
+      RENAME [ COLUMN ] column TO newcolumn
+  ALTER TABLE table
+      RENAME TO new_table
+  ALTER TABLE table
+      ADD table_constraint_definition
+  ALTER TABLE [ ONLY ] table 
+          DROP CONSTRAINT constraint { RESTRICT | CASCADE }
+  ALTER TABLE table
+          OWNER TO new_owner 
+
+View table:
+
+    CREATE [ OR REPLACE ] VIEW view [ ( column name list ) ] AS SELECT query
+
 =cut
 
 use strict;
 use vars qw[ $DEBUG $VERSION $GRAMMAR @EXPORT_OK ];
-$VERSION = sprintf "%d.%02d", q$Revision: 1.7 $ =~ /(\d+)\.(\d+)/;
+$VERSION = sprintf "%d.%02d", q$Revision: 1.23 $ =~ /(\d+)\.(\d+)/;
 $DEBUG   = 0 unless defined $DEBUG;
 
 use Data::Dumper;
@@ -122,36 +145,49 @@ eofile : /^\Z/
 
 statement : create
   | comment
+  | alter
   | grant
   | revoke
   | drop
+  | insert
   | connect
+  | update
+  | set
   | <error>
 
 connect : /^\s*\\\connect.*\n/
 
-revoke : /revoke/i WORD(s /,/) /on/i table_name /from/i name_with_opt_quotes(s /,/) ';'
+set : /SET/ /[^;]*/ ';'
+
+revoke : /revoke/i WORD(s /,/) /on/i TABLE(?) table_name /from/i name_with_opt_quotes(s /,/) ';'
     {
         my $table_name = $item{'table_name'};
         push @{ $tables{ $table_name }{'permissions'} }, {
             type       => 'revoke',
             actions    => $item[2],
-            users      => $item[6],
+            users      => $item[7],
         }
     }
 
-grant : /grant/i WORD(s /,/) /on/i table_name /to/i name_with_opt_quotes(s /,/) ';'
+grant : /grant/i WORD(s /,/) /on/i TABLE(?) table_name /to/i name_with_opt_quotes(s /,/) ';'
     {
         my $table_name = $item{'table_name'};
         push @{ $tables{ $table_name }{'permissions'} }, {
             type       => 'grant',
             actions    => $item[2],
-            users      => $item[6],
+            users      => $item[7],
         }
     }
 
 drop : /drop/i /[^;]*/ ';'
 
+insert : /insert/i /[^;]*/ ';'
+
+update : /update/i /[^;]*/ ';'
+
+#
+# Create table.
+#
 create : create_table table_name '(' create_definition(s /,/) ')' table_option(s?) ';'
     {
         my $table_name                       = $item{'table_name'};
@@ -167,21 +203,26 @@ create : create_table table_name '(' create_definition(s /,/) ')' table_option(s
                     { %$definition, order => $i };
                 $i++;
                                
-                if ( $definition->{'is_primary_key'} ) {
-                    push @{ $tables{ $table_name }{'indices'} }, {
-                        type   => 'primary_key',
-                        fields => [ $field_name ],
-                    };
-                }
-
-                for my $constraint ( @{ $definition->{'constaints'} || [] } ) {
-                    $constraint->{'fields' } = [ $field_name ];
-                    push @{$tables{ $table_name }{'constraints'}}, $constraint;
+                for my $constraint ( @{ $definition->{'constraints'} || [] } ) {
+                    $constraint->{'fields'} = [ $field_name ];
+                    push @{ $tables{ $table_name }{'constraints'} },
+                        $constraint;
                 }
             }
             elsif ( $definition->{'type'} eq 'constraint' ) {
                 $definition->{'type'} = $definition->{'constraint_type'};
-                push @{ $tables{ $table_name }{'constraints'} }, $definition;
+                # group FKs at the field level
+#                if ( $definition->{'type'} eq 'foreign_key' ) {
+#                    for my $fld ( @{ $definition->{'fields'} || [] } ) {
+#                        push @{ 
+#                            $tables{$table_name}{'fields'}{$fld}{'constraints'}
+#                        }, $definition;
+#                    }
+#                }
+#                else {
+                    push @{ $tables{ $table_name }{'constraints'} }, 
+                        $definition;
+#                }
             }
             else {
                 push @{ $tables{ $table_name }{'indices'} }, $definition;
@@ -189,13 +230,16 @@ create : create_table table_name '(' create_definition(s /,/) ')' table_option(s
         }
 
         for my $option ( @{ $item[6] } ) {
-            $tables{ $table_name }{'table_options'}{ $option->{'type'} } = 
+            $tables{ $table_name }{'table_options(s?)'}{ $option->{'type'} } = 
                 $option;
         }
 
         1;
     }
 
+#
+# Create index.
+#
 create : /create/i unique(?) /(index|key)/i index_name /on/i table_name using_method(?) '(' field_name(s /,/) ')' where_predicate(?) ';'
     {
         push @{ $tables{ $item{'table_name'} }{'indices'} },
@@ -208,44 +252,59 @@ create : /create/i unique(?) /(index|key)/i index_name /on/i table_name using_me
         ;
     }
 
+#
+# Create anything else (e.g., domain, function, etc.)
+#
+create : /create/i WORD /[^;]+/ ';'
+
 using_method : /using/i WORD { $item[2] }
 
 where_predicate : /where/i /[^;]+/
 
 create_definition : field
-    | index
     | table_constraint
     | <error>
 
 comment : /^\s*(?:#|-{2}).*\n/
 
-field : comment(s?) field_name data_type field_meta(s?)
+field : comment(s?) field_name data_type field_meta(s?) comment(s?)
     {
-        my ( $default, @constraints );
+        my ( $default, @constraints, $is_pk );
+        my $null = 1;
         for my $meta ( @{ $item[4] } ) {
-            $default = $meta if $meta->{'meta_type'} eq 'default';
-            push @constraints, $meta if $meta->{'meta_type'} eq 'constraint';
+            if ( $meta->{'type'} eq 'default' ) {
+                $default = $meta;
+                next;
+            }
+            elsif ( $meta->{'type'} eq 'not_null' ) {
+                $null = 0;
+                next;
+            }
+            elsif ( $meta->{'type'} eq 'primary_key' ) {
+                $is_pk = 1;
+            }
+
+            push @constraints, $meta if $meta->{'supertype'} eq 'constraint';
         }
 
-        my $null = ( grep { $_->{'type'} eq 'not_null' } @constraints ) ? 0 : 1;
+        my @comments = ( @{ $item[1] }, @{ $item[5] } );
 
-        $return = { 
+        $return = {
             type           => 'field',
             name           => $item{'field_name'}, 
             data_type      => $item{'data_type'}{'type'},
             size           => $item{'data_type'}{'size'},
-            list           => $item{'data_type'}{'list'},
             null           => $null,
             default        => $default->{'value'},
             constraints    => [ @constraints ],
-            comments       => $item[1],
+            comments       => [ @comments ],
+            is_primary_key => $is_pk || 0,
         } 
     }
     | <error>
 
 field_meta : default_val
-    |
-    column_constraint
+    | column_constraint
 
 column_constraint : constraint_name(?) column_constraint_type deferrable(?) deferred(?)
     {
@@ -255,7 +314,7 @@ column_constraint : constraint_name(?) column_constraint_type deferrable(?) defe
         my $expression = $desc->{'expression'} || '';
 
         $return              =  {
-            meta_type        => 'constraint',
+            supertype        => 'constraint',
             name             => $item{'constraint_name'}[0] || '',
             type             => $type,
             expression       => $type eq 'check' ? $expression : '',
@@ -285,27 +344,29 @@ column_constraint_type : /not null/i { $return = { type => 'not_null' } }
     /check/i '(' /[^)]+/ ')' 
         { $return = { type => 'check', expression => $item[2] } }
     |
-    /references/i table_name parens_value_list(?) match_type(?) on_delete_do(?) on_update_do(?)
+    /references/i table_name parens_word_list(?) match_type(?) key_action(s?)
     {
+        my ( $on_delete, $on_update );
+        for my $action ( @{ $item[5] || [] } ) {
+            $on_delete = $action->{'action'} if $action->{'type'} eq 'delete';
+            $on_update = $action->{'action'} if $action->{'type'} eq 'update';
+        }
+
         $return              =  {
             type             => 'foreign_key',
             reference_table  => $item[2],
-            reference_fields => $item[3],
+            reference_fields => $item[3][0],
             match_type       => $item[4][0],
-            on_delete_do     => $item[5][0],
-            on_update_do     => $item[6][0],
+            on_delete_do     => $on_delete,
+            on_update_do     => $on_update,
         }
     }
 
-index : primary_key_index
-    | unique_index
-    | normal_index
-
 table_name : name_with_opt_quotes
 
 field_name : name_with_opt_quotes
 
-name_with_opt_quotes : double_quote(?) WORD double_quote(?) { $item[2] }
+name_with_opt_quotes : double_quote(?) NAME double_quote(?) { $item[2] }
 
 double_quote: /"/
 
@@ -313,55 +374,110 @@ index_name : WORD
 
 data_type : pg_data_type parens_value_list(?)
     { 
-        my $type = $item[1];
+        my $data_type = $item[1];
 
         #
         # We can deduce some sizes from the data type's name.
         #
-        my $size; 
-        if ( ref $type eq 'ARRAY' ) {
-            $size = [ $type->[1] ];
-            $type = $type->[0];
-        }
-        else {
-            $size = $item[2][0] || '';
-        }
+        $data_type->{'size'} ||= $item[2][0];
 
-        $return  = { 
-            type => $type,
-            size => $size,
-        } 
+        $return  = $data_type;
     }
 
 pg_data_type :
-    /(bigint|int8|bigserial|serial8)/ { $return = [ 'integer', 8 ] }
+    /(bigint|int8|bigserial|serial8)/i
+        { 
+            $return = { 
+                type           => 'integer',
+                size           => [8],
+                auto_increment => 1,
+            };
+        }
     |
-    /(smallint|int2)/ { $return = [ 'integer', 2 ] }
+    /(smallint|int2)/i
+        { 
+            $return = {
+                type => 'integer', 
+                size => [2],
+            };
+        }
     |
-    /int(eger)?|int4/ { $return = [ 'integer', 4 ] }
+    /int(eger)?|int4/i
+        { 
+            $return = {
+                type => 'integer', 
+                size => [4],
+            };
+        }
     |
-    /(double precision|float8?)/ { $return = [ 'float', 8 ] }
+    /(double precision|float8?)/i
+        { 
+            $return = {
+                type => 'float', 
+                size => [8],
+            }; 
+        }
     |
-    /(real|float4)/ { $return = [ 'real', 4 ] }
+    /(real|float4)/i
+        { 
+            $return = {
+                type => 'real', 
+                size => [4],
+            };
+        }
     |
-    /serial4?/ { $return = [ 'serial', 4 ] }
+    /serial4?/i
+        { 
+            $return = { 
+                type           => 'integer',
+                size           => [4], 
+                auto_increment => 1,
+            };
+        }
     |
-    /bigserial/ { $return = [ 'serial', 8 ] }
+    /bigserial/i
+        { 
+            $return = { 
+                type           => 'integer', 
+                size           => [8], 
+                auto_increment => 1,
+            };
+        }
     |
-    /(bit varying|varbit)/ { $return = 'varbit' }
+    /(bit varying|varbit)/i
+        { 
+            $return = { type => 'varbit' };
+        }
     |
-    /character varying/ { $return = 'varchar' }
+    /character varying/i
+        { 
+            $return = { type => 'varchar' };
+        }
     |
-    /char(acter)?/ { $return = 'char' }
+    /char(acter)?/i
+        { 
+            $return = { type => 'char' };
+        }
     |
-    /bool(ean)?/ { $return = 'boolean' }
+    /bool(ean)?/i
+        { 
+            $return = { type => 'boolean' };
+        }
     |
-    /(bytea|binary data)/ { $return = 'binary' }
+    /bytea/i
+        { 
+            $return = { type => 'bytea' };
+        }
     |
-    /timestampz?/ { $return = 'timestamp' }
+    /timestampz?/i
+        { 
+            $return = { type => 'timestamp' };
+        }
     |
-    /(bit|box|cidr|circle|date|inet|interval|line|lseg|macaddr|money|numeric|decimal|path|point|polygon|text|time|varchar)/
-    { $item[1] }
+    /(bit|box|cidr|circle|date|inet|interval|line|lseg|macaddr|money|numeric|decimal|path|point|polygon|text|time|varchar)/i
+        { 
+            $return = { type => $item[1] };
+        }
 
 parens_value_list : '(' VALUE(s /,/) ')'
     { $item[2] }
@@ -376,12 +492,13 @@ num_range : DIGITS ',' DIGITS
     | DIGITS
     { $return = $item[1] }
 
-table_constraint : constraint_name(?) table_constraint_type deferrable(?) deferred(?)
+table_constraint : comment(s?) constraint_name(?) table_constraint_type deferrable(?) deferred(?) comment(s?)
     {
         my $desc       = $item{'table_constraint_type'};
         my $type       = $desc->{'type'};
         my $fields     = $desc->{'fields'};
         my $expression = $desc->{'expression'};
+        my @comments   = ( @{ $item[1] }, @{ $item[-1] } );
 
         $return              =  {
             name             => $item{'constraint_name'}[0] || '',
@@ -394,8 +511,9 @@ table_constraint : constraint_name(?) table_constraint_type deferrable(?) deferr
             reference_table  => $desc->{'reference_table'},
             reference_fields => $desc->{'reference_fields'},
             match_type       => $desc->{'match_type'}[0],
-            on_delete_do     => $desc->{'on_delete_do'}[0],
-            on_update_do     => $desc->{'on_update_do'}[0],
+            on_delete_do     => $desc->{'on_delete_do'},
+            on_update_do     => $desc->{'on_update_do'},
+            comments         => [ @comments ],
         } 
     }
 
@@ -423,16 +541,22 @@ table_constraint_type : /primary key/i '(' name_with_opt_quotes(s /,/) ')'
         }
     }
     |
-    /foreign key/i '(' name_with_opt_quotes(s /,/) ')' /references/i table_name parens_word_list(?) match_type(?) on_delete_do(?) on_update_do(?)
+    /foreign key/i '(' name_with_opt_quotes(s /,/) ')' /references/i table_name parens_word_list(?) match_type(?) key_action(s?)
     {
+        my ( $on_delete, $on_update );
+        for my $action ( @{ $item[9] || [] } ) {
+            $on_delete = $action->{'action'} if $action->{'type'} eq 'delete';
+            $on_update = $action->{'action'} if $action->{'type'} eq 'update';
+        }
+        
         $return              =  {
             type             => 'foreign_key',
             fields           => $item[3],
             reference_table  => $item[6],
             reference_fields => $item[7][0],
             match_type       => $item[8][0],
-            on_delete_do     => $item[9][0],
-            on_update_do     => $item[10][0],
+            on_delete_do     => $on_delete || '',
+            on_update_do     => $on_update || '',
         }
     }
 
@@ -447,55 +571,69 @@ match_type : /match full/i { 'match_full' }
     |
     /match partial/i { 'match_partial' }
 
-on_delete_do : /on delete/i WORD 
-    { $item[2] }
+key_action : key_delete 
+    |
+    key_update
 
-on_update_do : /on update/i WORD
-    { $item[2] }
+key_delete : /on delete/i key_mutation
+    { 
+        $return => { 
+            type   => 'delete',
+            action => $item[2],
+        };
+    }
 
-create_table : /create/i /table/i
+key_update : /on update/i key_mutation
+    { 
+        $return => { 
+            type   => 'update',
+            action => $item[2],
+        };
+    }
 
-create_index : /create/i /index/i
+key_mutation : /no action/i { $return = 'no_action' }
+    |
+    /restrict/i { $return = 'restrict' }
+    |
+    /cascade/i { $return = 'cascade' }
+    |
+    /set null/i { $return = 'set_null' }
+    |
+    /set default/i { $return = 'set_default' }
 
-default_val  : /default/i /(?:')?[\w\d.-]*(?:')?/ 
+alter : alter_table table_name /add/i table_constraint ';' 
     { 
-        my $val =  $item[2] || '';
-        $val    =~ s/'//g; 
-        $return =  {
-            meta_type => 'default',
-            value     => $val,
-        }
+        my $table_name = $item[2];
+        my $constraint = $item[4];
+        $constraint->{'type'} = $constraint->{'constraint_type'};
+        push @{ $tables{ $table_name }{'constraints'} }, $constraint;
     }
 
-auto_inc : /auto_increment/i { 1 }
+alter_table : /alter/i /table/i only(?)
 
-primary_key : /primary/i /key/i { 1 }
+only : /only/i
 
-primary_key_index : primary_key index_name(?) '(' field_name(s /,/) ')'
-    { 
-        $return    = { 
-            name   => $item{'index_name'}[0],
-            type   => 'primary_key',
-            fields => $item[4],
-        } 
-    }
+create_table : /create/i TABLE
+
+create_index : /create/i /index/i
 
-normal_index : key index_name(?) '(' name_with_opt_paren(s /,/) ')'
+default_val  : /default/i /(?:')?[\w\d().-]*(?:')?/ 
     { 
-        $return    = { 
-            name   => $item{'index_name'}[0],
-            type   => 'normal',
-            fields => $item[4],
-        } 
+        my $val =  $item[2] || '';
+        $val    =~ s/'//g; 
+        $return =  {
+            supertype => 'constraint',
+            type      => 'default',
+            value     => $val,
+        }
     }
-
-unique_index : unique key(?) index_name(?) '(' name_with_opt_paren(s /,/) ')'
+    | /null/i
     { 
-        $return    = { 
-            name   => $item{'index_name'}[0],
-            type   => 'unique',
-            fields => $item[5],
-        } 
+        $return =  {
+            supertype => 'constraint',
+            type      => 'default',
+            value     => 'NULL',
+        }
     }
 
 name_with_opt_paren : NAME parens_value_list(s?)
@@ -515,6 +653,8 @@ table_option : /inherits/i '(' name_with_opt_quotes(s /,/) ')'
         $return = { type => $item[1] =~ /out/i ? 'without_oids' : 'with_oids' }
     }
 
+TABLE : /table/i
+
 SEMICOLON : /\s*;\n?/
 
 WORD : /\w+/
@@ -527,6 +667,8 @@ NAME    : "`" /\w+/ "`"
     { $item[2] }
     | /\w+/
     { $item[1] }
+    | /[\$\w]+/
+    { $item[1] }
 
 VALUE   : /[-+]?\.?\d+(?:[eE]\d+)?/
     { $item[1] }
@@ -553,22 +695,82 @@ sub parse {
     my $result = $parser->startrule($data);
     die "Parse failed.\n" unless defined $result;
     warn Dumper($result) if $DEBUG;
-    return $result;
+
+    my $schema = $translator->schema;
+    my @tables = sort { 
+        $result->{ $a }->{'order'} <=> $result->{ $b }->{'order'}
+    } keys %{ $result };
+
+    for my $table_name ( @tables ) {
+        my $tdata =  $result->{ $table_name };
+        my $table =  $schema->add_table( 
+            name  => $tdata->{'table_name'},
+        ) or die $schema->error;
+
+        my @fields = sort { 
+            $tdata->{'fields'}->{$a}->{'order'} 
+            <=>
+            $tdata->{'fields'}->{$b}->{'order'}
+        } keys %{ $tdata->{'fields'} };
+
+        for my $fname ( @fields ) {
+            my $fdata = $tdata->{'fields'}{ $fname };
+            my $field = $table->add_field(
+                name              => $fdata->{'name'},
+                data_type         => $fdata->{'data_type'},
+                size              => $fdata->{'size'},
+                default_value     => $fdata->{'default'},
+                is_auto_increment => $fdata->{'is_auto_inc'},
+                is_nullable       => $fdata->{'null'},
+            ) or die $table->error;
+
+            $table->primary_key( $field->name ) if $fdata->{'is_primary_key'};
+
+            for my $cdata ( @{ $fdata->{'constraints'} } ) {
+                next unless $cdata->{'type'} eq 'foreign_key';
+                $cdata->{'fields'} ||= [ $field->name ];
+                push @{ $tdata->{'constraints'} }, $cdata;
+            }
+        }
+
+        for my $idata ( @{ $tdata->{'indices'} || [] } ) {
+            my $index  =  $table->add_index(
+                name   => $idata->{'name'},
+                type   => uc $idata->{'type'},
+                fields => $idata->{'fields'},
+            ) or die $table->error;
+        }
+
+        for my $cdata ( @{ $tdata->{'constraints'} || [] } ) {
+            my $constraint       =  $table->add_constraint(
+                name             => $cdata->{'name'},
+                type             => $cdata->{'type'},
+                fields           => $cdata->{'fields'},
+                reference_table  => $cdata->{'reference_table'},
+                reference_fields => $cdata->{'reference_fields'},
+                match_type       => $cdata->{'match_type'} || '',
+                on_delete        => $cdata->{'on_delete_do'},
+                on_update        => $cdata->{'on_update_do'},
+            ) or die $table->error;
+        }
+    }
+
+    return 1;
 }
 
 1;
 
-#-----------------------------------------------------
-# Where man is not nature is barren.
-# William Blake
-#-----------------------------------------------------
+# -------------------------------------------------------------------
+# Rescue the drowning and tie your shoestrings.
+# Henry David Thoreau 
+# -------------------------------------------------------------------
 
 =pod
 
 =head1 AUTHORS
 
 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>,
-Allen Day <allenday@users.sourceforge.net>.
+Allen Day <allenday@ucla.edu>.
 
 =head1 SEE ALSO