1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
|
<?php
namespace Illuminate\Tests\Integration\Database\MySql;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Str;
/**
* @requires extension pdo_mysql
* @requires OS Linux|Darwin
*/
class JoinLateralTest extends MySqlTestCase
{
protected function afterRefreshingDatabase()
{
Schema::create('users', function (Blueprint $table) {
$table->id('id');
$table->string('name');
});
Schema::create('posts', function (Blueprint $table) {
$table->id('id');
$table->string('title');
$table->integer('rating');
$table->unsignedBigInteger('user_id');
});
}
protected function destroyDatabaseMigrations()
{
Schema::drop('posts');
Schema::drop('users');
}
protected function setUp(): void
{
parent::setUp();
$this->checkMySqlVersion();
DB::table('users')->insert([
['name' => Str::random()],
['name' => Str::random()],
]);
DB::table('posts')->insert([
['title' => Str::random(), 'rating' => 1, 'user_id' => 1],
['title' => Str::random(), 'rating' => 3, 'user_id' => 1],
['title' => Str::random(), 'rating' => 7, 'user_id' => 1],
]);
}
protected function checkMySqlVersion()
{
$mySqlVersion = DB::select('select version()')[0]->{'version()'} ?? '';
if (strpos($mySqlVersion, 'Maria') !== false) {
$this->markTestSkipped('Lateral joins are not supported on MariaDB'.__CLASS__);
} elseif ((float) $mySqlVersion < '8.0.14') {
$this->markTestSkipped('Lateral joins are not supported on MySQL < 8.0.14'.__CLASS__);
}
}
public function testJoinLateral()
{
$subquery = DB::table('posts')
->select('title as best_post_title', 'rating as best_post_rating')
->whereColumn('user_id', 'users.id')
->orderBy('rating', 'desc')
->limit(2);
$userWithPosts = DB::table('users')
->where('id', 1)
->joinLateral($subquery, 'best_post')
->get();
$this->assertCount(2, $userWithPosts);
$this->assertEquals(7, $userWithPosts[0]->best_post_rating);
$this->assertEquals(3, $userWithPosts[1]->best_post_rating);
$userWithoutPosts = DB::table('users')
->where('id', 2)
->joinLateral($subquery, 'best_post')
->get();
$this->assertCount(0, $userWithoutPosts);
}
public function testLeftJoinLateral()
{
$subquery = DB::table('posts')
->select('title as best_post_title', 'rating as best_post_rating')
->whereColumn('user_id', 'users.id')
->orderBy('rating', 'desc')
->limit(2);
$userWithPosts = DB::table('users')
->where('id', 1)
->leftJoinLateral($subquery, 'best_post')
->get();
$this->assertCount(2, $userWithPosts);
$this->assertEquals(7, $userWithPosts[0]->best_post_rating);
$this->assertEquals(3, $userWithPosts[1]->best_post_rating);
$userWithoutPosts = DB::table('users')
->where('id', 2)
->leftJoinLateral($subquery, 'best_post')
->get();
$this->assertCount(1, $userWithoutPosts);
$this->assertNull($userWithoutPosts[0]->best_post_title);
$this->assertNull($userWithoutPosts[0]->best_post_rating);
}
}
|