Skip to content

Commit d256f23

Browse files
authored
Merge pull request #606 from utopia-php/alias-get-document
Default alias getDocument
2 parents 308cbeb + a0e2386 commit d256f23

File tree

3 files changed

+12
-19
lines changed

3 files changed

+12
-19
lines changed

src/Database/Adapter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1070,7 +1070,7 @@ abstract public function getKeywords(): array;
10701070
* @param string $prefix
10711071
* @return mixed
10721072
*/
1073-
abstract protected function getAttributeProjection(array $selections, string $prefix = ''): mixed;
1073+
abstract protected function getAttributeProjection(array $selections, string $prefix): mixed;
10741074

10751075
/**
10761076
* Get all selected attributes from queries

src/Database/Adapter/Pool.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ public function getKeywords(): array
455455
return $this->delegate(__FUNCTION__, \func_get_args());
456456
}
457457

458-
protected function getAttributeProjection(array $selections, string $prefix = ''): mixed
458+
protected function getAttributeProjection(array $selections, string $prefix): mixed
459459
{
460460
return $this->delegate(__FUNCTION__, \func_get_args());
461461
}

src/Database/Adapter/SQL.php

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -337,11 +337,13 @@ public function getDocument(string $collection, string $id, array $queries = [],
337337

338338
$forUpdate = $forUpdate ? 'FOR UPDATE' : '';
339339

340+
$alias = Query::DEFAULT_ALIAS;
341+
340342
$sql = "
341-
SELECT {$this->getAttributeProjection($selections)}
342-
FROM {$this->getSQLTable($name)}
343-
WHERE _uid = :_uid
344-
{$this->getTenantQuery($collection)}
343+
SELECT {$this->getAttributeProjection($selections, $alias)}
344+
FROM {$this->getSQLTable($name)} AS {$this->quote($alias)}
345+
WHERE {$this->quote($alias)}.{$this->quote('_uid')} = :_uid
346+
{$this->getTenantQuery($collection, $alias)}
345347
";
346348

347349
if ($this->getSupportForUpdateLock()) {
@@ -1680,13 +1682,10 @@ public function getTenantQuery(
16801682
* @return mixed
16811683
* @throws Exception
16821684
*/
1683-
protected function getAttributeProjection(array $selections, string $prefix = ''): mixed
1685+
protected function getAttributeProjection(array $selections, string $prefix): mixed
16841686
{
16851687
if (empty($selections) || \in_array('*', $selections)) {
1686-
if (!empty($prefix)) {
1687-
return "{$this->quote($prefix)}.*";
1688-
}
1689-
return '*';
1688+
return "{$this->quote($prefix)}.*";
16901689
}
16911690

16921691
$internalKeys = [
@@ -1703,14 +1702,8 @@ protected function getAttributeProjection(array $selections, string $prefix = ''
17031702
$selections[] = $this->getInternalKeyForAttribute($internalKey);
17041703
}
17051704

1706-
if (!empty($prefix)) {
1707-
foreach ($selections as &$selection) {
1708-
$selection = "{$this->quote($prefix)}.{$this->quote($this->filter($selection))}";
1709-
}
1710-
} else {
1711-
foreach ($selections as &$selection) {
1712-
$selection = "{$this->quote($this->filter($selection))}";
1713-
}
1705+
foreach ($selections as &$selection) {
1706+
$selection = "{$this->quote($prefix)}.{$this->quote($this->filter($selection))}";
17141707
}
17151708

17161709
return \implode(',', $selections);

0 commit comments

Comments
 (0)