I've written a bit of dynamic SQL many different ways... I generally group the statements by the FROM clause. In this case, the format of your queries look pretty similar. I might try Something Like(tm) this:

my ($sr_from, $sr_where); if( $ref_sid ) { $sr_from = ", Search_Results SR "; $sr_where = "AND P.id = SR.page_id AND SR.search_id = ? "; push @query_args, $ref_sid; } my $mquery =<<"SQL" INSERT INTO Search_Results ( search_id, page_id ) SELECT ?, P.id FROM Pages P, Documents D, Dirs DI $sr_from WHERE MATCH ( P.pgdata ) AGAINST ( ? IN BOOLEAN MODE ) AND P.doc_id = D.id AND D.dir_id = DI.id $sr_where SQL
I'm unsure if the bind parameter '?' is buying you anything in this query as each statement prepare() would have to see what is being SELECTed. Either way, be careful not let the interpolated variables be changed through the program interface.

All said, I'm thinking that SQL::Builder does look very interesting.


In reply to Re: Architecture of Constructing Complex SQL by beachbum
in thread Architecture of Constructing Complex SQL by friedo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.