coerce Sequence and Constraint into objects
[dbsrgits/SQL-Translator-2.0-ish.git] / lib / SQL / Translator / Object / Table.pm
1 use MooseX::Declare;
2 class SQL::Translator::Object::Table extends SQL::Translator::Object is dirty {
3     use MooseX::Types::Moose qw(Any Bool HashRef Str);
4     use MooseX::MultiMethods;
5     use SQL::Translator::Types qw(Column Constraint Index Schema Sequence);
6     clean;
7
8     use overload
9         '""'     => sub { shift->name },
10         'bool'   => sub { $_[0]->name || $_[0] },
11         fallback => 1,
12     ;
13
14     has 'name' => (
15         is => 'rw',
16         isa => Str,
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             remove_column => 'delete',
31         },
32         default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash },
33     );
34     
35     has 'indexes' => (
36         traits => ['Hash'],
37         is => 'rw',
38         isa => HashRef[Index],
39         handles => {
40             exists_index => 'exists',
41             index_ids    => 'keys',
42             get_indices  => 'values',
43             get_index    => 'get',
44             add_index    => 'set',
45             remove_index => 'delete',
46         },
47         default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash },
48     );
49     
50     has 'constraints' => (
51         traits => ['Hash'],
52         is => 'rw',
53         isa => HashRef[Constraint],
54         handles => {
55             exists_constraint => 'exists',
56             constraint_ids    => 'keys',
57             get_constraints   => 'values',
58             get_constraint    => 'get',
59             add_constraint    => 'set',
60         },
61         default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash },
62     );
63     
64     has 'sequences' => (
65         traits => ['Hash'],
66         is => 'rw',
67         isa => HashRef[Sequence],
68         handles => {
69             exists_sequence => 'exists',
70             sequence_ids    => 'keys',
71             get_sequences   => 'values',
72             get_sequence    => 'get',
73             add_sequence    => 'set',
74         },
75         default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash },
76     );
77
78     has 'schema' => (
79         is => 'rw',
80         isa => Schema,
81         weak_ref => 1,
82     );
83
84     has 'temporary' => (
85         is => 'rw',
86         isa => Bool,
87         default => 0
88     );
89
90     method add_field(Column $column does coerce) { $self->add_column($column) }
91
92     around add_column(Column $column does coerce) {
93         die "Can't use column name " . $column->name if $self->exists_column($column->name) || $column->name eq '';
94         $column->table($self);
95         return $self->$orig($column->name, $column);
96     }
97
98     around add_constraint(Constraint $constraint does coerce) {
99         my $name = $constraint->name;
100         if ($name eq '') {
101             my $idx = 0;
102             while ($self->exists_constraint('ANON' . $idx)) { $idx++ }
103             $name = 'ANON' . $idx;
104         }
105         $constraint->table($self);
106         $self->$orig($name, $constraint)
107     }
108
109     around add_index(Index $index does coerce) {
110         my $name = $index->name;
111         if ($name eq '') {
112             my $idx = 0;
113             while ($self->exists_index('ANON' . $idx)) { $idx++ }
114             $name = 'ANON' . $idx;
115         }
116         $index->table($self);
117         $self->$orig($name, $index)
118     }
119
120     around add_sequence(Sequence $sequence does coerce) { $self->$orig($sequence->name, $sequence) }
121
122     multi method primary_key(Any $) { grep /^PRIMARY KEY$/, $_->type for $self->get_constraints }
123     multi method primary_key(Str $column) { $self->get_column($column)->is_primary_key(1) }
124
125     method is_valid { return $self->get_columns ? 1 : undef }
126     method order { }
127
128     before name($name?) { die "Can't use table name $name, table already exists" if $name && $self->schema->exists_table($name) && $name ne $self->name }
129
130     multi method drop_column(Column $column, Int :$cascade = 0) {
131         die "Can't drop non-existant table " . $column->name unless $self->exists_column($column->name);
132         $self->remove_column($column->name);
133
134     }
135
136     multi method drop_column(Str $column, Int :$cascade = 0) {
137         die "Can't drop non-existant table " . $column unless $self->exists_column($column);
138         $self->remove_column($column);
139     }
140 }