Skip to content

Commit 120925c

Browse files
committed
Add default alias
1 parent 308cbeb commit 120925c

File tree

3 files changed

+49
-19
lines changed

3 files changed

+49
-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: 47 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()) {
@@ -1672,6 +1674,43 @@ public function getTenantQuery(
16721674
return "{$condition} ({$alias}{$dot}_tenant IN ({$bindings}) {$orIsNull})";
16731675
}
16741676

1677+
/**
1678+
* Get the SQL projection given the selected attributes
1679+
*
1680+
* @param array<Query> $selects
1681+
* @return string
1682+
* @throws Exception
1683+
*/
1684+
protected function addHiddenAttribute(array $selects): string
1685+
{
1686+
$hash = [Query::DEFAULT_ALIAS => true];
1687+
1688+
foreach ($selects as $select) {
1689+
$alias = $select->getAlias();
1690+
if (!isset($hash[$alias])){
1691+
$hash[$alias] = true;
1692+
}
1693+
}
1694+
1695+
$hash = array_keys($hash);
1696+
1697+
$strings = [];
1698+
1699+
foreach ($hash as $alias) {
1700+
$strings[] = $alias.'._uid as '.$this->quote($alias.'::$id');
1701+
$strings[] = $alias.'._id as '.$this->quote($alias.'::$internalId');
1702+
$strings[] = $alias.'._permissions as '.$this->quote($alias.'::$permissions');
1703+
$strings[] = $alias.'._createdAt as '.$this->quote($alias.'::$createdAt');
1704+
$strings[] = $alias.'._updatedAt as '.$this->quote($alias.'::$updatedAt');
1705+
1706+
if ($this->sharedTables) {
1707+
$strings[] = $alias.'._tenant as '.$this->quote($alias.'::$tenant');
1708+
}
1709+
}
1710+
1711+
return ', '.implode(', ', $strings);
1712+
}
1713+
16751714
/**
16761715
* Get the SQL projection given the selected attributes
16771716
*
@@ -1680,13 +1719,10 @@ public function getTenantQuery(
16801719
* @return mixed
16811720
* @throws Exception
16821721
*/
1683-
protected function getAttributeProjection(array $selections, string $prefix = ''): mixed
1722+
protected function getAttributeProjection(array $selections, string $prefix): mixed
16841723
{
16851724
if (empty($selections) || \in_array('*', $selections)) {
1686-
if (!empty($prefix)) {
1687-
return "{$this->quote($prefix)}.*";
1688-
}
1689-
return '*';
1725+
return "{$this->quote($prefix)}.*";
16901726
}
16911727

16921728
$internalKeys = [
@@ -1703,14 +1739,8 @@ protected function getAttributeProjection(array $selections, string $prefix = ''
17031739
$selections[] = $this->getInternalKeyForAttribute($internalKey);
17041740
}
17051741

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-
}
1742+
foreach ($selections as &$selection) {
1743+
$selection = "{$this->quote($prefix)}.{$this->quote($this->filter($selection))}";
17141744
}
17151745

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

0 commit comments

Comments
 (0)