Release commit for 1.62
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Producer / SQLite.pm
index ec35038..4623372 100644 (file)
@@ -21,11 +21,11 @@ use strict;
 use warnings;
 use Data::Dumper;
 use SQL::Translator::Schema::Constants;
-use SQL::Translator::Utils qw(debug header_comment parse_dbms_version);
+use SQL::Translator::Utils qw(debug header_comment parse_dbms_version batch_alter_table_statements);
 use SQL::Translator::Generator::DDL::SQLite;
 
 our ( $DEBUG, $WARN );
-our $VERSION = '1.59';
+our $VERSION = '1.62';
 $DEBUG = 0 unless defined $DEBUG;
 $WARN = 0 unless defined $WARN;
 
@@ -66,7 +66,8 @@ sub produce {
     local $NO_QUOTES = 0
       if $translator->quote_identifiers and $translator->quote_identifiers ne '0E0';
 
-    my $head = (header_comment() . "\n") unless $no_comments;
+    my $head;
+    $head = (header_comment() . "\n") unless $no_comments;
 
     my @create = ();
 
@@ -225,7 +226,6 @@ sub create_table
     #
     # Indices
     #
-    my $idx_name_default = 'A';
     for my $index ( $table->get_indices ) {
         push @index_defs, create_index($index);
     }
@@ -233,11 +233,13 @@ sub create_table
     #
     # Constraints
     #
-    my $c_name_default = 'A';
     for my $c ( $table->get_constraints ) {
         if ($c->type eq "FOREIGN KEY") {
             push @field_defs, create_foreignkey($c);
         }
+        elsif ($c->type eq "CHECK") {
+            push @field_defs, create_check_constraint($c);
+        }
         next unless $c->type eq UNIQUE;
         push @constraint_defs, create_constraint($c);
     }
@@ -247,6 +249,14 @@ sub create_table
     return (@create, $create_table, @index_defs, @constraint_defs );
 }
 
+sub create_check_constraint {
+    my $c     = shift;
+    my $check = '';
+    $check .= 'CONSTRAINT ' . _generator->quote( $c->name ) . ' ' if $c->name;
+    $check .= 'CHECK(' . $c->expression . ')';
+    return $check;
+}
+
 sub create_foreignkey {
     my $c = shift;
 
@@ -283,14 +293,13 @@ sub create_index
 {
     my ($index, $options) = @_;
 
-    my $name   = $index->name;
-    $name      = mk_name($name);
+    (my $index_table_name = $index->table->name) =~ s/^.+?\.//; # table name may not specify schema
+    my $name   = mk_name($index->name || "${index_table_name}_idx");
 
     my $type   = $index->type eq 'UNIQUE' ? "UNIQUE " : '';
 
     # strip any field size qualifiers as SQLite doesn't like these
     my @fields = map { s/\(\d+\)$//; _generator()->quote($_) } $index->fields;
-    (my $index_table_name = $index->table->name) =~ s/^.+?\.//; # table name may not specify schema
     $index_table_name = _generator()->quote($index_table_name);
     warn "removing schema name from '" . $index->table->name . "' to make '$index_table_name'\n" if $WARN;
     my $index_def =
@@ -304,10 +313,9 @@ sub create_constraint
 {
     my ($c, $options) = @_;
 
-    my $name   = $c->name;
-    $name      = mk_name($name);
-    my @fields = map _generator()->quote($_), $c->fields;
     (my $index_table_name = $c->table->name) =~ s/^.+?\.//; # table name may not specify schema
+    my $name   = mk_name($c->name || "${index_table_name}_idx");
+    my @fields = map _generator()->quote($_), $c->fields;
     $index_table_name = _generator()->quote($index_table_name);
     warn "removing schema name from '" . $c->table->name . "' to make '$index_table_name'\n" if $WARN;
 
@@ -345,7 +353,9 @@ sub create_trigger {
     $DB::single = 1;
     my $action = "";
     if (not ref $trigger->action) {
-      $action .= "BEGIN " . $trigger->action . " END";
+      $action = $trigger->action;
+      $action = "BEGIN " . $action . " END"
+        unless $action =~ /^ \s* BEGIN [\s\;] .*? [\s\;] END [\s\;]* $/six;
     }
     else {
       $action = $trigger->action->{for_each} . " "
@@ -374,7 +384,7 @@ sub create_trigger {
   return @statements;
 }
 
-sub alter_table { } # Noop
+sub alter_table { () } # Noop
 
 sub add_field {
   my ($field) = @_;
@@ -406,7 +416,7 @@ sub alter_drop_index {
 }
 
 sub batch_alter_table {
-  my ($table, $diffs) = @_;
+  my ($table, $diffs, $options) = @_;
 
   # If we have any of the following
   #
@@ -439,21 +449,7 @@ sub batch_alter_table {
        @{$diffs->{alter_field}}  == 0 &&
        @{$diffs->{drop_field}}   == 0
        ) {
-    return map {
-        my $meth = __PACKAGE__->can($_) or die __PACKAGE__ . " cant $_";
-        map { my $sql = $meth->(ref $_ eq 'ARRAY' ? @$_ : $_); $sql ?  ("$sql") : () } @{ $diffs->{$_} }
-
-      } grep { @{$diffs->{$_}} }
-    qw/rename_table
-       alter_drop_constraint
-       alter_drop_index
-       drop_field
-       add_field
-       alter_field
-       rename_field
-       alter_create_index
-       alter_create_constraint
-       alter_table/;
+    return batch_alter_table_statements($diffs, $options);
   }
 
   my @sql;