in reply to Module for substituting complex criteria into hand-written SQL queries
Check out SQL::Abstract::More, which extends the parent class with more flexible functionality (and uses named params, Blessed Be!)
So you can do something like:
Output:my $sqla = SQL::Abstract::More->new; my $time = time; my $status = 'active'; my ( $sql, @bind ) = $sqla->select( # new users yesterday -columns => 'id, name', -from => 'users', -where => { status => $status, first_seen_time => { '>' => $time - 86400, '<=' => $time, }, }, );
As you can see this SQL builder often adds unnecessary parentheses; I don't worry about these since any decent RDBMS will optimize them away.$sql: 'SELECT id, name FROM users WHERE ( ( ( first_seen_time <= ? AND + first_seen_time > ? ) AND status = ? ) )'; \@bind: [ 1492373509, 1492287109, 'active' ];
update: added example and then example output
Hope this helps!
The way forward always starts with a minimal test.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Module for substituting complex criteria into hand-written SQL queries
by Dallaylaen (Chaplain) on Apr 16, 2017 at 20:32 UTC | |
by 1nickt (Canon) on Apr 16, 2017 at 21:14 UTC |
In Section
Seekers of Perl Wisdom