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, \%values); # 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.
SQL::Maker creates where clause from this hashref via SQL::Maker::Condition.
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 HAVING clause
This option makes 'FOR UPDATE" clause.
my ($sql, @binds) = $builder->insert(user => {name => 'john'}); # => # INSERT INTO `user` (`name`) VALUES (?) # ['john']
Generate INSERT query.
Table name in scalar.
This is a values for INSERT statement.
my ($sql, @binds) = $builder->delete($table, \%where); # => # DELETE FROM `user` WHERE (`name` = ?) # ['john']
Generate DELETE query.
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]
Setting values.
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>
SQL::Abstract
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.
To install SQL::Maker, copy and paste the appropriate command in to your terminal.
cpanm
cpanm SQL::Maker
CPAN shell
perl -MCPAN -e shell install SQL::Maker
For more information on module installation, please visit the detailed CPAN module installation guide.