Commit | Line | Data |
4f4fd192 |
1 | use MooseX::Declare; |
0c4b09d0 |
2 | class SQL::Translator::Object::Table extends SQL::Translator::Object is dirty { |
1c607f61 |
3 | use MooseX::Types::Moose qw(Any Bool HashRef Str); |
5f184270 |
4 | use MooseX::MultiMethods; |
4f4fd192 |
5 | use SQL::Translator::Types qw(Column Constraint Index Schema Sequence); |
46ad29bb |
6 | use SQL::Translator::Object::Column; |
e2b6425f |
7 | use SQL::Translator::Object::Constraint; |
0c4b09d0 |
8 | clean; |
9 | |
10 | use overload |
11 | '""' => sub { shift->name }, |
12 | 'bool' => sub { $_[0]->name || $_[0] }, |
13 | fallback => 1, |
14 | ; |
15 | |
4f4fd192 |
16 | has 'name' => ( |
17 | is => 'rw', |
18 | isa => Str, |
19 | required => 1 |
20 | ); |
21 | |
22 | has 'columns' => ( |
720dcdc3 |
23 | traits => ['Hash'], |
4f4fd192 |
24 | is => 'rw', |
25 | isa => HashRef[Column], |
720dcdc3 |
26 | handles => { |
27 | exists_column => 'exists', |
28 | column_ids => 'keys', |
29 | get_columns => 'values', |
30 | get_column => 'get', |
31 | add_column => 'set', |
40fb14a9 |
32 | remove_column => 'delete', |
452c34e6 |
33 | clear_columns => 'clear', |
4f4fd192 |
34 | }, |
35 | default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash }, |
36 | ); |
37 | |
38 | has 'indexes' => ( |
720dcdc3 |
39 | traits => ['Hash'], |
4f4fd192 |
40 | is => 'rw', |
41 | isa => HashRef[Index], |
720dcdc3 |
42 | handles => { |
43 | exists_index => 'exists', |
44 | index_ids => 'keys', |
45 | get_indices => 'values', |
46 | get_index => 'get', |
47 | add_index => 'set', |
48020fcf |
48 | remove_index => 'delete', |
4f4fd192 |
49 | }, |
2e7ac234 |
50 | default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash }, |
4f4fd192 |
51 | ); |
52 | |
53 | has 'constraints' => ( |
720dcdc3 |
54 | traits => ['Hash'], |
4f4fd192 |
55 | is => 'rw', |
56 | isa => HashRef[Constraint], |
720dcdc3 |
57 | handles => { |
58 | exists_constraint => 'exists', |
59 | constraint_ids => 'keys', |
60 | get_constraints => 'values', |
61 | get_constraint => 'get', |
62 | add_constraint => 'set', |
bebe11d5 |
63 | remove_constraint => 'delete', |
4f4fd192 |
64 | }, |
49063029 |
65 | default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash }, |
4f4fd192 |
66 | ); |
67 | |
68 | has 'sequences' => ( |
720dcdc3 |
69 | traits => ['Hash'], |
4f4fd192 |
70 | is => 'rw', |
71 | isa => HashRef[Sequence], |
720dcdc3 |
72 | handles => { |
73 | exists_sequence => 'exists', |
74 | sequence_ids => 'keys', |
75 | get_sequences => 'values', |
76 | get_sequence => 'get', |
77 | add_sequence => 'set', |
f5ced714 |
78 | remove_sequence => 'delete', |
4f4fd192 |
79 | }, |
2e7ac234 |
80 | default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash }, |
4f4fd192 |
81 | ); |
f78a484a |
82 | |
2850baeb |
83 | has 'schema' => ( |
4f4fd192 |
84 | is => 'rw', |
2850baeb |
85 | isa => Schema, |
86 | weak_ref => 1, |
4f4fd192 |
87 | ); |
49063029 |
88 | |
2850baeb |
89 | has 'temporary' => ( |
b750d2f1 |
90 | is => 'rw', |
2850baeb |
91 | isa => Bool, |
92 | default => 0 |
b750d2f1 |
93 | ); |
94 | |
de1e817e |
95 | around add_column(Column $column does coerce) { |
96 | die "Can't use column name " . $column->name if $self->exists_column($column->name) || $column->name eq ''; |
e1bef8b9 |
97 | $column->table($self); |
de1e817e |
98 | return $self->$orig($column->name, $column); |
99 | } |
e1bef8b9 |
100 | |
78963c00 |
101 | around add_constraint(Constraint $constraint does coerce) { |
f34b818d |
102 | my $name = $constraint->name; |
103 | if ($name eq '') { |
c27fec89 |
104 | my $idx = 0; |
105 | while ($self->exists_constraint('ANON' . $idx)) { $idx++ } |
106 | $name = 'ANON' . $idx; |
f34b818d |
107 | } |
e1bef8b9 |
108 | $constraint->table($self); |
aa20eb7f |
109 | if ($constraint->has_type && $constraint->type eq 'PRIMARY KEY') { |
110 | $self->get_column($_)->is_primary_key(1) for $constraint->column_ids; |
111 | } |
f34b818d |
112 | $self->$orig($name, $constraint) |
113 | } |
e1bef8b9 |
114 | |
706009e5 |
115 | around add_index(Index $index does coerce) { |
c27fec89 |
116 | my $name = $index->name; |
117 | if ($name eq '') { |
118 | my $idx = 0; |
119 | while ($self->exists_index('ANON' . $idx)) { $idx++ } |
120 | $name = 'ANON' . $idx; |
121 | } |
e1bef8b9 |
122 | $index->table($self); |
c27fec89 |
123 | $self->$orig($name, $index) |
124 | } |
e1bef8b9 |
125 | |
78963c00 |
126 | around add_sequence(Sequence $sequence does coerce) { $self->$orig($sequence->name, $sequence) } |
51700db2 |
127 | |
e2b6425f |
128 | multi method primary_key { |
129 | my $constraints = $self->constraints; |
130 | for my $key (keys %$constraints) { |
131 | return $constraints->{$key} if $constraints->{$key}{type} eq 'PRIMARY KEY'; |
132 | } |
133 | return undef; |
134 | } |
135 | |
0a7ce17e |
136 | multi method primary_key(Str $column) { |
137 | die "Column $column does not exist!" unless $self->exists_column($column); |
138 | $self->get_column($column)->is_primary_key(1); |
e2b6425f |
139 | |
140 | my $primary_key = $self->primary_key; |
141 | unless (defined $primary_key) { |
142 | $primary_key = SQL::Translator::Object::Constraint->new({ type => 'PRIMARY KEY' }); |
143 | $self->add_constraint($primary_key); |
144 | } |
eea42e0a |
145 | $primary_key->add_field({ name => $column }) unless $primary_key->exists_column($column); ## FIX ME, change back to add_column once around add_column(coerce .. ) works |
e2b6425f |
146 | return $primary_key; |
0a7ce17e |
147 | } |
b750d2f1 |
148 | |
079f0c78 |
149 | method is_valid { return $self->get_columns ? 1 : undef } |
b750d2f1 |
150 | method order { } |
079f0c78 |
151 | |
40fb14a9 |
152 | before name($name?) { die "Can't use table name $name, table already exists" if $name && $self->schema->exists_table($name) && $name ne $self->name } |
079f0c78 |
153 | |
bebe11d5 |
154 | around remove_column(Column|Str $column, Int :$cascade = 0) { |
155 | my $name = is_Column($column) ? $column->name : $column; |
156 | die "Can't drop non-existant column " . $name unless $self->exists_column($name); |
157 | $self->$orig($name); |
40fb14a9 |
158 | } |
159 | |
bebe11d5 |
160 | around remove_index(Index|Str $index) { |
161 | my $name = is_Index($index) ? $index->name : $index; |
162 | die "Can't drop non-existant index " . $name unless $self->exists_index($name); |
163 | $self->$orig($name); |
1dde2bfe |
164 | } |
165 | |
bebe11d5 |
166 | around remove_constraint(Constraint|Str $constraint) { |
167 | my $name = is_Constraint($constraint) ? $constraint->name : $constraint; |
168 | die "Can't drop non-existant constraint " . $name unless $self->exists_constraint($name); |
169 | $self->$orig($name); |
1dde2bfe |
170 | } |
46ad29bb |
171 | |
172 | around BUILDARGS(ClassName $self: @args) { |
173 | my $args = $self->$orig(@args); |
174 | |
175 | my $fields = delete $args->{fields}; |
176 | |
177 | tie %{$args->{columns}}, 'Tie::IxHash'; |
178 | $args->{columns}{$_} = SQL::Translator::Object::Column->new( name => $_ ) for @$fields; |
179 | |
180 | return $args; |
181 | } |
4f4fd192 |
182 | } |