apparenlty add_column(coerce ...) works now
[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 Int Str);
4     use MooseX::MultiMethods;
5     use SQL::Translator::Types qw(Column Constraint Index Schema Sequence ColumnHash ConstraintHash IndexHash SequenceHash IxHash);
6     use SQL::Translator::Object::Column;
7     use SQL::Translator::Object::Constraint;
8     use Tie::IxHash;
9     clean;
10
11     use overload
12         '""'     => sub { shift->name },
13         'bool'   => sub { $_[0]->name || $_[0] },
14         fallback => 1,
15     ;
16
17     has 'name' => (
18         is => 'rw',
19         isa => Str,
20         required => 1
21     );
22     
23     has 'columns' => (
24         is => 'rw',
25         isa => IxHash, #ColumnHash,
26         handles => {
27             exists_column => 'EXISTS',
28             column_ids    => 'Keys',
29             get_columns   => 'Values',
30             get_column    => 'FETCH',
31             add_column    => 'STORE',
32             remove_column => 'DELETE',
33             has_columns   => 'Length',
34             clear_columns => 'CLEAR',
35         },
36         coerce => 1,
37         default => sub { Tie::IxHash->new() }
38     );
39     
40     has 'indexes' => (
41         is => 'rw',
42         isa => IxHash, #IndexHash,
43         handles => {
44             exists_index => 'EXISTS',
45             index_ids    => 'Keys',
46             get_indices  => 'Values',
47             get_index    => 'FETCH',
48             add_index    => 'STORE',
49             remove_index => 'DELETE',
50         },
51         coerce => 1,
52         default => sub { Tie::IxHash->new() }
53     );
54     
55     has 'constraints' => (
56         is => 'rw',
57         isa => IxHash, #ConstraintHash,
58         handles => {
59             exists_constraint => 'EXISTS',
60             constraint_ids    => 'Keys',
61             get_constraints   => 'Values',
62             get_constraint    => 'FETCH',
63             add_constraint    => 'STORE',
64             remove_constraint => 'DELETE',
65         },
66         coerce => 1,
67         default => sub { Tie::IxHash->new() }
68     );
69     
70     has 'sequences' => (
71         is => 'rw',
72         isa => IxHash, #SequenceHash,
73         handles => {
74             exists_sequence => 'EXISTS',
75             sequence_ids    => 'Keys',
76             get_sequences   => 'Values',
77             get_sequence    => 'FETCH',
78             add_sequence    => 'STORE',
79             remove_sequence => 'DELETE',
80         },
81         coerce => 1,
82         default => sub { Tie::IxHash->new() },
83     );
84
85     has 'schema' => (
86         is => 'rw',
87         isa => Schema,
88         weak_ref => 1,
89     );
90
91     has 'temporary' => (
92         is => 'rw',
93         isa => Bool,
94         default => 0
95     );
96
97     has '_order' => (
98         is => 'rw',
99         isa => Int,
100     );
101
102     around get_column(Column $column does coerce) {
103         $self->$orig($column->name);
104     }
105
106     around add_column(Column $column does coerce) {
107         die "Can't use column name " . $column->name if $self->exists_column($column->name) || $column->name eq '';
108         $column->table($self);
109         $self->$orig($column->name, $column);
110         return $self->get_column($column->name);
111     }
112
113     around add_constraint(Constraint $constraint does coerce) {
114         if ($constraint->type eq 'FOREIGN KEY') {
115             my @columns = $constraint->get_columns;
116             die "There are no columns associated with this foreign key constraint." unless scalar @columns;
117             for my $column (@columns) {
118                 die "Can't use column " . $column->name . ". It doesn't exist!" unless $self->exists_column($column);
119             }
120             die "Reference table " . $constraint->reference_table . " does not exist!" unless $self->schema->exists_table($constraint->reference_table);
121         }
122         my $name = $constraint->name;
123         if ($name eq '') {
124             my $idx = 0;
125             while ($self->exists_constraint('ANON' . $idx)) { $idx++ }
126             $name = 'ANON' . $idx;
127         }
128         $constraint->table($self);
129         if ($constraint->has_type && $constraint->type eq 'PRIMARY KEY') {
130             $self->get_column($_)->is_primary_key(1) for $constraint->column_ids;
131         }
132         $self->$orig($name, $constraint);
133         return $self->get_constraint($name);
134     }
135
136     around add_index(Index $index does coerce) {
137         my $name = $index->name;
138         if ($name eq '') {
139             my $idx = 0;
140             while ($self->exists_index('ANON' . $idx)) { $idx++ }
141             $name = 'ANON' . $idx;
142         }
143         $index->table($self);
144         $self->$orig($name, $index);
145         return $self->get_index($name);
146     }
147
148     around add_sequence(Sequence $sequence does coerce) {
149         $self->$orig($sequence->name, $sequence);
150         return $self->get_sequence($sequence->name);
151     }
152
153     multi method primary_key {
154         my $constraints = $self->constraints;
155         for my $key ($constraints->Keys) {
156             my $val = $constraints->FETCH($key);
157             return $val if $val->type eq 'PRIMARY KEY';
158         }
159         return;
160     }
161
162     multi method primary_key(Str $column) {
163         die "Column $column does not exist!" unless $self->exists_column($column);
164         $self->get_column($column)->is_primary_key(1);
165
166         my $primary_key = $self->primary_key;
167         unless (defined $primary_key) {
168             $primary_key = SQL::Translator::Object::Constraint->new({ type => 'PRIMARY KEY' });
169             $self->add_constraint($primary_key);
170         }
171         $primary_key->add_column( name => $column ) unless $primary_key->exists_column($column);
172         return $primary_key;
173     }
174
175     multi method order(Int $order) { $self->_order($order); }
176     multi method order {
177         my $order = $self->_order;
178         unless (defined $order && $order) {
179             my $tables = Tie::IxHash->new( map { $_->name => $_ } $self->schema->get_tables );
180             $order = $tables->Indices($self->name) || 0; $order++;
181             $self->_order($order);
182         }
183         return $order;
184     }
185
186     method is_valid { return $self->has_columns ? 1 : undef }
187
188     before name($name?) { die "Can't use table name $name, table already exists" if $name && $self->schema->exists_table($name) && $name ne $self->name }
189
190     around remove_column(Column|Str $column, Int :$cascade = 0) {
191         my $name = is_Column($column) ? $column->name : $column;
192         die "Can't drop non-existant column " . $name unless $self->exists_column($name);
193         $self->$orig($name);
194     }
195
196     around remove_index(Index|Str $index) {
197         my $name = is_Index($index) ? $index->name : $index;
198         die "Can't drop non-existant index " . $name unless $self->exists_index($name);
199         $self->$orig($name);
200     }
201
202     around remove_constraint(Constraint|Str $constraint) {
203         my $name = is_Constraint($constraint) ? $constraint->name : $constraint;
204         die "Can't drop non-existant constraint " . $name unless $self->exists_constraint($name);
205         $self->$orig($name);
206     }
207
208     around BUILDARGS(ClassName $self: @args) {
209         my $args = $self->$orig(@args);
210
211         my $fields = delete $args->{fields};
212
213         $args->{columns}{$_} = SQL::Translator::Object::Column->new( name => $_ ) for @$fields;
214
215         return $args;
216     }
217 }