Make sure DBICTest is always loaded first (purely bookkeep)
[dbsrgits/DBIx-Class.git] / t / resultset / bind_attr.t
CommitLineData
1c133e22 1use strict;
2use warnings;
3
4use Test::More;
5use lib qw(t/lib);
5e724964 6use DBICTest;
07a287ce 7use DBIC::SqlMakerTest;
8
1c133e22 9my $schema = DBICTest->init_schema;
10
1c133e22 11my $where_bind = {
12 where => \'name like ?',
13 bind => [ 'Cat%' ],
14};
15
16my $rs;
17
4ca1fd6f 18{
1c133e22 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' );
0491b597 33
1c133e22 34 $rs = $schema->resultset('Artist')->search({ artistid => 1})
35 ->search({}, $where_bind);
36
37 is ( $rs->count, 1, 'where/bind last' );
4ca1fd6f 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' );
1c133e22 46}
47
b8b55c8e 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');
1c133e22 57
b8b55c8e 58 $new_source->name(\<<'');
8273e845 59 ( SELECT a.*, cd.cdid AS cdid, cd.title AS title, cd.year AS year
b8b55c8e 60 FROM artist a
61 JOIN cd ON cd.artist = a.artistid
62 WHERE cd.year = ?)
1c133e22 63
b8b55c8e 64 $schema->register_extra_source('Complex' => $new_source);
1c133e22 65
b8b55c8e 66 $rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] });
67 is ( $rs->count, 1, 'cookbook arbitrary sql example' );
1c133e22 68
b8b55c8e 69 $rs = $schema->resultset('Complex')->search({ 'artistid' => 1 }, { bind => [ 1999 ] });
70 is ( $rs->count, 1, '...cookbook + search condition' );
1c133e22 71
b8b55c8e 72 $rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] })
73 ->search({ 'artistid' => 1 });
74 is ( $rs->count, 1, '...cookbook (bind first) + chained search' );
1c133e22 75
ea20ead0 76 $rs = $schema->resultset('Complex')->search({}, { bind => [ [{ sqlt_datatype => 'datetime'} => 1999 ] ] })->search({}, { where => \"title LIKE ?", bind => [ 'Spoon%' ] });
07a287ce 77 is_same_sql_bind(
af6aac2d 78 $rs->as_query,
0491b597 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 ?)",
07a287ce 80 [
ea20ead0 81 [ { sqlt_datatype => 'datetime' } => '1999' ],
0e773352 82 [ {} => 'Spoon%' ]
07a287ce 83 ],
84 'got correct SQL'
b8b55c8e 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' );
dbf95416 104
b8b55c8e 105 $rs = $schema->resultset('CustomSql')->search({}, { bind => [ 1999 ] })->search({}, { where => \"title LIKE ?", bind => [ 'Spoon%' ] });
106 is_same_sql_bind(
107 $rs->as_query,
0491b597 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 ?)",
b8b55c8e 109 [
8273e845 110 [ {} => '1999' ],
0e773352 111 [ {} => 'Spoon%' ]
b8b55c8e 112 ],
113 'got correct SQL (cookbook arbitrary SQL, in separate file)'
114 );
07a287ce 115}
116
56166f36 117done_testing;