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