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