I've looked a lots of ways to generate sql dynamically. I happen to like SQL::Interp quite a bit. You simply give it a list consisting of SQL and data structure references, and it generates SQL.

Pure SQL, I find very readable. Now, the problem with pure SQL is that it is not as easy to manipulate as a data structure.

So, the question is how to make sql manipulatable? Here's one approach: put any particular thing you might want to edit on a line by itself. In my case, all of my queries were the same except for the data range I was selecting.

The SQL::Interp data structure for today's data looks like this:

sub callbacks_for_today { my $username = shift or confess 'must supply username'; [" SELECT * FROM call_schedule INNER JOIN person ON (user_people_id=people.id) WHERE DATE_FORMAT( `callback_time` , '%Y-%m-%d' ) = DATE_FORMAT( NOW( ) , +'%Y-%m-%d' ) -- date_clause AND staff_people_id = users.people_id AND username =", \$username, " ORDER BY callback_time DESC "] }
Now, notice how one line is tagged with the SQL comment:  -- date_clause That is the primary way I wish to mod the SQL, so I cobbled up something to do it:
sub swap_sql { my ($sql_fragment, $sub_hint, $sub_expr) = @_; $sub_hint = "^.+-- $sub_hint"; # return data structures as is ref $sql_fragment and return $sql_fragment; $sql_fragment =~ s/$sub_hint/$sub_expr/m ; $sql_fragment; }
And then the code to gen sql for tomorrow just requests a line-oriented edit on today's SQL:
sub callbacks_for_tomorrow { my $username = shift or confess 'must supply username'; my $today_sql = callbacks_for_today($username); my $tomorrow_date_str = "DATE_FORMAT( `callback_time` , '%Y-%m-%d' + ) = DATE_FORMAT( DATE_ADD(NOW( ), INTERVAL 1 DAY) , '%Y-%m-%d' ) -- +date_clause"; my @tomorrow_sql = map (swap_sql($_, 'date_clause', $tomorrow_date +_str), @$today_sql); \@tomorrow_sql; }
It would be interesting to see how an ORM would allow for slicing, dicing and decorating a SQL query.

In reply to line-oriented dynamic sql by metaperl

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.