Remove TODO labels from blocks not using todo_skip() - no test changes
[dbsrgits/DBIx-Class.git] / t / resultset / bind_attr.t
CommitLineData
1c133e22 1use strict;
2use warnings;
3
4use Test::More;
5use lib qw(t/lib);
07a287ce 6use DBIC::SqlMakerTest;
7
8use_ok('DBICTest');
1c133e22 9
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 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' );
0491b597 34
1c133e22 35 $rs = $schema->resultset('Artist')->search({ artistid => 1})
36 ->search({}, $where_bind);
37
38 is ( $rs->count, 1, 'where/bind last' );
4ca1fd6f 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' );
1c133e22 47}
48
b8b55c8e 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');
1c133e22 58
b8b55c8e 59 $new_source->name(\<<'');
8273e845 60 ( SELECT a.*, cd.cdid AS cdid, cd.title AS title, cd.year AS year
b8b55c8e 61 FROM artist a
62 JOIN cd ON cd.artist = a.artistid
63 WHERE cd.year = ?)
1c133e22 64
b8b55c8e 65 $schema->register_extra_source('Complex' => $new_source);
1c133e22 66
b8b55c8e 67 $rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] });
68 is ( $rs->count, 1, 'cookbook arbitrary sql example' );
1c133e22 69
b8b55c8e 70 $rs = $schema->resultset('Complex')->search({ 'artistid' => 1 }, { bind => [ 1999 ] });
71 is ( $rs->count, 1, '...cookbook + search condition' );
1c133e22 72
b8b55c8e 73 $rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] })
74 ->search({ 'artistid' => 1 });
75 is ( $rs->count, 1, '...cookbook (bind first) + chained search' );
1c133e22 76
ea20ead0 77 $rs = $schema->resultset('Complex')->search({}, { bind => [ [{ sqlt_datatype => 'datetime'} => 1999 ] ] })->search({}, { where => \"title LIKE ?", bind => [ 'Spoon%' ] });
07a287ce 78 is_same_sql_bind(
af6aac2d 79 $rs->as_query,
0491b597 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 ?)",
07a287ce 81 [
ea20ead0 82 [ { sqlt_datatype => 'datetime' } => '1999' ],
0e773352 83 [ {} => 'Spoon%' ]
07a287ce 84 ],
85 'got correct SQL'
b8b55c8e 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' );
dbf95416 105
b8b55c8e 106 $rs = $schema->resultset('CustomSql')->search({}, { bind => [ 1999 ] })->search({}, { where => \"title LIKE ?", bind => [ 'Spoon%' ] });
107 is_same_sql_bind(
108 $rs->as_query,
0491b597 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 ?)",
b8b55c8e 110 [
8273e845 111 [ {} => '1999' ],
0e773352 112 [ {} => 'Spoon%' ]
b8b55c8e 113 ],
114 'got correct SQL (cookbook arbitrary SQL, in separate file)'
115 );
07a287ce 116}
117
56166f36 118done_testing;