Added a little to the POD to explain version dependency.
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Producer / Oracle.pm
index 32a7dfc..87595bc 100644 (file)
@@ -1,7 +1,7 @@
 package SQL::Translator::Producer::Oracle;
 
 # -------------------------------------------------------------------
-# $Id: Oracle.pm,v 1.23 2003-08-21 18:09:50 kycl4rk Exp $
+# $Id: Oracle.pm,v 1.26 2003-10-15 19:00:35 kycl4rk Exp $
 # -------------------------------------------------------------------
 # Copyright (C) 2003 Ken Y. Clark <kclark@cpan.org>,
 #                    darren chamberlain <darren@cpan.org>,
@@ -22,9 +22,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.23 $ =~ /(\d+)\.(\d+)/;
+$VERSION = sprintf "%d.%02d", q$Revision: 1.26 $ =~ /(\d+)\.(\d+)/;
 $DEBUG   = 0 unless defined $DEBUG;
 
 use SQL::Translator::Schema::Constants;
@@ -86,6 +103,13 @@ my %translate  = (
     macaddr             => 'varchar2',
     bit                 => 'number',
     'bit varying'       => 'number',
+
+    #
+    # Oracle types
+    #
+    number              => 'number',
+    varchar2            => 'varchar2',
+    long                => 'clob',
 );
 
 #
@@ -226,7 +250,11 @@ sub produce {
                 # then sub "1/0," otherwise just test the truthity of the
                 # argument and use that (naive?).
                 #
-                if ( $data_type =~ /^number$/i && $default !~ /^\d+$/ ) {
+                if ( 
+                    $data_type =~ /^number$/i && 
+                    $default   !~ /^\d+$/     &&
+                    $default   !~ m/null/i
+                ) {
                     if ( $default =~ /^true$/i ) {
                         $default = "'1'";
                     }
@@ -253,10 +281,11 @@ 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';
+#                my $constraint_name = mk_name( 
+#                    join('_', $table_name_ur, $field_name_ur ), 'nn' 
+#                );
+#                $field_def .= ' CONSTRAINT ' . $constraint_name . ' NOT NULL';
+                $field_def .= ' NOT NULL';
             }
 
             $field_def .= " $check" if $check;
@@ -307,6 +336,29 @@ sub produce {
         }
 
         #
+        # Table options
+        #
+        my @table_options;
+        for my $opt ( $table->options ) {
+            if ( ref $opt eq 'HASH' ) {
+                my ( $key, $value ) = each %$opt;
+                if ( ref $value eq 'ARRAY' ) {
+                    push @table_options, "$key\n(\n".  join ("\n",
+                        map { "  $_->[0]\t$_->[1]" } 
+                        map { [ each %$_ ] }
+                        @$value
+                    )."\n)";
+                }
+                elsif ( !defined $value ) {
+                    push @table_options, $key;
+                }
+                else {
+                    push @table_options, "$key    $value";
+                }
+            }
+        }
+
+        #
         # Table constraints
         #
         for my $c ( $table->get_constraints ) {
@@ -376,12 +428,12 @@ sub produce {
             next unless @fields;
 
             if ( $index_type eq PRIMARY_KEY ) {
-                $index_name = mk_name( $table_name, 'pk' );
+                $index_name ||= mk_name( $table_name, 'pk' );
                 push @field_defs, 'CONSTRAINT '.$index_name.' PRIMARY KEY '.
                     '(' . join( ', ', @fields ) . ')';
             }
             elsif ( $index_type eq NORMAL ) {
-                $index_name = mk_name( $table_name, $index_name || 'i' );
+                $index_name ||= mk_name( $table_name, $index_name || 'i' );
                 push @index_defs, 
                     "CREATE INDEX $index_name on $table_name_ur (".
                         join( ', ', @fields ).  
@@ -405,9 +457,11 @@ sub produce {
             }
         }
 
+        my $table_options = @table_options 
+            ? "\n".join("\n", @table_options) : '';
         $create_statement .= "CREATE TABLE $table_name_ur (\n" .
             join( ",\n", map { "  $_" } @field_defs, @constraint_defs ) .
-            "\n);"
+            "\n)$table_options;"
         ;
 
         $output .= join( "\n\n", 
@@ -503,40 +557,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