From 3b2658e423054e42d9caceeec7b675a0180d37ba Mon Sep 17 00:00:00 2001 From: Madeorsk Date: Sat, 9 Nov 2024 10:30:16 +0100 Subject: [PATCH] Allow to specify a custom alias for joined tables. --- src/PostgreSql/PostgreSqlAdapter.php | 3 ++- src/Query/Join/HasJoin.php | 25 +++++++++++++++---------- src/Query/Join/JoinBuilder.php | 3 ++- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/src/PostgreSql/PostgreSqlAdapter.php b/src/PostgreSql/PostgreSqlAdapter.php index 4fada0b..20308ee 100644 --- a/src/PostgreSql/PostgreSqlAdapter.php +++ b/src/PostgreSql/PostgreSqlAdapter.php @@ -277,7 +277,8 @@ class PostgreSqlAdapter extends DatabaseAdapter foreach ($joins as $join) { // For each join clause, build its conditions and append the built bindings. [$onSql, $onBindings] = $this->buildWhere($join->getConditions(), "ON"); - $sql .= "$join->type JOIN \"$join->table\" $onSql "; + $asSql = !empty($join->as) ? " AS $join->as" : ""; + $sql .= "$join->type JOIN \"$join->table\"$asSql $onSql "; array_push($bindings, ...$onBindings); } diff --git a/src/Query/Join/HasJoin.php b/src/Query/Join/HasJoin.php index d6162ef..37afd7a 100644 --- a/src/Query/Join/HasJoin.php +++ b/src/Query/Join/HasJoin.php @@ -27,11 +27,12 @@ trait HasJoin * Create a new join clause. * @param string $type Type of the join clause. * @param string $table Joined table. + * @param string|null $as Joined table alias. * @return JoinBuilder New join clause builder. */ - public function join(string $type, string $table): JoinBuilder + public function join(string $type, string $table, ?string $as = null): JoinBuilder { - $join = new JoinBuilder($this, $type, $table); + $join = new JoinBuilder($this, $type, $table, $as); $this->joins[] = $join; return $join; } @@ -39,40 +40,44 @@ trait HasJoin /** * Create a new inner join clause. * @param string $table Joined table. + * @param string|null $as Joined table alias. * @return JoinBuilder New join clause builder. */ - public function innerJoin(string $table): JoinBuilder + public function innerJoin(string $table, ?string $as = null): JoinBuilder { - return $this->join(JoinBuilder::INNER, $table); + return $this->join(JoinBuilder::INNER, $table, $as); } /** * Create a new outer join clause. * @param string $table Joined table. + * @param string|null $as Joined table alias. * @return JoinBuilder New join clause builder. */ - public function outerJoin(string $table): JoinBuilder + public function outerJoin(string $table, ?string $as = null): JoinBuilder { - return $this->join(JoinBuilder::OUTER, $table); + return $this->join(JoinBuilder::OUTER, $table, $as); } /** * Create a new left join clause. * @param string $table Joined table. + * @param string|null $as Joined table alias. * @return JoinBuilder New join clause builder. */ - public function leftJoin(string $table): JoinBuilder + public function leftJoin(string $table, ?string $as = null): JoinBuilder { - return $this->join(JoinBuilder::LEFT, $table); + return $this->join(JoinBuilder::LEFT, $table, $as); } /** * Create a new right join clause. * @param string $table Joined table. + * @param string|null $as Joined table alias. * @return JoinBuilder New join clause builder. */ - public function rightJoin(string $table): JoinBuilder + public function rightJoin(string $table, ?string $as = null): JoinBuilder { - return $this->join(JoinBuilder::RIGHT, $table); + return $this->join(JoinBuilder::RIGHT, $table, $as); } } diff --git a/src/Query/Join/JoinBuilder.php b/src/Query/Join/JoinBuilder.php index 8965a0f..2a044dd 100644 --- a/src/Query/Join/JoinBuilder.php +++ b/src/Query/Join/JoinBuilder.php @@ -15,8 +15,9 @@ class JoinBuilder * @param mixed $query The calling query. * @param string $type Join type (INNER, OUTER, LEFT, RIGHT, ...). * @param string $table The table to join. + * @param string|null $as Joined table alias. */ - public function __construct(protected mixed $query, public readonly string $type, public readonly string $table) + public function __construct(protected mixed $query, public readonly string $type, public readonly string $table, public readonly ?string $as = null) {} /**