Massive rewrite of bind handling, and overall simplification of ::Storage::DBI
[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 DBIC::SqlMakerTest;
7
8 use_ok('DBICTest');
9
10 my $schema = DBICTest->init_schema;
11
12 my $where_bind = {
13     where => \'name like ?',
14     bind  => [ 'Cat%' ],
15 };
16
17 my $rs;
18
19 TODO: {
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' );
34
35     $rs = $schema->resultset('Artist')->search({ artistid => 1})
36         ->search({}, $where_bind);
37
38     is ( $rs->count, 1, 'where/bind last' );
39 }
40
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');
50
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 = ?)
56
57   $schema->register_extra_source('Complex' => $new_source);
58
59   $rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] });
60   is ( $rs->count, 1, 'cookbook arbitrary sql example' );
61
62   $rs = $schema->resultset('Complex')->search({ 'artistid' => 1 }, { bind => [ 1999 ] });
63   is ( $rs->count, 1, '...cookbook + search condition' );
64
65   $rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] })
66       ->search({ 'artistid' => 1 });
67   is ( $rs->count, 1, '...cookbook (bind first) + chained search' );
68
69   $rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] })->search({}, { where => \"title LIKE ?", bind => [ 'Spoon%' ] });
70   is_same_sql_bind(
71     $rs->as_query,
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 ?)",
73     [
74       [ {} => '1999' ], 
75       [ {} => 'Spoon%' ]
76     ],
77     'got correct SQL'
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' );
97
98   $rs = $schema->resultset('CustomSql')->search({}, { bind => [ 1999 ] })->search({}, { where => \"title LIKE ?", bind => [ 'Spoon%' ] });
99   is_same_sql_bind(
100     $rs->as_query,
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 ?)",
102     [
103       [ {} => '1999' ], 
104       [ {} => 'Spoon%' ]
105     ],
106     'got correct SQL (cookbook arbitrary SQL, in separate file)'
107   );
108 }
109
110 TODO: {
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 }
118
119 done_testing;