Awesome non-quoted numeric default patch by Stephen Clouse
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Producer.pm
index b2ba10e..9119da6 100644 (file)
@@ -20,6 +20,7 @@ package SQL::Translator::Producer;
 
 use strict;
 use vars qw($VERSION);
+use Scalar::Util ();
 $VERSION = '1.59';
 
 sub produce { "" }
@@ -32,7 +33,9 @@ sub produce { "" }
 ## They are special per Producer, and provide support for the old 'now()'
 ## default value exceptions
 sub _apply_default_value {
-  my (undef, $field_ref, $default, $exceptions) = @_;
+  my (undef, $field, $field_ref, $exceptions) = @_;
+  my $default = $field->default_value;
+  return if !defined $default;
 
   if ($exceptions and ! ref $default) {
     for (my $i = 0; $i < @$exceptions; $i += 2) {
@@ -47,8 +50,14 @@ sub _apply_default_value {
     }
   }
 
+  my $type = lc $field->data_type;
+  my $is_numeric_datatype = ($type =~ /^(?:(?:big|medium|small|tiny)?int(?:eger)?|decimal|double|float|num(?:ber|eric)?|real)$/);
+
   if (ref $default) {
       $$field_ref .= " DEFAULT $$default";
+  } elsif ($is_numeric_datatype && Scalar::Util::looks_like_number ($default) ) {
+    # we need to check the data itself in addition to the datatype, for basic safety
+      $$field_ref .= " DEFAULT $default";
   } else {
       $$field_ref .= " DEFAULT '$default'";
   }