X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FSQL%2FTranslator%2FObject%2FConstraint.pm;h=d8f0bfe77de827ecf2ca66551fb1f0e623d055e6;hb=4d662d4c3b4cbb574836ee9ee391a696e3fb7c21;hp=e9fb5df7214cf87e7b88f1672dd50819c2b7cfeb;hpb=7aa485df58674f494ff946fc297e7d4de14e006f;p=dbsrgits%2FSQL-Translator-2.0-ish.git diff --git a/lib/SQL/Translator/Object/Constraint.pm b/lib/SQL/Translator/Object/Constraint.pm index e9fb5df..d8f0bfe 100644 --- a/lib/SQL/Translator/Object/Constraint.pm +++ b/lib/SQL/Translator/Object/Constraint.pm @@ -1,36 +1,88 @@ -package SQL::Translator::Object::Constraint; -use Moose; -use MooseX::Types::Moose qw(HashRef Str); -use MooseX::AttributeHelpers; -use SQL::Translator::Types qw(Column); -extends 'SQL::Translator::Object'; - -has 'name' => ( - is => 'rw', - isa => Str, - required => 1 -); - -has 'columns' => ( - metaclass => 'Collection::Hash', - is => 'rw', - isa => HashRef[Column], - provides => { - exists => 'exists_column', - keys => 'column_ids', - get => 'get_column', - set => 'set_column', - }, - required => 1 -); - -has 'type' => ( - is => 'rw', - isa => Str, - required => 1 -); - -no Moose; -__PACKAGE__->meta->make_immutable; - -1; +use MooseX::Declare; +class SQL::Translator::Object::Constraint extends SQL::Translator::Object { + use MooseX::Types::Moose qw(ArrayRef Bool HashRef Maybe Str Undef); + use MooseX::MultiMethods; + use SQL::Translator::Types qw(Column MatchType Table); + + has 'table' => ( + is => 'rw', + isa => Table, + weak_ref => 1, + ); + + has 'name' => ( + is => 'rw', + isa => Str, + default => '', + required => 1 + ); + + has 'columns' => ( + traits => ['Hash'], + is => 'rw', + isa => HashRef[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 }, + ); + + has 'type' => ( + is => 'rw', + isa => Str, + predicate => 'has_type', + required => 1, + ); + + has 'deferrable' => ( + is => 'rw', + isa => Bool, + default => 1 + ); + + has 'expression' => ( + is => 'rw', + isa => Str, + ); + + has 'reference_table' => ( + isa => Maybe[Str], + is => 'rw', + ); + + has 'reference_columns' => ( + isa => ArrayRef, + 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 'on_delete' => ( is => 'rw', required => 0); + has 'on_update' => ( is => 'rw', required => 0); + + 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 is_valid { return $self->has_type && scalar $self->column_ids ? 1 : undef } +}