Awesome non-quoted numeric default patch by Stephen Clouse
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Producer / SQLite.pm
index ade2ee3..2e7aa6a 100644 (file)
@@ -1,8 +1,6 @@
 package SQL::Translator::Producer::SQLite;
 
 # -------------------------------------------------------------------
-# $Id: SQLite.pm 1445 2009-02-07 17:50:03Z ashberlin $
-# -------------------------------------------------------------------
 # Copyright (C) 2002-2009 SQLFairy Authors
 #
 # This program is free software; you can redistribute it and/or
@@ -41,18 +39,16 @@ use strict;
 use warnings;
 use Data::Dumper;
 use SQL::Translator::Schema::Constants;
-use SQL::Translator::Utils qw(debug header_comment);
+use SQL::Translator::Utils qw(debug header_comment parse_dbms_version);
 
 use vars qw[ $VERSION $DEBUG $WARN ];
 
-$VERSION = '1.99';
+$VERSION = '1.59';
 $DEBUG = 0 unless defined $DEBUG;
 $WARN = 0 unless defined $WARN;
 
-our %used_identifiers = ();
 our $max_id_length    = 30;
-our %global_names;
-our %truncated;
+my %global_names;
 
 sub produce {
     my $translator     = shift;
@@ -62,14 +58,21 @@ sub produce {
     my $add_drop_table = $translator->add_drop_table;
     my $schema         = $translator->schema;
     my $producer_args  = $translator->producer_args;
-    my $sqlite_version = $producer_args->{sqlite_version} || 0;
+    my $sqlite_version = parse_dbms_version(
+        $producer_args->{sqlite_version}, 'perl'
+    );
     my $no_txn         = $producer_args->{no_transaction};
 
     debug("PKG: Beginning production\n");
 
+    %global_names = ();   #reset
+
+
+    my $head = (header_comment() . "\n") unless $no_comments;
+
     my @create = ();
-    push @create, header_comment unless ($no_comments);
-    $create[0] .= "\n\nBEGIN TRANSACTION" unless $no_txn;
+
+    push @create, "BEGIN TRANSACTION" unless $no_txn;
 
     for my $table ( $schema->get_tables ) {
         push @create, create_table($table, { no_comments => $no_comments,
@@ -91,35 +94,22 @@ sub produce {
       });
     }
 
+    push @create, "COMMIT" unless $no_txn;
+
     if (wantarray) {
-      push @create, "COMMIT" unless $no_txn;
-      return @create;
+      return ($head||(), @create);
     } else {
-      push @create, "COMMIT;\n" unless $no_txn;
-      return join(";\n\n", @create );
+      return join ('',
+        $head||(),
+        join(";\n\n", @create ),
+        ";\n",
+      );
     }
 }
 
 # -------------------------------------------------------------------
 sub mk_name {
-    my ($basename, $type, $scope, $critical) = @_;
-    my $basename_orig = $basename;
-    my $max_name      = !$max_id_length 
-                      ? length($type) + 1
-                      : $type 
-                      ? $max_id_length - (length($type) + 1) 
-                      : $max_id_length;
-    $basename         = substr( $basename, 0, $max_name ) 
-                        if length( $basename ) > $max_name;
-    $basename         =~ s/\./_/g;
-    my $name          = $type ? "${type}_$basename" : $basename;
-
-    if ( $basename ne $basename_orig and $critical ) {
-        my $show_type = $type ? "+'$type'" : "";
-        warn "Truncating '$basename_orig'$show_type to $max_id_length ",
-            "character limit to make '$name'\n" if $WARN;
-        $truncated{ $basename_orig } = $name;
-    }
+    my ($name, $scope, $critical) = @_;
 
     $scope ||= \%global_names;
     if ( my $prev = $scope->{ $name } ) {
@@ -147,19 +137,26 @@ sub create_view {
 
     # Header.  Should this look like what mysqldump produces?
     my $extra = $view->extra;
-    my $create = '';
-    $create .= "--\n-- View: ${view_name}\n--\n" unless $options->{no_comments};
-    $create .= "DROP VIEW IF EXISTS $view_name;\n" if $add_drop_view;
-    $create .= 'CREATE';
-    $create .= " TEMPORARY" if exists($extra->{temporary}) && $extra->{temporary};
-    $create .= ' VIEW';
-    $create .= " IF NOT EXISTS" if exists($extra->{if_not_exists}) && $extra->{if_not_exists};
-    $create .= " ${view_name}";
+    my @create;
+    push @create, "DROP VIEW IF EXISTS $view_name" if $add_drop_view;
+
+    my $create_view = 'CREATE';
+    $create_view .= " TEMPORARY" if exists($extra->{temporary}) && $extra->{temporary};
+    $create_view .= ' VIEW';
+    $create_view .= " IF NOT EXISTS" if exists($extra->{if_not_exists}) && $extra->{if_not_exists};
+    $create_view .= " ${view_name}";
 
     if( my $sql = $view->sql ){
-      $create .= " AS\n    ${sql}";
+      $create_view .= " AS\n    ${sql}";
+    }
+    push @create, $create_view;
+
+    # Tack the comment onto the first statement.
+    unless ($options->{no_comments}) {
+      $create[0] = "--\n-- View: ${view_name}\n--\n" . $create[0];
     }
-    return $create;
+
+    return @create;
 }
 
 
@@ -181,7 +178,7 @@ sub create_table
     #
     # Header.
     #
-    my $exists = ($sqlite_version >= 3.3) ? ' IF EXISTS' : '';
+    my $exists = ($sqlite_version >= 3.003) ? ' IF EXISTS' : '';
     my @create;
     my ($comment, $create_table) = "";
     $comment =  "--\n-- Table: $table_name\n--\n" unless $no_comments;
@@ -308,19 +305,16 @@ sub create_field
     # Null?
     $field_def .= ' NOT NULL' unless $field->is_nullable;
 
-    # Default?  XXX Need better quoting!
-    my $default = $field->default_value;
-    if (defined $default) {
-        SQL::Translator::Producer->_apply_default_value(
-            \$field_def,
-            $default, 
-            [
-             'NULL'              => \'NULL',
-             'now()'             => 'now()',
-             'CURRENT_TIMESTAMP' => 'CURRENT_TIMESTAMP',
-            ],
-        );
-    }
+    # Default?
+    SQL::Translator::Producer->_apply_default_value(
+        $field,
+        \$field_def,
+        [
+         'NULL'              => \'NULL',
+         'now()'             => 'now()',
+         'CURRENT_TIMESTAMP' => 'CURRENT_TIMESTAMP',
+        ],
+    );
 
     return $field_def;
 
@@ -331,7 +325,7 @@ sub create_index
     my ($index, $options) = @_;
 
     my $name   = $index->name;
-    $name      = mk_name($index->table->name, $name);
+    $name      = mk_name($name);
 
     my $type   = $index->type eq 'UNIQUE' ? "UNIQUE " : ''; 
 
@@ -351,7 +345,7 @@ sub create_constraint
     my ($c, $options) = @_;
 
     my $name   = $c->name;
-    $name      = mk_name($c->table->name, $name);
+    $name      = mk_name($name);
     my @fields = $c->fields;
     (my $index_table_name = $c->table->name) =~ s/^.+?\.//; # table name may not specify schema
     warn "removing schema name from '" . $c->table->name . "' to make '$index_table_name'\n" if $WARN;
@@ -367,43 +361,53 @@ sub create_trigger {
   my ($trigger, $options) = @_;
   my $add_drop = $options->{add_drop_trigger};
 
-  my $name = $trigger->name;
-  my @create;
-
-  push @create,  "DROP TRIGGER IF EXISTS $name" if $add_drop;
+  my @statements;
 
+  my $trigger_name = $trigger->name;
   my $events = $trigger->database_events;
-  die "Can't handle multiple events in triggers" if @$events > 1;
+  for my $evt ( @$events ) {
 
-  my $action = "";
+    my $trig_name = $trigger_name;
+    if (@$events > 1) {
+      $trig_name .= "_$evt";
 
-  $DB::single = 1;
-  unless (ref $trigger->action) {
-    $action .= "BEGIN " . $trigger->action . " END";
-  } else {
-    $action = $trigger->action->{for_each} . " "
-      if $trigger->action->{for_each};
+      warn "Multiple database events supplied for trigger '$trigger_name', ",
+        "creating trigger '$trig_name' for the '$evt' event.\n" if $WARN;
+    }
 
-    $action = $trigger->action->{when} . " "
-      if $trigger->action->{when};
+    push @statements,  "DROP TRIGGER IF EXISTS $trig_name" if $add_drop;
 
-    my $steps = $trigger->action->{steps} || [];
 
-    $action .= "BEGIN ";
-    for (@$steps) {
-      $action .= $_ . "; "
+    $DB::single = 1;
+    my $action = "";
+    if (not ref $trigger->action) {
+      $action .= "BEGIN " . $trigger->action . " END";
     }
-    $action .= "END";
-  }
+    else {
+      $action = $trigger->action->{for_each} . " "
+        if $trigger->action->{for_each};
 
-  push @create, "CREATE TRIGGER $name " .
-                $trigger->perform_action_when . " " .
-                $events->[0] .
-                " on " . $trigger->on_table . " " .
-                $action;
+      $action = $trigger->action->{when} . " "
+        if $trigger->action->{when};
+
+      my $steps = $trigger->action->{steps} || [];
+
+      $action .= "BEGIN ";
+      $action .= $_ . "; " for (@$steps);
+      $action .= "END";
+    }
+
+    push @statements, sprintf (
+      'CREATE TRIGGER %s %s %s on %s %s',
+      $trig_name,
+      $trigger->perform_action_when,
+      $evt,
+      $trigger->on_table,
+      $action
+    );
+  }
 
-  return @create;
-            
+  return @statements;
 }
 
 sub alter_table { } # Noop
@@ -534,7 +538,7 @@ SQL::Translator, http://www.sqlite.org/.
 
 =head1 AUTHOR
 
-Ken Y. Clark C<< <kclark@cpan.orgE> >>.
+Ken Youens-Clark C<< <kclark@cpan.orgE> >>.
 
 Diff code added by Ash Berlin C<< <ash@cpan.org> >>.