Commit | Line | Data |
1c133e22 |
1 | use strict; |
2 | use warnings; |
3 | |
4 | use Test::More; |
5 | use lib qw(t/lib); |
a5a7bb73 |
6 | use DBICTest ':DiffSQL'; |
07a287ce |
7 | |
1c133e22 |
8 | my $schema = DBICTest->init_schema; |
9 | |
1c133e22 |
10 | my $where_bind = { |
11 | where => \'name like ?', |
12 | bind => [ 'Cat%' ], |
13 | }; |
14 | |
15 | my $rs; |
16 | |
4ca1fd6f |
17 | { |
1c133e22 |
18 | # First, the simple cases... |
19 | $rs = $schema->resultset('Artist')->search( |
20 | { artistid => 1 }, |
21 | $where_bind, |
22 | ); |
23 | |
24 | is ( $rs->count, 1, 'where/bind combined' ); |
25 | |
26 | $rs= $schema->resultset('Artist')->search({}, $where_bind) |
27 | ->search({ artistid => 1}); |
28 | |
29 | is ( $rs->count, 1, 'where/bind first' ); |
0491b597 |
30 | |
1c133e22 |
31 | $rs = $schema->resultset('Artist')->search({ artistid => 1}) |
32 | ->search({}, $where_bind); |
33 | |
34 | is ( $rs->count, 1, 'where/bind last' ); |
4ca1fd6f |
35 | |
36 | # and the complex case |
4ca1fd6f |
37 | $rs = $schema->resultset('CustomSql')->search({}, { bind => [ 1999 ] }) |
38 | ->search({ 'artistid' => 1 }, { |
39 | where => \'title like ?', |
40 | bind => [ 'Spoon%' ] }); |
41 | is ( $rs->count, 1, '...cookbook + chained search with extra bind' ); |
1c133e22 |
42 | } |
43 | |
b8b55c8e |
44 | { |
45 | # More complex cases, based primarily on the Cookbook |
46 | # "Arbitrary SQL through a custom ResultSource" technique, |
47 | # which seems to be the only place the bind attribute is |
48 | # documented. Breaking this technique probably breaks existing |
49 | # application code. |
50 | my $source = DBICTest::Artist->result_source_instance; |
51 | my $new_source = $source->new($source); |
52 | $new_source->source_name('Complex'); |
1c133e22 |
53 | |
b8b55c8e |
54 | $new_source->name(\<<''); |
8273e845 |
55 | ( SELECT a.*, cd.cdid AS cdid, cd.title AS title, cd.year AS year |
b8b55c8e |
56 | FROM artist a |
57 | JOIN cd ON cd.artist = a.artistid |
58 | WHERE cd.year = ?) |
1c133e22 |
59 | |
b8b55c8e |
60 | $schema->register_extra_source('Complex' => $new_source); |
1c133e22 |
61 | |
b8b55c8e |
62 | $rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] }); |
63 | is ( $rs->count, 1, 'cookbook arbitrary sql example' ); |
1c133e22 |
64 | |
b8b55c8e |
65 | $rs = $schema->resultset('Complex')->search({ 'artistid' => 1 }, { bind => [ 1999 ] }); |
66 | is ( $rs->count, 1, '...cookbook + search condition' ); |
1c133e22 |
67 | |
b8b55c8e |
68 | $rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] }) |
69 | ->search({ 'artistid' => 1 }); |
70 | is ( $rs->count, 1, '...cookbook (bind first) + chained search' ); |
1c133e22 |
71 | |
ea20ead0 |
72 | $rs = $schema->resultset('Complex')->search({}, { bind => [ [{ sqlt_datatype => 'datetime'} => 1999 ] ] })->search({}, { where => \"title LIKE ?", bind => [ 'Spoon%' ] }); |
07a287ce |
73 | is_same_sql_bind( |
af6aac2d |
74 | $rs->as_query, |
0491b597 |
75 | "(SELECT me.artistid, me.name, me.rank, me.charfield FROM (SELECT a.*, cd.cdid AS cdid, cd.title AS title, cd.year AS year FROM artist a JOIN cd ON cd.artist = a.artistid WHERE cd.year = ?) me WHERE title LIKE ?)", |
07a287ce |
76 | [ |
ea20ead0 |
77 | [ { sqlt_datatype => 'datetime' } => '1999' ], |
0e773352 |
78 | [ {} => 'Spoon%' ] |
07a287ce |
79 | ], |
80 | 'got correct SQL' |
b8b55c8e |
81 | ); |
82 | } |
83 | |
84 | { |
85 | # More complex cases, based primarily on the Cookbook |
86 | # "Arbitrary SQL through a custom ResultSource" technique, |
87 | # which seems to be the only place the bind attribute is |
88 | # documented. Breaking this technique probably breaks existing |
89 | # application code. |
90 | |
91 | $rs = $schema->resultset('CustomSql')->search({}, { bind => [ 1999 ] }); |
92 | is ( $rs->count, 1, 'cookbook arbitrary sql example (in separate file)' ); |
93 | |
94 | $rs = $schema->resultset('CustomSql')->search({ 'artistid' => 1 }, { bind => [ 1999 ] }); |
95 | is ( $rs->count, 1, '...cookbook (in separate file) + search condition' ); |
96 | |
97 | $rs = $schema->resultset('CustomSql')->search({}, { bind => [ 1999 ] }) |
98 | ->search({ 'artistid' => 1 }); |
99 | is ( $rs->count, 1, '...cookbook (bind first, in separate file) + chained search' ); |
dbf95416 |
100 | |
b8b55c8e |
101 | $rs = $schema->resultset('CustomSql')->search({}, { bind => [ 1999 ] })->search({}, { where => \"title LIKE ?", bind => [ 'Spoon%' ] }); |
102 | is_same_sql_bind( |
103 | $rs->as_query, |
0491b597 |
104 | "(SELECT me.artistid, me.name, me.rank, me.charfield FROM (SELECT a.*, cd.cdid AS cdid, cd.title AS title, cd.year AS year FROM artist a JOIN cd ON cd.artist = a.artistid WHERE cd.year = ?) me WHERE title LIKE ?)", |
b8b55c8e |
105 | [ |
8273e845 |
106 | [ {} => '1999' ], |
0e773352 |
107 | [ {} => 'Spoon%' ] |
b8b55c8e |
108 | ], |
109 | 'got correct SQL (cookbook arbitrary SQL, in separate file)' |
110 | ); |
07a287ce |
111 | } |
112 | |
56166f36 |
113 | done_testing; |