make _table use expand/render code
[scpubgit/Q-Branch.git] / t / 08special_ops.t
CommitLineData
4f30591b 1use strict;
2use warnings;
3use Test::More;
4
5use SQL::Abstract::Test import => ['is_same_sql_bind'];
6
7use SQL::Abstract;
8
9my $sqlmaker = SQL::Abstract->new(special_ops => [
10
11 # special op for MySql MATCH (field) AGAINST(word1, word2, ...)
428975b0 12 {regex => qr/^match$/i,
4f30591b 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
428975b0 27 {regex => qr/^native$/i,
4f30591b 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
38my @tests = (
39
428975b0 40 #1
4f30591b 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
4f30591b 55for (@tests) {
56
57 my($stmt, @bind) = $sqlmaker->where($_->{where}, $_->{order});
58 is_same_sql_bind($stmt, \@bind, $_->{stmt}, $_->{bind});
59}
60
10e6c946 61done_testing;