make the DBIC required warning scoped
[scpubgit/Q-Branch.git] / t / 08special_ops.t
1 use strict;
2 use warnings;
3 use Test::More;
4
5 use SQL::Abstract::Test import => ['is_same_sql_bind'];
6
7 use SQL::Abstract;
8
9 my $sqlmaker = SQL::Abstract->new(special_ops => [
10
11   # special op for MySql MATCH (field) AGAINST(word1, word2, ...)
12   {regex => qr/^match$/i,
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
27   {regex => qr/^native$/i,
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
36 ], unary_ops => [
37   # unary op from Mojo::Pg
38   {regex => qr/^json$/i,
39    handler => sub { '?', { json => $_[2] } }
40   },
41 ]);
42
43 my @tests = (
44
45   #1
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
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
70 );
71
72 for (@tests) {
73
74   my($stmt, @bind) = $sqlmaker->where($_->{where}, $_->{order});
75   is_same_sql_bind($stmt, \@bind, $_->{stmt}, $_->{bind});
76 }
77
78 done_testing;