1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
8 use DBICTest ':DiffSQL';
10 my $schema = DBICTest->init_schema;
13 where => \'name like ?',
20 # First, the simple cases...
21 $rs = $schema->resultset('Artist')->search(
26 is ( $rs->count, 1, 'where/bind combined' );
28 $rs= $schema->resultset('Artist')->search({}, $where_bind)
29 ->search({ artistid => 1});
31 is ( $rs->count, 1, 'where/bind first' );
33 $rs = $schema->resultset('Artist')->search({ artistid => 1})
34 ->search({}, $where_bind);
36 is ( $rs->count, 1, 'where/bind last' );
38 # and the complex case
39 $rs = $schema->resultset('CustomSql')->search({}, { bind => [ 1999 ] })
40 ->search({ 'artistid' => 1 }, {
41 where => \'title like ?',
42 bind => [ 'Spoon%' ] });
43 is ( $rs->count, 1, '...cookbook + chained search with extra bind' );
47 # More complex cases, based primarily on the Cookbook
48 # "Arbitrary SQL through a custom ResultSource" technique,
49 # which seems to be the only place the bind attribute is
50 # documented. Breaking this technique probably breaks existing
52 my $source = DBICTest::Artist->result_source_instance;
53 my $new_source = $source->new($source);
54 $new_source->source_name('Complex');
56 $new_source->name(\<<'');
57 ( SELECT a.*, cd.cdid AS cdid, cd.title AS title, cd.year AS year
59 JOIN cd ON cd.artist = a.artistid
62 $schema->register_extra_source('Complex' => $new_source);
64 $rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] });
65 is ( $rs->count, 1, 'cookbook arbitrary sql example' );
67 $rs = $schema->resultset('Complex')->search({ 'artistid' => 1 }, { bind => [ 1999 ] });
68 is ( $rs->count, 1, '...cookbook + search condition' );
70 $rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] })
71 ->search({ 'artistid' => 1 });
72 is ( $rs->count, 1, '...cookbook (bind first) + chained search' );
74 $rs = $schema->resultset('Complex')->search({}, { bind => [ [{ sqlt_datatype => 'datetime'} => 1999 ] ] })->search({}, { where => \"title LIKE ?", bind => [ 'Spoon%' ] });
77 "(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 ?)",
79 [ { sqlt_datatype => 'datetime' } => '1999' ],
87 # More complex cases, based primarily on the Cookbook
88 # "Arbitrary SQL through a custom ResultSource" technique,
89 # which seems to be the only place the bind attribute is
90 # documented. Breaking this technique probably breaks existing
93 $rs = $schema->resultset('CustomSql')->search({}, { bind => [ 1999 ] });
94 is ( $rs->count, 1, 'cookbook arbitrary sql example (in separate file)' );
96 $rs = $schema->resultset('CustomSql')->search({ 'artistid' => 1 }, { bind => [ 1999 ] });
97 is ( $rs->count, 1, '...cookbook (in separate file) + search condition' );
99 $rs = $schema->resultset('CustomSql')->search({}, { bind => [ 1999 ] })
100 ->search({ 'artistid' => 1 });
101 is ( $rs->count, 1, '...cookbook (bind first, in separate file) + chained search' );
103 $rs = $schema->resultset('CustomSql')->search({}, { bind => [ 1999 ] })->search({}, { where => \"title LIKE ?", bind => [ 'Spoon%' ] });
106 "(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 ?)",
111 'got correct SQL (cookbook arbitrary SQL, in separate file)'