Mooify SQLT::Schema::Index
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Schema / Table.pm
index 6655e4d..36c2c09 100644 (file)
@@ -1,23 +1,5 @@
 package SQL::Translator::Schema::Table;
 
-# ----------------------------------------------------------------------
-# 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
-# -------------------------------------------------------------------
-
 =pod
 
 =head1 NAME
@@ -29,7 +11,7 @@ SQL::Translator::Schema::Table - SQL::Translator table object
   use SQL::Translator::Schema::Table;
   my $table = SQL::Translator::Schema::Table->new( name => 'foo' );
 
-=head1 DESCSIPTION
+=head1 DESCRIPTION
 
 C<SQL::Translator::Schema::Table> is the table object.
 
@@ -38,18 +20,19 @@ C<SQL::Translator::Schema::Table> is the table object.
 =cut
 
 use strict;
+use warnings;
 use SQL::Translator::Utils 'parse_list_arg';
 use SQL::Translator::Schema::Constants;
 use SQL::Translator::Schema::Constraint;
 use SQL::Translator::Schema::Field;
 use SQL::Translator::Schema::Index;
-use Data::Dumper;
 
-use base 'SQL::Translator::Schema::Object';
+use Carp::Clan '^SQL::Translator';
+use List::Util 'max';
 
-use vars qw( $VERSION );
+use base 'SQL::Translator::Schema::Object';
 
-$VERSION = '1.59';
+our $VERSION = '1.59';
 
 # Stringify to our name, being careful not to pass any args through so we don't
 # accidentally set it to undef. We also have to tweak bool so the object is
@@ -75,18 +58,6 @@ Object constructor.
 
 =cut
 
-sub new {
-  my $class = shift;
-  my $self = $class->SUPER::new (@_)
-    or return;
-
-  $self->{_order} = { map { $_ => 0 } qw/
-    field
-  /};
-
-  return $self;
-}
-
 sub add_constraint {
 
 =pod
@@ -325,11 +296,35 @@ existing field, you will get an error and the field will not be created.
             $self->error( $field_class->error );
     }
 
-    $field->order( ++$self->{_order}{field} );
+    my $existing_order = { map { $_->order => $_->name } $self->get_fields };
+
+    # supplied order, possible unordered assembly
+    if ( $field->order ) {
+        if($existing_order->{$field->order}) {
+            croak sprintf
+                "Requested order '%d' for column '%s' conflicts with already existing column '%s'",
+                $field->order,
+                $field->name,
+                $existing_order->{$field->order},
+            ;
+        }
+    }
+    else {
+        my $last_field_no = max(keys %$existing_order) || 0;
+        if ( $last_field_no != scalar keys %$existing_order ) {
+            croak sprintf
+                "Table '%s' field order incomplete - unable to auto-determine order for newly added field",
+                $self->name,
+            ;
+        }
+
+        $field->order( $last_field_no + 1 );
+    }
+
     # We know we have a name as the Field->new above errors if none given.
     my $field_name = $field->name;
 
-    if ( exists $self->{'fields'}{ $field_name } ) {
+    if ( $self->get_field($field_name) ) {
         return $self->error(qq[Can't create field: "$field_name" exists]);
     }
     else {