Properly detect and test mysql v3 default JOIN behavior
[dbsrgits/DBIx-Class.git] / t / sqlmaker / mysql.t
index 9de4c7f..b5ce8a5 100644 (file)
@@ -12,6 +12,7 @@ use DBIC::DebugObj;
 my $schema = DBICTest::Schema->connect (DBICTest->_database, { quote_char => '`' });
 # cheat
 require DBIx::Class::Storage::DBI::mysql;
+*DBIx::Class::Storage::DBI::mysql::_get_server_version = sub { 5 };
 bless ( $schema->storage, 'DBIx::Class::Storage::DBI::mysql' );
 
 # check that double-subqueries are properly wrapped
@@ -49,7 +50,7 @@ bless ( $schema->storage, 'DBIx::Class::Storage::DBI::mysql' );
     'Correct delete-SQL with double-wrapped subquery',
   );
 
-  # and a really contrived example (we test it live in t/71mysql.t)
+  # and a couple of really contrived examples (we test them live in t/71mysql.t)
   my $rs = $schema->resultset('Artist')->search({ name => { -like => 'baby_%' } });
   my ($count_sql, @count_bind) = @${$rs->count_rs->as_query};
   eval {
@@ -86,8 +87,84 @@ bless ( $schema->storage, 'DBIx::Class::Storage::DBI::mysql' );
     [ ("'baby_%'") x 2 ],
   );
 
+  eval {
+    $schema->resultset('CD')->search_related('artist',
+      { 'artist.name' => { -like => 'baby_with_%' } }
+    )->delete
+  };
+
+  is_same_sql_bind (
+    $sql,
+    \@bind,
+    q(
+      DELETE FROM `artist`
+      WHERE `artistid` IN (
+        SELECT *
+          FROM (
+            SELECT `artist`.`artistid`
+              FROM cd `me`
+              JOIN `artist` `artist`
+                ON `artist`.`artistid` = `me`.`artist`
+            WHERE `artist`.`name` LIKE ?
+          ) `_forced_double_subquery`
+      )
+    ),
+    [ "'baby_with_%'" ],
+  );
+
   $schema->storage->debugobj ($orig_debugobj);
   $schema->storage->debug ($orig_debug);
 }
 
+# Test support for straight joins
+{
+  my $cdsrc = $schema->source('CD');
+  my $artrel_info = $cdsrc->relationship_info ('artist');
+  $cdsrc->add_relationship(
+    'straight_artist',
+    $artrel_info->{class},
+    $artrel_info->{cond},
+    { %{$artrel_info->{attrs}}, join_type => 'straight' },
+  );
+  is_same_sql_bind (
+    $cdsrc->resultset->search({}, { prefetch => 'straight_artist' })->as_query,
+    '(
+      SELECT `me`.`cdid`, `me`.`artist`, `me`.`title`, `me`.`year`, `me`.`genreid`, `me`.`single_track`,
+             `straight_artist`.`artistid`, `straight_artist`.`name`, `straight_artist`.`rank`, `straight_artist`.`charfield`
+        FROM cd `me`
+        STRAIGHT_JOIN `artist` `straight_artist` ON `straight_artist`.`artistid` = `me`.`artist`
+    )',
+    [],
+    'straight joins correctly supported for mysql'
+  );
+}
+
+# Test support for inner joins on mysql v3
+for (
+  [ 3 => 'INNER JOIN' ],
+  [ 4 => 'JOIN' ],
+) {
+  my ($ver, $join_op) = @$_;
+
+  no warnings 'redefine';
+  local *DBIx::Class::Storage::DBI::mysql::_get_server_version = sub { $ver };
+
+  # we do not care at this point if data is available, just do a reconnect cycle
+  # to clear all caches
+  $schema->storage->disconnect;
+  $schema->storage->ensure_connected;
+
+  is_same_sql_bind (
+    $schema->resultset('CD')->search ({}, { prefetch => 'artist' })->as_query,
+    "(
+      SELECT `me`.`cdid`, `me`.`artist`, `me`.`title`, `me`.`year`, `me`.`genreid`, `me`.`single_track`,
+             `artist`.`artistid`, `artist`.`name`, `artist`.`rank`, `artist`.`charfield`
+        FROM cd `me`
+        $join_op `artist` `artist` ON `artist`.`artistid` = `me`.`artist`
+    )",
+    [],
+    "default join type works for version $ver",
+  );
+}
+
 done_testing;