Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix nested id queries not working #443

Merged
merged 3 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 6 additions & 0 deletions src/Database/Adapter/MariaDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -1651,6 +1651,8 @@ public function find(string $collection, array $queries = [], ?int $limit = 25,
$where = [];
$orders = [];

$queries = array_map(fn ($query) => clone $query, $queries);

$orderAttributes = \array_map(fn ($orderAttribute) => match ($orderAttribute) {
'$id' => '_uid',
'$internalId' => '_id',
Expand Down Expand Up @@ -1852,6 +1854,8 @@ public function count(string $collection, array $queries = [], ?int $max = null)
$where = [];
$limit = \is_null($max) ? '' : 'LIMIT :max';

$queries = array_map(fn ($query) => clone $query, $queries);

$conditions = $this->getSQLConditions($queries);
if(!empty($conditions)) {
$where[] = $conditions;
Expand Down Expand Up @@ -1923,6 +1927,8 @@ public function sum(string $collection, string $attribute, array $queries = [],
$where = [];
$limit = \is_null($max) ? '' : 'LIMIT :max';

$queries = array_map(fn ($query) => clone $query, $queries);

foreach ($queries as $query) {
$where[] = $this->getSQLCondition($query);
}
Expand Down
4 changes: 4 additions & 0 deletions src/Database/Adapter/Mongo.php
Original file line number Diff line number Diff line change
Expand Up @@ -951,6 +951,7 @@ public function updateAttribute(string $collection, string $id, string $type, in
public function find(string $collection, array $queries = [], ?int $limit = 25, ?int $offset = null, array $orderAttributes = [], array $orderTypes = [], array $cursor = [], string $cursorDirection = Database::CURSOR_AFTER): array
{
$name = $this->getNamespace() . '_' . $this->filter($collection);
$queries = array_map(fn ($query) => clone $query, $queries);

$filters = $this->buildFilters($queries);

Expand Down Expand Up @@ -1212,6 +1213,8 @@ public function count(string $collection, array $queries = [], ?int $max = null)
{
$name = $this->getNamespace() . '_' . $this->filter($collection);

$queries = array_map(fn ($query) => clone $query, $queries);

$filters = [];
$options = [];

Expand Down Expand Up @@ -1252,6 +1255,7 @@ public function sum(string $collection, string $attribute, array $queries = [],
$name = $this->getNamespace() . '_' . $this->filter($collection);

// queries
$queries = array_map(fn ($query) => clone $query, $queries);
$filters = $this->buildFilters($queries);

// permissions
Expand Down
6 changes: 6 additions & 0 deletions src/Database/Adapter/Postgres.php
Original file line number Diff line number Diff line change
Expand Up @@ -1578,6 +1578,8 @@ public function find(string $collection, array $queries = [], ?int $limit = 25,
$where = [];
$orders = [];

$queries = array_map(fn ($query) => clone $query, $queries);

$orderAttributes = \array_map(fn ($orderAttribute) => match ($orderAttribute) {
'$id' => '_uid',
'$internalId' => '_id',
Expand Down Expand Up @@ -1773,6 +1775,8 @@ public function count(string $collection, array $queries = [], ?int $max = null)
$where = [];
$limit = \is_null($max) ? '' : 'LIMIT :max';

$queries = array_map(fn ($query) => clone $query, $queries);

$conditions = $this->getSQLConditions($queries);
if(!empty($conditions)) {
$where[] = $conditions;
Expand Down Expand Up @@ -1837,6 +1841,8 @@ public function sum(string $collection, string $attribute, array $queries = [],
$where = [];
$limit = \is_null($max) ? '' : 'LIMIT :max';

$queries = array_map(fn ($query) => clone $query, $queries);

foreach ($queries as $query) {
$where[] = $this->getSQLCondition($query);
}
Expand Down
9 changes: 9 additions & 0 deletions src/Database/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ public function __construct(string $method, string $attribute = '', array $value
$this->values = $values;
}

public function __clone(): void
{
foreach ($this->values as $index => $value) {
if ($value instanceof self) {
$this->values[$index] = clone $value;
}
}
}

/**
* @return string
*/
Expand Down
62 changes: 62 additions & 0 deletions tests/e2e/Adapter/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -2903,6 +2903,7 @@ public function testFind(): array
Permission::delete(Role::user('1x')),
Permission::delete(Role::user('2x')),
],
'$id' => 'wip2',
'name' => 'Work in Progress 2',
'director' => 'TBD',
'year' => 2026,
Expand Down Expand Up @@ -4150,6 +4151,67 @@ public function testAndNested(): void
$this->assertEquals(3, $count);
}

public function testNestedIDQueries(): void
{
Authorization::setRole(Role::any()->toString());

static::getDatabase()->createCollection('movies_nested_id', permissions: [
Permission::create(Role::any()),
Permission::update(Role::users())
]);

$this->assertEquals(true, static::getDatabase()->createAttribute('movies_nested_id', 'name', Database::VAR_STRING, 128, true));

static::getDatabase()->createDocument('movies_nested_id', new Document([
'$id' => ID::custom('1'),
'$permissions' => [
Permission::read(Role::any()),
Permission::create(Role::any()),
Permission::update(Role::any()),
Permission::delete(Role::any()),
],
'name' => '1',
]));

static::getDatabase()->createDocument('movies_nested_id', new Document([
'$id' => ID::custom('2'),
'$permissions' => [
Permission::read(Role::any()),
Permission::create(Role::any()),
Permission::update(Role::any()),
Permission::delete(Role::any()),
],
'name' => '2',
]));

static::getDatabase()->createDocument('movies_nested_id', new Document([
'$id' => ID::custom('3'),
'$permissions' => [
Permission::read(Role::any()),
Permission::create(Role::any()),
Permission::update(Role::any()),
Permission::delete(Role::any()),
],
'name' => '3',
]));

$queries = [
Query::or([
Query::equal('$id', ["1"]),
Query::equal('$id', ["2"])
])
];

$documents = static::getDatabase()->find('movies_nested_id', $queries);
$this->assertCount(2, $documents);

// Make sure the query was not modified by reference
$this->assertEquals($queries[0]->getValues()[0]->getAttribute(), '$id');

$count = static::getDatabase()->count('movies_nested_id', $queries);
$this->assertEquals(2, $count);
}

/**
* @depends testFind
*/
Expand Down
Loading