use MooseX::Declare;
-class SQL::Translator::Object::Constraint {
- use MooseX::Types::Moose qw(ArrayRef Bool HashRef Maybe Str);
- use MooseX::AttributeHelpers;
- use SQL::Translator::Types qw(Column);
- 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,
+ 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',
- },
- curries => {
- set => {
- add_column => sub {
- my ($self, $body, $column) = @_;
- $self->$body($column->name, $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' => (
isa => Str,
);
- has 'options' => (
+ has 'reference_table' => (
+ isa => Maybe[Str],
is => 'rw',
+ );
+
+ has 'reference_columns' => (
isa => ArrayRef,
- auto_deref => 1
+ traits => ['Array'],
+ handles => {
+ reference_columns => 'elements',
+ add_reference_column => 'push',
+ },
+ default => sub { [] },
+ required => 1,
+ );
+
+ has 'match_type' => (
+ isa => MatchType,
+ is => 'rw',
+ coerce => 1,
+ lazy => 1,
+ default => ''
);
- has 'extra' => (
+ has '_order' => (
+ isa => Int,
is => 'rw',
- isa => HashRef,
- auto_deref => 1,
);
- method get_fields { return $self->get_columns }
- method fields { return $self->column_ids }
- method field_names { return $self->column_ids }
+ 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 match_type { }
+ method is_valid { return $self->has_type && scalar $self->column_ids ? 1 : undef }
}