7 use DBIC::SqlMakerTest;
9 my $schema = DBICTest->init_schema;
12 where => \'name like ?',
19 # First, the simple cases...
20 $rs = $schema->resultset('Artist')->search(
25 is ( $rs->count, 1, 'where/bind combined' );
27 $rs= $schema->resultset('Artist')->search({}, $where_bind)
28 ->search({ artistid => 1});
30 is ( $rs->count, 1, 'where/bind first' );
32 $rs = $schema->resultset('Artist')->search({ artistid => 1})
33 ->search({}, $where_bind);
35 is ( $rs->count, 1, 'where/bind last' );
37 # and the complex case
38 $rs = $schema->resultset('CustomSql')->search({}, { bind => [ 1999 ] })
39 ->search({ 'artistid' => 1 }, {
40 where => \'title like ?',
41 bind => [ 'Spoon%' ] });
42 is ( $rs->count, 1, '...cookbook + chained search with extra bind' );
46 # More complex cases, based primarily on the Cookbook
47 # "Arbitrary SQL through a custom ResultSource" technique,
48 # which seems to be the only place the bind attribute is
49 # documented. Breaking this technique probably breaks existing
51 my $source = DBICTest::Artist->result_source_instance;
52 my $new_source = $source->new($source);
53 $new_source->source_name('Complex');
55 $new_source->name(\<<'');
56 ( SELECT a.*, cd.cdid AS cdid, cd.title AS title, cd.year AS year
58 JOIN cd ON cd.artist = a.artistid
61 $schema->register_extra_source('Complex' => $new_source);
63 $rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] });
64 is ( $rs->count, 1, 'cookbook arbitrary sql example' );
66 $rs = $schema->resultset('Complex')->search({ 'artistid' => 1 }, { bind => [ 1999 ] });
67 is ( $rs->count, 1, '...cookbook + search condition' );
69 $rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] })
70 ->search({ 'artistid' => 1 });
71 is ( $rs->count, 1, '...cookbook (bind first) + chained search' );
73 $rs = $schema->resultset('Complex')->search({}, { bind => [ [{ sqlt_datatype => 'datetime'} => 1999 ] ] })->search({}, { where => \"title LIKE ?", bind => [ 'Spoon%' ] });
76 "(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 ?)",
78 [ { sqlt_datatype => 'datetime' } => '1999' ],
86 # More complex cases, based primarily on the Cookbook
87 # "Arbitrary SQL through a custom ResultSource" technique,
88 # which seems to be the only place the bind attribute is
89 # documented. Breaking this technique probably breaks existing
92 $rs = $schema->resultset('CustomSql')->search({}, { bind => [ 1999 ] });
93 is ( $rs->count, 1, 'cookbook arbitrary sql example (in separate file)' );
95 $rs = $schema->resultset('CustomSql')->search({ 'artistid' => 1 }, { bind => [ 1999 ] });
96 is ( $rs->count, 1, '...cookbook (in separate file) + search condition' );
98 $rs = $schema->resultset('CustomSql')->search({}, { bind => [ 1999 ] })
99 ->search({ 'artistid' => 1 });
100 is ( $rs->count, 1, '...cookbook (bind first, in separate file) + chained search' );
102 $rs = $schema->resultset('CustomSql')->search({}, { bind => [ 1999 ] })->search({}, { where => \"title LIKE ?", bind => [ 'Spoon%' ] });
105 "(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 ?)",
110 'got correct SQL (cookbook arbitrary SQL, in separate file)'