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