my
$sqlmaker
= SQL::Abstract->new(
special_ops
=> [
{
regex
=>
qr/^match$/
i,
handler
=>
sub
{
my
(
$self
,
$field
,
$op
,
$arg
) =
@_
;
$arg
= [
$arg
]
if
not
ref
$arg
;
my
$label
=
$self
->_quote(
$field
);
my
(
$placeholder
) =
$self
->_convert(
'?'
);
my
$placeholders
=
join
", "
, ((
$placeholder
) x
@$arg
);
my
$sql
=
$self
->_sqlcase(
'match'
) .
" ($label) "
.
$self
->_sqlcase(
'against'
) .
" ($placeholders) "
;
my
@bind
=
$self
->_bindtype(
$field
,
@$arg
);
return
(
$sql
,
@bind
);
}
},
{
regex
=>
qr/^native$/
i,
handler
=>
sub
{
my
(
$self
,
$field
,
$op
,
$arg
) =
@_
;
$arg
=~ s/
'/'
'/g;
my
$sql
=
"NATIVE (' $field $arg ')"
;
return
(
$sql
);
}
},
{
regex
=>
qr/^prior$/
i,
handler
=>
sub
{
my
(
$self
,
$lhs
,
$op
,
$rhs
) =
@_
;
my
(
$sql
,
@bind
) =
$self
->_recurse_where (
$rhs
);
$sql
=
sprintf
(
'%s = %s %s '
,
$self
->_convert(
$self
->_quote(
$lhs
)),
$self
->_sqlcase (
$op
),
$sql
);
return
(
$sql
,
@bind
);
},
},
],
unary_ops
=> [
{
regex
=>
qr/^json$/
i,
handler
=>
sub
{
'?'
, {
json
=>
$_
[2] } }
},
]);
my
@tests
= (
{
where
=> {
foo
=> {
-match
=>
'foo'
},
bar
=> {
-match
=> [
qw/foo bar/
]}},
stmt
=>
" WHERE ( MATCH (bar) AGAINST (?, ?) AND MATCH (foo) AGAINST (?) )"
,
bind
=> [
qw/foo bar foo/
],
},
{
where
=> {
foo
=> {
-native
=>
"PH IS 'bar'"
}},
stmt
=>
" WHERE ( NATIVE (' foo PH IS ''bar'' ') )"
,
bind
=> [],
},
{
where
=> {
foo
=> {
-json
=> {
bar
=>
'baz'
} } },
stmt
=>
"WHERE foo = ?"
,
bind
=> [ {
json
=> {
bar
=>
'baz'
} } ],
},
{
where
=> {
foo
=> {
'@>'
=> {
-json
=> {
bar
=>
'baz'
} } } },
stmt
=>
"WHERE foo @> ?"
,
bind
=> [ {
json
=> {
bar
=>
'baz'
} } ],
},
{
where
=> {
foo_id
=> {
'='
=> {
'-prior'
=> {
-ident
=>
'bar_id'
} } },
baz_id
=> {
'-prior'
=> {
-ident
=>
'quux_id'
} },
},
stmt
=>
' WHERE ( baz_id = PRIOR quux_id AND foo_id = ( PRIOR bar_id ) )'
,
bind
=> [],
},
);
for
(
@tests
) {
my
(
$stmt
,
@bind
) =
$sqlmaker
->where(
$_
->{where},
$_
->{order});
is_same_sql_bind(
$stmt
, \
@bind
,
$_
->{stmt},
$_
->{
bind
});
}
done_testing;