in reply to What Do Monks Think of SQL Query Builders?
This is likely to be a personal preference thing. As Corion says, some people like to write the SQL directly; others prefer a tool. Personally I use SQL::Abstract::More most of the time. I like it more than SQL::Abstract, upon which it is based, because it allows use of named parameters (I hate positional parameters), and also supports more complex clauses such as joins, etc.
Yes, in a way you will be learning one syntax instead of another. But after becoming familiar with it I think you'll find it intuitive and effective. One thing I really like is not having to think at all about quoting values, or even about placeholders. You write your values right into the call to SQL::Abstract::More, and get back the SQL and a list of bind values that will be plugged into the placeholders by the DBI. Here's an example that's based on a recent script I was working on:
Output:use SQL::Abstract::More; my $sql_builder = SQL::Abstract::More->new; my ( $sql, @bind ) = $sql_builder->select( -columns => "id, site_id", -from => "jobs", -where => { id => [ 10 .. 20 ], first_seen_time => [ "null", { "<" => time - 86400, ">" => time - 86400 * 8 }, ], }, ); print " SQL: $sql\n\n"; print "BIND: @bind\n";' ...
As you can see the builder adds lots of parentheses, some probably not necessary, but your RDBMS will optimize the query in any case.SQL: SELECT id, site_id FROM jobs WHERE ( ( ( first_seen_time = ? OR ( + first_seen_time < ? AND first_seen_time > ? ) ) AND ( id = ? OR id = + ? OR id = ? OR id = ? OR id = ? OR id = ? OR id = ? OR id = ? OR id += ? OR id = ? OR id = ? ) ) ) BIND: null 1486484465 1485879665 10 11 12 13 14 15 16 17 18 19 20
Hope this helps!
|
|---|