X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FSQL%2FTranslator%2FProducer%2FOracle.pm;h=d3f7a12856e1c738ed35b0d6cc268b3a8a60beea;hb=c0ec0e22d3f0e3852c00daac5ef5763010b410c3;hp=b2726a0316f9d6be540466e027dabe648462e907;hpb=0d3badf1ba563e8a7f138b7d68848800b11a8b26;p=dbsrgits%2FSQL-Translator.git diff --git a/lib/SQL/Translator/Producer/Oracle.pm b/lib/SQL/Translator/Producer/Oracle.pm index b2726a0..d3f7a12 100644 --- a/lib/SQL/Translator/Producer/Oracle.pm +++ b/lib/SQL/Translator/Producer/Oracle.pm @@ -1,23 +1,5 @@ package SQL::Translator::Producer::Oracle; -# ------------------------------------------------------------------- -# Copyright (C) 2002-2009 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 -# published by the Free Software Foundation; version 2. -# -# This program is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA -# 02111-1307 USA -# ------------------------------------------------------------------- - =head1 NAME SQL::Translator::Producer::Oracle - Oracle SQL producer @@ -106,10 +88,12 @@ context the slash will be still there to ensure compatibility with SQLPlus. =cut use strict; -use vars qw[ $VERSION $DEBUG $WARN ]; -$VERSION = '1.59'; +use warnings; +our ( $DEBUG, $WARN ); +our $VERSION = '1.59'; $DEBUG = 0 unless defined $DEBUG; +use base 'SQL::Translator::Producer'; use SQL::Translator::Schema::Constants; use SQL::Translator::Utils qw(header_comment); @@ -201,7 +185,6 @@ my %truncated; # Quote used to escape table, field, sequence and trigger names my $quote_char = '"'; -# ------------------------------------------------------------------- sub produce { my $translator = shift; $DEBUG = $translator->debug; @@ -218,16 +201,16 @@ sub produce { my $qf = 1 if $translator->quote_field_names; if ( $translator->parser_type =~ /mysql/i ) { - $create .= + $create .= "-- 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; - $create .= + $create .= "ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS';\n\n"; } - for my $table ( $schema->get_tables ) { + for my $table ( $schema->get_tables ) { my ( $table_def, $fk_def, $trigger_def, $index_def, $constraint_def ) = create_table( $table, { @@ -266,10 +249,9 @@ sub produce { $create .= ";\n\n"; # If wantarray is not set we have to add "/" in this statement # DBI->do() needs them omitted - # triggers may NOT end with a semicolon - $create .= join "/\n\n", @trigger_defs; - # for last trigger - $create .= "/\n\n"; + # triggers may NOT end with a semicolon but a "/" instead + $create .= "$_/\n\n" + for @trigger_defs; return $create; } } @@ -307,7 +289,7 @@ sub create_table { my ( $key, $value ) = each %$opt; if ( ref $value eq 'ARRAY' ) { push @table_options, "$key\n(\n". join ("\n", - map { " $_->[0]\t$_->[1]" } + map { " $_->[0]\t$_->[1]" } map { [ each %$_ ] } @$value )."\n)"; @@ -397,7 +379,7 @@ sub create_table { } if ( $c->match_type ) { - $def .= ' MATCH ' . + $def .= ' MATCH ' . ( $c->match_type =~ /full/i ) ? 'FULL' : 'PARTIAL'; } @@ -430,7 +412,7 @@ sub create_table { my ( $key, $value ) = each %$opt; if ( ref $value eq 'ARRAY' ) { push @table_options, "$key\n(\n". join ("\n", - map { " $_->[0]\t$_->[1]" } + map { " $_->[0]\t$_->[1]" } map { [ each %$_ ] } @$value )."\n)"; @@ -447,29 +429,29 @@ sub create_table { ? "\n".join("\n", @index_options) : ''; if ( $index_type eq PRIMARY_KEY ) { - $index_name = $index_name ? mk_name( $index_name ) + $index_name = $index_name ? mk_name( $index_name ) : mk_name( $table_name, 'pk' ); $index_name = quote($index_name, $qf); push @field_defs, 'CONSTRAINT '.$index_name.' PRIMARY KEY '. '(' . join( ', ', @fields ) . ')'; } elsif ( $index_type eq NORMAL ) { - $index_name = $index_name ? mk_name( $index_name ) + $index_name = $index_name ? mk_name( $index_name ) : mk_name( $table_name, $index_name || 'i' ); $index_name = quote($index_name, $qf); - push @index_defs, + push @index_defs, "CREATE INDEX $index_name on $table_name_q (". - join( ', ', @fields ). + join( ', ', @fields ). ")$index_options"; } elsif ( $index_type eq UNIQUE ) { - $index_name = $index_name ? mk_name( $index_name ) + $index_name = $index_name ? mk_name( $index_name ) : mk_name( $table_name, $index_name || 'i' ); $index_name = quote($index_name, $qf); - push @index_defs, + push @index_defs, "CREATE UNIQUE INDEX $index_name on $table_name_q (". - join( ', ', @fields ). - ")$index_options"; + join( ', ', @fields ). + ")$index_options"; } else { warn "Unknown index type ($index_type) on table $table_name.\n" @@ -480,14 +462,13 @@ sub create_table { if ( my @table_comments = $table->comments ) { for my $comment ( @table_comments ) { next unless $comment; - $comment =~ s/'/''/g; - push @field_comments, "COMMENT ON TABLE $table_name_q is\n '". - $comment . "'" unless $options->{no_comments} - ; + $comment = __PACKAGE__->_quote_string($comment); + push @field_comments, "COMMENT ON TABLE $table_name_q is\n $comment" + unless $options->{no_comments}; } } - my $table_options = @table_options + my $table_options = @table_options ? "\n".join("\n", @table_options) : ''; push @create, "CREATE TABLE $table_name_q (\n" . join( ",\n", map { " $_" } @field_defs, @@ -569,15 +550,14 @@ sub create_field { my @size = $field->size; my %extra = $field->extra; my $list = $extra{'list'} || []; - # \todo deal with embedded quotes - my $commalist = join( ', ', map { qq['$_'] } @$list ); + my $commalist = join( ', ', map { __PACKAGE__->_quote_string($_) } @$list ); if ( $data_type eq 'enum' ) { $check = "CHECK ($field_name_q IN ($commalist))"; $data_type = 'varchar2'; } elsif ( $data_type eq 'set' ) { - # XXX add a CHECK constraint maybe + # XXX add a CHECK constraint maybe # (trickier and slower, than enum :) $data_type = 'varchar2'; } @@ -604,7 +584,7 @@ sub create_field { } # - # Fixes ORA-02329: column of datatype LOB cannot be + # Fixes ORA-02329: column of datatype LOB cannot be # unique or a primary key # if ( $data_type eq 'clob' && $field->is_primary_key ) { @@ -630,7 +610,7 @@ sub create_field { # # Fixes ORA-00906: missing right parenthesis - # if size is 0 or undefined + # if size is 0 or undefined # for (qw/varchar2/) { if ( $data_type =~ /^($_)$/i ) { @@ -649,7 +629,7 @@ sub create_field { my $default = $field->default_value; if ( defined $default ) { # - # Wherein we try to catch a string being used as + # Wherein we try to catch a string being used as # a default value for a numerical field. If "true/false," # then sub "1/0," otherwise just test the truthity of the # argument and use that (naive?). @@ -658,8 +638,8 @@ sub create_field { $default = $$default; } elsif (ref $default) { $default = 'NULL'; - } elsif ( - $data_type =~ /^number$/i && + } elsif ( + $data_type =~ /^number$/i && $default !~ /^-?\d+$/ && $default !~ m/null/i ) { @@ -670,17 +650,17 @@ sub create_field { } else { $default = $default ? "'1'" : "'0'"; } - } elsif ( + } elsif ( $data_type =~ /date/ && ( - $default eq 'current_timestamp' + $default eq 'current_timestamp' || - $default eq 'now()' + $default eq 'now()' ) ) { $default = 'SYSDATE'; } else { - $default = $default =~ m/null/i ? 'NULL' : "'$default'" - } + $default = $default =~ m/null/i ? 'NULL' : __PACKAGE__->_quote_string($default); + } $field_def .= " DEFAULT $default", } @@ -723,11 +703,11 @@ sub create_field { if ( lc $field->data_type eq 'timestamp' ) { my $base_name = $table_name . "_". $field_name; my $trig_name = quote(mk_name( $base_name, 'ts' ), $qt); - my $trigger = + my $trigger = "CREATE OR REPLACE TRIGGER $trig_name\n". "BEFORE INSERT OR UPDATE ON $table_name_q\n". "FOR EACH ROW WHEN (new.$field_name_q IS NULL)\n". - "BEGIN \n". + "BEGIN\n". " SELECT sysdate INTO :new.$field_name_q FROM dual;\n". "END;\n"; @@ -737,10 +717,10 @@ sub create_field { push @field_defs, $field_def; if ( my $comment = $field->comments ) { - $comment =~ s/'/''/g; - push @field_comments, - "COMMENT ON COLUMN $table_name_q.$field_name_q is\n '" . - $comment . "';" unless $options->{no_comments}; + $comment =~ __PACKAGE__->_quote_string($comment); + push @field_comments, + "COMMENT ON COLUMN $table_name_q.$field_name_q is\n $comment;" + unless $options->{no_comments}; } return \@create, \@field_defs, \@trigger_defs, \@field_comments; @@ -752,7 +732,7 @@ sub create_view { my ($view, $options) = @_; my $qt = $options->{quote_table_names}; my $view_name = quote($view->name,$qt); - + my @create; push @create, qq[DROP VIEW $view_name] if $options->{add_drop_view}; @@ -764,18 +744,17 @@ sub create_view { return \@create; } -# ------------------------------------------------------------------- sub mk_name { - my $basename = shift || ''; - my $type = shift || ''; + my $basename = shift || ''; + my $type = shift || ''; $type = '' if $type =~ /^\d/; - my $scope = shift || ''; + my $scope = shift || ''; my $critical = shift || ''; my $basename_orig = $basename; - my $max_name = $type - ? $max_id_length - (length($type) + 1) + my $max_name = $type + ? $max_id_length - (length($type) + 1) : $max_id_length; - $basename = substr( $basename, 0, $max_name ) + $basename = substr( $basename, 0, $max_name ) if length( $basename ) > $max_name; my $name = $type ? "${type}_$basename" : $basename; @@ -805,10 +784,11 @@ sub mk_name { 1; -# ------------------------------------------------------------------- sub quote { my ($name, $q) = @_; - $q && $name ? "$quote_char$name$quote_char" : $name; + return $name unless $q && $name; + $name =~ s/\Q$quote_char/$quote_char$quote_char/g; + return "$quote_char$name$quote_char"; }