7 use DBICTest ':DiffSQL';
9 my $schema = DBICTest->connect_schema (DBICTest->_database, { quote_char => '`' });
11 require DBIx::Class::Storage::DBI::mysql;
12 *DBIx::Class::Storage::DBI::mysql::_get_server_version = sub { 5 };
13 bless ( $schema->storage, 'DBIx::Class::Storage::DBI::mysql' );
15 # check that double-subqueries are properly wrapped
17 # the expected SQL may seem wastefully nonsensical - this is due to
18 # CD's tablename being \'cd', which triggers the "this can be anything"
19 # mode, and forces a subquery. This in turn forces *another* subquery
20 # because mysql is being mysql
21 # Also we know it will fail - never deployed. All we care about is the
22 # SQL to compare, hence the eval
23 $schema->is_executed_sql_bind( sub {
24 eval { $schema->resultset ('CD')->update({ genreid => undef }) }
26 'UPDATE cd SET `genreid` = ? WHERE `cdid` IN ( SELECT * FROM ( SELECT `me`.`cdid` FROM cd `me` ) `_forced_double_subquery` )',
27 [ { dbic_colname => "genreid", sqlt_datatype => "integer" } => undef ],
28 ]], 'Correct update-SQL with double-wrapped subquery' );
30 # same comment as above
31 $schema->is_executed_sql_bind( sub {
32 eval { $schema->resultset ('CD')->delete }
34 'DELETE FROM cd WHERE `cdid` IN ( SELECT * FROM ( SELECT `me`.`cdid` FROM cd `me` ) `_forced_double_subquery` )',
35 ]], 'Correct delete-SQL with double-wrapped subquery' );
37 # and a couple of really contrived examples (we test them live in t/71mysql.t)
38 my $rs = $schema->resultset('Artist')->search({ name => { -like => 'baby_%' } });
39 my ($count_sql, @count_bind) = @${$rs->count_rs->as_query};
40 $schema->is_executed_sql_bind( sub {
42 $schema->resultset('Artist')->search(
44 -in => $rs->get_column('artistid')
47 )->update({ name => \[ "CONCAT( `name`, '_bell_out_of_', $count_sql )", @count_bind ] });
52 SET `name` = CONCAT(`name`, '_bell_out_of_', (
58 ) `_forced_double_subquery`
64 SELECT `me`.`artistid`
67 ) `_forced_double_subquery` )
69 ( [ { dbic_colname => "name", sqlt_datatype => "varchar", sqlt_size => 100 }
74 $schema->is_executed_sql_bind( sub {
76 $schema->resultset('CD')->search_related('artist',
77 { 'artist.name' => { -like => 'baby_with_%' } }
86 SELECT `artist`.`artistid`
88 JOIN `artist` `artist`
89 ON `artist`.`artistid` = `me`.`artist`
90 WHERE `artist`.`name` LIKE ?
91 ) `_forced_double_subquery`
94 [ { dbic_colname => "artist.name", sqlt_datatype => "varchar", sqlt_size => 100 }
99 # Test support for straight joins
101 my $cdsrc = $schema->source('CD');
102 my $artrel_info = $cdsrc->relationship_info ('artist');
103 $cdsrc->add_relationship(
105 $artrel_info->{class},
106 $artrel_info->{cond},
107 { %{$artrel_info->{attrs}}, join_type => 'straight' },
110 $cdsrc->resultset->search({}, { prefetch => 'straight_artist' })->as_query,
112 SELECT `me`.`cdid`, `me`.`artist`, `me`.`title`, `me`.`year`, `me`.`genreid`, `me`.`single_track`,
113 `straight_artist`.`artistid`, `straight_artist`.`name`, `straight_artist`.`rank`, `straight_artist`.`charfield`
115 STRAIGHT_JOIN `artist` `straight_artist` ON `straight_artist`.`artistid` = `me`.`artist`
118 'straight joins correctly supported for mysql'
122 # Test support for inner joins on mysql v3
124 [ 3 => 'INNER JOIN' ],
127 my ($ver, $join_op) = @$_;
129 # we do not care at this point if data is available, just do a reconnect cycle
130 # to clear the server version cache and then get a new maker
132 $schema->storage->disconnect;
133 $schema->storage->_sql_maker(undef);
135 no warnings 'redefine';
136 local *DBIx::Class::Storage::DBI::mysql::_get_server_version = sub { $ver };
138 $schema->storage->ensure_connected;
139 $schema->storage->sql_maker;
143 $schema->resultset('CD')->search ({}, { prefetch => 'artist' })->as_query,
145 SELECT `me`.`cdid`, `me`.`artist`, `me`.`title`, `me`.`year`, `me`.`genreid`, `me`.`single_track`,
146 `artist`.`artistid`, `artist`.`name`, `artist`.`rank`, `artist`.`charfield`
148 $join_op `artist` `artist` ON `artist`.`artistid` = `me`.`artist`
151 "default join type works for version $ver",