Massive rewrite of bind handling, and overall simplification of ::Storage::DBI
[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
19TODO: {
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' );
39}
40
b8b55c8e 41{
42 # More complex cases, based primarily on the Cookbook
43 # "Arbitrary SQL through a custom ResultSource" technique,
44 # which seems to be the only place the bind attribute is
45 # documented. Breaking this technique probably breaks existing
46 # application code.
47 my $source = DBICTest::Artist->result_source_instance;
48 my $new_source = $source->new($source);
49 $new_source->source_name('Complex');
1c133e22 50
b8b55c8e 51 $new_source->name(\<<'');
52 ( SELECT a.*, cd.cdid AS cdid, cd.title AS title, cd.year AS year
53 FROM artist a
54 JOIN cd ON cd.artist = a.artistid
55 WHERE cd.year = ?)
1c133e22 56
b8b55c8e 57 $schema->register_extra_source('Complex' => $new_source);
1c133e22 58
b8b55c8e 59 $rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] });
60 is ( $rs->count, 1, 'cookbook arbitrary sql example' );
1c133e22 61
b8b55c8e 62 $rs = $schema->resultset('Complex')->search({ 'artistid' => 1 }, { bind => [ 1999 ] });
63 is ( $rs->count, 1, '...cookbook + search condition' );
1c133e22 64
b8b55c8e 65 $rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] })
66 ->search({ 'artistid' => 1 });
67 is ( $rs->count, 1, '...cookbook (bind first) + chained search' );
1c133e22 68
07a287ce 69 $rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] })->search({}, { where => \"title LIKE ?", bind => [ 'Spoon%' ] });
07a287ce 70 is_same_sql_bind(
af6aac2d 71 $rs->as_query,
0491b597 72 "(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 73 [
0e773352 74 [ {} => '1999' ],
75 [ {} => 'Spoon%' ]
07a287ce 76 ],
77 'got correct SQL'
b8b55c8e 78 );
79}
80
81{
82 # More complex cases, based primarily on the Cookbook
83 # "Arbitrary SQL through a custom ResultSource" technique,
84 # which seems to be the only place the bind attribute is
85 # documented. Breaking this technique probably breaks existing
86 # application code.
87
88 $rs = $schema->resultset('CustomSql')->search({}, { bind => [ 1999 ] });
89 is ( $rs->count, 1, 'cookbook arbitrary sql example (in separate file)' );
90
91 $rs = $schema->resultset('CustomSql')->search({ 'artistid' => 1 }, { bind => [ 1999 ] });
92 is ( $rs->count, 1, '...cookbook (in separate file) + search condition' );
93
94 $rs = $schema->resultset('CustomSql')->search({}, { bind => [ 1999 ] })
95 ->search({ 'artistid' => 1 });
96 is ( $rs->count, 1, '...cookbook (bind first, in separate file) + chained search' );
dbf95416 97
b8b55c8e 98 $rs = $schema->resultset('CustomSql')->search({}, { bind => [ 1999 ] })->search({}, { where => \"title LIKE ?", bind => [ 'Spoon%' ] });
99 is_same_sql_bind(
100 $rs->as_query,
0491b597 101 "(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 102 [
0e773352 103 [ {} => '1999' ],
104 [ {} => 'Spoon%' ]
b8b55c8e 105 ],
106 'got correct SQL (cookbook arbitrary SQL, in separate file)'
107 );
07a287ce 108}
109
110TODO: {
1c133e22 111 local $TODO = 'bind args order needs fixing (semifor)';
112 $rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] })
113 ->search({ 'artistid' => 1 }, {
114 where => \'title like ?',
115 bind => [ 'Spoon%' ] });
116 is ( $rs->count, 1, '...cookbook + chained search with extra bind' );
117}
56166f36 118
119done_testing;