Retire DBIC/SqlMakerTest.pm now that SQLA::Test provides the same function
[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 DBICTest ':DiffSQL';
7
8 my $schema = DBICTest->init_schema;
9
10 my $where_bind = {
11     where => \'name like ?',
12     bind  => [ 'Cat%' ],
13 };
14
15 my $rs;
16
17 {
18     # First, the simple cases...
19     $rs = $schema->resultset('Artist')->search(
20             { artistid => 1 },
21             $where_bind,
22     );
23
24     is ( $rs->count, 1, 'where/bind combined' );
25
26     $rs= $schema->resultset('Artist')->search({}, $where_bind)
27         ->search({ artistid => 1});
28
29     is ( $rs->count, 1, 'where/bind first' );
30
31     $rs = $schema->resultset('Artist')->search({ artistid => 1})
32         ->search({}, $where_bind);
33
34     is ( $rs->count, 1, 'where/bind last' );
35
36     # and the complex case
37     $rs = $schema->resultset('CustomSql')->search({}, { bind => [ 1999 ] })
38         ->search({ 'artistid' => 1 }, {
39             where => \'title like ?',
40             bind => [ 'Spoon%' ] });
41     is ( $rs->count, 1, '...cookbook + chained search with extra bind' );
42 }
43
44 {
45   # More complex cases, based primarily on the Cookbook
46   # "Arbitrary SQL through a custom ResultSource" technique,
47   # which seems to be the only place the bind attribute is
48   # documented.  Breaking this technique probably breaks existing
49   # application code.
50   my $source = DBICTest::Artist->result_source_instance;
51   my $new_source = $source->new($source);
52   $new_source->source_name('Complex');
53
54   $new_source->name(\<<'');
55   ( SELECT a.*, cd.cdid AS cdid, cd.title AS title, cd.year AS year
56     FROM artist a
57     JOIN cd ON cd.artist = a.artistid
58     WHERE cd.year = ?)
59
60   $schema->register_extra_source('Complex' => $new_source);
61
62   $rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] });
63   is ( $rs->count, 1, 'cookbook arbitrary sql example' );
64
65   $rs = $schema->resultset('Complex')->search({ 'artistid' => 1 }, { bind => [ 1999 ] });
66   is ( $rs->count, 1, '...cookbook + search condition' );
67
68   $rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] })
69       ->search({ 'artistid' => 1 });
70   is ( $rs->count, 1, '...cookbook (bind first) + chained search' );
71
72   $rs = $schema->resultset('Complex')->search({}, { bind => [ [{ sqlt_datatype => 'datetime'} => 1999 ] ] })->search({}, { where => \"title LIKE ?", bind => [ 'Spoon%' ] });
73   is_same_sql_bind(
74     $rs->as_query,
75     "(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 ?)",
76     [
77       [ { sqlt_datatype => 'datetime' } => '1999' ],
78       [ {} => 'Spoon%' ]
79     ],
80     'got correct SQL'
81   );
82 }
83
84 {
85   # More complex cases, based primarily on the Cookbook
86   # "Arbitrary SQL through a custom ResultSource" technique,
87   # which seems to be the only place the bind attribute is
88   # documented.  Breaking this technique probably breaks existing
89   # application code.
90
91   $rs = $schema->resultset('CustomSql')->search({}, { bind => [ 1999 ] });
92   is ( $rs->count, 1, 'cookbook arbitrary sql example (in separate file)' );
93
94   $rs = $schema->resultset('CustomSql')->search({ 'artistid' => 1 }, { bind => [ 1999 ] });
95   is ( $rs->count, 1, '...cookbook (in separate file) + search condition' );
96
97   $rs = $schema->resultset('CustomSql')->search({}, { bind => [ 1999 ] })
98       ->search({ 'artistid' => 1 });
99   is ( $rs->count, 1, '...cookbook (bind first, in separate file) + chained search' );
100
101   $rs = $schema->resultset('CustomSql')->search({}, { bind => [ 1999 ] })->search({}, { where => \"title LIKE ?", bind => [ 'Spoon%' ] });
102   is_same_sql_bind(
103     $rs->as_query,
104     "(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 ?)",
105     [
106       [ {} => '1999' ],
107       [ {} => 'Spoon%' ]
108     ],
109     'got correct SQL (cookbook arbitrary SQL, in separate file)'
110   );
111 }
112
113 done_testing;