Well that's not it!!

So lets see how I am going to move those annoying 'sub SQL' that I have in my DA::View and DA::Element packages out of them and into my LSDs.

So going blindly where I have gone before though the I might Role for the Element class in DA::LSD::SQL and see what I can come up with. So I duly added this


package
DA::LSD::SQL_D::Element;
use Moose::Role;
sub retrieve {
my $self = shift;
if ( $self->alias() ) {
return $self->name() . " AS " . $self->alias();
}
else {
return $self->name();
}
}

Now I renamed the SQL sub to retrieve just to keep it separate and then after playing about for a long while the best I could come up with was this in my DA retrieve sub

...
if ($conn eq 'DBH'){
apply_all_roles( $lsd, "DA::LSD::SQL_D");
foreach my $elememt (@{$self->elements()}) {
apply_all_roles( $elememt, "DA::LSD::SQL_D::Element");
}
}
...

Then all I needed to do was change my _execute sub in LSD::SQL a little like this

--$sql .= $delimiter . $element->sql();
--$sql .= $delimiter . $element->retrieve();

well that solves that problem as I get this on the test
ok 9 - SQL correct

Well did it really solve the problem?? If I was to call up one of the Element objects I would see that they now do the Role DA::LSD::SQL_D::Element so another quick test

my $elements = $address->elements();
ok (does_role($elements->[0],'DA::LSD::SQL_D::Element'),"Does Role DA::LSD::SQL_D::Element" );

I get this
ok 10 - Does Role DA::LSD::SQL_D::Element

and after the Mongo call I run the test aging with the same result
ok 13 - Mongo Query correct
ok 14 - Does Role DA::LSD::SQL_D::Element

So code bleed again not the situation I wanted. Now there would be only one sub changing (I hope) so it work but I now I am in the situation of having to iterate over all the instances in my DA attributes to apply my LSD roles each time I run a query.

Fine for now with just two attributes but I will have at least eight when things are done. Not the best way to programe efficient code.

So I though I might be able to try something funky like creating a lazy attribute on my DA::View like this


has retrieve=>(
isa =>'Str',
is =>'rw',
lazy=>1,
builder=> sub { my $self=shift; return $self->lsd->retrieve()},
);

and the in that builder sub refer back to the LSD but that was a non-starter as I would have to pass to the DA::View some reference to the LSD though iteration and then take it out when done, which is just a little less problematic.

I may have gotten rid of the role and code bleed but still have all that iteration to do but now, twice as much.

So now I have gone from a muddled state into a very muddled state. Oh well there is always tomorrow.


lumpy-1198271dac_2875.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