From Code to Community: Sponsoring The Perl and Raku Conference 2025 Learn more

NAME

SQL::Composer - sql builder

SYNOPSIS

DESCRIPTION

Raw SQL

my $expr = SQL::Composer::Expression->new(expr => \'a = b');
my $sql = $expr->to_sql; # 'a = b'
my @bind = $expr->to_bind; # []

Raw SQL with bind

my $expr = SQL::Composer::Expression->new(expr => \['a = ?', 'b']);
my $sql = $expr->to_sql; # 'a = ?'
my @bind = $expr->to_bind; # 'b'

Simple SQL

my $expr = SQL::Composer::Expression->new(expr => [a => 'b']);
my $sql = $expr->to_sql;
is $sql, '`a` = ?';
my @bind = $expr->to_bind;
is_deeply \@bind, ['b'];

Expression with custom operator

my $expr = SQL::Composer::Expression->new(expr => [a => {'>' => 'b'}]);
my $sql = $expr->to_sql;
is $sql, '`a` > ?';
my @bind = $expr->to_bind;
is_deeply \@bind, ['b'];

Expression with column name

my $expr = SQL::Composer::Expression->new(expr => [a => {'-col' => 'b'}]);
my $sql = $expr->to_sql;
is $sql, '`a` = `b`';
my @bind = $expr->to_bind;
is_deeply \@bind, [];

Mixed logical expression

my $expr =
SQL::Composer::Expression->new(
expr => [-or => [a => 'b', -and => [c => 'd', 'e' => 'f']]]);
my $sql = $expr->to_sql; # '(`a` = ? OR (`c` = ? AND `e` = ?))'
my @bind = $expr->to_bind; # ['b', 'd', 'f']

IN

my $expr = SQL::Composer::Expression->new(expr => [a => ['b', 'c', 'd']]);
my $sql = $expr->to_sql; # '`a` IN (?,?,?)'
my @bind = $expr->to_bind; # ['b', 'c', 'd']