From: Matt S Trout Date: Sun, 24 Nov 2013 01:59:15 +0000 (+0000) Subject: first tests for identifier remapping and a slight refactoring X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=1f005975593c9215598d7ec497d881f328dac890;p=dbsrgits%2FDBIx-Class.git first tests for identifier remapping and a slight refactoring --- diff --git a/lib/DBIx/Class/ResultSet/Role/DQMethods.pm b/lib/DBIx/Class/ResultSet/Role/DQMethods.pm index 77f9b90..b186910 100644 --- a/lib/DBIx/Class/ResultSet/Role/DQMethods.pm +++ b/lib/DBIx/Class/ResultSet/Role/DQMethods.pm @@ -29,14 +29,19 @@ sub _apply_dq_where { sub _remap_identifiers { my ($self, $dq) = @_; - my $map = {}; + my $map = { + '' => { + -alias => $self->current_source_alias, + -rsrc => $self->result_source, + } + }; my $attrs = $self->_resolved_attrs; foreach my $j ( @{$attrs->{from}}[1 .. $#{$attrs->{from}} ] ) { next unless $j->[0]{-alias}; next unless $j->[0]{-join_path}; my $p = $map; $p = $p->{$_} ||= {} for map { keys %$_ } @{$j->[0]{-join_path}}; - $p->{''} = $j->[0]{-alias}; + $p->{''} = $j->[0]; } my $seen_join = { %{$attrs->{seen_join}||{}} }; @@ -46,12 +51,9 @@ sub _remap_identifiers { return $_ unless is_Identifier; my @el = @{$_->{elements}}; my $last = pop @el; - unless (@el) { - return Identifier($attrs->{alias}, $last); - } my $p = $map; $p = $p->{$_} ||= {} for @el; - if (my $alias = $p->{''}) { + if (my $alias = $p->{''}{'-alias'}) { return Identifier($alias, $last); } my $need = my $j = {}; diff --git a/t/dq/remap.t b/t/dq/remap.t new file mode 100644 index 0000000..e8beb52 --- /dev/null +++ b/t/dq/remap.t @@ -0,0 +1,39 @@ +use strict; +use warnings; + +use Test::More; +use Test::Exception; +use Test::Warn; +use lib qw(t/lib); +use Data::Query::ExprDeclare; +use Data::Query::ExprHelpers; +use DBICTest; +use DBIC::SqlMakerTest; + +my $schema = DBICTest->init_schema(); + +$schema->source($_)->resultset_class('DBIx::Class::ResultSet::WithDQMethods') + for qw(CD Tag); + +my $cds = $schema->resultset('CD'); + +is_deeply( + [ $cds->_remap_identifiers(Identifier('name')) ], + [ Identifier('me', 'name'), [] ], + 'Remap column on me' +); + +is_deeply( + [ $cds->_remap_identifiers(Identifier('artist', 'name')) ], + [ Identifier('artist', 'name'), [ { artist => {} } ] ], + 'Remap column on rel' +); + +is_deeply( + [ $cds->search({}, { join => { single_track => { cd => 'artist' } } }) + ->_remap_identifiers(Identifier('artist', 'name')) ], + [ Identifier('artist_2', 'name'), [ { artist => {} } ] ], + 'Remap column on rel with re-alias' +); + +done_testing;