Moose Gets Back up

Go back again day here in the Moose-Pen

Yesterday I was stymied by not being able to select out just one 'element/field' from a Database::Accessor before I sent it in as part of a condition. I did have something in place an option on my retrieve called 'only_elements'. Time to open up the API once more and make this an attribute that I can set on the Database::Accessor but I do not need to pass down to the DAD as I plan to do the filtering in the same sub 'get_dad_elements'

To start I will need a test and in this case I will adapt the test I already have for 'only_elements' in the '62_crud_options.t' test case;


...
--$da->retrieve($data,{only_elements=>{first_name=>1,user_id=>1}});
++$da->only_elements({first_name=>1,user_id=>1});
my $dad = $da->result->error(); #note to others this is a kludge for testing
ok($dad->element_count == 2,"Only two elements");
ok($dad->elements->[0]->name() eq 'first_name',"First name in correct place");
ok($dad->elements->[1]->name() eq 'user_id',"user_id in correct place");

Now to add that into Accessor.pm with this patch;

...
"Returns an ArrayRef of HasRefs the DADs that are installed. The keys in the HashRef are 'DAD=>DAD name,class=>the DB class,ver=>the DAD Version'"
);

has retrieve_elements => (
isa => 'HashRef',
traits => ['Hash','MooseX::MetaDescription::Meta::Trait'],
is => 'rw',
description => { not_in_DAD => 1 },
default => sub { {} },
);

has no_create => (

private_method get_dad_elements => sub {
my $self = shift;
my ($action,$opt) = @_;

$self->_identity_index(-1);
my @allowed;
for (my $index=0; $index < scalar(@{$self->elements()} ); $index++) {
my $element = $self->elements()->[$index];
if (ref($element) eq 'Database::Accessor::Param'){
push(@allowed,$element)
if ( $action eq Database::Accessor::Constants::RETRIEVE);
next;
}
if (ref($element) eq 'Database::Accessor::Param'){
push(@allowed,$element)
if ( $action eq Database::Accessor::Constants::RETRIEVE);
next;
}
next
-- if (exists($opt->{only_elements})
-- and !exists($opt->{only_elements}->{$element->name}));
++ if (!$self->only_elements->is_empty()
++ and !$self->only_elements->exists($element->name));


and that should be it. You will note that I am using Moose::Native::Traits again to make my code a little easier to read in my opinion. And the test run gets;

ok 3 - Caught only_elements must be correct type
not ok 4 - Only two elements
ok 5 - First name in correct place

hmm so something not right in my logic;

After a Dump here and there I found it was a bug in my test I forgot to retrieve on the da so I added that in


$da->only_elements({first_name=>1,user_id=>1});
$da->retrieve($data);
my $dad = $da->result->error(); #note to others this is a kludge for testing

and the result was

ok 1 - No param works
ok 2 - Caught non hash-ref for param
ok 3 - Caught only_elements must be correct type
ok 4 - Only two elements
ok 5 - First name in correct place
ok 6 - user_id in correct place

So how about fixing up the 'xt\32_where_operators.t' test next;

my $address_da = $address->da();
++$address_da->only_elements({id=>1});
my $people = $user_db->people_data();

and that should do it and a run of the test gives me;


not ok 4 - All 4 users retrieved with in
# Failed test 'All 4 users retrieved with in '
# at 32_where_operators.t line 98.
# Compared $data->[3][1]
# got : 'Diego'
# expect : 'James'

forgot about that data change again. So fixed that yet again and I get

ok 1 - All 4 users retrieved correctly with between
ok 2 - All 4 users retrieved correctly with is null
ok 3 - All 4 users retrieved with in
ok 4 - All 4 users retrieved with in using a DA

Not bad. I guess more XT tests tomorrow.

Swedish-moose-in-tree3.jpg

Leave a comment

About byterock

user-pic Long time Perl guy, a few CPAN mods allot of work on DBD::Oracle and a few YAPC presentations