add quoted reference to check if the table name contain a full declaration, it quote...
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Producer.pm
index 1b2baf2..9119da6 100644 (file)
@@ -1,8 +1,6 @@
 package SQL::Translator::Producer;
 
 # -------------------------------------------------------------------
-# $Id: Producer.pm,v 1.8 2006-06-07 16:28:59 schiffbruechige Exp $
-# -------------------------------------------------------------------
 # Copyright (C) 2002-4 SQLFairy Authors
 #
 # This program is free software; you can redistribute it and/or
@@ -22,10 +20,50 @@ package SQL::Translator::Producer;
 
 use strict;
 use vars qw($VERSION);
-$VERSION = sprintf "%d.%02d", q$Revision: 1.8 $ =~ /(\d+)\.(\d+)/;
+use Scalar::Util ();
+$VERSION = '1.59';
 
 sub produce { "" }
 
+# Do not rely on this if you are not bundled with SQL::Translator.
+# -- rjbs, 2008-09-30
+## $exceptions contains an arrayref of paired values
+## Each pair contains a pattern match or string, and a value to be used as
+## the default if matched.
+## They are special per Producer, and provide support for the old 'now()'
+## default value exceptions
+sub _apply_default_value {
+  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) {
+      my ($pat, $val) = @$exceptions[ $i, $i + 1 ];
+      if (ref $pat and $default =~ $pat) {
+          $default = $val;
+          last;
+      } elsif (lc $default eq lc $pat) {
+          $default = $val;
+          last
+      }
+    }
+  }
+
+  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'";
+  }
+
+}
+
 1;
 
 # -------------------------------------------------------------------
@@ -71,6 +109,8 @@ by the parser.  It is expected to return a string.
 
 =item drop_field($table, $old_field)
 
+=back
+
 =head1 AUTHORS
 
 Darren Chamberlain E<lt>darren@cpan.orgE<gt>,