7 use DBIC::SqlMakerTest;
9 my $schema = DBICTest->init_schema;
12 where => \'name like ?',
19 local $TODO = 'bind args order needs fixing (semifor)';
21 # First, the simple cases...
22 $rs = $schema->resultset('Artist')->search(
27 is ( $rs->count, 1, 'where/bind combined' );
29 $rs= $schema->resultset('Artist')->search({}, $where_bind)
30 ->search({ artistid => 1});
32 is ( $rs->count, 1, 'where/bind first' );
34 $rs = $schema->resultset('Artist')->search({ artistid => 1})
35 ->search({}, $where_bind);
37 is ( $rs->count, 1, 'where/bind last' );
39 # and the complex case
40 local $TODO = 'bind args order needs fixing (semifor)';
41 $rs = $schema->resultset('CustomSql')->search({}, { bind => [ 1999 ] })
42 ->search({ 'artistid' => 1 }, {
43 where => \'title like ?',
44 bind => [ 'Spoon%' ] });
45 is ( $rs->count, 1, '...cookbook + chained search with extra bind' );
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
54 my $source = DBICTest::Artist->result_source_instance;
55 my $new_source = $source->new($source);
56 $new_source->source_name('Complex');
58 $new_source->name(\<<'');
59 ( SELECT a.*, cd.cdid AS cdid, cd.title AS title, cd.year AS year
61 JOIN cd ON cd.artist = a.artistid
64 $schema->register_extra_source('Complex' => $new_source);
66 $rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] });
67 is ( $rs->count, 1, 'cookbook arbitrary sql example' );
69 $rs = $schema->resultset('Complex')->search({ 'artistid' => 1 }, { bind => [ 1999 ] });
70 is ( $rs->count, 1, '...cookbook + search condition' );
72 $rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] })
73 ->search({ 'artistid' => 1 });
74 is ( $rs->count, 1, '...cookbook (bind first) + chained search' );
76 $rs = $schema->resultset('Complex')->search({}, { bind => [ [{ sqlt_datatype => 'datetime'} => 1999 ] ] })->search({}, { where => \"title LIKE ?", bind => [ 'Spoon%' ] });
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 ?)",
81 [ { sqlt_datatype => 'datetime' } => '1999' ],
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
95 $rs = $schema->resultset('CustomSql')->search({}, { bind => [ 1999 ] });
96 is ( $rs->count, 1, 'cookbook arbitrary sql example (in separate file)' );
98 $rs = $schema->resultset('CustomSql')->search({ 'artistid' => 1 }, { bind => [ 1999 ] });
99 is ( $rs->count, 1, '...cookbook (in separate file) + search condition' );
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' );
105 $rs = $schema->resultset('CustomSql')->search({}, { bind => [ 1999 ] })->search({}, { where => \"title LIKE ?", bind => [ 'Spoon%' ] });
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 ?)",
113 'got correct SQL (cookbook arbitrary SQL, in separate file)'