6 use DBIC::SqlMakerTest;
10 my $schema = DBICTest->init_schema;
13 eval "use DBD::SQLite";
15 ? ( skip_all => 'needs DBD::SQLite for testing' )
20 where => \'name like ?',
27 local $TODO = 'bind args order needs fixing (semifor)';
29 # First, the simple cases...
30 $rs = $schema->resultset('Artist')->search(
35 is ( $rs->count, 1, 'where/bind combined' );
37 $rs= $schema->resultset('Artist')->search({}, $where_bind)
38 ->search({ artistid => 1});
40 is ( $rs->count, 1, 'where/bind first' );
42 $rs = $schema->resultset('Artist')->search({ artistid => 1})
43 ->search({}, $where_bind);
45 is ( $rs->count, 1, 'where/bind last' );
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 => [ 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 = ?) WHERE title LIKE ?)",
81 [ '!!dummy' => '1999' ],
82 [ '!!dummy' => 'Spoon%' ]
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 = ?) WHERE title LIKE ?)",
110 [ '!!dummy' => '1999' ],
111 [ '!!dummy' => 'Spoon%' ]
113 'got correct SQL (cookbook arbitrary SQL, in separate file)'
118 local $TODO = 'bind args order needs fixing (semifor)';
119 $rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] })
120 ->search({ 'artistid' => 1 }, {
121 where => \'title like ?',
122 bind => [ 'Spoon%' ] });
123 is ( $rs->count, 1, '...cookbook + chained search with extra bind' );