move drop_* to Compat, change to around remove_* in lieu of multi method drop_*
[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             remove_constraint => 'delete',
61         },
62         default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash },
63     );
64     
65     has 'sequences' => (
66         traits => ['Hash'],
67         is => 'rw',
68         isa => HashRef[Sequence],
69         handles => {
70             exists_sequence => 'exists',
71             sequence_ids    => 'keys',
72             get_sequences   => 'values',
73             get_sequence    => 'get',
74             add_sequence    => 'set',
75         },
76         default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash },
77     );
78
79     has 'schema' => (
80         is => 'rw',
81         isa => Schema,
82         weak_ref => 1,
83     );
84
85     has 'temporary' => (
86         is => 'rw',
87         isa => Bool,
88         default => 0
89     );
90
91     around add_column(Column $column does coerce) {
92         die "Can't use column name " . $column->name if $self->exists_column($column->name) || $column->name eq '';
93         $column->table($self);
94         return $self->$orig($column->name, $column);
95     }
96
97     around add_constraint(Constraint $constraint does coerce) {
98         my $name = $constraint->name;
99         if ($name eq '') {
100             my $idx = 0;
101             while ($self->exists_constraint('ANON' . $idx)) { $idx++ }
102             $name = 'ANON' . $idx;
103         }
104         $constraint->table($self);
105         $self->$orig($name, $constraint)
106     }
107
108     around add_index(Index $index does coerce) {
109         my $name = $index->name;
110         if ($name eq '') {
111             my $idx = 0;
112             while ($self->exists_index('ANON' . $idx)) { $idx++ }
113             $name = 'ANON' . $idx;
114         }
115         $index->table($self);
116         $self->$orig($name, $index)
117     }
118
119     around add_sequence(Sequence $sequence does coerce) { $self->$orig($sequence->name, $sequence) }
120
121     multi method primary_key(Any $) { grep /^PRIMARY KEY$/, $_->type for $self->get_constraints }
122     multi method primary_key(Str $column) { $self->get_column($column)->is_primary_key(1) }
123
124     method is_valid { return $self->get_columns ? 1 : undef }
125     method order { }
126
127     before name($name?) { die "Can't use table name $name, table already exists" if $name && $self->schema->exists_table($name) && $name ne $self->name }
128
129     around remove_column(Column|Str $column, Int :$cascade = 0) {
130         my $name = is_Column($column) ? $column->name : $column;
131         die "Can't drop non-existant column " . $name unless $self->exists_column($name);
132         $self->$orig($name);
133     }
134
135     around remove_index(Index|Str $index) {
136         my $name = is_Index($index) ? $index->name : $index;
137         die "Can't drop non-existant index " . $name unless $self->exists_index($name);
138         $self->$orig($name);
139     }
140
141     around remove_constraint(Constraint|Str $constraint) {
142         my $name = is_Constraint($constraint) ? $constraint->name : $constraint;
143         die "Can't drop non-existant constraint " . $name unless $self->exists_constraint($name);
144         $self->$orig($name);
145     }
146 }