Skip to content

Commit

Permalink
feat: Added lateralJoin support
Browse files Browse the repository at this point in the history
  • Loading branch information
Deploy committed Dec 28, 2024
1 parent b4e6e65 commit 9a97b55
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 1 deletion.
24 changes: 24 additions & 0 deletions src/Query/Concerns/BuildsJoins.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,28 @@ protected function newJoinClause(IlluminateQueryBuilder $parentQuery, $type, $ta
return new JoinClause($parentQuery, $type, $table);
}

/**
* Add a lateral join clause to the query.
*
* @param \Closure|\Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder<*>|string $query
* @param string $as
* @param string $type
* @return $this
*/
public function joinLateral($query, string $as, string $type = 'inner')
{
assert($query instanceof Builder);

$query->importTableAliases($this);
$query->importTableAliases([$as => $as]);
$this->importTableAliases($query);

[$query] = $this->createSub($query);

$expression = $query . ' as ' . $this->grammar->wrapTable($as);

$this->joins[] = $this->newJoinLateralClause($this, $type, new Expression($expression));

return $this;
}
}
2 changes: 1 addition & 1 deletion src/Query/Concerns/CompilesColumns.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ protected function normalizeColumnReferences(IlluminateQueryBuilder $query, stri

$references = explode('.', $column);


$tableAlias = $query->getTableAlias($references[0]);

if (isset($tableAlias)) {
Expand All @@ -184,6 +183,7 @@ protected function normalizeColumnReferences(IlluminateQueryBuilder $query, stri
array_unshift($references, $tableAlias);
}

// geen tableAlias, table is parent...waarom geen tableAlias?
if ($tableAlias === null && array_key_exists($table, $query->tableAliases)) {
array_unshift($references, $query->tableAliases[$table]);
}
Expand Down
1 change: 1 addition & 0 deletions src/Query/Concerns/CompilesJoins.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public function extractTableAndAlias(Builder $query, $join): array

return [$table, $alias];
}

}

$table = (string) $this->wrapTable($join->table);
Expand Down
41 changes: 41 additions & 0 deletions tests/Query/JoinTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,44 @@
'coordinate',
]);
});

test('joinLateral', function () {
$controlledLocations = DB::table('locations')
->whereColumn('locations.led_by', '==', 'characters.id')
->limit(3);

$leadingLadies = DB::table('characters')
->joinLateral(
$controlledLocations,
'controlled_territory',
)
->orderBy('name')
->get();

expect($leadingLadies)->toHaveCount(6);
});


test('joinLateral with selected fields', function () {
$controlledLocations = DB::table('locations')
->select('id as location_id', 'name as location_name')
->whereColumn('locations.led_by', '==', 'characters.id')
->orderBy('name')
->limit(3);

$leadingLadies = DB::table('characters')
->select('id', 'name', 'controlled_territory.location_name as territory_name')
->joinLateral(
$controlledLocations,
'controlled_territory',
)
->orderBy('name')
->get();

expect($leadingLadies)->toHaveCount(6);

expect(($leadingLadies[1])->name)->toBe('Cersei');
expect(($leadingLadies[2])->name)->toBe('Daenerys');
expect(($leadingLadies[2])->territory_name)->toBe('Astapor');
expect(($leadingLadies[5])->name)->toBe('Sansa');
});

0 comments on commit 9a97b55

Please sign in to comment.