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', |
4f4fd192 |
45 | }, |
2e7ac234 |
46 | default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash }, |
4f4fd192 |
47 | ); |
48 | |
49 | has 'constraints' => ( |
720dcdc3 |
50 | traits => ['Hash'], |
4f4fd192 |
51 | is => 'rw', |
52 | isa => HashRef[Constraint], |
720dcdc3 |
53 | handles => { |
54 | exists_constraint => 'exists', |
55 | constraint_ids => 'keys', |
56 | get_constraints => 'values', |
57 | get_constraint => 'get', |
58 | add_constraint => 'set', |
4f4fd192 |
59 | }, |
49063029 |
60 | default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash }, |
4f4fd192 |
61 | ); |
62 | |
63 | has 'sequences' => ( |
720dcdc3 |
64 | traits => ['Hash'], |
4f4fd192 |
65 | is => 'rw', |
66 | isa => HashRef[Sequence], |
720dcdc3 |
67 | handles => { |
68 | exists_sequence => 'exists', |
69 | sequence_ids => 'keys', |
70 | get_sequences => 'values', |
71 | get_sequence => 'get', |
72 | add_sequence => 'set', |
4f4fd192 |
73 | }, |
2e7ac234 |
74 | default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash }, |
4f4fd192 |
75 | ); |
f78a484a |
76 | |
2850baeb |
77 | has 'schema' => ( |
4f4fd192 |
78 | is => 'rw', |
2850baeb |
79 | isa => Schema, |
80 | weak_ref => 1, |
4f4fd192 |
81 | ); |
49063029 |
82 | |
2850baeb |
83 | has 'temporary' => ( |
b750d2f1 |
84 | is => 'rw', |
2850baeb |
85 | isa => Bool, |
86 | default => 0 |
b750d2f1 |
87 | ); |
88 | |
de1e817e |
89 | method add_field(Column $column does coerce) { $self->add_column($column) } |
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 ''; |
e1bef8b9 |
93 | $column->table($self); |
de1e817e |
94 | return $self->$orig($column->name, $column); |
95 | } |
e1bef8b9 |
96 | |
f34b818d |
97 | around add_constraint(Constraint $constraint) { |
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 | |
c27fec89 |
119 | around add_sequence(Sequence $sequence) { $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 | |
40fb14a9 |
129 | multi method drop_column(Column $column, Int :$cascade = 0) { |
130 | die "Can't drop non-existant table " . $column->name unless $self->exists_column($column->name); |
131 | $self->remove_column($column->name); |
132 | |
133 | } |
134 | |
135 | multi method drop_column(Str $column, Int :$cascade = 0) { |
136 | die "Can't drop non-existant table " . $column unless $self->exists_column($column); |
137 | $self->remove_column($column); |
138 | } |
4f4fd192 |
139 | } |