use SQL::Query::Builder;
use DBI;
my $dbh = DBI->connect(...);
# start to build out our query object
my $query = SELECT->FROM('table')->WHERE(col => [1..3]);
# because $query is an object we can still modify it
$query->LIMIT(2);
# so whats the current state of things?
my ($query, $bind_vars) = $query->build;
# $query is a string: SELECT * FROM table WHERE (`col` = ? OR `col` = ? OR `col` = ?) LIMIT 2
# $bind_vars is an arrayref [1,2,3]
# then when we are ready we pull the trigger
my $data = $dbh->selectall_arrayref($query->dbi(Slice=>{}));
All the exported functions are wrapers around object constructors.
Builds a SQL::Query::Builder::Query::Select object for you.
my $query = SELECT; #most basic case
Due to SQL::Query::Builder::Query::Select not needing anything at new, any items passed to SELECT will be passed to it's WHAT block.
SELECT('something') same as SELECT->WHAT('something')
If nothing is passed, WHAT defaults to '*'.
Build SQL::Query::Builder::Query::Part::JOIN setting 'type' accordingly. JOIN is only useful while defining the FROM block of the query. There are two context.
# 'USING' notation is a scalar value
SELECT->FROM(table, JOIN table2 => col)
# SELECT * FROM table JOIN table2 USING (col)
# 'ON' notation is a hashref as value
SELECT->FROM(table, JOIN table2 -> {'table.id' => 'table2.t1_id'})
# SELECT * FROM table JOIN table2 ON (`table`.`id` = `table2`.`t1_id`)
Builds a SQL::Query::Builder::Query::Part::Set, setting 'type' to 'AND' or 'OR'. There are two expected context, the following two notations resolve to the same.
AND{ col => 12, col => 13} # (`col` = ? AND `col` = ?)[12,13]
col => AND[12,13] # (`col` = ? AND `col` = ?)[12,13]
Same for OR:
OR{ col => 12, col => 13} # (`col` = ? OR `col` = ?)[12,13]
col => OR[12,13] # (`col` = ? OR `col` = ?)[12,13]
Context is derived based on the ref type that wraps the data.
Builds a SQL::Query::Builder::Query::Part::Set::IN object, unline AND/OR, IN only takes arrayrefs:
col => IN[12,13] # `col` IN (?,?) [12,13]
Builds a SQL::Query::Builder::Query::Part::OpValuePair object, setting 'type' to the correct op based on what was called.
col => GT 12 # `col` > ? [12]
col => GTE 12 # `col` >= ? [12]
col => LT 12 # `col` < ? [12]
col => LTE 12 # `col` <= ? [12]
col => EQ 12 # `col` = ? [12]
Currently NOT is not implimented, though it is exported.