cleanup multop rendering
[scpubgit/Q-Branch.git] / t / 08special_ops.t
CommitLineData
4f30591b 1use strict;
2use warnings;
3use Test::More;
4
5use SQL::Abstract::Test import => ['is_same_sql_bind'];
6
7use SQL::Abstract;
8
9my $sqlmaker = SQL::Abstract->new(special_ops => [
10
11 # special op for MySql MATCH (field) AGAINST(word1, word2, ...)
428975b0 12 {regex => qr/^match$/i,
4f30591b 13 handler => sub {
14 my ($self, $field, $op, $arg) = @_;
15 $arg = [$arg] if not ref $arg;
16 my $label = $self->_quote($field);
17 my ($placeholder) = $self->_convert('?');
18 my $placeholders = join ", ", (($placeholder) x @$arg);
19 my $sql = $self->_sqlcase('match') . " ($label) "
20 . $self->_sqlcase('against') . " ($placeholders) ";
21 my @bind = $self->_bindtype($field, @$arg);
22 return ($sql, @bind);
23 }
24 },
25
26 # special op for Basis+ NATIVE
428975b0 27 {regex => qr/^native$/i,
4f30591b 28 handler => sub {
29 my ($self, $field, $op, $arg) = @_;
30 $arg =~ s/'/''/g;
31 my $sql = "NATIVE (' $field $arg ')";
32 return ($sql);
33 }
34 },
35
96a8d74a 36], unary_ops => [
37 # unary op from Mojo::Pg
38 {regex => qr/^json$/i,
39 handler => sub { '?', { json => $_[2] } }
40 },
4f30591b 41]);
42
43my @tests = (
44
428975b0 45 #1
4f30591b 46 { where => {foo => {-match => 'foo'},
47 bar => {-match => [qw/foo bar/]}},
48 stmt => " WHERE ( MATCH (bar) AGAINST (?, ?) AND MATCH (foo) AGAINST (?) )",
49 bind => [qw/foo bar foo/],
50 },
51
52 #2
53 { where => {foo => {-native => "PH IS 'bar'"}},
54 stmt => " WHERE ( NATIVE (' foo PH IS ''bar'' ') )",
55 bind => [],
56 },
57
96a8d74a 58 #3
59 { where => { foo => { -json => { bar => 'baz' } } },
60 stmt => "WHERE foo = ?",
61 bind => [ { json => { bar => 'baz' } } ],
62 },
63
64 #4
65 { where => { foo => { '@>' => { -json => { bar => 'baz' } } } },
66 stmt => "WHERE foo @> ?",
67 bind => [ { json => { bar => 'baz' } } ],
68 },
69
4f30591b 70);
71
4f30591b 72for (@tests) {
73
74 my($stmt, @bind) = $sqlmaker->where($_->{where}, $_->{order});
75 is_same_sql_bind($stmt, \@bind, $_->{stmt}, $_->{bind});
76}
77
10e6c946 78done_testing;