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