Combined patches from RT#70734 and RT#44769
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Producer / SQLite.pm
index eb50e5e..5ac49b8 100644 (file)
@@ -1,23 +1,5 @@
 package SQL::Translator::Producer::SQLite;
 
-# -------------------------------------------------------------------
-# 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::SQLite - SQLite producer for SQL::Translator
@@ -40,10 +22,11 @@ use warnings;
 use Data::Dumper;
 use SQL::Translator::Schema::Constants;
 use SQL::Translator::Utils qw(debug header_comment parse_dbms_version);
+use SQL::Translator::ProducerUtils;
+my $util = SQL::Translator::ProducerUtils->new( quote_chars => q(') );
 
-use vars qw[ $VERSION $DEBUG $WARN ];
-
-$VERSION = '1.59';
+our ( $DEBUG, $WARN );
+our $VERSION = '1.59';
 $DEBUG = 0 unless defined $DEBUG;
 $WARN = 0 unless defined $WARN;
 
@@ -107,7 +90,6 @@ sub produce {
     }
 }
 
-# -------------------------------------------------------------------
 sub mk_name {
     my ($name, $scope, $critical) = @_;
 
@@ -125,15 +107,15 @@ sub mk_name {
     }
 
     $scope->{ $name }++;
-    return $name;
+    return $util->quote($name);
 }
 
 sub create_view {
     my ($view, $options) = @_;
     my $add_drop_view = $options->{add_drop_view};
 
-    my $view_name = $view->name;
-    $global_names{$view_name} = 1;
+    my $view_name = $util->quote($view->name);
+    $global_names{$view->name} = 1;
 
     debug("PKG: Looking at view '${view_name}'\n");
 
@@ -166,8 +148,8 @@ sub create_table
 {
     my ($table, $options) = @_;
 
-    my $table_name = $table->name;
-    $global_names{$table_name} = 1;
+    my $table_name = $util->quote($table->name);
+    $global_names{$table->name} = 1;
 
     my $no_comments = $options->{no_comments};
     my $add_drop_table = $options->{add_drop_table};
@@ -222,7 +204,7 @@ sub create_table
          ||
          ( @pk_fields && !grep /INTEGER PRIMARY KEY/, @field_defs )
          ) {
-        push @field_defs, 'PRIMARY KEY (' . join(', ', @pk_fields ) . ')';
+        push @field_defs, 'PRIMARY KEY (' . join(', ', map $util->quote($_), @pk_fields ) . ')';
     }
 
     #
@@ -253,8 +235,29 @@ sub create_table
 sub create_foreignkey {
     my $c = shift;
 
-    my $fk_sql = "FOREIGN KEY($c->{fields}[0]) REFERENCES ";
-    $fk_sql .= ( $c->{reference_table} || '' )."(".( $c->{reference_fields}[0] || '' ).")";
+    my @fields = $c->fields;
+    my @rfields = map { $_ || () } $c->reference_fields;
+    unless ( @rfields ) {
+        my $rtable_name = $c->reference_table;
+        if ( my $ref_table = $c->schema->get_table( $rtable_name ) ) {
+            push @rfields, $ref_table->primary_key;
+
+            die "FK constraint on " . $rtable_name . '.' . join('', @fields) . " has no reference fields\n"
+              unless @rfields;
+        }
+        else {
+            die "Can't find reference table '$rtable_name' in schema\n";
+        }
+    }
+
+    my $fk_sql = sprintf 'FOREIGN KEY (%s) REFERENCES %s(%s)',
+        join (', ', map { $util->quote($_) } @fields ),
+        $util->quote($c->reference_table),
+        join (', ', map { $util->quote($_) } @rfields )
+    ;
+
+    $fk_sql .= " ON DELETE " . $c->{on_delete} if $c->{on_delete};
+    $fk_sql .= " ON UPDATE " . $c->{on_update} if $c->{on_update};
 
     return $fk_sql;
 }
@@ -263,7 +266,7 @@ sub create_field
 {
     my ($field, $options) = @_;
 
-    my $field_name = $field->name;
+    my $field_name = $util->quote($field->name);
     debug("PKG: Looking at field '$field_name'\n");
     my $field_comments = $field->comments
         ? "-- " . $field->comments . "\n  "
@@ -346,8 +349,9 @@ sub create_index
     my $type   = $index->type eq 'UNIQUE' ? "UNIQUE " : '';
 
     # strip any field size qualifiers as SQLite doesn't like these
-    my @fields = map { s/\(\d+\)$//; $_ } $index->fields;
+    my @fields = map { s/\(\d+\)$//; $util->quote($_) } $index->fields;
     (my $index_table_name = $index->table->name) =~ s/^.+?\.//; # table name may not specify schema
+    $index_table_name = $util->quote($index_table_name);
     warn "removing schema name from '" . $index->table->name . "' to make '$index_table_name'\n" if $WARN;
     my $index_def =
     "CREATE ${type}INDEX $name ON " . $index_table_name .
@@ -362,8 +366,9 @@ sub create_constraint
 
     my $name   = $c->name;
     $name      = mk_name($name);
-    my @fields = $c->fields;
+    my @fields = map $util->quote($_), $c->fields;
     (my $index_table_name = $c->table->name) =~ s/^.+?\.//; # table name may not specify schema
+    $index_table_name = $util->quote($index_table_name);
     warn "removing schema name from '" . $c->table->name . "' to make '$index_table_name'\n" if $WARN;
 
     my $c_def =
@@ -393,6 +398,7 @@ sub create_trigger {
         "creating trigger '$trig_name' for the '$evt' event.\n" if $WARN;
     }
 
+    $trig_name = $util->quote($trig_name);
     push @statements,  "DROP TRIGGER IF EXISTS $trig_name" if $add_drop;
 
 
@@ -420,7 +426,7 @@ sub create_trigger {
       $trig_name,
       $trigger->perform_action_when,
       $evt,
-      $trigger->on_table,
+      $util->quote($trigger->on_table),
       $action
     );
   }
@@ -434,7 +440,7 @@ sub add_field {
   my ($field) = @_;
 
   return sprintf("ALTER TABLE %s ADD COLUMN %s",
-      $field->table->name, create_field($field))
+      $util->quote($field->table->name), create_field($field))
 }
 
 sub alter_create_index {
@@ -456,7 +462,7 @@ sub alter_drop_index {
   my ($constraint) = @_;
 
   return sprintf("DROP INDEX %s",
-      $constraint->name);
+      $util->quote($constraint->name));
 }
 
 sub batch_alter_table {
@@ -519,11 +525,11 @@ sub batch_alter_table {
     push @sql,$table_sql;
   };
 
-  push @sql, "INSERT INTO @{[$table_name]}_temp_alter SELECT @{[ join(', ', $old_table->get_fields)]} FROM @{[$old_table]}",
-             "DROP TABLE @{[$old_table]}",
+  push @sql, "INSERT INTO @{[$util->quote($table_name.'_temp_alter')]} SELECT @{[ join(', ', map $util->quote($_), $old_table->get_fields)]} FROM @{[$util->quote($old_table)]}",
+             "DROP TABLE @{[$util->quote($old_table)]}",
              create_table($table, { no_comments => 1 }),
-             "INSERT INTO @{[$table_name]} SELECT @{[ join(', ', $old_table->get_fields)]} FROM @{[$table_name]}_temp_alter",
-             "DROP TABLE @{[$table_name]}_temp_alter";
+             "INSERT INTO @{[$util->quote($table_name)]} SELECT @{[ join(', ', map $util->quote($_), $old_table->get_fields)]} FROM @{[$util->quote($table_name.'_temp_alter')]}",
+             "DROP TABLE @{[$util->quote($table_name.'_temp_alter')]}";
 
   return @sql;
 #  return join("", @sql, "");
@@ -531,15 +537,17 @@ sub batch_alter_table {
 
 sub drop_table {
   my ($table) = @_;
+  $table = $util->quote($table);
   return "DROP TABLE $table";
 }
 
 sub rename_table {
   my ($old_table, $new_table, $options) = @_;
 
-  my $qt = $options->{quote_table_names} || '';
+  $old_table = $util->quote($old_table);
+  $new_table = $util->quote($new_table);
 
-  return "ALTER TABLE $qt$old_table$qt RENAME TO $qt$new_table$qt";
+  return "ALTER TABLE $old_table RENAME TO $new_table";
 
 }