Documentation and changes
[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, ...)
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
40my @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
58plan tests => scalar(@tests);
59
60for (@tests) {
61
62 my($stmt, @bind) = $sqlmaker->where($_->{where}, $_->{order});
63 is_same_sql_bind($stmt, \@bind, $_->{stmt}, $_->{bind});
64}
65
66
67
68
69