Bumping version to 1.60
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Producer / DB2.pm
index 8cecc86..77f6904 100644 (file)
@@ -1,24 +1,5 @@
 package SQL::Translator::Producer::DB2;
 
-# -------------------------------------------------------------------
-# $Id: DB2.pm 1440 2009-01-17 16:31:57Z jawnsy $
-# -------------------------------------------------------------------
-# 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::DB2 - DB2 SQL producer
@@ -38,7 +19,9 @@ Creates an SQL DDL suitable for DB2.
 
 use warnings;
 use strict;
-use vars qw[ $DEBUG $WARN ];
+use warnings;
+our ( $DEBUG, $WARN );
+our $VERSION = '1.60';
 $DEBUG   = 0 unless defined $DEBUG;
 
 use SQL::Translator::Schema::Constants;
@@ -187,11 +170,9 @@ DEFAULT            JOIN           RELEASE       WRITE
 DEFAULTS           KEY            RENAME        YEAR
 DEFINITION         LABEL          REPEAT        YEARS
 DELETE             LANGUAGE       RESET
-DESCRIPTOR         LC_CTYPE       RESIGNAL 
+DESCRIPTOR         LC_CTYPE       RESIGNAL
 /;
 
-#------------------------------------------------------------------------------
-
 sub produce
 {
     my ($translator) = @_;
@@ -218,7 +199,7 @@ sub produce
             push @index_defs, create_index($index);
         }
 
-    }   
+    }
     my (@view_defs);
     foreach my $view ( $schema->get_views )
     {
@@ -244,7 +225,7 @@ sub produce
         if(length($name) > $length)   ## Maximum table name length is 18
         {
             warn "Table name $name is longer than $length characters, truncated" if $WARN;
-#             if(grep {$_ eq substr($name, 0, $length) } 
+#             if(grep {$_ eq substr($name, 0, $length) }
 #                               values(%{$objnames{$type}}))
 #             {
 #                 die "Got multiple matching table names when truncated";
@@ -267,7 +248,7 @@ sub create_table
 {
     my ($table, $options) = @_;
 
-    my $table_name = check_name($table->name, 'tables', 128); 
+    my $table_name = check_name($table->name, 'tables', 128);
     # this limit is 18 in older DB2s ! (<= 8)
 
     my (@field_defs, @comments);
@@ -296,7 +277,7 @@ sub create_table
 sub create_field
 {
     my ($field) = @_;
-    
+
     my $field_name = check_name($field->name, 'fields', 30);
 #    use Data::Dumper;
 #    print Dumper(\%dt_translate);
@@ -305,16 +286,16 @@ sub create_field
     my $size = $field->size();
 
     my $field_def = "$field_name $data_type";
-    $field_def .= $field->is_auto_increment ? 
+    $field_def .= $field->is_auto_increment ?
         ' GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1)' : '';
-    $field_def .= $data_type =~ /(CHAR|CLOB)/i ? "(${size})" : '';
+    $field_def .= $data_type =~ /(CHAR|CLOB|NUMERIC|DECIMAL)/i ? "(${size})" : '';
     $field_def .= !$field->is_nullable ? ' NOT NULL':'';
 #            $field_def .= $field->is_primary_key ? ' PRIMARY KEY':'';
-    $field_def .= !defined $field->default_value ? '' : 
+    $field_def .= !defined $field->default_value ? '' :
         $field->default_value =~ /current( |_)timestamp/i ||
-        $field->default_value =~ /\Qnow()\E/i ? 
+        $field->default_value =~ /\Qnow()\E/i ?
         ' DEFAULT CURRENT TIMESTAMP' : defined $field->default_value ?
-        (" DEFAULT " . ($data_type =~ /(INT|DOUBLE)/i ? 
+        (" DEFAULT " . ($data_type =~ /(INT|DOUBLE)/i ?
                         $field->default_value : "'" . $field->default_value . "'")
          ) : '';
 
@@ -368,7 +349,7 @@ sub create_constraint
     }
 
     return \@con_defs, \@fks;
-                      
+
 }
 
 sub create_view
@@ -387,19 +368,20 @@ sub create_trigger
     my ($trigger) = @_;
 # create: CREATE TRIGGER trigger_name before type /ON/i table_name reference_b(?) /FOR EACH ROW/i 'MODE DB2SQL' triggered_action
 
+    my $db_events = join ', ', $trigger->database_events;
     my $out = sprintf('CREATE TRIGGER %s %s %s ON %s %s %s MODE DB2SQL %s',
                       $trigger->name,
                       $trigger->perform_action_when || 'AFTER',
-                      $trigger->database_event =~ /update_on/i ? 
+                      $db_events =~ /update_on/i ?
                         ('UPDATE OF '. join(', ', $trigger->fields)) :
-                        $trigger->database_event || 'UPDATE',
+                        $db_events || 'UPDATE',
                       $trigger->table->name,
                       $trigger->extra->{reference} || 'REFERENCING OLD AS oldrow NEW AS newrow',
                       $trigger->extra->{granularity} || 'FOR EACH ROW',
                       $trigger->action );
 
     return $out;
-                      
+
 }
 
 sub alter_field