Big Moosse Catches up
Its condiment day here in the Moose-Pen
Now it is time to play catch-up with my Database::Accessor and make the same changes for 'links' that I did for 'conditions'.
First off I could create a new Role as both share the same basic attribute again but I think I will not do that and instead do this;
package
Database::Accessor::Link;
use Moose;
extends 'Database::Accessor::Base';
++ has predicates => (
++ is => 'rw',
++ isa => 'Predicate',
++ coerce => 1,
-- alias => 'conditions',
++ );
without the alias there.
Like yesterday's post I will start with an existing test case '35_links.t' and one my first run I had some expected coercion problems
Attribute (predicates) does not pass the type constraint because:
Validation failed for 'Predicate' with value ARRAY(0x41b967c) (not isa
Database::Accessor::Predicate) at
I went though the code and solved most of the new problems mostly by changing the test code; for eaxmple to get most of '35_links.t' to work all I needed to do was change links->predicates from an array ref to a singleton call
-- predicates => [
++ predicates =>
{
left => {
name => 'country_id',
view => 'People'
},
After a little playing about I realized that I was going down the wrong path. The above would work fine for SQL like this
FROM test JOIN new on test.id=new.test_id and new.test≶100 and
new.test>100;
but it would not work for an SQL like this
FROM test JOIN new on test.id=new.test_id and new.test≶100 and
(new.test>50 and new.pay>100);
That is not a predicate but a set of predicate or as I like to call the 'conditions'. So out with the abve change and in with this one
has conditions => (
isa => 'ArrayRefofConditions',
is => 'ro',
traits => ['Array'],
handles => { condition_count => 'count', },
default => sub { [] },
);
and now I have to change anyplace where I have 'predicates' when referring to a 'Link' and that took a few changes in a few tests. Finally I had to change the Test::Utils a little to account for the change and in about 30 mins of playing with the code I got both '35_links.t' and '45_dynamic_links.t' to pass.
Now for kicks I jumped ahead a little and gave '40_joins.t' from Driver::DBI a go just to see how much damage I did, and got this;
Can't locate object method "predicates" via package "Database::Accessor::Link" at
GitHub\database-accessor-driver-dbi\lib/Database/Accessor/Driver/DBI.pm line 196.
The first change was to the test where I did this change;
links =>{type=>'LEFT',
to =>{name=>'address'},
-- predicates=>[{left=>{name=>'id'},
++ conditions=>[{left=>{name=>'id'},
right=>{name=>'user_id',
view=>'address'}
}]},
to the input hash ref and then I simply had to change the '_join_clause' sub like this
my $clause = join(" "
,$join->type,
,Database::Accessor::Driver::DBI::SQL::JOIN
,$self->_table_sql($join->to)
,Database::Accessor::Driver::DBI::SQL::ON
, $self->_predicate_clause(
Database::Accessor::Driver::DBI::SQL::JOIN,
-- $join->predicates() )
++ $join->conditions() )
);
and in the '_predicate_clause sub this small change
foreach my $condition ( @{$conditions} ) {
if (ref($condition) eq 'Database::Accessor::Condition'){
-- foreach my $predicate ( @{$condition->predicates} } ) {
++ foreach my $predicate ( $condition->predicates } ) {
and I got my tests to run but I still did had a fail as there was still an issue with the view on one of the elements/fields. All in all I did not bugger up too many things.
Anyway back to the Database::Accessor test suite for tomorrow's post.
Leave a comment