POD fixes
[dbsrgits/DBIx-Class.git] / t / 47bind_attribute.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
12BEGIN {
13 eval "use DBD::SQLite";
14 plan $@
15 ? ( skip_all => 'needs DBD::SQLite for testing' )
07a287ce 16 : ( tests => 9 );
1c133e22 17}
18
1c133e22 19my $where_bind = {
20 where => \'name like ?',
21 bind => [ 'Cat%' ],
22};
23
24my $rs;
25
26TODO: {
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# More complex cases, based primarily on the Cookbook
49# "Arbitrary SQL through a custom ResultSource" technique,
50# which seems to be the only place the bind attribute is
51# documented. Breaking this technique probably breaks existing
52# application code.
53my $source = DBICTest::Artist->result_source_instance;
54my $new_source = $source->new($source);
55$new_source->source_name('Complex');
56
57$new_source->name(\<<'');
07a287ce 58( SELECT a.*, cd.cdid AS cdid, cd.title AS title, cd.year AS year
59 FROM artist a
60 JOIN cd ON cd.artist = a.artistid
61 WHERE cd.year = ?)
1c133e22 62
6eec9003 63$schema->register_extra_source('Complex' => $new_source);
1c133e22 64
65$rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] });
66is ( $rs->count, 1, 'cookbook arbitrary sql example' );
67
68$rs = $schema->resultset('Complex')->search({ 'artistid' => 1 }, { bind => [ 1999 ] });
69is ( $rs->count, 1, '...coobook + search condition' );
70
71$rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] })
72 ->search({ 'artistid' => 1 });
73is ( $rs->count, 1, '...cookbook (bind first) + chained search' );
74
07a287ce 75{
76 $rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] })->search({}, { where => \"title LIKE ?", bind => [ 'Spoon%' ] });
77 my ($sql, @bind) = @${$rs->as_query};
78 is_same_sql_bind(
79 $sql, \@bind,
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 = ?) WHERE title LIKE ?)",
81 [
82 [ '!!dummy' => '1999' ],
83 [ '!!dummy' => 'Spoon%' ]
84 ],
85 'got correct SQL'
86);
dbf95416 87
07a287ce 88}
89
90TODO: {
1c133e22 91 local $TODO = 'bind args order needs fixing (semifor)';
92 $rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] })
93 ->search({ 'artistid' => 1 }, {
94 where => \'title like ?',
95 bind => [ 'Spoon%' ] });
96 is ( $rs->count, 1, '...cookbook + chained search with extra bind' );
97}