Skip to content

Commit 75f206d

Browse files
Merge pull request #86 from utopia-php/fix-83-count-include-parens-for-or-queries
Fix: statements with OR clauses when counting documents
2 parents 5fc0476 + ff6c596 commit 75f206d

2 files changed

Lines changed: 33 additions & 12 deletions

File tree

src/Database/Adapter/MariaDB.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,8 @@ public function count(string $collection, array $queries = [], int $max = 0): in
654654
$conditions[] = $this->getSQLCondition('table_main.'.$query->getAttribute(), $query->getOperator(), ':attribute_'.$i.'_'.$key.'_'.$query->getAttribute(), $value);
655655
}
656656

657-
$where[] = implode(' OR ', $conditions);
657+
$condition = implode(' OR ', $conditions);
658+
$where[] = empty($condition) ? '' : '('.$condition.')';
658659
}
659660

660661
$stmt = $this->getPDO()->prepare("SELECT COUNT(1) as sum FROM (SELECT 1 FROM {$this->getNamespace()}.{$name} table_main

tests/Database/Base.php

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -915,17 +915,6 @@ public function testFind(Document $document)
915915
$documents = static::getDatabase()->find('movies', [], 2, 0, ['price', 'year'], [Database::ORDER_DESC, Database::ORDER_ASC], $movies[0], Database::CURSOR_BEFORE);
916916
$this->assertEmpty(count($documents));
917917

918-
/**
919-
* ORDER BY - After Exception
920-
*/
921-
922-
$document = new Document([
923-
'$collection' => 'other collection'
924-
]);
925-
926-
$this->expectException(Exception::class);
927-
static::getDatabase()->find('movies', [], 2, 0, [], [], $document);
928-
929918
/**
930919
* Limit
931920
*/
@@ -947,6 +936,26 @@ public function testFind(Document $document)
947936
$this->assertEquals('Frozen II', $documents[1]['name']);
948937
$this->assertEquals('Work in Progress', $documents[2]['name']);
949938
$this->assertEquals('Work in Progress 2', $documents[3]['name']);
939+
940+
/**
941+
* Test that OR queries are handled correctly
942+
*/
943+
$documents = static::getDatabase()->find('movies', [
944+
new Query('director', Query::TYPE_EQUAL, ['TBD', 'Joe Johnston']),
945+
new Query('year', Query::TYPE_EQUAL, [2025]),
946+
]);
947+
$this->assertEquals(1, count($documents));
948+
949+
/**
950+
* ORDER BY - After Exception
951+
* Must be last assertion in test
952+
*/
953+
$document = new Document([
954+
'$collection' => 'other collection'
955+
]);
956+
957+
$this->expectException(Exception::class);
958+
static::getDatabase()->find('movies', [], 2, 0, [], [], $document);
950959
}
951960

952961
/**
@@ -985,6 +994,17 @@ public function testCount()
985994
$count = static::getDatabase()->count('movies', [], 3);
986995
$this->assertEquals(3, $count);
987996
Authorization::reset();
997+
998+
/**
999+
* Test that OR queries are handled correctly
1000+
*/
1001+
Authorization::disable();
1002+
$count = static::getDatabase()->count('movies', [
1003+
new Query('director', Query::TYPE_EQUAL, ['TBD', 'Joe Johnston']),
1004+
new Query('year', Query::TYPE_EQUAL, [2025]),
1005+
]);
1006+
$this->assertEquals(1, $count);
1007+
Authorization::reset();
9881008
}
9891009

9901010
/**

0 commit comments

Comments
 (0)