use Tie::IxHash methods and accessors
[dbsrgits/SQL-Translator-2.0-ish.git] / lib / SQL / Translator / Object / Table.pm
index 4ca5c6b..0ce2861 100644 (file)
@@ -1,11 +1,19 @@
 use MooseX::Declare;
-class SQL::Translator::Object::Table {
-    use MooseX::Types::Moose qw(Bool HashRef Maybe Str);
-    use MooseX::AttributeHelpers;
-    use SQL::Translator::Types qw(Column Constraint Index Schema Sequence);
-    use SQL::Translator::Object::Schema;
-    extends 'SQL::Translator::Object';
-    
+class SQL::Translator::Object::Table extends SQL::Translator::Object is dirty {
+    use MooseX::Types::Moose qw(Any Bool HashRef Int Str);
+    use MooseX::MultiMethods;
+    use SQL::Translator::Types qw(Column Constraint Index Schema Sequence ColumnHash ConstraintHash IndexHash SequenceHash IxHash);
+    use SQL::Translator::Object::Column;
+    use SQL::Translator::Object::Constraint;
+    use Tie::IxHash;
+    clean;
+
+    use overload
+        '""'     => sub { shift->name },
+        'bool'   => sub { $_[0]->name || $_[0] },
+        fallback => 1,
+    ;
+
     has 'name' => (
         is => 'rw',
         isa => Str,
@@ -13,102 +21,197 @@ class SQL::Translator::Object::Table {
     );
     
     has 'columns' => (
-        metaclass => 'Collection::Hash',
         is => 'rw',
-        isa => HashRef[Column],
-        provides => {
-            exists => 'exists_column',
-            keys   => 'column_ids',
-            values => 'get_columns',
-            get    => 'get_column',
+        isa => IxHash, #ColumnHash,
+        handles => {
+            exists_column => 'EXISTS',
+            column_ids    => 'Keys',
+            get_columns   => 'Values',
+            get_column    => 'FETCH',
+            add_column    => 'STORE',
+            remove_column => 'DELETE',
+            has_columns   => 'Length',
+            clear_columns => 'CLEAR',
         },
-        curries => {
-            set => {
-                add_column => sub {
-                    my ($self, $body, $column) = @_;
-                    $self->$body($column->name, $column);
-                }
-            }
-        },
-        default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash },
+        coerce => 1,
+        default => sub { Tie::IxHash->new() }
     );
     
     has 'indexes' => (
-        metaclass => 'Collection::Hash',
         is => 'rw',
-        isa => HashRef[Index],
-        provides => {
-            exists => 'exists_index',
-            keys   => 'index_ids',
-            values => 'get_indices',
-            get    => 'get_index',
-        },
-        curries => {
-            set => {
-                add_index => sub {
-                    my ($self, $body, $index) = @_;
-                    $self->$body($index->name, $index);
-                }
-            }
+        isa => IxHash, #IndexHash,
+        handles => {
+            exists_index => 'EXISTS',
+            index_ids    => 'Keys',
+            get_indices  => 'Values',
+            get_index    => 'FETCH',
+            add_index    => 'STORE',
+            remove_index => 'DELETE',
         },
-        default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash },
+        coerce => 1,
+        default => sub { Tie::IxHash->new() }
     );
     
     has 'constraints' => (
-        metaclass => 'Collection::Hash',
         is => 'rw',
-        isa => HashRef[Constraint],
-        provides => {
-            exists => 'exists_constraint',
-            keys   => 'constraint_ids',
-            values => 'get_constraints',
-            get    => 'get_constraint',
-        },
-        curries => {
-            set => {
-                add_constraint => sub {
-                    my ($self, $body, $constraint) = @_;
-                    $self->$body($constraint->name, $constraint);
-                }
-            }
+        isa => IxHash, #ConstraintHash,
+        handles => {
+            exists_constraint => 'EXISTS',
+            constraint_ids    => 'Keys',
+            get_constraints   => 'Values',
+            get_constraint    => 'FETCH',
+            add_constraint    => 'STORE',
+            remove_constraint => 'DELETE',
         },
-        default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash },
+        coerce => 1,
+        default => sub { Tie::IxHash->new() }
     );
     
     has 'sequences' => (
-        metaclass => 'Collection::Hash',
         is => 'rw',
-        isa => HashRef[Sequence],
-        provides => {
-            exists => 'exists_sequence',
-            keys   => 'sequence_ids',
-            values => 'get_sequences',
-            get    => 'get_sequence',
+        isa => IxHash, #SequenceHash,
+        handles => {
+            exists_sequence => 'EXISTS',
+            sequence_ids    => 'Keys',
+            get_sequences   => 'Values',
+            get_sequence    => 'FETCH',
+            add_sequence    => 'STORE',
+            remove_sequence => 'DELETE',
         },
-        curries => {
-            set => {
-                add_sequence => sub {
-                    my ($self, $body, $sequence) = @_;
-                    $self->$body($sequence->name, $sequence);
-                }
-            }
-        },
-        default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash },
+        coerce => 1,
+        default => sub { Tie::IxHash->new() },
     );
 
-    has 'comments' => (
+    has 'schema' => (
         is => 'rw',
-        isa => Maybe[Str],
+        isa => Schema,
+        weak_ref => 1,
     );
-    
+
     has 'temporary' => (
         is => 'rw',
         isa => Bool,
         default => 0
     );
 
-    method get_fields { return $self->get_columns }
-    method primary_key(Str $column) {
+    has '_order' => (
+        is => 'rw',
+        isa => Int,
+    );
+
+    around get_column(Column $column does coerce) {
+        $self->$orig($column->name);
+    }
+
+    around add_column(Column $column does coerce) {
+        die "Can't use column name " . $column->name if $self->exists_column($column->name) || $column->name eq '';
+        $column->table($self);
+        $self->$orig($column->name, $column);
+        return $self->get_column($column->name);
+    }
+
+    around add_constraint(Constraint $constraint does coerce) {
+        if ($constraint->type eq 'FOREIGN KEY') {
+            my @columns = $constraint->get_columns;
+            die "There are no columns associated with this foreign key constraint." unless scalar @columns;
+            for my $column (@columns) {
+                die "Can't use column " . $column->name . ". It doesn't exist!" unless $self->exists_column($column);
+            }
+            die "Reference table " . $constraint->reference_table . " does not exist!" unless $self->schema->exists_table($constraint->reference_table);
+        }
+        my $name = $constraint->name;
+        if ($name eq '') {
+            my $idx = 0;
+            while ($self->exists_constraint('ANON' . $idx)) { $idx++ }
+            $name = 'ANON' . $idx;
+        }
+        $constraint->table($self);
+        if ($constraint->has_type && $constraint->type eq 'PRIMARY KEY') {
+            $self->get_column($_)->is_primary_key(1) for $constraint->column_ids;
+        }
+        $self->$orig($name, $constraint);
+        return $self->get_constraint($name);
+    }
+
+    around add_index(Index $index does coerce) {
+        my $name = $index->name;
+        if ($name eq '') {
+            my $idx = 0;
+            while ($self->exists_index('ANON' . $idx)) { $idx++ }
+            $name = 'ANON' . $idx;
+        }
+        $index->table($self);
+        $self->$orig($name, $index);
+        return $self->get_index($name);
+    }
+
+    around add_sequence(Sequence $sequence does coerce) {
+        $self->$orig($sequence->name, $sequence);
+        return $self->get_sequence($sequence->name);
+    }
+
+    multi method primary_key {
+        my $constraints = $self->constraints;
+        for my $key ($constraints->Keys) {
+            my $val = $constraints->FETCH($key);
+            return $val if $val->type eq 'PRIMARY KEY';
+        }
+        return;
+    }
+
+    multi method primary_key(Str $column) {
+        die "Column $column does not exist!" unless $self->exists_column($column);
         $self->get_column($column)->is_primary_key(1);
+
+        my $primary_key = $self->primary_key;
+        unless (defined $primary_key) {
+            $primary_key = SQL::Translator::Object::Constraint->new({ type => 'PRIMARY KEY' });
+            $self->add_constraint($primary_key);
+        }
+        $primary_key->add_field({ name => $column }) unless $primary_key->exists_column($column); ## FIX ME, change back to add_column once around add_column(coerce .. ) works
+        return $primary_key;
+    }
+
+    multi method order(Int $order) { $self->_order($order); }
+    multi method order {
+        my $order = $self->_order;
+        unless (defined $order && $order) {
+            my $tables = Tie::IxHash->new( map { $_->name => $_ } $self->schema->get_tables );
+            $order = $tables->Indices($self->name) || 0; $order++;
+            $self->_order($order);
+        }
+        return $order;
+    }
+
+    method is_valid { return $self->has_columns ? 1 : undef }
+
+    before name($name?) { die "Can't use table name $name, table already exists" if $name && $self->schema->exists_table($name) && $name ne $self->name }
+
+    around remove_column(Column|Str $column, Int :$cascade = 0) {
+        my $name = is_Column($column) ? $column->name : $column;
+        die "Can't drop non-existant column " . $name unless $self->exists_column($name);
+        $self->$orig($name);
+    }
+
+    around remove_index(Index|Str $index) {
+        my $name = is_Index($index) ? $index->name : $index;
+        die "Can't drop non-existant index " . $name unless $self->exists_index($name);
+        $self->$orig($name);
+    }
+
+    around remove_constraint(Constraint|Str $constraint) {
+        my $name = is_Constraint($constraint) ? $constraint->name : $constraint;
+        die "Can't drop non-existant constraint " . $name unless $self->exists_constraint($name);
+        $self->$orig($name);
+    }
+
+    around BUILDARGS(ClassName $self: @args) {
+        my $args = $self->$orig(@args);
+
+        my $fields = delete $args->{fields};
+
+        $args->{columns}{$_} = SQL::Translator::Object::Column->new( name => $_ ) for @$fields;
+
+        return $args;
     }
 }