71d1b97720daffb60b783fa81fdacb9ba7994480
[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 DBICTest;
7 use DBIC::SqlMakerTest;
8
9 my $schema = DBICTest->init_schema;
10
11 my $where_bind = {
12     where => \'name like ?',
13     bind  => [ 'Cat%' ],
14 };
15
16 my $rs;
17
18 {
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' );
31
32     $rs = $schema->resultset('Artist')->search({ artistid => 1})
33         ->search({}, $where_bind);
34
35     is ( $rs->count, 1, 'where/bind last' );
36
37     # and the complex case
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' );
43 }
44
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');
54
55   $new_source->name(\<<'');
56   ( SELECT a.*, cd.cdid AS cdid, cd.title AS title, cd.year AS year
57     FROM artist a
58     JOIN cd ON cd.artist = a.artistid
59     WHERE cd.year = ?)
60
61   $schema->register_extra_source('Complex' => $new_source);
62
63   $rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] });
64   is ( $rs->count, 1, 'cookbook arbitrary sql example' );
65
66   $rs = $schema->resultset('Complex')->search({ 'artistid' => 1 }, { bind => [ 1999 ] });
67   is ( $rs->count, 1, '...cookbook + search condition' );
68
69   $rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] })
70       ->search({ 'artistid' => 1 });
71   is ( $rs->count, 1, '...cookbook (bind first) + chained search' );
72
73   $rs = $schema->resultset('Complex')->search({}, { bind => [ [{ sqlt_datatype => 'datetime'} => 1999 ] ] })->search({}, { where => \"title LIKE ?", bind => [ 'Spoon%' ] });
74   is_same_sql_bind(
75     $rs->as_query,
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 ?)",
77     [
78       [ { sqlt_datatype => 'datetime' } => '1999' ],
79       [ {} => 'Spoon%' ]
80     ],
81     'got correct SQL'
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' );
101
102   $rs = $schema->resultset('CustomSql')->search({}, { bind => [ 1999 ] })->search({}, { where => \"title LIKE ?", bind => [ 'Spoon%' ] });
103   is_same_sql_bind(
104     $rs->as_query,
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 ?)",
106     [
107       [ {} => '1999' ],
108       [ {} => 'Spoon%' ]
109     ],
110     'got correct SQL (cookbook arbitrary SQL, in separate file)'
111   );
112 }
113
114 done_testing;