Add Travis-CI config
[dbsrgits/SQL-Abstract.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 ]);
37
38 my @tests = (
39
40   #1
41   { where => {foo => {-match => 'foo'},
42               bar => {-match => [qw/foo bar/]}},
43     stmt  => " WHERE ( MATCH (bar) AGAINST (?, ?) AND MATCH (foo) AGAINST (?) )",
44     bind  => [qw/foo bar foo/],
45   },
46
47   #2
48   { where => {foo => {-native => "PH IS 'bar'"}},
49     stmt  => " WHERE ( NATIVE (' foo PH IS ''bar'' ') )",
50     bind  => [],
51   },
52
53 );
54
55 for (@tests) {
56
57   my($stmt, @bind) = $sqlmaker->where($_->{where}, $_->{order});
58   is_same_sql_bind($stmt, \@bind, $_->{stmt}, $_->{bind});
59 }
60
61 done_testing;