consolidate multiple ALTER TABLE statements for efficiency, modernize deb packaging...
[DBIx-DBSchema.git] / DBSchema / Table.pm
index 6463ee0..01382ef 100644 (file)
@@ -4,13 +4,13 @@ use strict;
 use vars qw($VERSION $DEBUG %create_params);
 use Carp;
 #use Exporter;
-use DBIx::DBSchema::_util qw(_load_driver _dbh);
-use DBIx::DBSchema::Column 0.07;
+use DBIx::DBSchema::_util qw(_load_driver _dbh _parse_opt);
+use DBIx::DBSchema::Column 0.14;
 use DBIx::DBSchema::Index;
 use DBIx::DBSchema::ColGroup::Unique;
 use DBIx::DBSchema::ColGroup::Index;
 
-$VERSION = '0.06';
+$VERSION = '0.08';
 $DEBUG = 0;
 
 =head1 NAME
@@ -360,7 +360,7 @@ sub local_options {
   if ( defined($value) ) {
     $self->{local_options} = $value;
   } else {
-    $self->{local_options};
+    defined $self->{local_options} ? $self->{local_options} : '';
   }
 }
 
@@ -617,12 +617,13 @@ specified database, will attempt to use generic SQL syntax.
 #gosh, false laziness w/DBSchema::sql_update_schema
 
 sub sql_alter_table {
-  my( $self, $new, $dbh ) = ( shift, shift, _dbh(@_) );
+  my($self, $opt, $new, $dbh) = ( shift, _parse_opt(\@_), shift, _dbh(@_) );
 
   my $driver = _load_driver($dbh);
 
   my $table = $self->name;
 
+  my @at = ();
   my @r = ();
   my @r_later = ();
   my $tempnum = 1;
@@ -634,25 +635,27 @@ sub sql_alter_table {
   foreach my $column ( $new->columns ) {
 
     if ( $self->column($column) )  {
-
       warn "  $table.$column exists\n" if $DEBUG > 1;
 
-      push @r,
-        $self->column($column)->sql_alter_column( $new->column($column), $dbh );
+      my ($alter_table, $sql) = 
+        $self->column($column)->sql_alter_column( $new->column($column),
+                                                  $dbh,
+                                                  $opt,
+                                                );
+      push @at, @$alter_table;
+      push @r, @$sql;
 
     } else {
-  
       warn "column $table.$column does not exist.\n" if $DEBUG > 1;
 
-      push @r,
-        $new->column($column)->sql_add_column( $dbh );
+      my ($alter_table, $sql) = $new->column($column)->sql_add_column( $dbh );
+      push @at, @$alter_table;
+      push @r, @$sql;
   
     }
   
   }
 
-  #should eventually drop columns not in $new...
-  
   ###
   # indices
   ###
@@ -718,9 +721,11 @@ sub sql_alter_table {
 
     warn "column $table.$column should be dropped.\n" if $DEBUG;
 
-    push @r, $self->column($column)->sql_drop_column( $dbh );
+    push @at, $self->column($column)->sql_drop_column( $dbh );
 
   }
+
+  unshift @r, "ALTER TABLE $table ", join(', ', @at) if @at;
   
   ###
   # return the statements
@@ -764,7 +769,7 @@ with no indices.
 
 Copyright (c) 2000-2007 Ivan Kohler
 Copyright (c) 2000 Mail Abuse Prevention System LLC
-Copyright (c) 2007 Freeside Internet Services, Inc.
+Copyright (c) 2007-2013 Freeside Internet Services, Inc.
 All rights reserved.
 This program is free software; you can redistribute it and/or modify it under
 the same terms as Perl itself.
@@ -782,8 +787,6 @@ the object after sql_create_table, make a copy beforehand.
 
 Some of the logic in new_odbc might be better abstracted into Column.pm etc.
 
-sql_alter_table ought to drop columns not in $new
-
 Add methods to get and set specific indices, by name? (like column COLUMN_NAME)
 
 indices method should be a setter, not just a getter?