c4f99c43168f896ea4cd9e2004d64e42a7dedd12
[dbsrgits/DBIx-Class.git] / t / bind / attribute.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 BEGIN {
13     eval "use DBD::SQLite";
14     plan $@
15         ? ( skip_all => 'needs DBD::SQLite for testing' )
16         : ( tests => 13 );
17 }
18
19 my $where_bind = {
20     where => \'name like ?',
21     bind  => [ 'Cat%' ],
22 };
23
24 my $rs;
25
26 TODO: {
27     local $TODO = 'bind args order needs fixing (semifor)';
28
29     # First, the simple cases...
30     $rs = $schema->resultset('Artist')->search(
31             { artistid => 1 },
32             $where_bind,
33     );
34
35     is ( $rs->count, 1, 'where/bind combined' );
36
37     $rs= $schema->resultset('Artist')->search({}, $where_bind)
38         ->search({ artistid => 1});
39
40     is ( $rs->count, 1, 'where/bind first' );
41
42     $rs = $schema->resultset('Artist')->search({ artistid => 1})
43         ->search({}, $where_bind);
44
45     is ( $rs->count, 1, 'where/bind last' );
46 }
47
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');
57
58   $new_source->name(\<<'');
59   ( SELECT a.*, cd.cdid AS cdid, cd.title AS title, cd.year AS year 
60     FROM artist a
61     JOIN cd ON cd.artist = a.artistid
62     WHERE cd.year = ?)
63
64   $schema->register_extra_source('Complex' => $new_source);
65
66   $rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] });
67   is ( $rs->count, 1, 'cookbook arbitrary sql example' );
68
69   $rs = $schema->resultset('Complex')->search({ 'artistid' => 1 }, { bind => [ 1999 ] });
70   is ( $rs->count, 1, '...cookbook + search condition' );
71
72   $rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] })
73       ->search({ 'artistid' => 1 });
74   is ( $rs->count, 1, '...cookbook (bind first) + chained search' );
75
76   $rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] })->search({}, { where => \"title LIKE ?", bind => [ 'Spoon%' ] });
77   is_same_sql_bind(
78     $rs->as_query,
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 ?)",
80     [
81       [ '!!dummy' => '1999' ], 
82       [ '!!dummy' => 'Spoon%' ]
83     ],
84     'got correct SQL'
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' );
104
105   $rs = $schema->resultset('CustomSql')->search({}, { bind => [ 1999 ] })->search({}, { where => \"title LIKE ?", bind => [ 'Spoon%' ] });
106   is_same_sql_bind(
107     $rs->as_query,
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 ?)",
109     [
110       [ '!!dummy' => '1999' ], 
111       [ '!!dummy' => 'Spoon%' ]
112     ],
113     'got correct SQL (cookbook arbitrary SQL, in separate file)'
114   );
115 }
116
117 TODO: {
118     local $TODO = 'bind args order needs fixing (semifor)';
119     $rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] })
120         ->search({ 'artistid' => 1 }, {
121             where => \'title like ?',
122             bind => [ 'Spoon%' ] });
123     is ( $rs->count, 1, '...cookbook + chained search with extra bind' );
124 }