From: Justin Hunter Date: Sat, 3 Oct 2009 04:48:45 +0000 (-0700) Subject: move backcompat into Compat.pm and apply the role to Object.pm X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=035b85031c5d6fdce4b2ea18c243707b84b0d869;p=dbsrgits%2FSQL-Translator-2.0-ish.git move backcompat into Compat.pm and apply the role to Object.pm --- diff --git a/lib/SQL/Translator/Object.pm b/lib/SQL/Translator/Object.pm index e1ba3ac..fcab281 100644 --- a/lib/SQL/Translator/Object.pm +++ b/lib/SQL/Translator/Object.pm @@ -1,5 +1,5 @@ use MooseX::Declare; -class SQL::Translator::Object { +class SQL::Translator::Object with SQL::Translator::Object::Compat { use Tie::IxHash; use MooseX::MultiMethods; use MooseX::Types::Moose qw(Any ArrayRef HashRef Str); diff --git a/lib/SQL/Translator/Object/Compat.pm b/lib/SQL/Translator/Object/Compat.pm new file mode 100644 index 0000000..21be0ad --- /dev/null +++ b/lib/SQL/Translator/Object/Compat.pm @@ -0,0 +1,28 @@ +use MooseX::Declare; +role SQL::Translator::Object::Compat { + use MooseX::MultiMethods; + + multi method fields(Str $columns) { + my @columns = split /\s*,\s*/, $columns; + for my $column (@columns) { + die "Column '$column' does not exist!" unless $self->table->exists_column($column); + $self->add_column($self->table->get_column($column)); + } + $self->column_ids; + } + + multi method fields(ArrayRef $columns) { + for my $column (@$columns) { + die "Column '$column' does not exist!" unless $self->table->exists_column($column); + $self->add_column($self->table->get_column($column)); + } + $self->column_ids; + } + + multi method fields(Any $) { $self->column_ids } + + method get_fields { $self->get_columns } + method get_field { $self->get_column } + method field_names { $self->column_ids } + method reference_fields { $self->reference_columns } +} diff --git a/lib/SQL/Translator/Object/Constraint.pm b/lib/SQL/Translator/Object/Constraint.pm index e79acf5..ad6fd6d 100644 --- a/lib/SQL/Translator/Object/Constraint.pm +++ b/lib/SQL/Translator/Object/Constraint.pm @@ -1,6 +1,7 @@ use MooseX::Declare; class SQL::Translator::Object::Constraint extends SQL::Translator::Object { use MooseX::Types::Moose qw(ArrayRef Bool HashRef Maybe Str Undef); + use MooseX::MultiMethods; use SQL::Translator::Types qw(Column Table); has 'table' => ( @@ -25,10 +26,6 @@ class SQL::Translator::Object::Constraint extends SQL::Translator::Object { get_columns => 'values', get_column => 'get', add_column => 'set', - - ## compat - get_fields => 'values', - fields => 'keys', }, default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash }, ); diff --git a/lib/SQL/Translator/Object/Index.pm b/lib/SQL/Translator/Object/Index.pm index 2841caf..828ee21 100644 --- a/lib/SQL/Translator/Object/Index.pm +++ b/lib/SQL/Translator/Object/Index.pm @@ -26,9 +26,6 @@ class SQL::Translator::Object::Index extends SQL::Translator::Object { get_columns => 'values', get_column => 'get', add_column => 'set', - - ## compat - get_fields => 'values', }, default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash }, ); @@ -42,19 +39,5 @@ class SQL::Translator::Object::Index extends SQL::Translator::Object { around add_column(Column $column) { $self->$orig($column->name, $column) } - multi method fields(Str $columns) { - for (split /,/, $columns) { die "Column $_ does not exist!" unless $self->table->exists_column($_) } - $self->add_column($self->table->get_column($_)) for split /,/, $columns; - $self->column_ids; - } - - multi method fields(@columns) { - for (@columns) { die "Column $_ does not exist!" unless $self->table->exists_column($_) } - $self->add_column($self->table->get_column($_)) for @columns; - $self->column_ids; - } - - multi method fields(Any $) { $self->column_ids } - method is_valid { $self->table && scalar $self->get_columns ? 1 : undef } } diff --git a/lib/SQL/Translator/Object/Table.pm b/lib/SQL/Translator/Object/Table.pm index 46a4219..ebbf320 100644 --- a/lib/SQL/Translator/Object/Table.pm +++ b/lib/SQL/Translator/Object/Table.pm @@ -28,11 +28,6 @@ class SQL::Translator::Object::Table extends SQL::Translator::Object is dirty { get_column => 'get', add_column => 'set', remove_column => 'delete', - - ## compat - get_fields => 'values', - get_field => 'get', - fields => 'keys', }, default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash }, ); diff --git a/lib/SQL/Translator/Object/Trigger.pm b/lib/SQL/Translator/Object/Trigger.pm index ddd95ab..cf85168 100644 --- a/lib/SQL/Translator/Object/Trigger.pm +++ b/lib/SQL/Translator/Object/Trigger.pm @@ -19,10 +19,6 @@ class SQL::Translator::Object::Trigger extends SQL::Translator::Object { get_columns => 'values', get_column => 'get', add_column => 'set', - - ## compat - get_fields => 'values', - fields => 'keys', }, default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash }, ); diff --git a/lib/SQL/Translator/Object/View.pm b/lib/SQL/Translator/Object/View.pm index a185421..e800cbb 100644 --- a/lib/SQL/Translator/Object/View.pm +++ b/lib/SQL/Translator/Object/View.pm @@ -19,10 +19,6 @@ class SQL::Translator::Object::View extends SQL::Translator::Object { get_columns => 'values', get_column => 'get', add_column => 'set', - - ## compat - get_fields => 'values', - fields => 'keys', }, default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash }, );