Modified comment rules.
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Parser / PostgreSQL.pm
index e8492ad..72f8ff0 100644 (file)
@@ -1,12 +1,9 @@
 package SQL::Translator::Parser::PostgreSQL;
 
 # -------------------------------------------------------------------
-# $Id: PostgreSQL.pm,v 1.34 2003-12-10 23:09:19 kycl4rk Exp $
+# $Id: PostgreSQL.pm,v 1.38 2004-03-01 17:39:22 kycl4rk Exp $
 # -------------------------------------------------------------------
-# Copyright (C) 2003 Ken Y. Clark <kclark@cpan.org>,
-#                    Allen Day <allenday@users.sourceforge.net>,
-#                    darren chamberlain <darren@cpan.org>,
-#                    Chris Mungall <cjm@fruitfly.org>
+# Copyright (C) 2002-4 SQLFairy Authors
 #
 # This program is free software; you can redistribute it and/or
 # modify it under the terms of the GNU General Public License as
@@ -111,7 +108,7 @@ View table:
 
 use strict;
 use vars qw[ $DEBUG $VERSION $GRAMMAR @EXPORT_OK ];
-$VERSION = sprintf "%d.%02d", q$Revision: 1.34 $ =~ /(\d+)\.(\d+)/;
+$VERSION = sprintf "%d.%02d", q$Revision: 1.38 $ =~ /(\d+)\.(\d+)/;
 $DEBUG   = 0 unless defined $DEBUG;
 
 use Data::Dumper;
@@ -230,7 +227,7 @@ create : create_table table_name '(' create_definition(s /,/) ')' table_option(s
         1;
     }
 
-create : /create/i unique(?) /(index|key)/i index_name /on/i table_name using_method(?) '(' field_name(s /,/) ')' where_predicate(?) ';'
+create : CREATE unique(?) /(index|key)/i index_name /on/i table_name using_method(?) '(' field_name(s /,/) ')' where_predicate(?) ';'
     {
         push @{ $tables{ $item{'table_name'} }{'indices'} },
             {
@@ -245,9 +242,9 @@ create : /create/i unique(?) /(index|key)/i index_name /on/i table_name using_me
     }
 
 #
-# Create anything else (e.g., domain, function, etc.)
+# Create anything else (e.g., domain, etc.)
 #
-create : /create/i WORD /[^;]+/ ';'
+create : CREATE WORD /[^;]+/ ';'
     { @table_comments = (); }
 
 using_method : /using/i WORD { $item[2] }
@@ -258,15 +255,15 @@ create_definition : field
     | table_constraint
     | <error>
 
-table_comment : comment
-    {
-        my $comment = $item[1];
+comment : /^\s*(?:#|-{2})(.*)\n/ 
+    { 
+        my $comment =  $item[1];
+        $comment    =~ s/^\s*(#|-*)\s*//;
+        $comment    =~ s/\s*$//;
         $return     = $comment;
         push @table_comments, $comment;
     }
 
-comment : /^\s*(?:#|-{2}).*\n/
-
 comment_on_table : /comment/i /on/i /table/i table_name /is/i comment_phrase ';'
     {
         push @{ $tables{ $item{'table_name'} }{'comments'} }, $item{'comment_phrase'};
@@ -290,7 +287,7 @@ comment_phrase : /'.*?'|NULL/
         $return = $val;
     }
 
-field : comment(s?) field_name data_type field_meta(s?) comment(s?)
+field : field_comment(s?) field_name data_type field_meta(s?) field_comment(s?)
     {
         my ( $default, @constraints, $is_pk );
         my $is_nullable = 1;
@@ -326,6 +323,14 @@ field : comment(s?) field_name data_type field_meta(s?) comment(s?)
     }
     | <error>
 
+field_comment : /^\s*(?:#|-{2})(.*)\n/ 
+    { 
+        my $comment =  $item[1];
+        $comment    =~ s/^\s*(#|-*)\s*//;
+        $comment    =~ s/\s*$//;
+        $return     = $comment;
+    }
+
 field_meta : default_val
     | column_constraint
 
@@ -608,7 +613,7 @@ key_action : key_delete
 
 key_delete : /on delete/i key_mutation
     { 
-        $return => { 
+        $return = { 
             type   => 'delete',
             action => $item[2],
         };
@@ -616,7 +621,7 @@ key_delete : /on delete/i key_mutation
 
 key_update : /on update/i key_mutation
     { 
-        $return => { 
+        $return = { 
             type   => 'update',
             action => $item[2],
         };
@@ -761,9 +766,9 @@ restrict_or_cascade : /restrict/i |
 # End basically useless stuff. - ky
 #
 
-create_table : /create/i TABLE
+create_table : CREATE TABLE
 
-create_index : /create/i /index/i
+create_index : CREATE /index/i
 
 default_val  : DEFAULT /(\d+|'[^']*'|\w+\(.*?\))|\w+/
     { 
@@ -805,6 +810,8 @@ ADD : /add/i
 
 ALTER : /alter/i
 
+CREATE : /create/i
+
 ONLY : /only/i
 
 DEFAULT : /default/i
@@ -871,6 +878,8 @@ sub parse {
             name  => $tdata->{'table_name'},
         ) or die "Couldn't create table '$table_name': " . $schema->error;
 
+        $table->comments( $tdata->{'comments'} );
+
         my @fields = sort { 
             $tdata->{'fields'}->{ $a }->{'order'} 
             <=>
@@ -887,6 +896,7 @@ sub parse {
                 default_value     => $fdata->{'default'},
                 is_auto_increment => $fdata->{'is_auto_increment'},
                 is_nullable       => $fdata->{'is_nullable'},
+                comments          => $fdata->{'comments'},
             ) or die $table->error;
 
             $table->primary_key( $field->name ) if $fdata->{'is_primary_key'};