Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce Join class in QueryBuilder #3830

Merged
merged 1 commit into from
Jan 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions lib/Doctrine/DBAL/Query/Join.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?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);
}
}
42 changes: 14 additions & 28 deletions lib/Doctrine/DBAL/Query/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
use function is_array;
use function is_object;
use function key;
use function strtoupper;
use function substr;

/**
Expand Down Expand Up @@ -426,7 +425,7 @@ public function add(string $sqlPartName, $sqlPart, bool $append = false) : self
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)]))) {
BenMorel marked this conversation as resolved.
Show resolved Hide resolved
$key = key($sqlPart);
$this->sqlParts[$sqlPartName][$key][] = $sqlPart[$key];
} elseif ($isMultiple) {
Expand Down Expand Up @@ -673,12 +672,7 @@ public function join(string $fromAlias, string $join, string $alias, ?string $co
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);
}

Expand All @@ -702,12 +696,7 @@ public function innerJoin(string $fromAlias, string $join, string $alias, ?strin
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);
}

Expand All @@ -731,12 +720,7 @@ public function leftJoin(string $fromAlias, string $join, string $alias, ?string
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);
}

Expand Down Expand Up @@ -770,7 +754,7 @@ public function set(string $key, string $value)
* ->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();
Expand Down Expand Up @@ -1315,17 +1299,19 @@ private function getSQLForJoins(string $fromAlias, array &$knownAliases) : strin

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);
}
}

Expand Down