I build a query depending on certain conditions - such as type of operation, or the presence of a certain value - by adding fields to a INSERT statement. But then I have to branch for different DBI executes with different lists of parameters, like this:
if ($x) {$extraFields .= ' , X'; $extraValues= ',? '}
if ($y) {$extraFields .= ' , Y, Z'; $extraValues= ',?, ? '}
my $theBasicQuery = "INSERT INTO sometable (A, B, $extraFields) VALUES (?, ? $extraValues)";
$sth = $dbh->prepare($theBasicQuery) or error
# but I dont want to have to do this if for execute
if ($x) {$sth->execute(1,2,99);}
if ($y) {$sth->execute(1,2,88, 77);}
I would prefer to do something like this:
{$sth->execute($anArrayWithDifferentParams);}
Is this possible? Or is there another way to do something similar?
jointhe array when you form your query string?