MidLifeXis was so kind as to recommend my module DBIx::PreQL. It's aim is to simplify the horrendous task of maintaining dynamically generated SQL.
Here's one way to handle the your particular case.
my $preql = <<'PREQL'
* SELECT
* Name
* FROM
CUST Customer
SALE Sales
* WHERE
CUST CustID = ?customer_id?
SALES SalesID = ?sales_id?
PREQL
my ($query, @params) = DBIx::PreQL->build_query(
query => $preql,
data => {
sales_id => $salesid,
customer_id => $custid,
},
wanted => $x > 10 ? 'CUST' : 'SALES'
);
my $got = $db->selectall_hashref( $query, @params );
Why do all this stuff?
- You get the placeholders you wanted for your dynamic query.
- This is the least sucky way I've found to build dynamic SQL. The code I maintain has tons of dynamic SQL and I spent years experimenting before I hit upon this approach.
- You will get a good error message if you put yourself in a condition where you are expecting a customer id but one isn't available.
In fact, at $WORK, we have found it worthwhile to convert static queries to PreQL. It has helped with debugging and maintenance. Because all the placeholders are labeled, they are easier to understand. PreQL also provides useful error messages that identify missing, but expected placeholder values.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.