Skip to content

Commit 92da81f

Browse files
committed
Re-force internal attribute selection
1 parent 60591ab commit 92da81f

5 files changed

Lines changed: 110 additions & 237 deletions

File tree

src/Database/Adapter/MariaDB.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,7 @@ public function createCollection(string $name, array $attributes = [], array $in
128128
$indexLength = $index->getAttribute('lengths')[$nested] ?? '';
129129
$indexLength = (empty($indexLength)) ? '' : '(' . (int)$indexLength . ')';
130130
$indexOrder = $index->getAttribute('orders')[$nested] ?? '';
131-
132-
$indexAttribute = match ($attribute) {
133-
'$id' => '_uid',
134-
'$createdAt' => '_createdAt',
135-
'$updatedAt' => '_updatedAt',
136-
default => $attribute
137-
};
138-
131+
$indexAttribute = $this->getInternalKeyForAttribute($attribute);
139132
$indexAttribute = $this->filter($indexAttribute);
140133

141134
if ($indexType === Database::INDEX_FULLTEXT) {

src/Database/Adapter/SQL.php

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1684,30 +1684,18 @@ protected function getAttributeProjection(array $selections, string $prefix = ''
16841684
return '*';
16851685
}
16861686

1687-
$selections = \array_diff($selections, ['$id', '$permissions', '$collection']);
1687+
$internalKeys = [
1688+
'$id',
1689+
'$internalId',
1690+
'$permissions',
1691+
'$createdAt',
1692+
'$updatedAt',
1693+
];
16881694

1689-
$selections[] = $this->getInternalKeyForAttribute('$id');
1690-
$selections[] = $this->getInternalKeyForAttribute('$permissions');
1695+
$selections = \array_diff($selections, [...$internalKeys, '$collection']);
16911696

1692-
if (\in_array('$internalId', $selections)) {
1693-
$selections[] = $this->getInternalKeyForAttribute('$internalId');
1694-
$selections = \array_diff($selections, ['$internalId']);
1695-
}
1696-
if (\in_array('$createdAt', $selections)) {
1697-
$selections[] = $this->getInternalKeyForAttribute('$createdAt');
1698-
$selections = \array_diff($selections, ['$createdAt']);
1699-
}
1700-
if (\in_array('$updatedAt', $selections)) {
1701-
$selections[] = $this->getInternalKeyForAttribute('$updatedAt');
1702-
$selections = \array_diff($selections, ['$updatedAt']);
1703-
}
1704-
if (\in_array('$collection', $selections)) {
1705-
$selections[] = $this->getInternalKeyForAttribute('$collection');
1706-
$selections = \array_diff($selections, ['$collection']);
1707-
}
1708-
if (\in_array('$tenant', $selections)) {
1709-
$selections[] = $this->getInternalKeyForAttribute('$tenant');
1710-
$selections = \array_diff($selections, ['$tenant']);
1697+
foreach ($internalKeys as $internalKey) {
1698+
$selections[] = $this->getInternalKeyForAttribute($internalKey);
17111699
}
17121700

17131701
if (!empty($prefix)) {
@@ -1720,7 +1708,7 @@ protected function getAttributeProjection(array $selections, string $prefix = ''
17201708
}
17211709
}
17221710

1723-
return \implode(', ', $selections);
1711+
return \implode(',', $selections);
17241712
}
17251713

17261714
protected function getInternalKeyForAttribute(string $attribute): string

src/Database/Database.php

Lines changed: 9 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3169,6 +3169,7 @@ public function deleteIndex(string $collection, string $id): bool
31693169
*/
31703170
public function getDocument(string $collection, string $id, array $queries = [], bool $forUpdate = false): Document
31713171
{
3172+
31723173
if ($collection === self::METADATA && $id === self::METADATA) {
31733174
return new Document(self::COLLECTION);
31743175
}
@@ -3319,20 +3320,6 @@ public function getDocument(string $collection, string $id, array $queries = [],
33193320
}
33203321
}
33213322

3322-
// Remove internal attributes if not queried for select query
3323-
// $id, $permissions and $collection are the default selected attributes for (MariaDB, MySQL, SQLite, Postgres)
3324-
// All internal attributes are default selected attributes for (MongoDB)
3325-
foreach ($queries as $query) {
3326-
if ($query->getMethod() === Query::TYPE_SELECT) {
3327-
$values = $query->getValues();
3328-
foreach ($this->getInternalAttributes() as $internalAttribute) {
3329-
if (!\in_array($internalAttribute['$id'], $values)) {
3330-
$document->removeAttribute($internalAttribute['$id']);
3331-
}
3332-
}
3333-
}
3334-
}
3335-
33363323
$this->trigger(self::EVENT_DOCUMENT_READ, $document);
33373324

33383325
return $document;
@@ -6034,6 +6021,7 @@ public function find(string $collection, array $queries = [], string $forPermiss
60346021
if ($this->resolveRelationships && (empty($selects) || !empty($nestedSelections))) {
60356022
$node = $this->silent(fn () => $this->populateDocumentRelationships($collection, $node, $nestedSelections));
60366023
}
6024+
60376025
$node = $this->casting($collection, $node);
60386026
$node = $this->decode($collection, $node, $selections);
60396027

@@ -6044,20 +6032,6 @@ public function find(string $collection, array $queries = [], string $forPermiss
60446032

60456033
unset($query);
60466034

6047-
// Remove internal attributes which are not queried
6048-
foreach ($queries as $query) {
6049-
if ($query->getMethod() === Query::TYPE_SELECT) {
6050-
$values = $query->getValues();
6051-
foreach ($results as $result) {
6052-
foreach ($this->getInternalAttributes() as $internalAttribute) {
6053-
if (!\in_array($internalAttribute['$id'], $values)) {
6054-
$result->removeAttribute($internalAttribute['$id']);
6055-
}
6056-
}
6057-
}
6058-
}
6059-
}
6060-
60616035
$this->trigger(self::EVENT_DOCUMENT_FIND, $results);
60626036

60636037
return $results;
@@ -6370,20 +6344,12 @@ public function decode(Document $collection, Document $document, array $selectio
63706344
}
63716345
}
63726346

6373-
if (empty($selections) || \in_array($key, $selections) || \in_array('*', $selections)) {
6374-
if (
6375-
empty($selections)
6376-
|| \in_array($key, $selections)
6377-
|| \in_array('*', $selections)
6378-
|| \in_array($key, ['$createdAt', '$updatedAt'])
6379-
) {
6380-
// Prevent null values being set for createdAt and updatedAt
6381-
if (\in_array($key, ['$createdAt', '$updatedAt']) && $value[0] === null) {
6382-
continue;
6383-
} else {
6384-
$document->setAttribute($key, ($array) ? $value : $value[0]);
6385-
}
6386-
}
6347+
if (
6348+
empty($selections)
6349+
|| \in_array($key, $selections)
6350+
|| \in_array('*', $selections)
6351+
) {
6352+
$document->setAttribute($key, ($array) ? $value : $value[0]);
63876353
}
63886354
}
63896355

@@ -6565,7 +6531,7 @@ private function validateSelections(Document $collection, array $queries): array
65656531
$selections[] = '$updatedAt';
65666532
$selections[] = '$permissions';
65676533

6568-
return $selections;
6534+
return \array_values(\array_unique($selections));
65696535
}
65706536

65716537
/**

0 commit comments

Comments
 (0)