in reply to Re: Looking for neater solution with dynamic query
in thread Looking for neater solution with dynamic query

Unless I missed something I think there are only 4 possiblities.

my $queries = ( "SELECT * FROM or_mod;" "SELECT * FROM or_mod WHERE upper(component) = upper(?)"; "SELECT * FROM or_mod WHERE upper(env) = upper(?)"; "SELECT * FROM or_mod WHERE upper(component) = upper(?) AND upper(env) = upper(?)"; ); my $sth; my @bind; if (defined($query{env}) &&(defined $query{component}) { $sth = $dbh->prepare($queries->[3]); $sth->execute($query{component}, $query{env}); } elsif ( defined ($query{env}) ) { $sth = $dbh->prepare($queries->[2]); $sth->execute($query{env}); } elsif ( defined ($query{component}) ) { $sth = $dbh->prepare($queries->[1]); $sth->execute($query{component}); } else { $sth = $dbh->prepare($queries->[0]); $sth->execute; }

I'm not entirely sure that is much better than dynamicaly generating them, or better at all for that matter. In fact I think the dynamic solution is better because it is dynamic. BTW newer MySQL caches all queries whether you ask or not, I don't know if that holds for bound queries as well. If it does then as long as you are binding, even if it seems lame for this single call, it will give you benefits in the long run.


___________
Eric Hodges

Replies are listed 'Best First'.
Re^3: Looking for neater solution with dynamic query
by pg (Canon) on Aug 24, 2005 at 22:37 UTC

    I had this thought... the problem is that this cannot be used as a generic solution, as one can easily run into queries that have more complex where conditions.

    However there is one benefit, the queries are now real prepared/binding queries. It is a taken and given thing. I decided to pass.