Skip to content

Commit bdd9b71

Browse files
committed
Use LazyPromise for lazy inner joins
1 parent c3f6536 commit bdd9b71

File tree

1 file changed

+34
-31
lines changed

1 file changed

+34
-31
lines changed

src/Repository.php

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Plasma\SQL\QueryBuilder;
66
use Plasma\SQL\QueryExpressions\Fragment;
77
use Ramsey\Uuid\Uuid;
8+
use React\Promise\LazyPromise;
89
use React\Promise\Promise;
910
use React\Promise\PromiseInterface;
1011
use Rx\Observable;
@@ -278,41 +279,43 @@ private function buildTree(array $row, InspectedEntityInterface $entity, string
278279
}
279280

280281
if ($join->getType() === 'inner' && ($join->getLazy() === JoinInterface::IS_LAZY || $entity->getClass() === $join->getEntity()->getClass())) {
281-
$tree[$join->getProperty()] = new Promise(function (callable $resolve, callable $reject) use ($row, $join, $tableKey): void {
282-
foreach ($join->getClause() as $clause) {
283-
if ($row[$this->tableAliases[$tableKey]][$clause->getLocalKey()] === null) {
284-
$resolve(null);
285-
286-
return;
282+
$tree[$join->getProperty()] = new LazyPromise(function () use ($row, $join, $tableKey): PromiseInterface {
283+
return new Promise(function (callable $resolve, callable $reject) use ($row, $join, $tableKey): void {
284+
foreach ($join->getClause() as $clause) {
285+
if ($row[$this->tableAliases[$tableKey]][$clause->getLocalKey()] === null) {
286+
$resolve(null);
287+
288+
return;
289+
}
287290
}
288-
}
289291

290-
$where = [];
291-
292-
foreach ($join->getClause() as $clause) {
293-
$onLeftSide = $clause->getForeignKey();
294-
if ($clause->getForeignFunction() !== null) {
295-
/** @psalm-suppress PossiblyNullOperand */
296-
$onLeftSide = new Fragment($clause->getForeignFunction() . '(' . $onLeftSide . ')');
297-
}
298-
if ($clause->getForeignCast() !== null) {
299-
/** @psalm-suppress PossiblyNullOperand */
300-
$onLeftSide = new Fragment('CAST(' . (string)$onLeftSide . ' AS ' . $clause->getForeignCast() . ')');
292+
$where = [];
293+
294+
foreach ($join->getClause() as $clause) {
295+
$onLeftSide = $clause->getForeignKey();
296+
if ($clause->getForeignFunction() !== null) {
297+
/** @psalm-suppress PossiblyNullOperand */
298+
$onLeftSide = new Fragment($clause->getForeignFunction() . '(' . $onLeftSide . ')');
299+
}
300+
if ($clause->getForeignCast() !== null) {
301+
/** @psalm-suppress PossiblyNullOperand */
302+
$onLeftSide = new Fragment('CAST(' . (string)$onLeftSide . ' AS ' . $clause->getForeignCast() . ')');
303+
}
304+
305+
$where[] = [
306+
$onLeftSide,
307+
'=',
308+
$row[$this->tableAliases[$tableKey]][$clause->getLocalKey()],
309+
];
301310
}
302311

303-
$where[] = [
304-
$onLeftSide,
305-
'=',
306-
$row[$this->tableAliases[$tableKey]][$clause->getLocalKey()],
307-
];
308-
}
309-
310-
$this->client
311-
->getRepository($join->getEntity()
312-
->getClass())
313-
->fetch($where, [], self::SINGLE)
314-
->toPromise()
315-
->then($resolve, $reject);
312+
$this->client
313+
->getRepository($join->getEntity()
314+
->getClass())
315+
->fetch($where, [], self::SINGLE)
316+
->toPromise()
317+
->then($resolve, $reject);
318+
});
316319
});
317320

318321
continue;

0 commit comments

Comments
 (0)