File: 08special_ops.t

package info (click to toggle)
libsql-abstract-perl 1.72-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 532 kB
  • sloc: perl: 6,501; makefile: 14
file content (69 lines) | stat: -rw-r--r-- 1,542 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/perl

use strict;
use warnings;
use Test::More;

use SQL::Abstract::Test import => ['is_same_sql_bind'];

use SQL::Abstract;

my $sqlmaker = SQL::Abstract->new(special_ops => [

  # special op for MySql MATCH (field) AGAINST(word1, word2, ...)
  {regex => qr/^match$/i, 
   handler => sub {
     my ($self, $field, $op, $arg) = @_;
     $arg = [$arg] if not ref $arg;
     my $label         = $self->_quote($field);
     my ($placeholder) = $self->_convert('?');
     my $placeholders  = join ", ", (($placeholder) x @$arg);
     my $sql           = $self->_sqlcase('match') . " ($label) "
                       . $self->_sqlcase('against') . " ($placeholders) ";
     my @bind = $self->_bindtype($field, @$arg);
     return ($sql, @bind);
     }
   },

  # special op for Basis+ NATIVE
  {regex => qr/^native$/i, 
   handler => sub {
     my ($self, $field, $op, $arg) = @_;
     $arg =~ s/'/''/g;
     my $sql = "NATIVE (' $field $arg ')";
     return ($sql);
     }
   },

]);

my @tests = (

  #1 
  { where => {foo => {-match => 'foo'},
              bar => {-match => [qw/foo bar/]}},
    stmt  => " WHERE ( MATCH (bar) AGAINST (?, ?) AND MATCH (foo) AGAINST (?) )",
    bind  => [qw/foo bar foo/],
  },

  #2
  { where => {foo => {-native => "PH IS 'bar'"}},
    stmt  => " WHERE ( NATIVE (' foo PH IS ''bar'' ') )",
    bind  => [],
  },

);


plan tests => scalar(@tests);

for (@tests) {

  my($stmt, @bind) = $sqlmaker->where($_->{where}, $_->{order});
  is_same_sql_bind($stmt, \@bind, $_->{stmt}, $_->{bind});
}