added some attributes
[dbsrgits/SQL-Translator-2.0-ish.git] / lib / SQL / Translator / Object / Constraint.pm
index 66bffc9..d8f0bfe 100644 (file)
@@ -1,33 +1,33 @@
 use MooseX::Declare;
-class SQL::Translator::Object::Constraint {
+class SQL::Translator::Object::Constraint extends SQL::Translator::Object {
     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';
+    use MooseX::MultiMethods;
+    use SQL::Translator::Types qw(Column MatchType Table);
 
     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',
+        traits => ['Hash'],
         is => 'rw',
         isa => HashRef[Column],
-        provides => {
-            exists => 'exists_column',
-            keys   => 'column_ids',
-            values => 'get_columns',
-            get    => 'get_column',
-            set    => 'add_column',
+        handles => {
+            exists_column => 'exists',
+            column_ids    => 'keys',
+            get_columns   => 'values',
+            get_column    => 'get',
+            add_column    => 'set',
+            clear_columns => 'clear',
         },
         default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash },
     );
@@ -35,13 +35,14 @@ class SQL::Translator::Object::Constraint {
     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,18 +56,33 @@ 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,
     );
 
-    around add_column(Column $column) { $self->$orig($column->name, $column) }
+    has 'match_type' => (
+        isa => MatchType,
+        is => 'rw',
+        coerce => 1,
+        lazy => 1,
+        default => ''
+    );
 
-    method get_fields { $self->get_columns }
-    method fields { $self->column_ids }
-    method field_names { $self->column_ids }
+    has 'on_delete' => ( is => 'rw', required => 0);
+    has 'on_update' => ( is => 'rw', required => 0);
 
-    method reference_fields { $self->reference_columns }
+    around add_column(Column $column) {
+        if ($self->has_type && $self->type eq 'PRIMARY KEY') {
+            $column->is_primary_key(1);
+        }
+        $self->$orig($column->name, $column)
+    }
 
-    method match_type { }
+    method is_valid { return $self->has_type && scalar $self->column_ids ? 1 : undef }
 }