set table/schema when added to table/schema, rather then on object construction
[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             ## compat
33             get_fields    => 'values',
34             get_field     => 'get',
35             fields        => 'keys',
36         },
37         default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash },
38     );
39     
40     has 'indexes' => (
41         traits => ['Hash'],
42         is => 'rw',
43         isa => HashRef[Index],
44         handles => {
45             exists_index => 'exists',
46             index_ids    => 'keys',
47             get_indices  => 'values',
48             get_index    => 'get',
49             add_index    => 'set',
50         },
51         default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash },
52     );
53     
54     has 'constraints' => (
55         traits => ['Hash'],
56         is => 'rw',
57         isa => HashRef[Constraint],
58         handles => {
59             exists_constraint => 'exists',
60             constraint_ids    => 'keys',
61             get_constraints   => 'values',
62             get_constraint    => 'get',
63             add_constraint    => 'set',
64         },
65         default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash },
66     );
67     
68     has 'sequences' => (
69         traits => ['Hash'],
70         is => 'rw',
71         isa => HashRef[Sequence],
72         handles => {
73             exists_sequence => 'exists',
74             sequence_ids    => 'keys',
75             get_sequences   => 'values',
76             get_sequence    => 'get',
77             add_sequence    => 'set',
78         },
79         default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash },
80     );
81
82     has 'schema' => (
83         is => 'rw',
84         isa => Schema,
85         weak_ref => 1,
86     );
87
88     has 'temporary' => (
89         is => 'rw',
90         isa => Bool,
91         default => 0
92     );
93
94     method add_field(Column $column does coerce) { $self->add_column($column) }
95
96     around add_column(Column $column does coerce) {
97         die "Can't use column name " . $column->name if $self->exists_column($column->name) || $column->name eq '';
98         $column->table($self);
99         return $self->$orig($column->name, $column);
100     }
101
102     around add_constraint(Constraint $constraint) {
103         my $name = $constraint->name;
104         if ($name eq '') {
105             my $idx = 0;
106             while ($self->exists_constraint('ANON' . $idx)) { $idx++ }
107             $name = 'ANON' . $idx;
108         }
109         $constraint->table($self);
110         $self->$orig($name, $constraint)
111     }
112
113     around add_index(Index $index does coerce) {
114         my $name = $index->name;
115         if ($name eq '') {
116             my $idx = 0;
117             while ($self->exists_index('ANON' . $idx)) { $idx++ }
118             $name = 'ANON' . $idx;
119         }
120         $index->table($self);
121         $self->$orig($name, $index)
122     }
123
124     around add_sequence(Sequence $sequence) { $self->$orig($sequence->name, $sequence) }
125
126     multi method primary_key(Any $) { grep /^PRIMARY KEY$/, $_->type for $self->get_constraints }
127     multi method primary_key(Str $column) { $self->get_column($column)->is_primary_key(1) }
128
129     method is_valid { return $self->get_columns ? 1 : undef }
130     method order { }
131
132     before name($name?) { die "Can't use table name $name, table already exists" if $name && $self->schema->exists_table($name) && $name ne $self->name }
133
134     multi method drop_column(Column $column, Int :$cascade = 0) {
135         die "Can't drop non-existant table " . $column->name unless $self->exists_column($column->name);
136         $self->remove_column($column->name);
137
138     }
139
140     multi method drop_column(Str $column, Int :$cascade = 0) {
141         die "Can't drop non-existant table " . $column unless $self->exists_column($column);
142         $self->remove_column($column);
143     }
144 }