From: Justin Hunter Date: Tue, 6 Oct 2009 21:45:43 +0000 (-0700) Subject: move drop_* to Compat, change to around remove_* in lieu of multi method drop_* X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=bebe11d511e3f3b86b6965e380a6bc8bd38e73b7;p=dbsrgits%2FSQL-Translator-2.0-ish.git move drop_* to Compat, change to around remove_* in lieu of multi method drop_* --- diff --git a/lib/SQL/Translator/Object/Column.pm b/lib/SQL/Translator/Object/Column.pm index af973f2..b815228 100644 --- a/lib/SQL/Translator/Object/Column.pm +++ b/lib/SQL/Translator/Object/Column.pm @@ -21,7 +21,7 @@ class SQL::Translator::Object::Column extends SQL::Translator::Object is dirty { is => 'rw', isa => Str, required => 1, - trigger => sub { my ($self, $new, $old) = @_; if (defined $old) { $self->table->drop_column($old); $self->table->add_column($self) } } + trigger => sub { my ($self, $new, $old) = @_; if (defined $old) { $self->table->remove_column($old); $self->table->add_column($self) } } ); has 'data_type' => ( diff --git a/lib/SQL/Translator/Object/Compat.pm b/lib/SQL/Translator/Object/Compat.pm index 21be0ad..5db54c2 100644 --- a/lib/SQL/Translator/Object/Compat.pm +++ b/lib/SQL/Translator/Object/Compat.pm @@ -1,5 +1,8 @@ use MooseX::Declare; role SQL::Translator::Object::Compat { + use MooseX::Types qw(Any ArrayRef Int Str); + use SQL::Translator::Types qw(Column Constraint Index Table); + use MooseX::MultiMethods; multi method fields(Str $columns) { @@ -21,6 +24,13 @@ role SQL::Translator::Object::Compat { multi method fields(Any $) { $self->column_ids } + method add_field(Column $column does coerce) { $self->add_column($column) } + + method drop_table(Table|Str $table, Int :$cascade = 0) { $self->remove_table($table, cascade => $cascade) } + method drop_column(Column|Str $column, Int :$cascade = 0) { $self->remove_column($column, cascade => $cascade) } + method drop_index(Index|Str $index) { $self->remove_index($index) } + method drop_constraint(Constraint|Str $constraint) { $self->remove_constraint($constraint) } + method get_fields { $self->get_columns } method get_field { $self->get_column } method field_names { $self->column_ids } diff --git a/lib/SQL/Translator/Object/Schema.pm b/lib/SQL/Translator/Object/Schema.pm index 8fee3c7..5017bcf 100644 --- a/lib/SQL/Translator/Object/Schema.pm +++ b/lib/SQL/Translator/Object/Schema.pm @@ -73,27 +73,22 @@ class SQL::Translator::Object::Schema extends SQL::Translator::Object { default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash }, ); - around add_table(Table $table) { + around add_table(Table $table does coerce) { die "Can't use table name " . $table->name if $self->exists_table($table->name) || $table->name eq ''; $table->schema($self); $self->$orig($table->name, $table); } - around add_view(View $view) { $self->$orig($view->name, $view) } - around add_procedure(Procedure $procedure) { $self->$orig($procedure->name, $procedure) } - around add_trigger(Trigger $trigger) { $self->$orig($trigger->name, $trigger) } + around add_view(View $view does coerce) { $self->$orig($view->name, $view) } + around add_procedure(Procedure $procedure does coerce) { $self->$orig($procedure->name, $procedure) } + around add_trigger(Trigger $trigger does coerce) { $self->$orig($trigger->name, $trigger) } method is_valid { return $self->get_tables ? 1 : undef } - multi method drop_table(Table $table, Int :$cascade = 0) { - die "Can't drop non-existant table " . $table->name unless $self->exists_table($table->name); - $self->remove_table($table->name); - - } - - multi method drop_table(Str $table, Int :$cascade = 0) { - die "Can't drop non-existant table " . $table unless $self->exists_table($table); - $self->remove_table($table); + around remove_table(Table|Str $table, Int :$cascade = 0) { + my $name = is_Table($table) ? $table->name : $table; + die "Can't drop non-existant table " . $name unless $self->exists_table($name); + $self->$orig($name); } method order { } diff --git a/lib/SQL/Translator/Object/Table.pm b/lib/SQL/Translator/Object/Table.pm index f9bd68b..b6b78d6 100644 --- a/lib/SQL/Translator/Object/Table.pm +++ b/lib/SQL/Translator/Object/Table.pm @@ -57,6 +57,7 @@ class SQL::Translator::Object::Table extends SQL::Translator::Object is dirty { get_constraints => 'values', get_constraint => 'get', add_constraint => 'set', + remove_constraint => 'delete', }, default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash }, ); @@ -87,8 +88,6 @@ class SQL::Translator::Object::Table extends SQL::Translator::Object is dirty { default => 0 ); - method add_field(Column $column does coerce) { $self->add_column($column) } - around add_column(Column $column does coerce) { die "Can't use column name " . $column->name if $self->exists_column($column->name) || $column->name eq ''; $column->table($self); @@ -127,25 +126,21 @@ class SQL::Translator::Object::Table extends SQL::Translator::Object is dirty { before name($name?) { die "Can't use table name $name, table already exists" if $name && $self->schema->exists_table($name) && $name ne $self->name } - multi method drop_column(Column $column, Int :$cascade = 0) { - die "Can't drop non-existant column " . $column->name unless $self->exists_column($column->name); - $self->remove_column($column->name); - + around remove_column(Column|Str $column, Int :$cascade = 0) { + my $name = is_Column($column) ? $column->name : $column; + die "Can't drop non-existant column " . $name unless $self->exists_column($name); + $self->$orig($name); } - multi method drop_column(Str $column, Int :$cascade = 0) { - die "Can't drop non-existant column " . $column unless $self->exists_column($column); - $self->remove_column($column); - } - - multi method drop_index(Index $index) { - die "Can't drop non-existant index " . $index->name unless $self->exists_index($index->name); - $self->remove_index($index->name); - + around remove_index(Index|Str $index) { + my $name = is_Index($index) ? $index->name : $index; + die "Can't drop non-existant index " . $name unless $self->exists_index($name); + $self->$orig($name); } - multi method drop_index(Str $index) { - die "Can't drop non-existant index " . $index unless $self->exists_index($index); - $self->remove_index($index); + around remove_constraint(Constraint|Str $constraint) { + my $name = is_Constraint($constraint) ? $constraint->name : $constraint; + die "Can't drop non-existant constraint " . $name unless $self->exists_constraint($name); + $self->$orig($name); } }