
SQL::Maker - Yet another SQL builder

use SQL::Maker;
my $builder = SQL::Maker->new(
driver => 'SQLite', # or your favorite driver
);
# SELECT
($sql, @binds) = $builder->select($table, \@fields, \%where, \%opt);
# INSERT
($sql, @binds) = $builder->insert($table, \%values);
# DELETE
($sql, @binds) = $builder->delete($table, \%where);
# UPDATE
($sql, @binds) = $builder->update($table, \%set, \%where);
($sql, @binds) = $builder->update($table, \@set, \%where);

SQL::Maker is yet another SQL builder class. It is based on DBIx::Skinny's SQL generator.

Create new instance of SQL::Maker.
Attributes are following:
Driver name is required. The driver type is needed to create SQL string.
This is the character that a table or column name will be quoted with.
Default: auto detect from $driver.
This is the character that separates a table and column name.
Default: '.'
This is the character that separates a part of statements.
Default: '\n'
Create new instance of SQL::Maker::Select from the settings from $builder.
This method returns instance of SQL::Maker::Select.
my ($sql, @binds) = $builder->select('user', ['*'], {name => 'john'}, {order_by => 'user_id DESC'});
# =>
# SELECT * FROM `user` WHERE (`name` = ?) ORDER BY user_id DESC
# ['john']
This method returns SQL string and bind variables for SELECT statement.
Table name for FROM clause in scalar or arrayref. You can specify the instance of SQL::Maker::Select for sub-query.
This is a list for retrieving fields from database.
where clause from hashref or arrayref via SQL::Maker::Condition, or SQL::Maker::Condition object.
This is a options for SELECT statement
This is a prefix for SELECT statement.
For example, you can provide the 'SELECT SQL_CALC_FOUND_ROWS '. It's useful for MySQL.
Default Value: 'SELECT '
This option makes 'LIMIT $n' clause.
This option makes 'OFFSET $n' clause.
This option makes ORDER BY clause
You can write it as following forms:
$builder->select(..., order_by => 'foo DESC, bar ASC');
$builder->select(..., order_by => ['foo DESC', 'bar ASC']);
$builder->select(..., order_by => {foo => 'DESC'});
$builder->select(..., order_by => [{foo => 'DESC'}, {bar => 'ASC'}]);
This option makes GROUP BY clause
You can write it as following forms:
$builder->select(..., group_by => 'foo DESC, bar ASC');
$builder->select(..., group_by => ['foo DESC', 'bar ASC']);
$builder->select(..., group_by => {foo => 'DESC'});
$builder->select(..., group_by => [{foo => 'DESC'}, {bar => 'ASC'}]);
This option makes HAVING clause
This option makes 'FOR UPDATE" clause.
This option makes 'JOIN' via SQL::Maker::Condition.
my ($sql, @binds) = $builder->insert(user => {name => 'john'});
# =>
# INSERT INTO `user` (`name`) VALUES (?)
# ['john']
Generate INSERT query.
my ($sql, @binds) = $builder->delete($table, \%where);
# =>
# DELETE FROM `user` WHERE (`name` = ?)
# ['john']
Generate DELETE query.
Table name in scalar.
where clause from hashref or arrayref via SQL::Maker::Condition, or SQL::Maker::Condition object.
Generate UPDATE query.
my ($sql, @binds) = $builder->update('user', ['name' => 'john', email => 'john@example.com'], {user_id => 3});
# =>
# 'UPDATE `user` SET `name` = ?, `email` = ? WHERE (`user_id` = ?)'
# ['john','john@example.com',3]
Table name in scalar.
Setting values.
where clause from hashref or arrayref via SQL::Maker::Condition, or SQL::Maker::Condition object.
Create new SQL::Maker::Condition object from $builder settings.
Where clause from hashref or arrayref via SQL::Maker::Condition, or SQL::Maker::Condition object.

SQL::Maker supports plugin system. Write the code like following.
package My::SQL::Maker;
use parent qw/SQL::Maker/;
__PACKAGE__->load_plugin('InsertMulti');

I need more extensible one.
So, this module contains SQL::Maker::Select, the extensible SELECT clause object.

Tokuhiro Matsuno <tokuhirom AAJKLFJEF@ GMAIL COM>

Whole code was taken from DBIx::Skinny by nekokak++.

Copyright (C) Tokuhiro Matsuno
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.