SQL::Format - Yet another yet another SQL builder
use SQL::Format; my ($stmt, @bind) = sqlf 'SELECT %c FROM %t WHERE %w' => ( [qw/bar baz/], # %c 'foo', # %t { hoge => 'fuga', piyo => [qw/100 200 300/], }, # %w ); # $stmt: SELECT `bar`, `baz` FROM `foo` WHERE (`hoge` = ?) AND (`piyo` IN (?, ?, ?)) # @bind: ('fuga', 100, 200, 300); ($stmt, @bind) = sqlf 'SELECT %c FROM %t WHERE %w %o' => ( '*', # %c 'foo', # %t { hoge => 'fuga' }, # w { order_by => { bar => 'DESC' }, limit => 100, offset => 10, }, # %o ); # $stmt: SELECT * FROM `foo` WHERE (`hoge` = ?) ORDER BY `bar` DESC LIMIT 100 OFFSET 10 # @bind: (`fuga`) ($stmt, @bind) = sqlf 'UPDATE %t SET %s' => ( foo => { bar => 'baz', 'hoge => 'fuga' }, ); # $stmt: UPDATE `foo` SET `bar` = ?, `hoge` = ? # @bind: ('baz', 'fuga') my $sqlf = SQL::Format->new( quote_char => '', # do not quote limit_dialect => 'LimitXY', # mysql style limit-offset ); ($stmt, @bind) = $sqlf->select(foo => [qw/bar baz/], { hoge => 'fuga', }, { order_by => 'bar', limit => 100, offset => 10, }); # $stmt: SELECT bar, baz FROM foo WHERE (hoge = ?) ORDER BY bar LIMIT 10, 100 # @bind: ('fuga') ($stmt, @bind) = $sqlf->insert(foo => { bar => 'baz', hoge => 'fuga' }); # $stmt: INSERT INTO foo (bar, hoge) VALUES (?, ?) # @bind: ('baz', 'fuga') ($stmt, @bind) = $sqlf->update(foo => { bar => 'xxx' }, { hoge => 'fuga' }); # $stmt: UPDATE foo SET bar = ? WHERE hoge = ? # @bind: ('xxx', 'fuga') ($stmt, @bind) = $sqlf->delete(foo => { hoge => 'fuga' }); # $stmt: DELETE FROM foo WHERE (hoge = ?) # @bind: ('fuga')
SQL::Format is a easy to SQL query building library.
THIS MODULE IS ALPHA LEVEL INTERFACE!!
Generate SQL from formatted output conversion.
my ($stmt, @bind) = sqlf 'SELECT %c FROM %t WHERE %w' => ( [qw/bar baz/], # %c 'foo', # %t { hoge => 'fuga', piyo => [100, 200, 300], }, # %w ); # $stmt: SELECT `foo` FROM `bar`, `baz WHERE (`hoge` = ?) AND (`piyo` IN (?, ?, ?)) # @bind: ('fuga', 100, 200, 300)
Currently implemented formatters are:
This format is a table name.
($stmt, @bind) = sqlf '%t', 'table_name'; # $stmt => `table_name` ($stmt, @bind) = sqlf '%t', [qw/tableA tableB/]; # $stmt => `tableA`, `tableB` ($stmt, @bind) = sqlf '%t', { tableA => 't1' }; # $stmt => `tableA` `t1` ($stmt, @bind) = sqlf '%t', { tableA => { index => { type => 'force', keys => [qw/key1 key2/] }, alias => 't1', }; # $stmt: `tableA` `t1` FORCE INDEX (`key1`, `key2`)
This format is a column name.
($stmt, @bind) = sqlf '%c', 'column_name'; # $stmt => `column_name` ($stmt, @bind) = sqlf '%c', [qw/colA colB/]; # $stmt => `colA`, `colB` ($stmt, @bind) = sqlf '%c', '*'; # $stmt => * ($stmt, @bind) = sqlf '%c', [\'COUNT(*)', colC]; # $stmt => COUNT(*), `colC`
This format is a where clause.
($stmt, @bind) = sqlf '%w', { foo => 'bar' }; # $stmt: (`foo` = ?) # @bind: ("bar") ($stmt, @bind) = sqlf '%w', { foo => 'bar', baz => [qw/100 200 300/], }; # $stmt: (`baz` IN (?, ?, ?) AND (`foo` = ?) # @bind: (100, 200, 300, 'bar')
This format is a options. Currently specified are:
This option makes LIMIT $n clause.
LIMIT $n
($stmt, @bind) = sqlf '%o', { limit => 100 }; # $stmt => LIMIT 100
This option makes OFFSET $n clause. You must be specified both limit option.
OFFSET $n
($stmt, @bind) = sqlf '%o', { limit => 100, offset => 20 }; # $stmt => LIMIT 100 OFFSET 20
You can change limit dialects from $SQL::Format::LIMIT_DIALECT.
$SQL::Format::LIMIT_DIALECT
This option makes ORDER BY clause.
ORDER BY
($stmt, @bind) = sqlf '%o', { order_by => 'foo' }; # $stmt => ORDER BY `foo` ($stmt, @bind) = sqlf '%o', { order_by => { foo => 'DESC' } }; # $stmt => ORDER BY `foo` DESC ($stmt, @bind) = sqlf '%o', { order_by => ['foo', { -asc => 'bar' } ] }; # $stmt => ORDER BY `foo`, `bar` ASC
This option makes GROUP BY clause. Argument value some as order_by option.
GROUP BY
order_by
($stmt, @bind) = sqlf '%o', { group_by => { foo => 'DESC' } }; # $stmt => GROUP BY `foo` DESC
This option makes HAVING clause. Argument value some as where clause.
HAVING
where
($stmt, @bind) = sqlf '%o', { having => { foo => 'bar' } }; # $stmt: HAVING (`foo` = ?) # @bind: ('bar')
This format is join clause.
($stmt, @bind) = sqlf '%j', { table => 'bar', condition => 'foo.id = bar.id' }; # $stmt: INNER JOIN `bar` ON (foo.id = bar.id) ($stmt, @bind) = sqlf '%j', { type => 'left', table => { bar => 'b' }, condition => { 'f.id' => 'b.id', 'f.updated_at' => \['UNIX_TIMESTAMP()', '2012-12-12'] 'f.created_at' => { '>' => 'b.created_at' }, }, }; # $stmt: LEFT JOIN `bar` `b` ON (`f`.`id` = `b.id`)
This format is set clause.
($stmt, @bind) = sqlf '%s', { bar => 'baz' }; # $stmt: `bar` = ? # @bind: ('baz') ($stmt, @bind) = sqlf '%s', { bar => 'baz', 'hoge' => \'UNIX_TIMESTAMP()' }; # $stmt: `bar` = ?, `hoge` = UNIX_TIMESTAMP() # @bind: ('baz') ($stmt, @bind) = sqlf '%s', { bar => 'baz', hoge => \['CONCAT(?, ?)', 'ya', 'ppo'], }; # $stmt: `bar` = ?, `hoge` = CONCAT(?, ?) # @bind: ('baz', 'ya', 'ppo')
For more examples, see also SQL::Format::Spec.
You can change the behavior by changing the global variable.
This is a quote character for table or column name.
Default value is "`".
"`"
This is a separate character for table or column name.
Default value is ".".
"."
This is a delimiter for between columns.
Default value is ", ".
", "
This is a types for dialects of limit-offset.
You can choose are:
LimitOffset # LIMIT 100 OFFSET 20 (SQLite / PostgreSQL / MySQL) LimitXY # LIMIT 20, 100 (MySQL / SQLite) LimitYX # LIMIT 100, 20 (other)
Default value is LimitOffset".
LimitOffset"
Create a new instance of SQL::Format.
SQL::Format
my $sqlf = SQL::Format->new( quote_char => '', limit_dialect => 'LimitXY', );
%options specify are:
%options
Default value is $SQL::Format::QUOTE_CHAR.
$SQL::Format::QUOTE_CHAR
Default value is $SQL::Format::NAME_SEP.
$SQL::Format::NAME_SEP
Default value is $SQL::Format::DELIMITER.
$SQL::Format::DELIMITER
Default value is $SQL::Format::LIMIT_DIALECT.
This method same as sqlf function.
sqlf
my ($stmt, @bind) = $self->format('SELECT %c FROM %t WHERE %w', [qw/bar baz/], 'foo', { hoge => 'fuga' }, ); # $stmt: SELECT `bar`, `baz` FROM ` foo` WHERE (`hoge` = ?) # @bind: ('fuga')
This method returns SQL string and bind parameters for SELECT statement.
SELECT
my ($stmt, @bind) = $sqlf->select(foo => [qw/bar baz/], { hoge => 'fuga', piyo => [100, 200, 300], }); # $stmt: SELECT `foo` FROM `bar`, `baz` WHERE (`hoge` = ?) AND (`piyo` IN (?, ?, ?)) # @bind: ('fuga', 100, 200, 300)
Argument details are:
Same as %t format.
%t
Same as %c format.
%c
Same as %w format.
%w
This is prefix for SELECT statement.
my ($stmt, @bind) = $sqlf->select(foo => '*', { bar => 'baz' }, { prefix => 'SELECT SQL_CALC_FOUND_ROWS' }); # $stmt: SELECT SQL_CALC_FOUND_ROWS * FROM `foo` WHERE (`bar` = ?) # @bind: ('baz')
Default value is SELECT.
Additional value for after the SELECT statement.
my ($stmt, @bind) = $sqlf->select(foo => '*', { bar => 'baz' }, { suffix => 'FOR UPDATE' }); # $stmt: SELECT * FROM `foo` WHERE (bar = ?) FOR UPDATE # @bind: ('baz')
Default value is ''
''
See also %o format.
%o
This method returns SQL string and bind parameters for INSERT statement.
INSERT
my ($stmt, @bind) = $sqlf->insert(foo => { bar => 'baz', hoge => 'fuga' }); # $stmt: INSERT INTO `foo` (`bar`, `hoge`) VALUES (?, ?) # @bind: ('baz', 'fuga') my ($stmt, @bind) = $sqlf->insert(foo => [ hoge => \'NOW()', fuga => \['UNIX_TIMESTAMP()', '2012-12-12 12:12:12'], ]); # $stmt: INSERT INTO `foo` (`hoge`, `fuga`) VALUES (NOW(), UNIX_TIMESTAMP(?)) # @bind: ('2012-12-12 12:12:12')
This is a table name for target of INSERT.
This is a VALUES clause INSERT statement.
Currently supported types are:
# \%values case { foo => 'bar' } { foo => \'NOW()' } { foo => \['UNIX_TIMESTAMP()', '2012-12-12 12:12:12'] } # \@values case [ foo => 'bar' ] [ foo => \'NOW()' ] [ foo => \['UNIX_TIMESTAMP()', '2012-12-12 12:12:12'] ]
This is a prefix for INSERT statement.
my ($stmt, @bind) = $sqlf->insert(foo => { bar => baz }, { prefix => 'INSERT IGNORE' }); # $stmt: INSERT IGNORE INTO `foo` (`bar`) VALUES (?) # @bind: ('baz')
Default value is INSERT.
This method returns SQL string and bind parameters for UPDATE statement.
UPDATE
my ($stmt, @bind) = $sqlf->update(foo => { bar => 'baz' }, { hoge => 'fuga' }); # $stmt: UPDATE `foo` SET `bar` = ? WHERE (`hoge` = ?) # @bind: ('baz', 'fuga')
This is a table name for target of UPDATE.
This is a SET clause for INSERT statement.
This is a prefix for UPDATE statement.
my ($stmt, @bind) = $sqlf->update( 'foo' # table { bar => 'baz' }, # sets { hoge => 'fuga' }, # where { prefix => 'UPDATE LOW_PRIORITY' }, # opts ); # $stmt: UPDATE LOW_PRIORITY `foo` SET `bar` = ? WHERE (`hoge` = ?) # @bind: ('baz', 'fuga')
Default value is UPDATE.
This method returns SQL string and bind parameters for DELETE statement.
DELETE
my ($stmt, @bind) = $sqlf->delete(foo => { bar => 'baz' }); # $stmt: DELETE FROM `foo` WHERE (`bar = ?) # @bind: ('baz')
This is a table name for target of DELETE.
This is a prefix for DELETE statement.
my ($stmt, @bind) = $sqlf->delete(foo => { bar => 'baz' }, { prefix => 'DELETE LOW_PRIORITY' }); # $stmt: DELETE LOW_PRIORITY FROM `foo` WHERE (`bar` = ?) # @bind: ('baz')
Default value is DELETE.
This method returns SQL string and bind parameters for bulk insert.
my ($stmt, @bind) = $self->insert_multi( foo => [qw/bar baz/], [ [qw/hoge fuga/], [qw/fizz buzz/], ], ); # $stmt: INSERT INTO `foo` (`bar`, `baz`) VALUES (?, ?), (?, ?) # @bind: (qw/hoge fuga fizz buzz/)
This is a columns for target of INSERT.
This is a values parameters. Must be ARRAY within ARRAY.
my ($stmt, @bind) = $sqlf->insert_multi( foo => [qw/bar baz/], [ [qw/foo bar/], [\'NOW()', \['UNIX_TIMESTAMP(?)', '2012-12-12 12:12:12'] ], ], ); # $stmt: INSERT INTO `foo` (`bar`, `baz`) VALUES (?, ?), (NOW(), UNIX_TIMESTAMP(?)) # @bind: (qw/foo bar/, '2012-12-12 12:12:12')
my ($stmt, @bind) = $sqlf->insert_multi(..., { prefix => 'INSERT IGNORE INTO' }); # $stmt: INSERT IGNORE INTO ...
Default value is INSERT INTO.
INSERT INTO
Some as %s format.
%s
If this value specified then add ON DUPLICATE KEY UPDATE statement.
ON DUPLICATE KEY UPDATE
my ($stmt, @bind) = $sqlf->insert_multi( foo => [qw/bar baz/], [ [qw/hoge fuga/], [qw/fizz buzz/], ], { update => { bar => 'piyo' } }, ); # $stmt: INSERT INTO `foo` (`bar`, `baz`) VALUES (?, ?), (?, ?) ON DUPLICATE KEY UPDATE `bar` = ? # @bind: (qw/hoge fuga fizz buzz piyo/)
This method is a wrapper for insert_multi().
insert_multi()
Argument dialects are:
Same as insert_multi()
This is a values parameters. Must be HASH within ARRAY.
my ($stmt, @bind) = $sqlf->insert_multi_from_hash(foo => [ { bar => 'hoge', baz => 'fuga' }, { bar => 'fizz', baz => 'buzz' }, ]); # $stmt: INSERT INTO `foo` (`bar`, `baz`) VALUES (?, ?), (?, ?) # @bind: (qw/hoge fuga fizz buzz/)
This method generate "INSERT INTO ... ON DUPLICATE KEY UPDATE" query for MySQL.
my ($stmt, @bind) = $sqlf->insert_on_duplicate( foo => { bar => 'hoge', baz => 'fuga', }, { bar => \'VALUES(bar)', baz => 'piyo', }, ); # $stmt: INSERT INTO `foo` (`bar`, `baz`) VALUES (?, ?) ON DUPLICATE KEY UPDATE `bar` = VALUES(bar), baz = 'piyo' # @bind: (qw/hoge fuga piyo/)
This is a values parameters.
This is a ON DUPLICATE KEY UPDATE parameters.
my ($stmt, @bind) = $sqlf->insert_on_duplicate(..., { prefix => 'INSERT IGNORE INTO' }); # $stmt: INSERT IGNORE INTO ...
xaicron <xaicron {at} cpan.org>
Copyright 2012 - xaicron
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
SQL::Format::Spec
SQL::Maker
SQL::Abstract
To install SQL::Format, copy and paste the appropriate command in to your terminal.
cpanm
cpanm SQL::Format
CPAN shell
perl -MCPAN -e shell install SQL::Format
For more information on module installation, please visit the detailed CPAN module installation guide.