X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Fbind%2Fattribute.t;h=1a1d0c7d7616778492df807537715eb97fe0947d;hb=f66e15f39f65384d95c4a254b9aefe14f32e49e3;hp=b9946a4967131559d72f21cd86a677be208f5241;hpb=56095091b1629f5482ec4e344fd304d7863ea126;p=dbsrgits%2FDBIx-Class.git diff --git a/t/bind/attribute.t b/t/bind/attribute.t index b9946a4..1a1d0c7 100644 --- a/t/bind/attribute.t +++ b/t/bind/attribute.t @@ -13,7 +13,7 @@ BEGIN { eval "use DBD::SQLite"; plan $@ ? ( skip_all => 'needs DBD::SQLite for testing' ) - : ( tests => 9 ); + : ( tests => 13 ); } my $where_bind = { @@ -45,34 +45,34 @@ TODO: { is ( $rs->count, 1, 'where/bind last' ); } -# More complex cases, based primarily on the Cookbook -# "Arbitrary SQL through a custom ResultSource" technique, -# which seems to be the only place the bind attribute is -# documented. Breaking this technique probably breaks existing -# application code. -my $source = DBICTest::Artist->result_source_instance; -my $new_source = $source->new($source); -$new_source->source_name('Complex'); +{ + # More complex cases, based primarily on the Cookbook + # "Arbitrary SQL through a custom ResultSource" technique, + # which seems to be the only place the bind attribute is + # documented. Breaking this technique probably breaks existing + # application code. + my $source = DBICTest::Artist->result_source_instance; + my $new_source = $source->new($source); + $new_source->source_name('Complex'); -$new_source->name(\<<''); -( 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 = ?) + $new_source->name(\<<''); + ( 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 = ?) -$schema->register_extra_source('Complex' => $new_source); + $schema->register_extra_source('Complex' => $new_source); -$rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] }); -is ( $rs->count, 1, 'cookbook arbitrary sql example' ); + $rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] }); + is ( $rs->count, 1, 'cookbook arbitrary sql example' ); -$rs = $schema->resultset('Complex')->search({ 'artistid' => 1 }, { bind => [ 1999 ] }); -is ( $rs->count, 1, '...coobook + search condition' ); + $rs = $schema->resultset('Complex')->search({ 'artistid' => 1 }, { bind => [ 1999 ] }); + is ( $rs->count, 1, '...cookbook + search condition' ); -$rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] }) - ->search({ 'artistid' => 1 }); -is ( $rs->count, 1, '...cookbook (bind first) + chained search' ); + $rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] }) + ->search({ 'artistid' => 1 }); + is ( $rs->count, 1, '...cookbook (bind first) + chained search' ); -{ $rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] })->search({}, { where => \"title LIKE ?", bind => [ 'Spoon%' ] }); is_same_sql_bind( $rs->as_query, @@ -82,8 +82,36 @@ is ( $rs->count, 1, '...cookbook (bind first) + chained search' ); [ '!!dummy' => 'Spoon%' ] ], 'got correct SQL' -); + ); +} + +{ + # More complex cases, based primarily on the Cookbook + # "Arbitrary SQL through a custom ResultSource" technique, + # which seems to be the only place the bind attribute is + # documented. Breaking this technique probably breaks existing + # application code. + + $rs = $schema->resultset('CustomSql')->search({}, { bind => [ 1999 ] }); + is ( $rs->count, 1, 'cookbook arbitrary sql example (in separate file)' ); + + $rs = $schema->resultset('CustomSql')->search({ 'artistid' => 1 }, { bind => [ 1999 ] }); + is ( $rs->count, 1, '...cookbook (in separate file) + search condition' ); + + $rs = $schema->resultset('CustomSql')->search({}, { bind => [ 1999 ] }) + ->search({ 'artistid' => 1 }); + is ( $rs->count, 1, '...cookbook (bind first, in separate file) + chained search' ); + $rs = $schema->resultset('CustomSql')->search({}, { bind => [ 1999 ] })->search({}, { where => \"title LIKE ?", bind => [ 'Spoon%' ] }); + is_same_sql_bind( + $rs->as_query, + "(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 ?)", + [ + [ '!!dummy' => '1999' ], + [ '!!dummy' => 'Spoon%' ] + ], + 'got correct SQL (cookbook arbitrary SQL, in separate file)' + ); } TODO: {