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:
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 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 "] }
And then the code to gen sql for tomorrow just requests a line-oriented edit on today's SQL: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; }
It would be interesting to see how an ORM would allow for slicing, dicing and decorating a SQL query.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; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: line-oriented dynamic sql
by tilly (Archbishop) on Feb 13, 2009 at 16:23 UTC | |
by metaperl (Curate) on Feb 13, 2009 at 17:37 UTC | |
|
Re: line-oriented dynamic sql
by zby (Vicar) on Feb 13, 2009 at 16:42 UTC | |
by metaperl (Curate) on Feb 13, 2009 at 17:35 UTC |