Commit | Line | Data |
4f4fd192 |
1 | use MooseX::Declare; |
ebf2721d |
2 | class SQL::Translator::Object::Constraint extends SQL::Translator::Object { |
f49a2a49 |
3 | use MooseX::Types::Moose qw(ArrayRef Bool HashRef Maybe Str Undef); |
035b8503 |
4 | use MooseX::MultiMethods; |
3c557f72 |
5 | use SQL::Translator::Types qw(Column MatchType Table); |
f49a2a49 |
6 | |
7 | has 'table' => ( |
8 | is => 'rw', |
9 | isa => Table, |
f49a2a49 |
10 | weak_ref => 1, |
11 | ); |
4f4fd192 |
12 | |
13 | has 'name' => ( |
14 | is => 'rw', |
70ab6d8c |
15 | isa => Str, |
16 | default => '', |
4f4fd192 |
17 | required => 1 |
18 | ); |
19 | |
20 | has 'columns' => ( |
28bd628e |
21 | traits => ['Hash'], |
4f4fd192 |
22 | is => 'rw', |
23 | isa => HashRef[Column], |
28bd628e |
24 | handles => { |
25 | exists_column => 'exists', |
26 | column_ids => 'keys', |
27 | get_columns => 'values', |
28 | get_column => 'get', |
29 | add_column => 'set', |
452c34e6 |
30 | clear_columns => 'clear', |
4f4fd192 |
31 | }, |
32 | default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash }, |
33 | ); |
34 | |
35 | has 'type' => ( |
36 | is => 'rw', |
37 | isa => Str, |
aa20eb7f |
38 | predicate => 'has_type', |
ad203890 |
39 | required => 1, |
4f4fd192 |
40 | ); |
526e74d1 |
41 | |
b750d2f1 |
42 | has 'deferrable' => ( |
43 | is => 'rw', |
44 | isa => Bool, |
075b652f |
45 | default => 1 |
b750d2f1 |
46 | ); |
47 | |
48 | has 'expression' => ( |
49 | is => 'rw', |
50 | isa => Str, |
51 | ); |
52 | |
d63c8da0 |
53 | has 'reference_table' => ( |
54 | isa => Maybe[Str], |
55 | is => 'rw', |
56 | ); |
57 | |
58 | has 'reference_columns' => ( |
106f5e00 |
59 | isa => ArrayRef, |
60 | traits => ['Array'], |
61 | handles => { |
62 | reference_columns => 'elements', |
63 | }, |
64 | default => sub { [] }, |
ad203890 |
65 | required => 1, |
cb13230a |
66 | ); |
67 | |
68 | has 'match_type' => ( |
3c557f72 |
69 | isa => MatchType, |
d191bceb |
70 | is => 'rw', |
3c557f72 |
71 | coerce => 1, |
d191bceb |
72 | lazy => 1, |
73 | default => '' |
d63c8da0 |
74 | ); |
75 | |
aa20eb7f |
76 | around add_column(Column $column) { |
77 | if ($self->has_type && $self->type eq 'PRIMARY KEY') { |
78 | $column->is_primary_key(1); |
79 | } |
80 | $self->$orig($column->name, $column) |
81 | } |
82 | |
83 | method is_valid { return $self->has_type && scalar $self->column_ids ? 1 : undef } |
4f4fd192 |
84 | } |