dc6a618c4bb984bde631099819e7ee78276a1e41
[dbsrgits/SQL-Abstract.git] / t / 08special_ops.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5 use Test::More;
6
7 use SQL::Abstract::Test import => ['is_same_sql_bind'];
8
9 use SQL::Abstract;
10
11 my $sqlmaker = SQL::Abstract->new(special_ops => [
12
13   # special op for MySql MATCH (field) AGAINST(word1, word2, ...)
14   {regex => qr/^match$/i,
15    handler => sub {
16      my ($self, $field, $op, $arg) = @_;
17      $arg = [$arg] if not ref $arg;
18      my $label         = $self->_quote($field);
19      my ($placeholder) = $self->_convert('?');
20      my $placeholders  = join ", ", (($placeholder) x @$arg);
21      my $sql           = $self->_sqlcase('match') . " ($label) "
22                        . $self->_sqlcase('against') . " ($placeholders) ";
23      my @bind = $self->_bindtype($field, @$arg);
24      return ($sql, @bind);
25      }
26    },
27
28   # special op for Basis+ NATIVE
29   {regex => qr/^native$/i,
30    handler => sub {
31      my ($self, $field, $op, $arg) = @_;
32      $arg =~ s/'/''/g;
33      my $sql = "NATIVE (' $field $arg ')";
34      return ($sql);
35      }
36    },
37
38 ]);
39
40 my @tests = (
41
42   #1
43   { where => {foo => {-match => 'foo'},
44               bar => {-match => [qw/foo bar/]}},
45     stmt  => " WHERE ( MATCH (bar) AGAINST (?, ?) AND MATCH (foo) AGAINST (?) )",
46     bind  => [qw/foo bar foo/],
47   },
48
49   #2
50   { where => {foo => {-native => "PH IS 'bar'"}},
51     stmt  => " WHERE ( NATIVE (' foo PH IS ''bar'' ') )",
52     bind  => [],
53   },
54
55 );
56
57 for (@tests) {
58
59   my($stmt, @bind) = $sqlmaker->where($_->{where}, $_->{order});
60   is_same_sql_bind($stmt, \@bind, $_->{stmt}, $_->{bind});
61 }
62
63 done_testing;