2 class SQL::Translator::Object::Table extends SQL::Translator::Object is dirty {
3 use MooseX::Types::Moose qw(Any Bool HashRef Int Str);
4 use MooseX::MultiMethods;
5 use SQL::Translator::Types qw(Column Constraint Index Schema Sequence ColumnHash ConstraintHash IndexHash SequenceHash IxHash);
6 use SQL::Translator::Object::Column;
7 use SQL::Translator::Object::Constraint;
12 '""' => sub { shift->name },
13 'bool' => sub { $_[0]->name || $_[0] },
25 isa => IxHash, #ColumnHash,
27 exists_column => 'EXISTS',
29 get_columns => 'Values',
30 get_column => 'FETCH',
31 add_column => 'STORE',
32 remove_column => 'DELETE',
33 has_columns => 'Length',
34 clear_columns => 'CLEAR',
37 default => sub { Tie::IxHash->new() }
42 isa => IxHash, #IndexHash,
44 exists_index => 'EXISTS',
46 get_indices => 'Values',
49 remove_index => 'DELETE',
52 default => sub { Tie::IxHash->new() }
55 has 'constraints' => (
57 isa => IxHash, #ConstraintHash,
59 exists_constraint => 'EXISTS',
60 constraint_ids => 'Keys',
61 get_constraints => 'Values',
62 get_constraint => 'FETCH',
63 add_constraint => 'STORE',
64 remove_constraint => 'DELETE',
67 default => sub { Tie::IxHash->new() }
72 isa => IxHash, #SequenceHash,
74 exists_sequence => 'EXISTS',
75 sequence_ids => 'Keys',
76 get_sequences => 'Values',
77 get_sequence => 'FETCH',
78 add_sequence => 'STORE',
79 remove_sequence => 'DELETE',
82 default => sub { Tie::IxHash->new() },
102 around add_column(Column $column does coerce) {
103 die "Can't use column name " . $column->name if $self->exists_column($column->name) || $column->name eq '';
104 $column->table($self);
105 $self->$orig($column->name, $column);
106 return $self->get_column($column->name);
109 around add_constraint(Constraint $constraint does coerce) {
110 my $name = $constraint->name;
113 while ($self->exists_constraint('ANON' . $idx)) { $idx++ }
114 $name = 'ANON' . $idx;
116 $constraint->table($self);
117 if ($constraint->has_type && $constraint->type eq 'PRIMARY KEY') {
118 $self->get_column($_)->is_primary_key(1) for $constraint->column_ids;
120 $self->$orig($name, $constraint);
121 return $self->get_constraint($name);
124 around add_index(Index $index does coerce) {
125 my $name = $index->name;
128 while ($self->exists_index('ANON' . $idx)) { $idx++ }
129 $name = 'ANON' . $idx;
131 $index->table($self);
132 $self->$orig($name, $index);
133 return $self->get_index($name);
136 around add_sequence(Sequence $sequence does coerce) {
137 $self->$orig($sequence->name, $sequence);
138 return $self->get_sequence($sequence->name);
141 multi method primary_key {
142 my $constraints = $self->constraints;
143 for my $key (keys %$constraints) {
144 return $constraints->{$key} if $constraints->{$key}{type} eq 'PRIMARY KEY';
149 multi method primary_key(Str $column) {
150 die "Column $column does not exist!" unless $self->exists_column($column);
151 $self->get_column($column)->is_primary_key(1);
153 my $primary_key = $self->primary_key;
154 unless (defined $primary_key) {
155 $primary_key = SQL::Translator::Object::Constraint->new({ type => 'PRIMARY KEY' });
156 $self->add_constraint($primary_key);
158 $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
162 multi method order(Int $order) { $self->_order($order); }
164 my $order = $self->_order;
165 unless (defined $order && $order) {
166 my $tables = Tie::IxHash->new( map { $_->name => $_ } $self->schema->get_tables );
167 $order = $tables->Indices($self->name) || 0; $order++;
168 $self->_order($order);
173 method is_valid { return $self->get_columns ? 1 : undef }
175 before name($name?) { die "Can't use table name $name, table already exists" if $name && $self->schema->exists_table($name) && $name ne $self->name }
177 around remove_column(Column|Str $column, Int :$cascade = 0) {
178 my $name = is_Column($column) ? $column->name : $column;
179 die "Can't drop non-existant column " . $name unless $self->exists_column($name);
183 around remove_index(Index|Str $index) {
184 my $name = is_Index($index) ? $index->name : $index;
185 die "Can't drop non-existant index " . $name unless $self->exists_index($name);
189 around remove_constraint(Constraint|Str $constraint) {
190 my $name = is_Constraint($constraint) ? $constraint->name : $constraint;
191 die "Can't drop non-existant constraint " . $name unless $self->exists_constraint($name);
195 around BUILDARGS(ClassName $self: @args) {
196 my $args = $self->$orig(@args);
198 my $fields = delete $args->{fields};
200 $args->{columns}{$_} = SQL::Translator::Object::Column->new( name => $_ ) for @$fields;