Fixed bug where the "is_auto_increment" flag wasn't being set for serial
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Parser / PostgreSQL.pm
index 22896c4..39f5691 100644 (file)
@@ -1,7 +1,7 @@
 package SQL::Translator::Parser::PostgreSQL;
 
 # -------------------------------------------------------------------
-# $Id: PostgreSQL.pm,v 1.24 2003-08-12 22:00:28 kycl4rk Exp $
+# $Id: PostgreSQL.pm,v 1.28 2003-08-20 18:58:54 kycl4rk Exp $
 # -------------------------------------------------------------------
 # Copyright (C) 2003 Ken Y. Clark <kclark@cpan.org>,
 #                    Allen Day <allenday@users.sourceforge.net>,
@@ -111,7 +111,7 @@ View table:
 
 use strict;
 use vars qw[ $DEBUG $VERSION $GRAMMAR @EXPORT_OK ];
-$VERSION = sprintf "%d.%02d", q$Revision: 1.24 $ =~ /(\d+)\.(\d+)/;
+$VERSION = sprintf "%d.%02d", q$Revision: 1.28 $ =~ /(\d+)\.(\d+)/;
 $DEBUG   = 0 unless defined $DEBUG;
 
 use Data::Dumper;
@@ -290,15 +290,16 @@ field : comment(s?) field_name data_type field_meta(s?) comment(s?)
         my @comments = ( @{ $item[1] }, @{ $item[5] } );
 
         $return = {
-            type           => 'field',
-            name           => $item{'field_name'}, 
-            data_type      => $item{'data_type'}{'type'},
-            size           => $item{'data_type'}{'size'},
-            null           => $null,
-            default        => $default->{'value'},
-            constraints    => [ @constraints ],
-            comments       => [ @comments ],
-            is_primary_key => $is_pk || 0,
+            type              => 'field',
+            name              => $item{'field_name'}, 
+            data_type         => $item{'data_type'}{'type'},
+            size              => $item{'data_type'}{'size'},
+            null              => $null,
+            default           => $default->{'value'},
+            constraints       => [ @constraints ],
+            comments          => [ @comments ],
+            is_primary_key    => $is_pk || 0,
+            is_auto_increment => $item{'data_type'}{'is_auto_increment'},
         } 
     }
     | <error>
@@ -385,12 +386,11 @@ data_type : pg_data_type parens_value_list(?)
     }
 
 pg_data_type :
-    /(bigint|int8|bigserial|serial8)/i
+    /(bigint|int8)/i
         { 
             $return = { 
-                type           => 'integer',
-                size           => [8],
-                auto_increment => 1,
+                type => 'integer',
+                size => [8],
             };
         }
     |
@@ -402,13 +402,21 @@ pg_data_type :
             };
         }
     |
-    /int(eger)?|int4/i
+    /(integer|int4?)/i # interval must come before this
         { 
             $return = {
                 type => 'integer', 
                 size => [4],
             };
         }
+    |    
+    /(real|float4)/i
+        { 
+            $return = {
+                type => 'real', 
+                size => [4],
+            };
+        }
     |
     /(double precision|float8?)/i
         { 
@@ -418,29 +426,21 @@ pg_data_type :
             }; 
         }
     |
-    /(real|float4)/i
-        { 
-            $return = {
-                type => 'real', 
-                size => [4],
-            };
-        }
-    |
-    /serial4?/i
+    /(bigserial|serial8)/i
         { 
             $return = { 
-                type           => 'integer',
-                size           => [4], 
-                auto_increment => 1,
+                type              => 'integer', 
+                size              => [8], 
+                is_auto_increment => 1,
             };
         }
     |
-    /bigserial/i
+    /serial4?/i
         { 
             $return = { 
-                type           => 'integer', 
-                size           => [8], 
-                auto_increment => 1,
+                type              => 'integer',
+                size              => [4], 
+                is_auto_increment => 1,
             };
         }
     |
@@ -469,12 +469,12 @@ pg_data_type :
             $return = { type => 'bytea' };
         }
     |
-    /timestampz?/i
+    /(timestamptz|timestamp)/i
         { 
             $return = { type => 'timestamp' };
         }
     |
-    /(bit|box|cidr|circle|date|inet|interval|line|lseg|macaddr|money|numeric|decimal|path|point|polygon|text|time|varchar)/i
+    /(bit|box|cidr|circle|date|inet|interval|line|lseg|macaddr|money|numeric|decimal|path|point|polygon|text|timetz|time|varchar)/i
         { 
             $return = { type => $item[1] };
         }
@@ -617,7 +617,7 @@ create_table : /create/i TABLE
 
 create_index : /create/i /index/i
 
-default_val  : /default/i /(\d+|'[^']+'|\w+\(.*?\))/
+default_val  : /default/i /(\d+|'[^']*'|\w+\(.*?\))|\w+/
     { 
         my $val =  defined $item[2] ? $item[2] : '';
         $val    =~ s/^'|'$//g; 
@@ -705,7 +705,7 @@ sub parse {
         my $tdata =  $result->{ $table_name };
         my $table =  $schema->add_table( 
             name  => $tdata->{'table_name'},
-        ) or die $schema->error;
+        ) or die "Couldn't create table '$table_name': " . $schema->error;
 
         my @fields = sort { 
             $tdata->{'fields'}->{$a}->{'order'} 
@@ -720,7 +720,7 @@ sub parse {
                 data_type         => $fdata->{'data_type'},
                 size              => $fdata->{'size'},
                 default_value     => $fdata->{'default'},
-                is_auto_increment => $fdata->{'is_auto_inc'},
+                is_auto_increment => $fdata->{'is_auto_increment'},
                 is_nullable       => $fdata->{'null'},
             ) or die $table->error;