From 120925c071d774e61d29b32d774c96b401f1cf7a Mon Sep 17 00:00:00 2001 From: fogelito Date: Wed, 11 Jun 2025 09:37:19 +0300 Subject: [PATCH 1/2] Add default alias --- src/Database/Adapter.php | 2 +- src/Database/Adapter/Pool.php | 2 +- src/Database/Adapter/SQL.php | 64 +++++++++++++++++++++++++---------- 3 files changed, 49 insertions(+), 19 deletions(-) diff --git a/src/Database/Adapter.php b/src/Database/Adapter.php index d3584a437..88fd7d64f 100644 --- a/src/Database/Adapter.php +++ b/src/Database/Adapter.php @@ -1070,7 +1070,7 @@ abstract public function getKeywords(): array; * @param string $prefix * @return mixed */ - abstract protected function getAttributeProjection(array $selections, string $prefix = ''): mixed; + abstract protected function getAttributeProjection(array $selections, string $prefix): mixed; /** * Get all selected attributes from queries diff --git a/src/Database/Adapter/Pool.php b/src/Database/Adapter/Pool.php index b6bc54b08..302338aa9 100644 --- a/src/Database/Adapter/Pool.php +++ b/src/Database/Adapter/Pool.php @@ -455,7 +455,7 @@ public function getKeywords(): array return $this->delegate(__FUNCTION__, \func_get_args()); } - protected function getAttributeProjection(array $selections, string $prefix = ''): mixed + protected function getAttributeProjection(array $selections, string $prefix): mixed { return $this->delegate(__FUNCTION__, \func_get_args()); } diff --git a/src/Database/Adapter/SQL.php b/src/Database/Adapter/SQL.php index 4c335bfba..c3427ae83 100644 --- a/src/Database/Adapter/SQL.php +++ b/src/Database/Adapter/SQL.php @@ -337,11 +337,13 @@ public function getDocument(string $collection, string $id, array $queries = [], $forUpdate = $forUpdate ? 'FOR UPDATE' : ''; + $alias = Query::DEFAULT_ALIAS; + $sql = " - SELECT {$this->getAttributeProjection($selections)} - FROM {$this->getSQLTable($name)} - WHERE _uid = :_uid - {$this->getTenantQuery($collection)} + SELECT {$this->getAttributeProjection($selections, $alias)} + FROM {$this->getSQLTable($name)} AS {$this->quote($alias)} + WHERE {$this->quote($alias)}.{$this->quote('_uid')} = :_uid + {$this->getTenantQuery($collection, $alias)} "; if ($this->getSupportForUpdateLock()) { @@ -1672,6 +1674,43 @@ public function getTenantQuery( return "{$condition} ({$alias}{$dot}_tenant IN ({$bindings}) {$orIsNull})"; } + /** + * Get the SQL projection given the selected attributes + * + * @param array $selects + * @return string + * @throws Exception + */ + protected function addHiddenAttribute(array $selects): string + { + $hash = [Query::DEFAULT_ALIAS => true]; + + foreach ($selects as $select) { + $alias = $select->getAlias(); + if (!isset($hash[$alias])){ + $hash[$alias] = true; + } + } + + $hash = array_keys($hash); + + $strings = []; + + foreach ($hash as $alias) { + $strings[] = $alias.'._uid as '.$this->quote($alias.'::$id'); + $strings[] = $alias.'._id as '.$this->quote($alias.'::$internalId'); + $strings[] = $alias.'._permissions as '.$this->quote($alias.'::$permissions'); + $strings[] = $alias.'._createdAt as '.$this->quote($alias.'::$createdAt'); + $strings[] = $alias.'._updatedAt as '.$this->quote($alias.'::$updatedAt'); + + if ($this->sharedTables) { + $strings[] = $alias.'._tenant as '.$this->quote($alias.'::$tenant'); + } + } + + return ', '.implode(', ', $strings); + } + /** * Get the SQL projection given the selected attributes * @@ -1680,13 +1719,10 @@ public function getTenantQuery( * @return mixed * @throws Exception */ - protected function getAttributeProjection(array $selections, string $prefix = ''): mixed + protected function getAttributeProjection(array $selections, string $prefix): mixed { if (empty($selections) || \in_array('*', $selections)) { - if (!empty($prefix)) { - return "{$this->quote($prefix)}.*"; - } - return '*'; + return "{$this->quote($prefix)}.*"; } $internalKeys = [ @@ -1703,14 +1739,8 @@ protected function getAttributeProjection(array $selections, string $prefix = '' $selections[] = $this->getInternalKeyForAttribute($internalKey); } - if (!empty($prefix)) { - foreach ($selections as &$selection) { - $selection = "{$this->quote($prefix)}.{$this->quote($this->filter($selection))}"; - } - } else { - foreach ($selections as &$selection) { - $selection = "{$this->quote($this->filter($selection))}"; - } + foreach ($selections as &$selection) { + $selection = "{$this->quote($prefix)}.{$this->quote($this->filter($selection))}"; } return \implode(',', $selections); From a0e2386f71cfeef922190242a54ad94c8d69c580 Mon Sep 17 00:00:00 2001 From: fogelito Date: Wed, 11 Jun 2025 09:39:48 +0300 Subject: [PATCH 2/2] Remove function --- src/Database/Adapter/SQL.php | 37 ------------------------------------ 1 file changed, 37 deletions(-) diff --git a/src/Database/Adapter/SQL.php b/src/Database/Adapter/SQL.php index c3427ae83..31bc7e6a3 100644 --- a/src/Database/Adapter/SQL.php +++ b/src/Database/Adapter/SQL.php @@ -1674,43 +1674,6 @@ public function getTenantQuery( return "{$condition} ({$alias}{$dot}_tenant IN ({$bindings}) {$orIsNull})"; } - /** - * Get the SQL projection given the selected attributes - * - * @param array $selects - * @return string - * @throws Exception - */ - protected function addHiddenAttribute(array $selects): string - { - $hash = [Query::DEFAULT_ALIAS => true]; - - foreach ($selects as $select) { - $alias = $select->getAlias(); - if (!isset($hash[$alias])){ - $hash[$alias] = true; - } - } - - $hash = array_keys($hash); - - $strings = []; - - foreach ($hash as $alias) { - $strings[] = $alias.'._uid as '.$this->quote($alias.'::$id'); - $strings[] = $alias.'._id as '.$this->quote($alias.'::$internalId'); - $strings[] = $alias.'._permissions as '.$this->quote($alias.'::$permissions'); - $strings[] = $alias.'._createdAt as '.$this->quote($alias.'::$createdAt'); - $strings[] = $alias.'._updatedAt as '.$this->quote($alias.'::$updatedAt'); - - if ($this->sharedTables) { - $strings[] = $alias.'._tenant as '.$this->quote($alias.'::$tenant'); - } - } - - return ', '.implode(', ', $strings); - } - /** * Get the SQL projection given the selected attributes *