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', |
4f4fd192 |
31 | }, |
32 | default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash }, |
33 | ); |
34 | |
35 | has 'indexes' => ( |
720dcdc3 |
36 | traits => ['Hash'], |
4f4fd192 |
37 | is => 'rw', |
38 | isa => HashRef[Index], |
720dcdc3 |
39 | handles => { |
40 | exists_index => 'exists', |
41 | index_ids => 'keys', |
42 | get_indices => 'values', |
43 | get_index => 'get', |
44 | add_index => 'set', |
48020fcf |
45 | remove_index => 'delete', |
4f4fd192 |
46 | }, |
2e7ac234 |
47 | default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash }, |
4f4fd192 |
48 | ); |
49 | |
50 | has 'constraints' => ( |
720dcdc3 |
51 | traits => ['Hash'], |
4f4fd192 |
52 | is => 'rw', |
53 | isa => HashRef[Constraint], |
720dcdc3 |
54 | handles => { |
55 | exists_constraint => 'exists', |
56 | constraint_ids => 'keys', |
57 | get_constraints => 'values', |
58 | get_constraint => 'get', |
59 | add_constraint => 'set', |
bebe11d5 |
60 | remove_constraint => 'delete', |
4f4fd192 |
61 | }, |
49063029 |
62 | default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash }, |
4f4fd192 |
63 | ); |
64 | |
65 | has 'sequences' => ( |
720dcdc3 |
66 | traits => ['Hash'], |
4f4fd192 |
67 | is => 'rw', |
68 | isa => HashRef[Sequence], |
720dcdc3 |
69 | handles => { |
70 | exists_sequence => 'exists', |
71 | sequence_ids => 'keys', |
72 | get_sequences => 'values', |
73 | get_sequence => 'get', |
74 | add_sequence => 'set', |
4f4fd192 |
75 | }, |
2e7ac234 |
76 | default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash }, |
4f4fd192 |
77 | ); |
f78a484a |
78 | |
2850baeb |
79 | has 'schema' => ( |
4f4fd192 |
80 | is => 'rw', |
2850baeb |
81 | isa => Schema, |
82 | weak_ref => 1, |
4f4fd192 |
83 | ); |
49063029 |
84 | |
2850baeb |
85 | has 'temporary' => ( |
b750d2f1 |
86 | is => 'rw', |
2850baeb |
87 | isa => Bool, |
88 | default => 0 |
b750d2f1 |
89 | ); |
90 | |
de1e817e |
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 ''; |
e1bef8b9 |
93 | $column->table($self); |
de1e817e |
94 | return $self->$orig($column->name, $column); |
95 | } |
e1bef8b9 |
96 | |
78963c00 |
97 | around add_constraint(Constraint $constraint does coerce) { |
f34b818d |
98 | my $name = $constraint->name; |
99 | if ($name eq '') { |
c27fec89 |
100 | my $idx = 0; |
101 | while ($self->exists_constraint('ANON' . $idx)) { $idx++ } |
102 | $name = 'ANON' . $idx; |
f34b818d |
103 | } |
e1bef8b9 |
104 | $constraint->table($self); |
f34b818d |
105 | $self->$orig($name, $constraint) |
106 | } |
e1bef8b9 |
107 | |
706009e5 |
108 | around add_index(Index $index does coerce) { |
c27fec89 |
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 | } |
e1bef8b9 |
115 | $index->table($self); |
c27fec89 |
116 | $self->$orig($name, $index) |
117 | } |
e1bef8b9 |
118 | |
78963c00 |
119 | around add_sequence(Sequence $sequence does coerce) { $self->$orig($sequence->name, $sequence) } |
51700db2 |
120 | |
5f184270 |
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) } |
b750d2f1 |
123 | |
079f0c78 |
124 | method is_valid { return $self->get_columns ? 1 : undef } |
b750d2f1 |
125 | method order { } |
079f0c78 |
126 | |
40fb14a9 |
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 } |
079f0c78 |
128 | |
bebe11d5 |
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); |
40fb14a9 |
133 | } |
134 | |
bebe11d5 |
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); |
1dde2bfe |
139 | } |
140 | |
bebe11d5 |
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); |
1dde2bfe |
145 | } |
4f4fd192 |
146 | } |