1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
9 use DBICTest ':DiffSQL';
11 my $schema = DBICTest::Schema->connect (DBICTest->_database, { quote_char => '`' });
13 require DBIx::Class::Storage::DBI::mysql;
14 *DBIx::Class::Storage::DBI::mysql::_get_server_version = sub { 5 };
15 bless ( $schema->storage, 'DBIx::Class::Storage::DBI::mysql' );
17 # check that double-subqueries are properly wrapped
19 # the expected SQL may seem wastefully nonsensical - this is due to
20 # CD's tablename being \'cd', which triggers the "this can be anything"
21 # mode, and forces a subquery. This in turn forces *another* subquery
22 # because mysql is being mysql
23 # Also we know it will fail - never deployed. All we care about is the
24 # SQL to compare, hence the eval
25 $schema->is_executed_sql_bind( sub {
26 eval { $schema->resultset ('CD')->update({ genreid => undef }) }
28 'UPDATE cd SET `genreid` = ? WHERE `cdid` IN ( SELECT * FROM ( SELECT `me`.`cdid` FROM cd `me` ) `_forced_double_subquery` )',
29 [ { dbic_colname => "genreid", sqlt_datatype => "integer" } => undef ],
30 ]], 'Correct update-SQL with double-wrapped subquery' );
32 # same comment as above
33 $schema->is_executed_sql_bind( sub {
34 eval { $schema->resultset ('CD')->delete }
36 'DELETE FROM cd WHERE `cdid` IN ( SELECT * FROM ( SELECT `me`.`cdid` FROM cd `me` ) `_forced_double_subquery` )',
37 ]], 'Correct delete-SQL with double-wrapped subquery' );
39 # and a couple of really contrived examples (we test them live in t/71mysql.t)
40 my $rs = $schema->resultset('Artist')->search({ name => { -like => 'baby_%' } });
41 my ($count_sql, @count_bind) = @${$rs->count_rs->as_query};
42 $schema->is_executed_sql_bind( sub {
44 $schema->resultset('Artist')->search(
46 -in => $rs->get_column('artistid')
49 )->update({ name => \[ "CONCAT( `name`, '_bell_out_of_', $count_sql )", @count_bind ] });
54 SET `name` = CONCAT(`name`, '_bell_out_of_', (
60 ) `_forced_double_subquery`
66 SELECT `me`.`artistid`
69 ) `_forced_double_subquery` )
71 ( [ { dbic_colname => "name", sqlt_datatype => "varchar", sqlt_size => 100 }
76 $schema->is_executed_sql_bind( sub {
78 $schema->resultset('CD')->search_related('artist',
79 { 'artist.name' => { -like => 'baby_with_%' } }
88 SELECT `artist`.`artistid`
90 JOIN `artist` `artist`
91 ON `artist`.`artistid` = `me`.`artist`
92 WHERE `artist`.`name` LIKE ?
93 ) `_forced_double_subquery`
96 [ { dbic_colname => "artist.name", sqlt_datatype => "varchar", sqlt_size => 100 }
101 # Test support for straight joins
103 my $cdsrc = $schema->source('CD');
104 my $artrel_info = $cdsrc->relationship_info ('artist');
105 $cdsrc->add_relationship(
107 $artrel_info->{class},
108 $artrel_info->{cond},
109 { %{$artrel_info->{attrs}}, join_type => 'straight' },
112 $cdsrc->resultset->search({}, { prefetch => 'straight_artist' })->as_query,
114 SELECT `me`.`cdid`, `me`.`artist`, `me`.`title`, `me`.`year`, `me`.`genreid`, `me`.`single_track`,
115 `straight_artist`.`artistid`, `straight_artist`.`name`, `straight_artist`.`rank`, `straight_artist`.`charfield`
117 STRAIGHT_JOIN `artist` `straight_artist` ON `straight_artist`.`artistid` = `me`.`artist`
120 'straight joins correctly supported for mysql'
124 # Test support for inner joins on mysql v3
126 [ 3 => 'INNER JOIN' ],
129 my ($ver, $join_op) = @$_;
131 # we do not care at this point if data is available, just do a reconnect cycle
132 # to clear the server version cache and then get a new maker
134 $schema->storage->disconnect;
135 $schema->storage->_sql_maker(undef);
137 no warnings 'redefine';
138 local *DBIx::Class::Storage::DBI::mysql::_get_server_version = sub { $ver };
140 $schema->storage->ensure_connected;
141 $schema->storage->sql_maker;
145 $schema->resultset('CD')->search ({}, { prefetch => 'artist' })->as_query,
147 SELECT `me`.`cdid`, `me`.`artist`, `me`.`title`, `me`.`year`, `me`.`genreid`, `me`.`single_track`,
148 `artist`.`artistid`, `artist`.`name`, `artist`.`rank`, `artist`.`charfield`
150 $join_op `artist` `artist` ON `artist`.`artistid` = `me`.`artist`
153 "default join type works for version $ver",