NAME

SQL::OOP::Where - WHERE factory class

SYNOPSIS

use SQL::OOP::Where;

my $where = SQL::OOP::Where->new();
my $cond1 = $where->cmp($operator, $field, $value);
my $cond2 = $where->is_null('some_field');
my $cond3 = $where->is_not_null('some_field');
my $cond4 = $where->between('some_field', 1, 2);
my $cond5 = $where->in('some_field', [1, 2, 3]);
my $cond6 = $where->cmp_nested($operator, $field, $select_obj);

my $sql  = $cond1->to_string;
my @bind = $cond1->bind;

# combine conditions
my $cond7 = $where->or($cond1, $cond2);
$cond7->append($cond3);
my $cond8 = $where->and($cond7, $where->and($cond4, $cond5));

my $sql  = $cond8->to_string;
my @bind = $cond8->bind;

# SQL::Abstract style
my %seed = (a => 'b', c => 'd');
my $cond10 = $where->and_hash(\%seed); # default operator is '='
my $cond11 = $where->and_hash(\%seed, "LIKE");
my $cond12 = $where->or_hash(\%seed); # default operator is '='
my $cond13 = $where->or_hash(\%seed, "LIKE");

my $sql  = $cond13->to_string;
my @bind = $cond13->bind;

DESCRIPTION

SQL::OOP::Where is a Factory Class for WHERE clause elements. All methods of this returns SQL::OOP::Base or SQL::OOP::Array.

METHODS

SQL::OOP::Where->new

Returns SQL::OOP::Where instance. This class instance is just for convenience. All methods in this class also can be called as Class method.

my $util = SQL::OOP::Where->new;

$instance->cmp($operator, $fieldname, $value)

Generates 1 operator expression.

my $where = SQL::OOP::Where->new;
$where->cmp('=', 'col1', 'value') # "col1" = ?
$where->cmp('=', ['table', 'col1'], 'value') # "table"."col1" = ?

$instance->cmp_nested($fieldname, $object)

Generates 1 operator expression with sub query in value

$instance->in($fieldname, $array_ref)

Generates IN clause

my $where = SQL::OOP::Where->new;
$where->in('col1', ['candidate1', 'candidate2']) # "col1" IN (?, ?)
$where->in(['table', 'col1'], ['c1', 'c2']) # "table"."col1" IN (?, ?)

$instance->not_in($fieldname, $array_ref)

Generates NOT IN clause

my $where = SQL::OOP::Where->new;
$where->not_in('col1', ['val1', 'val2']) # "col1" NOT IN (?, ?)
$where->not_in(['tbl', 'col1'], ['v1', 'v2']) # "tbl"."col1" NOT IN (?, ?)

$instance->between($fieldname, $upper, $lower)

Generates BETWEEN clause

my $where = SQL::OOP::Where->new;
$where->between('col1', 5, 10]) # "col1" BETWEEN ? AND ?
$where->between(['table', 'col1'], 5, 10) # "table"."col1" BETWEEN ? AND ?

$instance->is_not_null($fieldname)

Generates IS NOT NULL clause

my $where = SQL::OOP::Where->new;
$where->is_not_null('col1') # "col1" IS NOT NULL
$where->is_not_null(['table', 'col1']) # "table"."col1" IS NOT NULL

$instance->is_null($fieldname)

Generates IS NULL clause

my $where = SQL::OOP::Where->new;
$where->is_null('col1') # "col1" IS NULL
$where->is_null(['table', 'col1']) # "table"."col1" IS NULL

$instance->or(@array)

Generates OR expression in SQL::OOP::Array

$instance->or_hash(%hash_ref)

Generates OR expression in SQL::OOP::Array by hash

$instance->and(@array)

Generates AND expression in SQL::OOP::Array

$instance->and_hash(%hash_ref)

Generates AND expression in SQL::OOP::Array by hash

AUTHOR

Sugama Keita, <sugama@jamadam.com>

COPYRIGHT AND LICENSE

Copyright (C) 2011 by Sugama Keita.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.