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