set table/schema when added to table/schema, rather then on object construction
[dbsrgits/SQL-Translator-2.0-ish.git] / lib / SQL / Translator / Object / Table.pm
index e91731f..46a4219 100644 (file)
@@ -1,11 +1,16 @@
 use MooseX::Declare;
-class SQL::Translator::Object::Table {
-    use MooseX::Types::Moose qw(Bool HashRef Str);
-    use MooseX::AttributeHelpers;
+class SQL::Translator::Object::Table extends SQL::Translator::Object is dirty {
+    use MooseX::Types::Moose qw(Any Bool HashRef Str);
+    use MooseX::MultiMethods;
     use SQL::Translator::Types qw(Column Constraint Index Schema Sequence);
-    use SQL::Translator::Object::Schema;
-    extends 'SQL::Translator::Object';
-    
+    clean;
+
+    use overload
+        '""'     => sub { shift->name },
+        'bool'   => sub { $_[0]->name || $_[0] },
+        fallback => 1,
+    ;
+
     has 'name' => (
         is => 'rw',
         isa => Str,
@@ -13,88 +18,127 @@ class SQL::Translator::Object::Table {
     );
     
     has 'columns' => (
-        metaclass => 'Collection::Hash',
+        traits => ['Hash'],
         is => 'rw',
         isa => HashRef[Column],
-        provides => {
-            exists => 'exists_column',
-            keys   => 'column_ids',
-            get    => 'get_column',
-        },
-        curries => {
-            set => {
-                add_column => sub {
-                    my ($self, $body, $column) = @_;
-                    $self->$body($column->name, $column);
-                }
-            }
+        handles => {
+            exists_column => 'exists',
+            column_ids    => 'keys',
+            get_columns   => 'values',
+            get_column    => 'get',
+            add_column    => 'set',
+            remove_column => 'delete',
+
+            ## compat
+            get_fields    => 'values',
+            get_field     => 'get',
+            fields        => 'keys',
         },
         default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash },
     );
     
     has 'indexes' => (
-        metaclass => 'Collection::Hash',
+        traits => ['Hash'],
         is => 'rw',
         isa => HashRef[Index],
-        provides => {
-            exists => 'exists_index',
-            keys   => 'index_ids',
-            get    => 'get_index',
-        },
-        curries => {
-            set => {
-                add_index => sub {
-                    my ($self, $body, $index) = @_;
-                    $self->$body($index->name, $index);
-                }
-            }
+        handles => {
+            exists_index => 'exists',
+            index_ids    => 'keys',
+            get_indices  => 'values',
+            get_index    => 'get',
+            add_index    => 'set',
         },
-        default => sub { {} },
+        default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash },
     );
     
     has 'constraints' => (
-        metaclass => 'Collection::Hash',
+        traits => ['Hash'],
         is => 'rw',
         isa => HashRef[Constraint],
-        provides => {
-            exists => 'exists_constraint',
-            keys   => 'constraint_ids',
-            get    => 'get_constraint',
-        },
-        curries => {
-            set => {
-                add_constraint => sub {
-                    my ($self, $body, $constraint) = @_;
-                    $self->$body($constraint->name, $constraint);
-                }
-            }
+        handles => {
+            exists_constraint => 'exists',
+            constraint_ids    => 'keys',
+            get_constraints   => 'values',
+            get_constraint    => 'get',
+            add_constraint    => 'set',
         },
-        default => sub { {} },
+        default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash },
     );
     
     has 'sequences' => (
-        metaclass => 'Collection::Hash',
+        traits => ['Hash'],
         is => 'rw',
         isa => HashRef[Sequence],
-        provides => {
-            exists => 'exists_sequence',
-            keys   => 'sequence_ids',
-            get    => 'get_sequence',
+        handles => {
+            exists_sequence => 'exists',
+            sequence_ids    => 'keys',
+            get_sequences   => 'values',
+            get_sequence    => 'get',
+            add_sequence    => 'set',
         },
-        curries => {
-            set => {
-                add_sequence => sub {
-                    my ($self, $body, $sequence) = @_;
-                    $self->$body($sequence->name, $sequence);
-                }
-            }
-        },
-        default => sub { {} },
+        default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash },
     );
-    
+
+    has 'schema' => (
+        is => 'rw',
+        isa => Schema,
+        weak_ref => 1,
+    );
+
     has 'temporary' => (
         is => 'rw',
         isa => Bool,
         default => 0
     );
+
+    method add_field(Column $column does coerce) { $self->add_column($column) }
+
+    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);
+        return $self->$orig($column->name, $column);
+    }
+
+    around add_constraint(Constraint $constraint) {
+        my $name = $constraint->name;
+        if ($name eq '') {
+            my $idx = 0;
+            while ($self->exists_constraint('ANON' . $idx)) { $idx++ }
+            $name = 'ANON' . $idx;
+        }
+        $constraint->table($self);
+        $self->$orig($name, $constraint)
+    }
+
+    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)
+    }
+
+    around add_sequence(Sequence $sequence) { $self->$orig($sequence->name, $sequence) }
+
+    multi method primary_key(Any $) { grep /^PRIMARY KEY$/, $_->type for $self->get_constraints }
+    multi method primary_key(Str $column) { $self->get_column($column)->is_primary_key(1) }
+
+    method is_valid { return $self->get_columns ? 1 : undef }
+    method order { }
+
+    before name($name?) { die "Can't use table name $name, table already exists" if $name && $self->schema->exists_table($name) && $name ne $self->name }
+
+    multi method drop_column(Column $column, Int :$cascade = 0) {
+        die "Can't drop non-existant table " . $column->name unless $self->exists_column($column->name);
+        $self->remove_column($column->name);
+
+    }
+
+    multi method drop_column(Str $column, Int :$cascade = 0) {
+        die "Can't drop non-existant table " . $column unless $self->exists_column($column);
+        $self->remove_column($column);
+    }
 }