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' );
42 # More complex cases, based primarily on the Cookbook
43 # "Arbitrary SQL through a custom ResultSource" technique,
44 # which seems to be the only place the bind attribute is
45 # documented. Breaking this technique probably breaks existing
47 my $source = DBICTest::Artist->result_source_instance;
48 my $new_source = $source->new($source);
49 $new_source->source_name('Complex');
51 $new_source->name(\<<'');
52 ( SELECT a.*, cd.cdid AS cdid, cd.title AS title, cd.year AS year
54 JOIN cd ON cd.artist = a.artistid
57 $schema->register_extra_source('Complex' => $new_source);
59 $rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] });
60 is ( $rs->count, 1, 'cookbook arbitrary sql example' );
62 $rs = $schema->resultset('Complex')->search({ 'artistid' => 1 }, { bind => [ 1999 ] });
63 is ( $rs->count, 1, '...cookbook + search condition' );
65 $rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] })
66 ->search({ 'artistid' => 1 });
67 is ( $rs->count, 1, '...cookbook (bind first) + chained search' );
69 $rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] })->search({}, { where => \"title LIKE ?", bind => [ 'Spoon%' ] });
72 "(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 ?)",
74 [ '!!dummy' => '1999' ],
75 [ '!!dummy' => 'Spoon%' ]
82 # More complex cases, based primarily on the Cookbook
83 # "Arbitrary SQL through a custom ResultSource" technique,
84 # which seems to be the only place the bind attribute is
85 # documented. Breaking this technique probably breaks existing
88 $rs = $schema->resultset('CustomSql')->search({}, { bind => [ 1999 ] });
89 is ( $rs->count, 1, 'cookbook arbitrary sql example (in separate file)' );
91 $rs = $schema->resultset('CustomSql')->search({ 'artistid' => 1 }, { bind => [ 1999 ] });
92 is ( $rs->count, 1, '...cookbook (in separate file) + search condition' );
94 $rs = $schema->resultset('CustomSql')->search({}, { bind => [ 1999 ] })
95 ->search({ 'artistid' => 1 });
96 is ( $rs->count, 1, '...cookbook (bind first, in separate file) + chained search' );
98 $rs = $schema->resultset('CustomSql')->search({}, { bind => [ 1999 ] })->search({}, { where => \"title LIKE ?", bind => [ 'Spoon%' ] });
101 "(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 ?)",
103 [ '!!dummy' => '1999' ],
104 [ '!!dummy' => 'Spoon%' ]
106 'got correct SQL (cookbook arbitrary SQL, in separate file)'
111 local $TODO = 'bind args order needs fixing (semifor)';
112 $rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] })
113 ->search({ 'artistid' => 1 }, {
114 where => \'title like ?',
115 bind => [ 'Spoon%' ] });
116 is ( $rs->count, 1, '...cookbook + chained search with extra bind' );