6 use DBIC::SqlMakerTest;
10 my $schema = DBICTest->init_schema;
13 where => \'name like ?',
20 local $TODO = 'bind args order needs fixing (semifor)';
22 # First, the simple cases...
23 $rs = $schema->resultset('Artist')->search(
28 is ( $rs->count, 1, 'where/bind combined' );
30 $rs= $schema->resultset('Artist')->search({}, $where_bind)
31 ->search({ artistid => 1});
33 is ( $rs->count, 1, 'where/bind first' );
35 $rs = $schema->resultset('Artist')->search({ artistid => 1})
36 ->search({}, $where_bind);
38 is ( $rs->count, 1, 'where/bind last' );
40 # and the complex case
41 local $TODO = 'bind args order needs fixing (semifor)';
42 $rs = $schema->resultset('CustomSql')->search({}, { bind => [ 1999 ] })
43 ->search({ 'artistid' => 1 }, {
44 where => \'title like ?',
45 bind => [ 'Spoon%' ] });
46 is ( $rs->count, 1, '...cookbook + chained search with extra bind' );
50 # More complex cases, based primarily on the Cookbook
51 # "Arbitrary SQL through a custom ResultSource" technique,
52 # which seems to be the only place the bind attribute is
53 # documented. Breaking this technique probably breaks existing
55 my $source = DBICTest::Artist->result_source_instance;
56 my $new_source = $source->new($source);
57 $new_source->source_name('Complex');
59 $new_source->name(\<<'');
60 ( SELECT a.*, cd.cdid AS cdid, cd.title AS title, cd.year AS year
62 JOIN cd ON cd.artist = a.artistid
65 $schema->register_extra_source('Complex' => $new_source);
67 $rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] });
68 is ( $rs->count, 1, 'cookbook arbitrary sql example' );
70 $rs = $schema->resultset('Complex')->search({ 'artistid' => 1 }, { bind => [ 1999 ] });
71 is ( $rs->count, 1, '...cookbook + search condition' );
73 $rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] })
74 ->search({ 'artistid' => 1 });
75 is ( $rs->count, 1, '...cookbook (bind first) + chained search' );
77 $rs = $schema->resultset('Complex')->search({}, { bind => [ [{ sqlt_datatype => 'datetime'} => 1999 ] ] })->search({}, { where => \"title LIKE ?", bind => [ 'Spoon%' ] });
80 "(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 ?)",
82 [ { sqlt_datatype => 'datetime' } => '1999' ],
90 # More complex cases, based primarily on the Cookbook
91 # "Arbitrary SQL through a custom ResultSource" technique,
92 # which seems to be the only place the bind attribute is
93 # documented. Breaking this technique probably breaks existing
96 $rs = $schema->resultset('CustomSql')->search({}, { bind => [ 1999 ] });
97 is ( $rs->count, 1, 'cookbook arbitrary sql example (in separate file)' );
99 $rs = $schema->resultset('CustomSql')->search({ 'artistid' => 1 }, { bind => [ 1999 ] });
100 is ( $rs->count, 1, '...cookbook (in separate file) + search condition' );
102 $rs = $schema->resultset('CustomSql')->search({}, { bind => [ 1999 ] })
103 ->search({ 'artistid' => 1 });
104 is ( $rs->count, 1, '...cookbook (bind first, in separate file) + chained search' );
106 $rs = $schema->resultset('CustomSql')->search({}, { bind => [ 1999 ] })->search({}, { where => \"title LIKE ?", bind => [ 'Spoon%' ] });
109 "(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 ?)",
114 'got correct SQL (cookbook arbitrary SQL, in separate file)'