Port whitespace cleanup from master 428975b0
[dbsrgits/SQL-Abstract.git] / t / 08special_ops.t
CommitLineData
4f30591b 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5use Test::More;
6
7use SQL::Abstract::Test import => ['is_same_sql_bind'];
8
9use SQL::Abstract;
10
11my $sqlmaker = SQL::Abstract->new(special_ops => [
12
13 # special op for MySql MATCH (field) AGAINST(word1, word2, ...)
f2a0d52b 14 {regex => qr/^match$/i,
4f30591b 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
f2a0d52b 29 {regex => qr/^native$/i,
4f30591b 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
40my @tests = (
41
f2a0d52b 42 #1
4f30591b 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
4f30591b 57for (@tests) {
58
59 my($stmt, @bind) = $sqlmaker->where($_->{where}, $_->{order});
60 is_same_sql_bind($stmt, \@bind, $_->{stmt}, $_->{bind});
61}
62
10e6c946 63done_testing;