Institute a central "load this first in testing" package
[dbsrgits/DBIx-Class.git] / t / resultset / bind_attr.t
CommitLineData
c0329273 1BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2
1c133e22 3use strict;
4use warnings;
5
6use Test::More;
c0329273 7
a5a7bb73 8use DBICTest ':DiffSQL';
07a287ce 9
1c133e22 10my $schema = DBICTest->init_schema;
11
1c133e22 12my $where_bind = {
13 where => \'name like ?',
14 bind => [ 'Cat%' ],
15};
16
17my $rs;
18
4ca1fd6f 19{
1c133e22 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' );
0491b597 32
1c133e22 33 $rs = $schema->resultset('Artist')->search({ artistid => 1})
34 ->search({}, $where_bind);
35
36 is ( $rs->count, 1, 'where/bind last' );
4ca1fd6f 37
38 # and the complex case
4ca1fd6f 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' );
1c133e22 44}
45
b8b55c8e 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');
1c133e22 55
b8b55c8e 56 $new_source->name(\<<'');
8273e845 57 ( SELECT a.*, cd.cdid AS cdid, cd.title AS title, cd.year AS year
b8b55c8e 58 FROM artist a
59 JOIN cd ON cd.artist = a.artistid
60 WHERE cd.year = ?)
1c133e22 61
b8b55c8e 62 $schema->register_extra_source('Complex' => $new_source);
1c133e22 63
b8b55c8e 64 $rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] });
65 is ( $rs->count, 1, 'cookbook arbitrary sql example' );
1c133e22 66
b8b55c8e 67 $rs = $schema->resultset('Complex')->search({ 'artistid' => 1 }, { bind => [ 1999 ] });
68 is ( $rs->count, 1, '...cookbook + search condition' );
1c133e22 69
b8b55c8e 70 $rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] })
71 ->search({ 'artistid' => 1 });
72 is ( $rs->count, 1, '...cookbook (bind first) + chained search' );
1c133e22 73
ea20ead0 74 $rs = $schema->resultset('Complex')->search({}, { bind => [ [{ sqlt_datatype => 'datetime'} => 1999 ] ] })->search({}, { where => \"title LIKE ?", bind => [ 'Spoon%' ] });
07a287ce 75 is_same_sql_bind(
af6aac2d 76 $rs->as_query,
0491b597 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 ?)",
07a287ce 78 [
ea20ead0 79 [ { sqlt_datatype => 'datetime' } => '1999' ],
0e773352 80 [ {} => 'Spoon%' ]
07a287ce 81 ],
82 'got correct SQL'
b8b55c8e 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' );
dbf95416 102
b8b55c8e 103 $rs = $schema->resultset('CustomSql')->search({}, { bind => [ 1999 ] })->search({}, { where => \"title LIKE ?", bind => [ 'Spoon%' ] });
104 is_same_sql_bind(
105 $rs->as_query,
0491b597 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 ?)",
b8b55c8e 107 [
8273e845 108 [ {} => '1999' ],
0e773352 109 [ {} => 'Spoon%' ]
b8b55c8e 110 ],
111 'got correct SQL (cookbook arbitrary SQL, in separate file)'
112 );
07a287ce 113}
114
56166f36 115done_testing;