added some attributes
[dbsrgits/SQL-Translator-2.0-ish.git] / lib / SQL / Translator / Object / Constraint.pm
1 use MooseX::Declare;
2 class SQL::Translator::Object::Constraint extends SQL::Translator::Object {
3     use MooseX::Types::Moose qw(ArrayRef Bool HashRef Maybe Str Undef);
4     use MooseX::MultiMethods;
5     use SQL::Translator::Types qw(Column MatchType Table);
6
7     has 'table' => (
8         is => 'rw',
9         isa => Table,
10         weak_ref => 1,
11     );
12     
13     has 'name' => (
14         is => 'rw',
15         isa => Str,
16         default => '',
17         required => 1
18     );
19     
20     has 'columns' => (
21         traits => ['Hash'],
22         is => 'rw',
23         isa => HashRef[Column],
24         handles => {
25             exists_column => 'exists',
26             column_ids    => 'keys',
27             get_columns   => 'values',
28             get_column    => 'get',
29             add_column    => 'set',
30             clear_columns => 'clear',
31         },
32         default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash },
33     );
34     
35     has 'type' => (
36         is => 'rw',
37         isa => Str,
38         predicate => 'has_type',
39         required => 1,
40     );
41
42     has 'deferrable' => (
43         is => 'rw',
44         isa => Bool,
45         default => 1
46     );
47
48     has 'expression' => (
49         is => 'rw',
50         isa => Str,
51     );
52
53     has 'reference_table' => (
54         isa => Maybe[Str],
55         is => 'rw',
56     );
57
58     has 'reference_columns' => (
59         isa => ArrayRef,
60         traits => ['Array'],
61         handles => {
62             reference_columns => 'elements',
63             add_reference_column => 'push',
64         },
65         default => sub { [] },
66         required => 1,
67     );
68
69     has 'match_type' => (
70         isa => MatchType,
71         is => 'rw',
72         coerce => 1,
73         lazy => 1,
74         default => ''
75     );
76
77     has 'on_delete' => ( is => 'rw', required => 0);
78     has 'on_update' => ( is => 'rw', required => 0);
79
80     around add_column(Column $column) {
81         if ($self->has_type && $self->type eq 'PRIMARY KEY') {
82             $column->is_primary_key(1);
83         }
84         $self->$orig($column->name, $column)
85     }
86
87     method is_valid { return $self->has_type && scalar $self->column_ids ? 1 : undef }
88 }