add an around BUILDARGS to process fields arg properly
[dbsrgits/SQL-Translator-2.0-ish.git] / lib / SQL / Translator / Object / Constraint.pm
index 3f46a30..91dbff0 100644 (file)
@@ -1,47 +1,50 @@
 use MooseX::Declare;
-class SQL::Translator::Object::Constraint {
-    use MooseX::Types::Moose qw(ArrayRef Bool HashRef Maybe Str Undef);
-    use MooseX::AttributeHelpers;
-    use SQL::Translator::Types qw(Column Table);
-    extends 'SQL::Translator::Object';
+class SQL::Translator::Object::Constraint extends SQL::Translator::Object {
+    use MooseX::Types::Moose qw(ArrayRef Bool HashRef Int Maybe Str Undef);
+    use MooseX::MultiMethods;
+    use SQL::Translator::Types qw(Column MatchType Table ColumnHash IxHash);
+    use Tie::IxHash;
 
     has 'table' => (
         is => 'rw',
         isa => Table,
-        required => 1,
         weak_ref => 1,
     );
     
     has 'name' => (
         is => 'rw',
-        isa => Maybe[Str],
+        isa => Str,
+        default => '',
         required => 1
     );
     
     has 'columns' => (
-        metaclass => 'Collection::Hash',
         is => 'rw',
-        isa => HashRef[Column],
-        provides => {
-            exists => 'exists_column',
-            keys   => 'column_ids',
-            values => 'get_columns',
-            get    => 'get_column',
-            set    => 'add_column',
+        isa => IxHash, #ColumnHash,
+        handles => {
+            exists_column => 'EXISTS',
+            column_ids    => 'Keys',
+            get_columns   => 'Values',
+            get_column    => 'FETCH',
+            add_column    => 'STORE',
+            remove_column => 'DELETE',
+            clear_columns => 'CLEAR',
         },
-        default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash },
+        coerce => 1,
+        default => sub { Tie::IxHash->new() }
     );
     
     has 'type' => (
         is => 'rw',
         isa => Str,
-        required => 1
+        predicate => 'has_type',
+        required => 1,
     );
 
     has 'deferrable' => (
         is => 'rw',
         isa => Bool,
-        default => 0
+        default => 1
     );
 
     has 'expression' => (
@@ -55,30 +58,63 @@ class SQL::Translator::Object::Constraint {
     );
 
     has 'reference_columns' => (
-         isa => ArrayRef | Undef,
-         is => 'rw',
-         auto_deref => 1
+        isa => ArrayRef,
+        traits => ['Array'],
+        handles => {
+            reference_columns => 'elements',
+            add_reference_column => 'push',
+        },
+        default => sub { [] },
+        required => 1,
     );
 
-    has 'options' => (
+    has 'match_type' => (
+        isa => MatchType,
         is => 'rw',
-        isa => ArrayRef,
-        auto_deref => 1
+        coerce => 1,
+        lazy => 1,
+        default => ''
     );
 
-    has 'extra' => (
+    has '_order' => (
+        isa => Int,
         is => 'rw',
-        isa => HashRef,
-        auto_deref => 1,
     );
 
-    around add_column(Column $column) { $self->$orig($column->name, $column) }
+    has 'on_delete' => ( is => 'rw', required => 0);
+    has 'on_update' => ( is => 'rw', required => 0);
+
+    around add_column(Column $column does coerce) {
+        if ($self->has_type && $self->type eq 'PRIMARY KEY') {
+            $column->is_primary_key(1);
+        }
+        $self->$orig($column->name, $column);
+        return $self->get_column($column->name);
+    }
+
+    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_type && scalar $self->column_ids ? 1 : undef }
+
+    around BUILDARGS(ClassName $self: @args) {
+        my $args = $self->$orig(@args);
 
-    method get_fields { $self->get_columns }
-    method fields { $self->column_ids }
-    method field_names { $self->column_ids }
+        my $fields = delete $args->{fields} || [];
 
-    method reference_fields { $self->reference_columns }
+        $fields = ref($fields) eq 'ARRAY' ? $fields : [ $fields ];
+        my $ix_hash = Tie::IxHash->new();
+        $ix_hash->STORE($_, SQL::Translator::Object::Column->new( name => $_ )) for @$fields;
+        $args->{columns} = $ix_hash;
 
-    method match_type { }
+        return $args;
+     }
 }