6 # Fixed properly in mainline
7 BEGIN { delete $ENV{DBICTEST_SQLITE_USE_FILE} }
12 use DBIC::SqlMakerTest;
15 my $schema = DBICTest::Schema->connect (DBICTest->_database, { quote_char => '`' });
17 require DBIx::Class::Storage::DBI::mysql;
18 bless ( $schema->storage, 'DBIx::Class::Storage::DBI::mysql' );
20 # check that double-subqueries are properly wrapped
23 my $debugobj = DBIC::DebugObj->new (\$sql, \@bind);
24 my $orig_debugobj = $schema->storage->debugobj;
25 my $orig_debug = $schema->storage->debug;
27 $schema->storage->debugobj ($debugobj);
28 $schema->storage->debug (1);
30 # the expected SQL may seem wastefully nonsensical - this is due to
31 # CD's tablename being \'cd', which triggers the "this can be anything"
32 # mode, and forces a subquery. This in turn forces *another* subquery
33 # because mysql is being mysql
34 # Also we know it will fail - never deployed. All we care about is the
36 eval { $schema->resultset ('CD')->update({ genreid => undef }) };
40 'UPDATE cd SET `genreid` = ? WHERE `cdid` IN ( SELECT * FROM ( SELECT `me`.`cdid` FROM cd `me` ) `_forced_double_subquery` )',
42 'Correct update-SQL with double-wrapped subquery',
45 # same comment as above
46 eval { $schema->resultset ('CD')->delete };
50 'DELETE FROM cd WHERE `cdid` IN ( SELECT * FROM ( SELECT `me`.`cdid` FROM cd `me` ) `_forced_double_subquery` )',
52 'Correct delete-SQL with double-wrapped subquery',
55 # and a couple of really contrived examples (we test them live in t/71mysql.t)
56 my $rs = $schema->resultset('Artist')->search({ name => { -like => 'baby_%' } });
57 my ($count_sql, @count_bind) = @${$rs->count_rs->as_query};
59 $schema->resultset('Artist')->search(
61 -in => $rs->get_column('artistid')
64 )->update({ name => \[ "CONCAT( `name`, '_bell_out_of_', $count_sql )", @count_bind ] });
72 SET `name` = CONCAT(`name`, '_bell_out_of_', (
78 ) `_forced_double_subquery`
84 SELECT `me`.`artistid`
87 ) `_forced_double_subquery` )
93 $schema->resultset('CD')->search_related('artist',
94 { 'artist.name' => { -like => 'baby_with_%' } }
103 WHERE `artistid` IN (
106 SELECT `artist`.`artistid`
108 INNER JOIN `artist` `artist`
109 ON `artist`.`artistid` = `me`.`artist`
110 WHERE `artist`.`name` LIKE ?
111 ) `_forced_double_subquery`
117 $schema->storage->debugobj ($orig_debugobj);
118 $schema->storage->debug ($orig_debug);
121 # Test support for straight joins
123 my $cdsrc = $schema->source('CD');
124 my $artrel_info = $cdsrc->relationship_info ('artist');
125 $cdsrc->add_relationship(
127 $artrel_info->{class},
128 $artrel_info->{cond},
129 { %{$artrel_info->{attrs}}, join_type => 'straight' },
132 $cdsrc->resultset->search({}, { prefetch => 'straight_artist' })->as_query,
134 SELECT `me`.`cdid`, `me`.`artist`, `me`.`title`, `me`.`year`, `me`.`genreid`, `me`.`single_track`,
135 `straight_artist`.`artistid`, `straight_artist`.`name`, `straight_artist`.`rank`, `straight_artist`.`charfield`
137 STRAIGHT_JOIN `artist` `straight_artist` ON `straight_artist`.`artistid` = `me`.`artist`
140 'straight joins correctly supported for mysql'