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); |
0c4b09d0 |
6 | clean; |
7 | |
8 | use overload |
9 | '""' => sub { shift->name }, |
10 | 'bool' => sub { $_[0]->name || $_[0] }, |
11 | fallback => 1, |
12 | ; |
13 | |
4f4fd192 |
14 | has 'name' => ( |
15 | is => 'rw', |
16 | isa => Str, |
17 | required => 1 |
18 | ); |
19 | |
20 | has 'columns' => ( |
720dcdc3 |
21 | traits => ['Hash'], |
4f4fd192 |
22 | is => 'rw', |
23 | isa => HashRef[Column], |
720dcdc3 |
24 | handles => { |
25 | exists_column => 'exists', |
26 | column_ids => 'keys', |
27 | get_columns => 'values', |
28 | get_column => 'get', |
29 | add_column => 'set', |
40fb14a9 |
30 | remove_column => 'delete', |
452c34e6 |
31 | clear_columns => 'clear', |
4f4fd192 |
32 | }, |
33 | default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash }, |
34 | ); |
35 | |
36 | has 'indexes' => ( |
720dcdc3 |
37 | traits => ['Hash'], |
4f4fd192 |
38 | is => 'rw', |
39 | isa => HashRef[Index], |
720dcdc3 |
40 | handles => { |
41 | exists_index => 'exists', |
42 | index_ids => 'keys', |
43 | get_indices => 'values', |
44 | get_index => 'get', |
45 | add_index => 'set', |
48020fcf |
46 | remove_index => 'delete', |
4f4fd192 |
47 | }, |
2e7ac234 |
48 | default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash }, |
4f4fd192 |
49 | ); |
50 | |
51 | has 'constraints' => ( |
720dcdc3 |
52 | traits => ['Hash'], |
4f4fd192 |
53 | is => 'rw', |
54 | isa => HashRef[Constraint], |
720dcdc3 |
55 | handles => { |
56 | exists_constraint => 'exists', |
57 | constraint_ids => 'keys', |
58 | get_constraints => 'values', |
59 | get_constraint => 'get', |
60 | add_constraint => 'set', |
bebe11d5 |
61 | remove_constraint => 'delete', |
4f4fd192 |
62 | }, |
49063029 |
63 | default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash }, |
4f4fd192 |
64 | ); |
65 | |
66 | has 'sequences' => ( |
720dcdc3 |
67 | traits => ['Hash'], |
4f4fd192 |
68 | is => 'rw', |
69 | isa => HashRef[Sequence], |
720dcdc3 |
70 | handles => { |
71 | exists_sequence => 'exists', |
72 | sequence_ids => 'keys', |
73 | get_sequences => 'values', |
74 | get_sequence => 'get', |
75 | add_sequence => 'set', |
f5ced714 |
76 | remove_sequence => 'delete', |
4f4fd192 |
77 | }, |
2e7ac234 |
78 | default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash }, |
4f4fd192 |
79 | ); |
f78a484a |
80 | |
2850baeb |
81 | has 'schema' => ( |
4f4fd192 |
82 | is => 'rw', |
2850baeb |
83 | isa => Schema, |
84 | weak_ref => 1, |
4f4fd192 |
85 | ); |
49063029 |
86 | |
2850baeb |
87 | has 'temporary' => ( |
b750d2f1 |
88 | is => 'rw', |
2850baeb |
89 | isa => Bool, |
90 | default => 0 |
b750d2f1 |
91 | ); |
92 | |
de1e817e |
93 | around add_column(Column $column does coerce) { |
94 | die "Can't use column name " . $column->name if $self->exists_column($column->name) || $column->name eq ''; |
e1bef8b9 |
95 | $column->table($self); |
de1e817e |
96 | return $self->$orig($column->name, $column); |
97 | } |
e1bef8b9 |
98 | |
78963c00 |
99 | around add_constraint(Constraint $constraint does coerce) { |
f34b818d |
100 | my $name = $constraint->name; |
101 | if ($name eq '') { |
c27fec89 |
102 | my $idx = 0; |
103 | while ($self->exists_constraint('ANON' . $idx)) { $idx++ } |
104 | $name = 'ANON' . $idx; |
f34b818d |
105 | } |
e1bef8b9 |
106 | $constraint->table($self); |
aa20eb7f |
107 | if ($constraint->has_type && $constraint->type eq 'PRIMARY KEY') { |
108 | $self->get_column($_)->is_primary_key(1) for $constraint->column_ids; |
109 | } |
f34b818d |
110 | $self->$orig($name, $constraint) |
111 | } |
e1bef8b9 |
112 | |
706009e5 |
113 | around add_index(Index $index does coerce) { |
c27fec89 |
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 | } |
e1bef8b9 |
120 | $index->table($self); |
c27fec89 |
121 | $self->$orig($name, $index) |
122 | } |
e1bef8b9 |
123 | |
78963c00 |
124 | around add_sequence(Sequence $sequence does coerce) { $self->$orig($sequence->name, $sequence) } |
51700db2 |
125 | |
a20ea10d |
126 | multi method primary_key { grep /^PRIMARY KEY$/, $_->type for $self->get_constraints } |
5f184270 |
127 | multi method primary_key(Str $column) { $self->get_column($column)->is_primary_key(1) } |
b750d2f1 |
128 | |
079f0c78 |
129 | method is_valid { return $self->get_columns ? 1 : undef } |
b750d2f1 |
130 | method order { } |
079f0c78 |
131 | |
40fb14a9 |
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 } |
079f0c78 |
133 | |
bebe11d5 |
134 | around remove_column(Column|Str $column, Int :$cascade = 0) { |
135 | my $name = is_Column($column) ? $column->name : $column; |
136 | die "Can't drop non-existant column " . $name unless $self->exists_column($name); |
137 | $self->$orig($name); |
40fb14a9 |
138 | } |
139 | |
bebe11d5 |
140 | around remove_index(Index|Str $index) { |
141 | my $name = is_Index($index) ? $index->name : $index; |
142 | die "Can't drop non-existant index " . $name unless $self->exists_index($name); |
143 | $self->$orig($name); |
1dde2bfe |
144 | } |
145 | |
bebe11d5 |
146 | around remove_constraint(Constraint|Str $constraint) { |
147 | my $name = is_Constraint($constraint) ? $constraint->name : $constraint; |
148 | die "Can't drop non-existant constraint " . $name unless $self->exists_constraint($name); |
149 | $self->$orig($name); |
1dde2bfe |
150 | } |
4f4fd192 |
151 | } |