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