6 use DBICTest ':DiffSQL';
8 my $schema = DBICTest->init_schema;
11 where => \'name like ?',
18 # First, the simple cases...
19 $rs = $schema->resultset('Artist')->search(
24 is ( $rs->count, 1, 'where/bind combined' );
26 $rs= $schema->resultset('Artist')->search({}, $where_bind)
27 ->search({ artistid => 1});
29 is ( $rs->count, 1, 'where/bind first' );
31 $rs = $schema->resultset('Artist')->search({ artistid => 1})
32 ->search({}, $where_bind);
34 is ( $rs->count, 1, 'where/bind last' );
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' );
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
50 my $source = DBICTest::Artist->result_source_instance;
51 my $new_source = $source->new($source);
52 $new_source->source_name('Complex');
54 $new_source->name(\<<'');
55 ( SELECT a.*, cd.cdid AS cdid, cd.title AS title, cd.year AS year
57 JOIN cd ON cd.artist = a.artistid
60 $schema->register_extra_source('Complex' => $new_source);
62 $rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] });
63 is ( $rs->count, 1, 'cookbook arbitrary sql example' );
65 $rs = $schema->resultset('Complex')->search({ 'artistid' => 1 }, { bind => [ 1999 ] });
66 is ( $rs->count, 1, '...cookbook + search condition' );
68 $rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] })
69 ->search({ 'artistid' => 1 });
70 is ( $rs->count, 1, '...cookbook (bind first) + chained search' );
72 $rs = $schema->resultset('Complex')->search({}, { bind => [ [{ sqlt_datatype => 'datetime'} => 1999 ] ] })->search({}, { where => \"title LIKE ?", bind => [ 'Spoon%' ] });
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 ?)",
77 [ { sqlt_datatype => 'datetime' } => '1999' ],
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
91 $rs = $schema->resultset('CustomSql')->search({}, { bind => [ 1999 ] });
92 is ( $rs->count, 1, 'cookbook arbitrary sql example (in separate file)' );
94 $rs = $schema->resultset('CustomSql')->search({ 'artistid' => 1 }, { bind => [ 1999 ] });
95 is ( $rs->count, 1, '...cookbook (in separate file) + search condition' );
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' );
101 $rs = $schema->resultset('CustomSql')->search({}, { bind => [ 1999 ] })->search({}, { where => \"title LIKE ?", bind => [ 'Spoon%' ] });
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 ?)",
109 'got correct SQL (cookbook arbitrary SQL, in separate file)'