Updates for dump method.
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Producer / Oracle.pm
index d25b227..c3febb1 100644 (file)
@@ -1,11 +1,9 @@
 package SQL::Translator::Producer::Oracle;
 
 # -------------------------------------------------------------------
-# $Id: Oracle.pm,v 1.25 2003-10-04 01:21:10 kycl4rk Exp $
+# $Id: Oracle.pm,v 1.30 2004-02-09 23:02:15 kycl4rk 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
@@ -22,9 +20,26 @@ package SQL::Translator::Producer::Oracle;
 # 02111-1307  USA
 # -------------------------------------------------------------------
 
+=head1 NAME
+
+SQL::Translator::Producer::Oracle - Oracle SQL producer
+
+=head1 SYNOPSIS
+
+  use SQL::Translator;
+
+  my $t = SQL::Translator->new( parser => '...', producer => 'Oracle' );
+  print $translator->translate( $file );
+
+=head1 DESCRIPTION
+
+Creates an SQL DDL suitable for Oracle.
+
+=cut
+
 use strict;
 use vars qw[ $VERSION $DEBUG $WARN ];
-$VERSION = sprintf "%d.%02d", q$Revision: 1.25 $ =~ /(\d+)\.(\d+)/;
+$VERSION = sprintf "%d.%02d", q$Revision: 1.30 $ =~ /(\d+)\.(\d+)/;
 $DEBUG   = 0 unless defined $DEBUG;
 
 use SQL::Translator::Schema::Constants;
@@ -145,8 +160,11 @@ sub produce {
 
     if ( $translator->parser_type =~ /mysql/i ) {
         $output .= 
-        "-- We assume that default NLS_DATE_FORMAT has been changed\n".
-        "-- but we set it here anyway to be self-consistent.\n".
+            "-- We assume that default NLS_DATE_FORMAT has been changed\n".
+            "-- but we set it here anyway to be self-consistent.\n"
+            unless $no_comments;
+
+        $output .= 
         "ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS';\n\n";
     }
 
@@ -196,7 +214,8 @@ sub produce {
             else {
                 $data_type  = defined $translate{ $data_type } ?
                               $translate{ $data_type } :
-                              die "Unknown datatype: $data_type\n";
+                              $data_type;
+                $data_type ||= 'varchar2';
             }
             
             #
@@ -249,7 +268,11 @@ sub produce {
                     }
                 }
                 elsif ( 
-                    $data_type =~ /date/ && $default eq 'current_timestamp' 
+                    $data_type =~ /date/ && (
+                        $default eq 'current_timestamp' 
+                        ||
+                        $default eq 'now()' 
+                    )
                 ) {
                     $default = 'SYSDATE';
                 }
@@ -264,10 +287,6 @@ sub produce {
             # Not null constraint
             #
             unless ( $field->is_nullable ) {
-#                my $constraint_name = mk_name( 
-#                    join('_', $table_name_ur, $field_name_ur ), 'nn' 
-#                );
-#                $field_def .= ' CONSTRAINT ' . $constraint_name . ' NOT NULL';
                 $field_def .= ' NOT NULL';
             }
 
@@ -284,7 +303,7 @@ sub produce {
                 push @trigger_defs, 
                     "CREATE SEQUENCE $seq_name;\n" .
                     "CREATE OR REPLACE TRIGGER $trigger_name\n" .
-                    "BEFORE INSERT ON $table_name\n" .
+                    "BEFORE INSERT ON $table_name_ur\n" .
                     "FOR EACH ROW WHEN (\n" .
                         " new.$field_name_ur IS NULL".
                         " OR new.$field_name_ur = 0\n".
@@ -314,7 +333,7 @@ sub produce {
             if ( my $comment = $field->comments ) {
                 push @field_comments, 
                     "COMMENT ON COLUMN $table_name.$field_name_ur is\n  '".
-                    $comment."';";
+                    $comment."';" unless $no_comments;
             }
         }
 
@@ -358,6 +377,15 @@ sub produce {
             }
             elsif ( $c->type eq UNIQUE ) {
                 $name ||= mk_name( $table_name, 'u' );
+                for my $f ( $c->fields ) {
+                    my $field_def = $table->get_field( $f ) or next;
+                    my $dtype     = $translate{ $field_def->data_type } or next;
+                    if ( $WARN && $dtype =~ /clob/i ) {
+                        warn "Oracle will not allow UNIQUE constraints on " .
+                             "CLOB field '" . $field_def->table->name . '.' .
+                             $field_def->name . ".'\n"
+                    }
+                }
                 push @constraint_defs, "CONSTRAINT $name UNIQUE " .
                     '(' . join( ', ', @fields ) . ')';
             }
@@ -435,7 +463,7 @@ sub produce {
             for my $comment ( @table_comments ) {
                 next unless $comment;
                 push @field_comments, "COMMENT ON TABLE $table_name is\n  '".
-                    $comment."';"
+                    $comment."';" unless $no_comments
                 ;
             }
         }
@@ -540,40 +568,19 @@ sub unreserve {
 # Oscar Wilde
 # -------------------------------------------------------------------
 
-=head1 NAME
-
-SQL::Translator::Producer::Oracle - Oracle SQL producer
-
-=head1 SYNOPSIS
-
-  use SQL::Translator::Parser::MySQL;
-  use SQL::Translator::Producer::Oracle;
-
-  my $original_create = ""; # get this from somewhere...
-  my $translator = SQL::Translator->new;
-
-  $translator->parser("SQL::Translator::Parser::MySQL");
-  $translator->producer("SQL::Translator::Producer::Oracle");
-
-  my $new_create = $translator->translate($original_create);
-
-=head1 DESCRIPTION
-
-SQL::Translator::Producer::Oracle takes a parsed data structure,
-created by a SQL::Translator::Parser subclass, and turns it into a
-create string suitable for use with an Oracle database.
+=pod
 
 =head1 CREDITS
 
-A hearty "thank-you" to Tim Bunce for much of the logic stolen from 
-his "mysql2ora" script.
+Mad props to Tim Bunce for much of the logic stolen from his "mysql2ora"
+script.
 
 =head1 AUTHOR
 
-Ken Y. Clark E<lt>kclark@cpan.orgE<gt>
+Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
 
 =head1 SEE ALSO
 
-perl(1).
+SQL::Translator, DDL::Oracle, mysql2ora.
 
 =cut