Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions src/Database/Adapter/MariaDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,7 @@ public function createCollection(string $name, array $attributes = [], array $in
$indexLength = $index->getAttribute('lengths')[$nested] ?? '';
$indexLength = (empty($indexLength)) ? '' : '(' . (int)$indexLength . ')';
$indexOrder = $index->getAttribute('orders')[$nested] ?? '';

$indexAttribute = match ($attribute) {
'$id' => '_uid',
'$createdAt' => '_createdAt',
'$updatedAt' => '_updatedAt',
default => $attribute
};

$indexAttribute = $this->getInternalKeyForAttribute($attribute);
$indexAttribute = $this->filter($indexAttribute);

if ($indexType === Database::INDEX_FULLTEXT) {
Expand Down
34 changes: 11 additions & 23 deletions src/Database/Adapter/SQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -1684,30 +1684,18 @@ protected function getAttributeProjection(array $selections, string $prefix = ''
return '*';
}

$selections = \array_diff($selections, ['$id', '$permissions', '$collection']);
Comment thread
abnegate marked this conversation as resolved.
$internalKeys = [
'$id',
'$internalId',
'$permissions',
'$createdAt',
'$updatedAt',
];
Comment on lines +1687 to +1693

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not using the const because we don't want collection or tenant here


$selections[] = $this->getInternalKeyForAttribute('$id');
$selections[] = $this->getInternalKeyForAttribute('$permissions');
$selections = \array_diff($selections, [...$internalKeys, '$collection']);
Comment thread
abnegate marked this conversation as resolved.

if (\in_array('$internalId', $selections)) {
$selections[] = $this->getInternalKeyForAttribute('$internalId');
$selections = \array_diff($selections, ['$internalId']);
}
if (\in_array('$createdAt', $selections)) {
$selections[] = $this->getInternalKeyForAttribute('$createdAt');
$selections = \array_diff($selections, ['$createdAt']);
}
if (\in_array('$updatedAt', $selections)) {
$selections[] = $this->getInternalKeyForAttribute('$updatedAt');
$selections = \array_diff($selections, ['$updatedAt']);
}
if (\in_array('$collection', $selections)) {
$selections[] = $this->getInternalKeyForAttribute('$collection');
$selections = \array_diff($selections, ['$collection']);
}
if (\in_array('$tenant', $selections)) {
$selections[] = $this->getInternalKeyForAttribute('$tenant');
$selections = \array_diff($selections, ['$tenant']);
foreach ($internalKeys as $internalKey) {
$selections[] = $this->getInternalKeyForAttribute($internalKey);
}

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

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

protected function getInternalKeyForAttribute(string $attribute): string
Expand Down
51 changes: 8 additions & 43 deletions src/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -3319,20 +3319,6 @@ public function getDocument(string $collection, string $id, array $queries = [],
}
}

// Remove internal attributes if not queried for select query
// $id, $permissions and $collection are the default selected attributes for (MariaDB, MySQL, SQLite, Postgres)
// All internal attributes are default selected attributes for (MongoDB)
foreach ($queries as $query) {
if ($query->getMethod() === Query::TYPE_SELECT) {
$values = $query->getValues();
foreach ($this->getInternalAttributes() as $internalAttribute) {
if (!\in_array($internalAttribute['$id'], $values)) {
$document->removeAttribute($internalAttribute['$id']);
}
}
}
}

$this->trigger(self::EVENT_DOCUMENT_READ, $document);

return $document;
Expand Down Expand Up @@ -6034,6 +6020,7 @@ public function find(string $collection, array $queries = [], string $forPermiss
if ($this->resolveRelationships && (empty($selects) || !empty($nestedSelections))) {
$node = $this->silent(fn () => $this->populateDocumentRelationships($collection, $node, $nestedSelections));
}

$node = $this->casting($collection, $node);
$node = $this->decode($collection, $node, $selections);

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

unset($query);

// Remove internal attributes which are not queried
foreach ($queries as $query) {
if ($query->getMethod() === Query::TYPE_SELECT) {
$values = $query->getValues();
foreach ($results as $result) {
foreach ($this->getInternalAttributes() as $internalAttribute) {
if (!\in_array($internalAttribute['$id'], $values)) {
$result->removeAttribute($internalAttribute['$id']);
}
}
}
}
}

$this->trigger(self::EVENT_DOCUMENT_FIND, $results);

return $results;
Expand Down Expand Up @@ -6370,20 +6343,12 @@ public function decode(Document $collection, Document $document, array $selectio
}
}

if (empty($selections) || \in_array($key, $selections) || \in_array('*', $selections)) {
if (
empty($selections)
|| \in_array($key, $selections)
|| \in_array('*', $selections)
|| \in_array($key, ['$createdAt', '$updatedAt'])
) {
// Prevent null values being set for createdAt and updatedAt
if (\in_array($key, ['$createdAt', '$updatedAt']) && $value[0] === null) {
continue;
} else {
$document->setAttribute($key, ($array) ? $value : $value[0]);
}
}
if (
empty($selections)
|| \in_array($key, $selections)
|| \in_array('*', $selections)
) {
$document->setAttribute($key, ($array) ? $value : $value[0]);
}
}

Expand Down Expand Up @@ -6565,7 +6530,7 @@ private function validateSelections(Document $collection, array $queries): array
$selections[] = '$updatedAt';
$selections[] = '$permissions';

return $selections;
return \array_values(\array_unique($selections));
}

/**
Expand Down
Loading