SQLite and YAML tests broken, fixed
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Producer / PostgreSQL.pm
index 216ba91..f97669c 100644 (file)
@@ -1,11 +1,9 @@
 package SQL::Translator::Producer::PostgreSQL;
 
 # -------------------------------------------------------------------
-# $Id: PostgreSQL.pm,v 1.18 2003-09-26 22:48:53 kycl4rk Exp $
+# $Id: PostgreSQL.pm,v 1.23 2005-07-05 16:20:43 mwz444 Exp $
 # -------------------------------------------------------------------
-# Copyright (C) 2003 Ken Y. Clark <kclark@cpan.org>,
-#                    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
@@ -26,11 +24,21 @@ package SQL::Translator::Producer::PostgreSQL;
 
 SQL::Translator::Producer::PostgreSQL - PostgreSQL producer for SQL::Translator
 
+=head1 SYNOPSIS
+
+  my $t = SQL::Translator->new( parser => '...', producer => 'PostgreSQL' );
+  $t->translate;
+
+=head1 DESCRIPTION
+
+Creates a DDL suitable for PostgreSQL.  Very heavily based on the Oracle
+producer.
+
 =cut
 
 use strict;
 use vars qw[ $DEBUG $WARN $VERSION ];
-$VERSION = sprintf "%d.%02d", q$Revision: 1.18 $ =~ /(\d+)\.(\d+)/;
+$VERSION = sprintf "%d.%02d", q$Revision: 1.23 $ =~ /(\d+)\.(\d+)/;
 $DEBUG = 1 unless defined $DEBUG;
 
 use SQL::Translator::Schema::Constants;
@@ -110,7 +118,7 @@ my %reserved = map { $_, 1 } qw[
     UNION UNIQUE USER USING VERBOSE WHEN WHERE
 ];
 
-my $max_id_length    = 30;
+my $max_id_length    = 62;
 my %used_identifiers = ();
 my %global_names;
 my %unreserve;
@@ -176,10 +184,18 @@ sub produce {
         $table_name       = mk_name( $table_name, '', undef, 1 );
         my $table_name_ur = unreserve($table_name);
 
+print STDERR "$table_name table_name\n";
         my ( @comments, @field_defs, @sequence_defs, @constraint_defs );
 
         push @comments, "--\n-- Table: $table_name_ur\n--" unless $no_comments;
 
+        if ( $table->comments and !$no_comments ){
+            my $c = "-- Comments: \n-- ";
+            $c .= join "\n-- ",  $table->comments;
+            $c .= "\n--";
+            push @comments, $c;
+        }
+
         #
         # Fields
         #
@@ -189,7 +205,11 @@ sub produce {
                 $field->name, '', \%field_name_scope, 1 
             );
             my $field_name_ur = unreserve( $field_name, $table_name );
-            my $field_def     = qq["$field_name_ur"];
+            my $field_comments = $field->comments 
+                ? "-- " . $field->comments . "\n  " 
+                : '';
+
+            my $field_def     = $field_comments.qq["$field_name_ur"];
 
             #
             # Datatype
@@ -406,7 +426,10 @@ sub produce {
         );
     }
 
-    $output .= join( "\n\n", @fks );
+    if ( @fks ) {
+        $output .= "--\n-- Foreign Key Definitions\n--\n\n" unless $no_comments;
+        $output .= join( "\n\n", @fks );
+    }
 
     if ( $WARN ) {
         if ( %truncated ) {
@@ -514,8 +537,12 @@ sub next_unused_name {
 
 =pod
 
+=head1 SEE ALSO
+
+SQL::Translator, SQL::Translator::Producer::Oracle.
+
 =head1 AUTHOR
 
-Ken Y. Clark E<lt>kclark@cpan.orgE<gt>
+Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
 
 =cut