From: Justin Hunter Date: Thu, 21 Oct 2010 21:55:59 +0000 (-0700) Subject: return values when using native traits X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=00e289412e615f4822630691f17044a18c8ae127;p=dbsrgits%2FSQL-Translator-2.0-ish.git return values when using native traits --- diff --git a/lib/SQL/Translator/Object/Constraint.pm b/lib/SQL/Translator/Object/Constraint.pm index 505b2a8..4a76da8 100644 --- a/lib/SQL/Translator/Object/Constraint.pm +++ b/lib/SQL/Translator/Object/Constraint.pm @@ -88,7 +88,8 @@ class SQL::Translator::Object::Constraint extends SQL::Translator::Object { if ($self->has_type && $self->type eq 'PRIMARY KEY') { $column->is_primary_key(1); } - $self->$orig($column->name, $column) + $self->$orig($column->name, $column); + return $self->get_column($column->name); } multi method order(Int $order) { $self->_order($order); } diff --git a/lib/SQL/Translator/Object/Index.pm b/lib/SQL/Translator/Object/Index.pm index f5729a0..dbca752 100644 --- a/lib/SQL/Translator/Object/Index.pm +++ b/lib/SQL/Translator/Object/Index.pm @@ -40,7 +40,10 @@ class SQL::Translator::Object::Index extends SQL::Translator::Object { default => 'NORMAL', ); - around add_column(Column $column) { $self->$orig($column->name, $column) } + around add_column(Column $column) { + $self->$orig($column->name, $column); + return $self->get_column($column->name); + } method is_valid { $self->table && scalar $self->get_columns ? 1 : undef } } diff --git a/lib/SQL/Translator/Object/Schema.pm b/lib/SQL/Translator/Object/Schema.pm index 547308a..0789c72 100644 --- a/lib/SQL/Translator/Object/Schema.pm +++ b/lib/SQL/Translator/Object/Schema.pm @@ -18,85 +18,89 @@ class SQL::Translator::Object::Schema extends SQL::Translator::Object { ); has 'tables' => ( - traits => ['Hash'], is => 'rw', - isa => HashRef[Table], + isa => IxHash, #TableHash, handles => { - exists_table => 'exists', - table_ids => 'keys', - get_tables => 'values', - get_table => 'get', - add_table => 'set', - remove_table => 'delete', + exists_table => 'EXISTS', + table_ids => 'Keys', + get_tables => 'Values', + get_table => 'FETCH', + add_table => 'STORE', + remove_table => 'DELETE', }, - default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash }, + coerce => 1, + default => sub { Tie::IxHash->new() } ); has 'views' => ( - traits => ['Hash'], is => 'rw', - isa => HashRef[View], + isa => IxHash, #ViewHash, handles => { - exists_view => 'exists', - view_ids => 'keys', - get_views => 'values', - get_view => 'get', - add_view => 'set', - remove_view => 'delete', + exists_view => 'EXISTS', + view_ids => 'Keys', + get_views => 'Values', + get_view => 'FETCH', + add_view => 'STORE', + remove_view => 'DELETE', }, - default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash }, + coerce => 1, + default => sub { Tie::IxHash->new() } ); has 'procedures' => ( - traits => ['Hash'], is => 'rw', - isa => HashRef[Procedure], + isa => IxHash, #ProcedureHash, handles => { - exists_procedure => 'exists', - procedure_ids => 'keys', - get_procedures => 'values', - get_procedure => 'get', - add_procedure => 'set', - remove_procedure => 'delete', + exists_procedure => 'EXISTS', + procedure_ids => 'Keys', + get_procedures => 'Values', + get_procedure => 'FETCH', + add_procedure => 'STORE', + remove_procedure => 'DELETE', }, - default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash }, + coerce => 1, + default => sub { Tie::IxHash->new() } ); has 'triggers' => ( - traits => ['Hash'], is => 'rw', - isa => HashRef[Trigger], + isa => IxHash, #TriggerHash, handles => { - exists_trigger => 'exists', - trigger_ids => 'keys', - get_triggers => 'values', - get_trigger => 'get', - add_trigger => 'set', - remove_trigger => 'delete', + exists_trigger => 'EXISTS', + trigger_ids => 'Keys', + get_triggers => 'Values', + get_trigger => 'FETCH', + add_trigger => 'STORE', + remove_trigger => 'DELETE', }, - default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash }, + coerce => 1, + default => sub { Tie::IxHash->new() } ); 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); + return $self->get_table($table->name); } around add_view(View $view does coerce) { die "Can't use view name " . $view->name if $self->exists_view($view->name) || $view->name eq ''; $view->schema($self); - $self->$orig($view->name, $view) + $self->$orig($view->name, $view); + return $self->get_view($view->name); } around add_procedure(Procedure $procedure does coerce) { $procedure->schema($self); - $self->$orig($procedure->name, $procedure) + $self->$orig($procedure->name, $procedure); + return $self->get_procedure($procedure->name); } around add_trigger(Trigger $trigger does coerce) { $trigger->schema($self); - $self->$orig($trigger->name, $trigger);; + $self->$orig($trigger->name, $trigger); + return $self->get_trigger($trigger->name); } method is_valid { return $self->get_tables ? 1 : undef } diff --git a/lib/SQL/Translator/Object/Table.pm b/lib/SQL/Translator/Object/Table.pm index a377f29..773fc3b 100644 --- a/lib/SQL/Translator/Object/Table.pm +++ b/lib/SQL/Translator/Object/Table.pm @@ -102,7 +102,8 @@ class SQL::Translator::Object::Table extends SQL::Translator::Object is dirty { 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); - return $self->$orig($column->name, $column); + $self->$orig($column->name, $column); + return $self->get_column($column->name); } around add_constraint(Constraint $constraint does coerce) { @@ -116,7 +117,8 @@ class SQL::Translator::Object::Table extends SQL::Translator::Object is dirty { if ($constraint->has_type && $constraint->type eq 'PRIMARY KEY') { $self->get_column($_)->is_primary_key(1) for $constraint->column_ids; } - $self->$orig($name, $constraint) + $self->$orig($name, $constraint); + return $self->get_constraint($name); } around add_index(Index $index does coerce) { @@ -127,10 +129,14 @@ class SQL::Translator::Object::Table extends SQL::Translator::Object is dirty { $name = 'ANON' . $idx; } $index->table($self); - $self->$orig($name, $index) + $self->$orig($name, $index); + return $self->get_index($name); } - around add_sequence(Sequence $sequence does coerce) { $self->$orig($sequence->name, $sequence) } + around add_sequence(Sequence $sequence does coerce) { + $self->$orig($sequence->name, $sequence); + return $self->get_sequence($sequence->name); + } multi method primary_key { my $constraints = $self->constraints; diff --git a/lib/SQL/Translator/Object/Trigger.pm b/lib/SQL/Translator/Object/Trigger.pm index f0f0730..e972903 100644 --- a/lib/SQL/Translator/Object/Trigger.pm +++ b/lib/SQL/Translator/Object/Trigger.pm @@ -74,7 +74,10 @@ class SQL::Translator::Object::Trigger extends SQL::Translator::Object { is => 'rw', ); - around add_column(Column $column) { $self->$orig($column->name, $column) } + around add_column(Column $column) { + $self->$orig($column->name, $column); + return $self->get_column($column->name); + } multi method database_events(Str $database_event) { $self->add_database_event($database_event); $self->database_events } multi method database_events(ArrayRef $database_events) { $self->add_database_event($_) for @$database_events; $self->database_events }