add (and use) _default_value method, relatively similar to old _apply_default_value
[dbsrgits/SQL-Translator-2.0-ish.git] / lib / SQL / Translator / Producer.pm
index 397ee0e..7363ee9 100644 (file)
@@ -1,35 +1,23 @@
 use MooseX::Declare;
 class SQL::Translator::Producer {
     use SQL::Translator::Constants qw(:sqlt_types);
-    use MooseX::Types::Moose qw(Bool HashRef Str);
-    use SQL::Translator::Types qw(Column Schema Table);
+    use MooseX::Types::Moose qw(ArrayRef Bool HashRef ScalarRef Str);
+    use SQL::Translator::Types qw(Column Table Translator);
     
-    has 'schema' => (
-        isa => Schema,
-        is => 'rw',
-        required => 1
-    );
-    
-    has 'no_comments' => (
-        isa => Bool,
-        is => 'rw',
-        lazy => 1, 
-        default => 0
-    );
-    
-    has 'drop_table' => (
-        isa => Bool,
-        is => 'rw',
-        lazy => 1,
-        default => 1
-    );
-
     has 'data_type_mapping' => (
         isa => HashRef,
         is => 'ro',
         lazy_build => 1
     );
 
+    has 'translator' => (
+        isa => Translator,
+        is => 'ro',
+        weak_ref => 1,
+        required => 1,
+        handles => [ qw(schema) ],
+    );
+
     method _build_data_type_mapping {
         return { 
             SQL_LONGVARCHAR() => 'text',
@@ -46,7 +34,7 @@ class SQL::Translator::Producer {
 
     method produce {
         my $schema = $self->schema;
-    
+
         $self->_create_table($_) for values %{$schema->tables};
     }
     
@@ -73,4 +61,21 @@ class SQL::Translator::Producer {
         $column_def .= ' NOT NULL' unless $column->is_nullable;
         $column_def;
     }
+
+    method _default_value(ScalarRef|Str $default, ArrayRef $exceptions) {
+      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
+          }
+        }
+      }
+
+      return ref($default) ? " DEFAULT $$default" : " DEFAULT '$default'";
+    }
 }