X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FSQL%2FTranslator%2FProducer%2FOracle.pm;h=9855f058e1d3ddf5120dab16f55b793a3037eabe;hb=ba3cb849637500653756bd27b6f6dc55b85187c3;hp=32a7dfcee7fdb9d93b813de3688fd2c5b48127af;hpb=4dfb038012e648789d2d69cffad4b601e96c955b;p=dbsrgits%2FSQL-Translator.git diff --git a/lib/SQL/Translator/Producer/Oracle.pm b/lib/SQL/Translator/Producer/Oracle.pm index 32a7dfc..9855f05 100644 --- a/lib/SQL/Translator/Producer/Oracle.pm +++ b/lib/SQL/Translator/Producer/Oracle.pm @@ -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.27 2003-10-15 20:39:15 kycl4rk Exp $ # ------------------------------------------------------------------- # Copyright (C) 2003 Ken Y. Clark , # darren chamberlain , @@ -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.27 $ =~ /(\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', ); # @@ -138,8 +162,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"; } @@ -226,7 +253,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 +284,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; @@ -302,7 +334,30 @@ 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; + } + } + + # + # 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"; + } } } @@ -376,12 +431,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 ). @@ -400,14 +455,16 @@ 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 ; } } + 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 +560,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 Ekclark@cpan.orgE +Ken Y. Clark Ekclark@cpan.orgE. =head1 SEE ALSO -perl(1). +SQL::Translator, DDL::Oracle, mysql2ora. =cut