Unverified Commit c950aab1 authored by Sergei Morozov's avatar Sergei Morozov Committed by GitHub

Merge pull request #3830 from BenMorel/sql-part-join

Introduce Join class in QueryBuilder
parents 6e4599d5 58c104a6
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Query;
/**
* @internal
*/
final class Join
{
/** @var string */
public $type;
/** @var string */
public $table;
/** @var string */
public $alias;
/** @var string|null */
public $condition;
private function __construct(string $type, string $table, string $alias, ?string $condition)
{
$this->type = $type;
$this->table = $table;
$this->alias = $alias;
$this->condition = $condition;
}
public static function inner(string $table, string $alias, ?string $condition) : Join
{
return new self('INNER', $table, $alias, $condition);
}
public static function left(string $table, string $alias, ?string $condition) : Join
{
return new self('LEFT', $table, $alias, $condition);
}
public static function right(string $table, string $alias, ?string $condition) : Join
{
return new self('RIGHT', $table, $alias, $condition);
}
}
......@@ -20,7 +20,6 @@ use function implode;
use function is_array;
use function is_object;
use function key;
use function strtoupper;
use function substr;
/**
......@@ -426,7 +425,7 @@ class QueryBuilder
foreach ($sqlPart as $part) {
$this->sqlParts[$sqlPartName][] = $part;
}
} elseif ($isArray && is_array($sqlPart[key($sqlPart)])) {
} elseif ($isArray && (is_array($sqlPart[key($sqlPart)]) || is_object($sqlPart[key($sqlPart)]))) {
$key = key($sqlPart);
$this->sqlParts[$sqlPartName][$key][] = $sqlPart[$key];
} elseif ($isMultiple) {
......@@ -673,12 +672,7 @@ class QueryBuilder
public function innerJoin(string $fromAlias, string $join, string $alias, ?string $condition = null)
{
return $this->add('join', [
$fromAlias => [
'joinType' => 'inner',
'joinTable' => $join,
'joinAlias' => $alias,
'joinCondition' => $condition,
],
$fromAlias => Join::inner($join, $alias, $condition),
], true);
}
......@@ -702,12 +696,7 @@ class QueryBuilder
public function leftJoin(string $fromAlias, string $join, string $alias, ?string $condition = null)
{
return $this->add('join', [
$fromAlias => [
'joinType' => 'left',
'joinTable' => $join,
'joinAlias' => $alias,
'joinCondition' => $condition,
],
$fromAlias => Join::left($join, $alias, $condition),
], true);
}
......@@ -731,12 +720,7 @@ class QueryBuilder
public function rightJoin(string $fromAlias, string $join, string $alias, ?string $condition = null)
{
return $this->add('join', [
$fromAlias => [
'joinType' => 'right',
'joinTable' => $join,
'joinAlias' => $alias,
'joinCondition' => $condition,
],
$fromAlias => Join::right($join, $alias, $condition),
], true);
}
......@@ -770,7 +754,7 @@ class QueryBuilder
* ->from('counters', 'c')
* ->where('c.id = ?');
*
* // You can optionally programatically build and/or expressions
* // You can optionally programmatically build and/or expressions
* $qb = $conn->createQueryBuilder();
*
* $or = $qb->expr()->orx();
......@@ -1315,17 +1299,19 @@ class QueryBuilder
if (isset($this->sqlParts['join'][$fromAlias])) {
foreach ($this->sqlParts['join'][$fromAlias] as $join) {
if (array_key_exists($join['joinAlias'], $knownAliases)) {
throw NonUniqueAlias::new($join['joinAlias'], array_keys($knownAliases));
/** @var Join $join */
if (array_key_exists($join->alias, $knownAliases)) {
throw NonUniqueAlias::new($join->alias, array_keys($knownAliases));
}
$sql .= ' ' . strtoupper($join['joinType'])
. ' JOIN ' . $join['joinTable'] . ' ' . $join['joinAlias']
. ' ON ' . ((string) $join['joinCondition']);
$knownAliases[$join['joinAlias']] = true;
$sql .= ' ' . $join->type
. ' JOIN ' . $join->table . ' ' . $join->alias
. ' ON ' . ((string) $join->condition);
$knownAliases[$join->alias] = true;
}
foreach ($this->sqlParts['join'][$fromAlias] as $join) {
$sql .= $this->getSQLForJoins($join['joinAlias'], $knownAliases);
/** @var Join $join */
$sql .= $this->getSQLForJoins($join->alias, $knownAliases);
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment