Improve trigger 'scope' attribute support (RT#119997)
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Producer / Oracle.pm
index e0d805b..d3f7a12 100644 (file)
@@ -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;
@@ -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;
     }
 }
@@ -480,10 +462,9 @@ 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};
             }
         }
 
@@ -569,8 +550,7 @@ 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))";
@@ -679,7 +659,7 @@ sub create_field {
                 ) {
             $default = 'SYSDATE';
         } else {
-            $default = $default =~ m/null/i ? 'NULL' : "'$default'"
+            $default = $default =~ m/null/i ? 'NULL' : __PACKAGE__->_quote_string($default);
         }
 
         $field_def .= " DEFAULT $default",
@@ -727,7 +707,7 @@ sub create_field {
           "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;
+        $comment =~ __PACKAGE__->_quote_string($comment);
         push @field_comments,
-          "COMMENT ON COLUMN $table_name_q.$field_name_q is\n '" .
-            $comment . "';" unless $options->{no_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;
@@ -764,7 +744,6 @@ sub create_view {
     return \@create;
 }
 
-# -------------------------------------------------------------------
 sub mk_name {
     my $basename      = shift || '';
     my $type          = shift || '';
@@ -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";
 }