diff --git a/bin/tasks/query.php b/bin/tasks/query.php index 80854a3db..0da48dd3b 100644 --- a/bin/tasks/query.php +++ b/bin/tasks/query.php @@ -3,6 +3,7 @@ /** * @var CLI */ global $cli; + use Faker\Factory; use MongoDB\Client; use Utopia\Cache\Cache; @@ -17,19 +18,21 @@ use Utopia\Database\Validator\Authorization; use Utopia\Validator\Numeric; use Utopia\Validator\Text; + $cli ->task('query') ->desc('Query mock data') ->param('adapter', '', new Text(0), 'Database adapter', false) ->param('name', '', new Text(0), 'Name of created database.', false) ->param('limit', 25, new Numeric(), 'Limit on queried documents', true) - ->action(function ($adapter, $name, $limit) { + ->action(function (string $adapter, string $name, int $limit) { $database = null; switch ($adapter) { case 'mongodb': $options = ["typeMap" => ['root' => 'array', 'document' => 'array', 'array' => 'array']]; - $client = new Client('mongodb://mongo/', + $client = new Client( + 'mongodb://mongo/', [ 'username' => 'root', 'password' => 'example', @@ -130,7 +133,8 @@ fclose($f); }); -function runQueries($database, $limit) { +function runQueries(Database $database, int $limit) +{ $results = []; // Recent travel blogs $query = ["created.greater(1262322000)", "genre.equal('travel')"]; @@ -151,21 +155,23 @@ function runQueries($database, $limit) { return $results; } -function addRoles($faker, $count) { - for ($i=0; $i < $count; $i++) { +function addRoles($faker, $count) +{ + for ($i = 0; $i < $count; $i++) { Authorization::setRole($faker->numerify('user####')); } return count(Authorization::getRoles()); } -function runQuery($query, $database, $limit) { - Console::log('Running query: ['.implode(', ', $query).']'); - $query = array_map(function($q) { +function runQuery(array $query, Database $database, int $limit) +{ + Console::log('Running query: [' . implode(', ', $query) . ']'); + $query = array_map(function ($q) { return Query::parse($q); }, $query); $start = microtime(true); - $documents = $database->find('articles', $query, $limit); + $database->find('articles', array_merge($query, [Query::limit($limit)])); $time = microtime(true) - $start; Console::success("{$time} s"); return $time; diff --git a/src/Database/Adapter.php b/src/Database/Adapter.php index 88cf12759..affce9848 100644 --- a/src/Database/Adapter.php +++ b/src/Database/Adapter.php @@ -314,16 +314,10 @@ abstract public function deleteDocument(string $collection, string $id): bool; * * @param string $collection * @param \Utopia\Database\Query[] $queries - * @param int $limit - * @param int $offset - * @param array $orderAttributes - * @param array $orderTypes - * @param array $cursor Array copy of document used for before/after pagination - * @param string $cursorDirection * * @return Document[] */ - abstract public function find(string $collection, array $queries = [], int $limit = 25, int $offset = 0, array $orderAttributes = [], array $orderTypes = [], array $cursor = [], string $cursorDirection = Database::CURSOR_AFTER): array; + abstract public function find(string $collection, array $queries = []): array; /** * Sum an attribute @@ -459,11 +453,11 @@ abstract public function getSupportForCasting(): bool; * @throws Exception * @return string */ - public function filter(string $value):string + public function filter(string $value): string { $value = preg_replace("/[^A-Za-z0-9\_\-]/", '', $value); - if(\is_null($value)) { + if (\is_null($value)) { throw new Exception('Failed to filter key'); } diff --git a/src/Database/Adapter/MariaDB.php b/src/Database/Adapter/MariaDB.php index c9fd88aab..dfe039bed 100644 --- a/src/Database/Adapter/MariaDB.php +++ b/src/Database/Adapter/MariaDB.php @@ -770,23 +770,60 @@ public function deleteDocument(string $collection, string $id): bool * * @param string $collection * @param Query[] $queries - * @param int $limit - * @param int $offset - * @param array $orderAttributes - * @param array $orderTypes - * @param array $cursor - * @param string $cursorDirection * @return Document[] * @throws Exception * @throws PDOException */ - public function find(string $collection, array $queries = [], int $limit = 25, int $offset = 0, array $orderAttributes = [], array $orderTypes = [], array $cursor = [], string $cursorDirection = Database::CURSOR_AFTER): array + public function find(string $collection, array $queries = []): array { $name = $this->filter($collection); $roles = Authorization::getRoles(); $where = ['1=1']; $orders = []; + $filters = []; + $limit = 25; + $offset = 0; + $orderAttributes = []; + $orderTypes = []; + $cursor = null; + $cursorDirection = Database::CURSOR_AFTER; + foreach ($queries as $query) { + if (!$query instanceof Query) continue; + + $method = $query->getMethod(); + $attribute = $query->getAttribute(); + $values = $query->getValues(); + switch ($method) { + case Query::TYPE_ORDERASC: + case Query::TYPE_ORDERDESC: + if (!empty($attribute)) { + $orderAttributes[] = $attribute; + } + + $orderTypes[] = $method == Query::TYPE_ORDERASC ? Database::ORDER_ASC : Database::ORDER_DESC; + break; + + case Query::TYPE_LIMIT: + $limit = $values[0]; + break; + + case Query::TYPE_OFFSET: + $offset = $values[0]; + break; + + case Query::TYPE_CURSORAFTER: + case Query::TYPE_CURSORBEFORE: + $cursor = $values[0]; + $cursorDirection = $method == Query::TYPE_CURSORAFTER ? Database::CURSOR_AFTER : Database::CURSOR_BEFORE; + break; + + default: + $filters[] = $query; + break; + } + } + $orderAttributes = \array_map(fn ($orderAttribute) => match ($orderAttribute) { '$id' => '_uid', '$createdAt' => '_createdAt', @@ -852,7 +889,7 @@ public function find(string $collection, array $queries = [], int $limit = 25, i } } - foreach ($queries as $i => $query) { + foreach ($filters as $i => $query) { $query->setAttribute(match ($query->getAttribute()) { '$id' => '_uid', '$createdAt' => '_createdAt', @@ -887,7 +924,7 @@ public function find(string $collection, array $queries = [], int $limit = 25, i LIMIT :offset, :limit; "); - foreach ($queries as $i => $query) { + foreach ($filters as $i => $query) { if ($query->getMethod() === Query::TYPE_SEARCH) continue; foreach ($query->getValues() as $key => $value) { $stmt->bindValue(':attribute_' . $i . '_' . $key . '_' . $query->getAttribute(), $value, $this->getPDOType($value)); diff --git a/src/Database/Database.php b/src/Database/Database.php index 4f253a01f..ad616649b 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -203,7 +203,6 @@ function (?string $value) { return $value; } ); - } /** @@ -362,7 +361,7 @@ public function delete(string $name): bool public function createCollection(string $id, array $attributes = [], array $indexes = []): Document { $collection = $this->getCollection($id); - if(!$collection->isEmpty() && $id !== self::METADATA){ + if (!$collection->isEmpty() && $id !== self::METADATA) { throw new Duplicate('Collection ' . $id . ' Exists!'); } @@ -431,7 +430,10 @@ public function listCollections($limit = 25, $offset = 0): array { Authorization::disable(); - $result = $this->find(self::METADATA, [], $limit, $offset); + $result = $this->find(self::METADATA, [ + Query::limit($limit), + Query::offset($offset) + ]); Authorization::reset(); @@ -593,9 +595,9 @@ private function updateAttributeMeta(string $collection, string $id, callable $u $attributes = $collection->getAttribute('attributes', []); - $attributeIndex = \array_search($id, \array_map(fn($attribute) => $attribute['$id'], $attributes)); + $attributeIndex = \array_search($id, \array_map(fn ($attribute) => $attribute['$id'], $attributes)); - if($attributeIndex === false) { + if ($attributeIndex === false) { throw new Exception('Attribute not found'); } @@ -621,7 +623,7 @@ private function updateAttributeMeta(string $collection, string $id, callable $u */ public function updateAttributeRequired(string $collection, string $id, bool $required): void { - $this->updateAttributeMeta($collection, $id, function($attribute) use($required) { + $this->updateAttributeMeta($collection, $id, function ($attribute) use ($required) { $attribute->setAttribute('required', $required); }); } @@ -637,7 +639,7 @@ public function updateAttributeRequired(string $collection, string $id, bool $re */ public function updateAttributeFormat(string $collection, string $id, string $format): void { - $this->updateAttributeMeta($collection, $id, function($attribute) use($format) { + $this->updateAttributeMeta($collection, $id, function ($attribute) use ($format) { if (!Structure::hasFormat($format, $attribute->getAttribute('type'))) { throw new Exception('Format ("' . $format . '") not available for this attribute type ("' . $attribute->getAttribute('type') . '")'); } @@ -657,7 +659,7 @@ public function updateAttributeFormat(string $collection, string $id, string $fo */ public function updateAttributeFormatOptions(string $collection, string $id, array $formatOptions): void { - $this->updateAttributeMeta($collection, $id, function($attribute) use($formatOptions) { + $this->updateAttributeMeta($collection, $id, function ($attribute) use ($formatOptions) { $attribute->setAttribute('formatOptions', $formatOptions); }); } @@ -673,7 +675,7 @@ public function updateAttributeFormatOptions(string $collection, string $id, arr */ public function updateAttributeFilters(string $collection, string $id, array $filters): void { - $this->updateAttributeMeta($collection, $id, function($attribute) use($filters) { + $this->updateAttributeMeta($collection, $id, function ($attribute) use ($filters) { $attribute->setAttribute('filters', $filters); }); } @@ -689,7 +691,7 @@ public function updateAttributeFilters(string $collection, string $id, array $fi */ public function updateAttributeDefault(string $collection, string $id, $default = null): void { - $this->updateAttributeMeta($collection, $id, function($attribute) use($default) { + $this->updateAttributeMeta($collection, $id, function ($attribute) use ($default) { if ($attribute->getAttribute('required') === true) { throw new Exception('Cannot set a default value on a required attribute'); } @@ -718,7 +720,7 @@ public function updateAttributeDefault(string $collection, string $id, $default throw new Exception('Unknown attribute type for: ' . $default); break; } - + $attribute->setAttribute('default', $default); }); } @@ -739,20 +741,20 @@ public function updateAttributeDefault(string $collection, string $id, $default */ public function updateAttribute(string $collection, string $id, string $type = null, int $size = null, bool $signed = null, bool $array = null): bool { - $this->updateAttributeMeta($collection, $id, function($attribute, $collectionDoc, $attributeIndex) use($collection, $id, $type, $size, $signed, $array, &$success) { - if($type !== null || $size !== null || $signed !== null || $array !== null) { + $this->updateAttributeMeta($collection, $id, function ($attribute, $collectionDoc, $attributeIndex) use ($collection, $id, $type, $size, $signed, $array, &$success) { + if ($type !== null || $size !== null || $signed !== null || $array !== null) { $type ??= $attribute->getAttribute('type'); $size ??= $attribute->getAttribute('size'); $signed ??= $attribute->getAttribute('signed'); $array ??= $attribute->getAttribute('array'); - + switch ($type) { case self::VAR_STRING: if ($size > $this->adapter->getStringLimit()) { throw new Exception('Max size allowed for string is: ' . number_format($this->adapter->getStringLimit())); } break; - + case self::VAR_INTEGER: $limit = ($signed) ? $this->adapter->getIntLimit() / 2 : $this->adapter->getIntLimit(); if ($size > $limit) { @@ -766,7 +768,7 @@ public function updateAttribute(string $collection, string $id, string $type = n throw new Exception('Unknown attribute type: ' . $type); break; } - + $attribute ->setAttribute('type', $type) ->setAttribute('size', $size) @@ -783,7 +785,7 @@ public function updateAttribute(string $collection, string $id, string $type = n ) { throw new LimitException('Row width limit reached. Cannot create new attribute.'); } - + $this->adapter->updateAttribute($collection, $id, $type, $size, $signed, $array); } }); @@ -871,15 +873,15 @@ public function renameAttribute(string $collection, string $old, string $new): b $attributes = $collection->getAttribute('attributes', []); $indexes = $collection->getAttribute('indexes', []); - $attribute = \in_array($old, \array_map(fn($attribute) => $attribute['$id'], $attributes)); + $attribute = \in_array($old, \array_map(fn ($attribute) => $attribute['$id'], $attributes)); - if($attribute === false) { + if ($attribute === false) { throw new Exception('Attribute not found'); } - $attributeNew = \in_array($new, \array_map(fn($attribute) => $attribute['$id'], $attributes)); + $attributeNew = \in_array($new, \array_map(fn ($attribute) => $attribute['$id'], $attributes)); - if($attributeNew !== false) { + if ($attributeNew !== false) { throw new DuplicateException('Attribute name already used'); } @@ -894,7 +896,7 @@ public function renameAttribute(string $collection, string $old, string $new): b foreach ($indexes as $index) { $indexAttributes = $index->getAttribute('attributes', []); - $indexAttributes = \array_map(fn($attribute) => ($attribute === $old) ? $new : $attribute , $indexAttributes); + $indexAttributes = \array_map(fn ($attribute) => ($attribute === $old) ? $new : $attribute, $indexAttributes); $index->setAttribute('attributes', $indexAttributes); } @@ -924,15 +926,15 @@ public function renameIndex(string $collection, string $old, string $new): bool $indexes = $collection->getAttribute('indexes', []); - $index = \in_array($old, \array_map(fn($index) => $index['$id'], $indexes)); + $index = \in_array($old, \array_map(fn ($index) => $index['$id'], $indexes)); - if($index === false) { + if ($index === false) { throw new Exception('Index not found'); } - $indexNew = \in_array($new, \array_map(fn($index) => $index['$id'], $indexes)); + $indexNew = \in_array($new, \array_map(fn ($index) => $index['$id'], $indexes)); - if($indexNew !== false) { + if ($indexNew !== false) { throw new DuplicateException('Index name already used'); } @@ -1141,8 +1143,7 @@ public function createDocument(string $collection, Document $document): Document ->setAttribute('$id', empty($document->getId()) ? $this->getId() : $document->getId()) ->setAttribute('$collection', $collection->getId()) ->setAttribute('$createdAt', $time) - ->setAttribute('$updatedAt', $time) - ; + ->setAttribute('$updatedAt', $time); $document = $this->encode($collection, $document); @@ -1266,17 +1267,11 @@ public function deleteCachedDocument(string $collection, string $id): bool * * @param string $collection * @param Query[] $queries - * @param int $limit - * @param int $offset - * @param array $orderAttributes - * @param array $orderTypes - * @param Document|null $cursor - * @param string $cursorDirection * * @return Document[] * @throws Exception */ - public function find(string $collection, array $queries = [], int $limit = 25, int $offset = 0, array $orderAttributes = [], array $orderTypes = [], Document $cursor = null, string $cursorDirection = self::CURSOR_AFTER): array + public function find(string $collection, array $queries = []): array { $collection = $this->getCollection($collection); @@ -1288,7 +1283,7 @@ public function find(string $collection, array $queries = [], int $limit = 25, i $queries = self::convertQueries($collection, $queries); - $results = $this->adapter->find($collection->getId(), $queries, $limit, $offset, $orderAttributes, $orderTypes, $cursor, $cursorDirection); + $results = $this->adapter->find($collection->getId(), $queries); foreach ($results as &$node) { $node = $this->casting($collection, $node); @@ -1310,9 +1305,9 @@ public function find(string $collection, array $queries = [], int $limit = 25, i * * @return Document|bool */ - public function findOne(string $collection, array $queries = [], int $offset = 0, array $orderAttributes = [], array $orderTypes = [], Document $cursor = null, string $cursorDirection = Database::CURSOR_AFTER) + public function findOne(string $collection, array $queries = []) { - $results = $this->find($collection, $queries, /*limit*/ 1, $offset, $orderAttributes, $orderTypes, $cursor, $cursorDirection); + $results = $this->find($collection, \array_merge([Query::limit(1)], $queries)); return \reset($results); } @@ -1528,7 +1523,7 @@ public function casting(Document $collection, Document $document): Document $type = $attribute['type'] ?? ''; $array = $attribute['array'] ?? false; $value = $document->getAttribute($key, null); - if(is_null($value)) { + if (is_null($value)) { continue; } @@ -1539,7 +1534,7 @@ public function casting(Document $collection, Document $document): Document } foreach ($value as &$node) { - if(is_null($value)) { + if (is_null($value)) { continue; } switch ($type) { @@ -1585,7 +1580,7 @@ protected function encodeAttribute(string $name, $value, Document $document) } try { - if(array_key_exists($name, $this->instanceFilters)) { + if (array_key_exists($name, $this->instanceFilters)) { $value = $this->instanceFilters[$name]['encode']($value, $document, $this); } else { $value = self::$filters[$name]['encode']($value, $document, $this); @@ -1616,7 +1611,7 @@ protected function decodeAttribute(string $name, $value, Document $document) } try { - if(array_key_exists($name, $this->instanceFilters)) { + if (array_key_exists($name, $this->instanceFilters)) { $value = $this->instanceFilters[$name]['decode']($value, $document, $this); } else { $value = self::$filters[$name]['decode']($value, $document, $this); @@ -1676,18 +1671,18 @@ public function getIndexLimit() * @return Query[] * @throws Exception */ - public static function convertQueries(Document $collection, array $queries):array + public static function convertQueries(Document $collection, array $queries): array { $attributes = $collection->getAttribute('attributes', []); - foreach ($attributes as $v){ + foreach ($attributes as $v) { /* @var $v Document */ switch ($v->getAttribute('type')) { case Database::VAR_DATETIME: - foreach ($queries as $qk => $q){ - if($q->getAttribute() === $v->getId()){ + foreach ($queries as $qk => $q) { + if ($q->getAttribute() === $v->getId()) { $arr = $q->getValues(); - foreach ($arr as $vk => $vv){ + foreach ($arr as $vk => $vv) { $arr[$vk] = DateTime::setTimezone($vv); } $q->setValues($arr); @@ -1699,5 +1694,4 @@ public static function convertQueries(Document $collection, array $queries):arra } return $queries; } - -} \ No newline at end of file +} diff --git a/src/Database/Query.php b/src/Database/Query.php index 0df5b5275..369e609e0 100644 --- a/src/Database/Query.php +++ b/src/Database/Query.php @@ -454,17 +454,17 @@ public static function greaterThanEqual(string $attribute, $value): self /** * Helper method to create Query with contains method */ - public static function contains(string $attribute, $value): self + public static function contains(string $attribute, array $values): self { - return new self(self::TYPE_CONTAINS, $attribute, [$value]); + return new self(self::TYPE_CONTAINS, $attribute, $values); } /** * Helper method to create Query with search method */ - public static function search(string $attribute, $value): self + public static function search(string $attribute, array $values): self { - return new self(self::TYPE_SEARCH, $attribute, [$value]); + return new self(self::TYPE_SEARCH, $attribute, $values); } /** @@ -502,7 +502,7 @@ public static function offset(int $value): self /** * Helper method to create Query with cursorAfter method */ - public static function cursorAfter(string $value): self + public static function cursorAfter(Document $value): self { return new self(self::TYPE_CURSORAFTER, values: [$value]); } @@ -510,7 +510,7 @@ public static function cursorAfter(string $value): self /** * Helper method to create Query with cursorBefore method */ - public static function cursorBefore(string $value): self + public static function cursorBefore(Document $value): self { return new self(self::TYPE_CURSORBEFORE, values: [$value]); } diff --git a/tests/Database/Adapter/MongoDBTest.php b/tests/Database/Adapter/MongoDBTest.php index 1a671db19..ffffc3a0d 100644 --- a/tests/Database/Adapter/MongoDBTest.php +++ b/tests/Database/Adapter/MongoDBTest.php @@ -113,7 +113,9 @@ public function testListDocumentSearch(Document $document) 'empty' => [], ])); - $documents = static::getDatabase()->find('documents', [new Query(Query::TYPE_SEARCH, 'string', ['*test+alias@email-provider.com'])]); + $documents = static::getDatabase()->find('documents', [ + Query::search('string', ['*test+alias@email-provider.com']) + ]); $this->assertEquals(1, count($documents)); diff --git a/tests/Database/Base.php b/tests/Database/Base.php index 0387f1059..c2628b3c0 100644 --- a/tests/Database/Base.php +++ b/tests/Database/Base.php @@ -666,7 +666,7 @@ public function testListDocumentSearch(Document $document) * Allow reserved keywords for search */ $documents = static::getDatabase()->find('documents', [ - new Query(Query::TYPE_SEARCH, 'string', ['*test+alias@email-provider.com']), + Query::search('string', ['*test+alias@email-provider.com']), ]); $this->assertEquals(1, count($documents)); @@ -881,17 +881,33 @@ public function testFind(Document $document) /** * Check $id: Notice, this orders ID names alphabetically, not by internal numeric ID */ - $documents = static::getDatabase()->find('movies', [], 25, 0, ['$id'], [Database::ORDER_DESC]); + $documents = static::getDatabase()->find('movies', [ + Query::limit(25), + Query::offset(0), + Query::orderDesc('$id'), + ]); $this->assertEquals($lastDocumentId, $documents[0]->getId()); - $documents = static::getDatabase()->find('movies', [], 25, 0, ['$id'], [Database::ORDER_ASC]); + $documents = static::getDatabase()->find('movies', [ + Query::limit(25), + Query::offset(0), + Query::orderAsc('$id'), + ]); $this->assertEquals($firstDocumentId, $documents[0]->getId()); /** * Check internal numeric ID sorting */ - $documents = static::getDatabase()->find('movies', [], 25, 0, [], [Database::ORDER_DESC]); + $documents = static::getDatabase()->find('movies', [ + Query::limit(25), + Query::offset(0), + Query::orderDesc(''), + ]); $this->assertEquals($movieDocuments[\count($movieDocuments) - 1]->getId(), $documents[0]->getId()); - $documents = static::getDatabase()->find('movies', [], 25, 0, [], [Database::ORDER_ASC]); + $documents = static::getDatabase()->find('movies', [ + Query::limit(25), + Query::offset(0), + Query::orderAsc(''), + ]); $this->assertEquals($movieDocuments[0]->getId(), $documents[0]->getId()); @@ -908,7 +924,7 @@ public function testFind(Document $document) * Check an Integer condition */ $documents = static::getDatabase()->find('movies', [ - new Query(Query::TYPE_EQUAL, 'year', [2019]), + Query::equal('year', [2019]), ]); $this->assertEquals(2, count($documents)); @@ -919,7 +935,7 @@ public function testFind(Document $document) * Boolean condition */ $documents = static::getDatabase()->find('movies', [ - new Query(Query::TYPE_EQUAL, 'active', [true]), + Query::equal('active', [true]), ]); $this->assertEquals(4, count($documents)); @@ -928,7 +944,7 @@ public function testFind(Document $document) * String condition */ $documents = static::getDatabase()->find('movies', [ - new Query(Query::TYPE_EQUAL, 'director', ['TBD']), + Query::equal('director', ['TBD']), ]); $this->assertEquals(2, count($documents)); @@ -937,22 +953,21 @@ public function testFind(Document $document) * Not Equal query */ $documents = static::getDatabase()->find('movies', [ - new Query(Query::TYPE_NOTEQUAL, 'director', ['TBD', 'Joe Johnston']), + Query::notEqual('director', 'TBD'), ]); $this->assertGreaterThan(0, count($documents)); foreach ($documents as $document) { - $isAllowed = $document['director'] !== 'TBD' || $document['director'] !== 'Joe Johnston'; - $this->assertTrue($isAllowed); + $this->assertTrue($$document['director'] !== 'TBD'); } /** * Float condition */ $documents = static::getDatabase()->find('movies', [ - new Query(Query::TYPE_LESSER, 'price', [26.00]), - new Query(Query::TYPE_GREATER, 'price', [25.98]), + Query::lessThan('price', 26.00), + Query::greaterThan('price', 25.98), ]); // TODO@kodumbeats hacky way to pass mariadb tests @@ -962,7 +977,7 @@ public function testFind(Document $document) * Array contains condition */ $documents = static::getDatabase()->find('movies', [ - new Query(Query::TYPE_CONTAINS, 'generes', ['comics']), + Query::contains('generes', ['comics']) ]); $this->assertEquals(2, count($documents)); @@ -971,7 +986,7 @@ public function testFind(Document $document) * Array contains OR condition */ $documents = static::getDatabase()->find('movies', [ - new Query(Query::TYPE_CONTAINS, 'generes', ['comics', 'kids']), + Query::contains('generes', ['comics', 'kids']), ]); $this->assertEquals(4, count($documents)); @@ -984,7 +999,7 @@ public function testFind(Document $document) $this->assertEquals(true, $success); $documents = static::getDatabase()->find('movies', [ - new Query(Query::TYPE_SEARCH, 'name', ['captain']), + Query::search('name', ['captain']), ]); $this->assertEquals(2, count($documents)); @@ -995,7 +1010,7 @@ public function testFind(Document $document) // TODO: Looks like the MongoDB implementation is a bit more complex, skipping that for now. if (in_array(static::getAdapterName(), ['mysql', 'mariadb'])) { $documents = static::getDatabase()->find('movies', [ - new Query(Query::TYPE_SEARCH, 'name', ['cap']), + Query::search('name', ['cap']), ]); $this->assertEquals(2, count($documents)); @@ -1005,8 +1020,8 @@ public function testFind(Document $document) * Multiple conditions */ $documents = static::getDatabase()->find('movies', [ - new Query(Query::TYPE_EQUAL, 'director', ['TBD']), - new Query(Query::TYPE_EQUAL, 'year', [2026]), + Query::equal('director', ['TBD']), + Query::equal('year', [2026]), ]); $this->assertEquals(1, count($documents)); @@ -1015,7 +1030,7 @@ public function testFind(Document $document) * Multiple conditions and OR values */ $documents = static::getDatabase()->find('movies', [ - new Query(Query::TYPE_EQUAL, 'name', ['Frozen II', 'Captain Marvel']), + Query::equal('name', ['Frozen II', 'Captain Marvel']), ]); $this->assertEquals(2, count($documents)); @@ -1026,7 +1041,7 @@ public function testFind(Document $document) * $id condition */ $documents = static::getDatabase()->find('movies', [ - new Query(Query::TYPE_EQUAL, '$id', ['frozen']), + Query::equal('$id', ['frozen']), ]); $this->assertEquals(1, count($documents)); @@ -1035,7 +1050,12 @@ public function testFind(Document $document) /** * ORDER BY */ - $documents = static::getDatabase()->find('movies', [], 25, 0, ['price', 'name'], [Database::ORDER_DESC]); + $documents = static::getDatabase()->find('movies', [ + Query::limit(25), + Query::offset(0), + Query::orderDesc('price'), + Query::orderAsc('name') + ]); $this->assertEquals(6, count($documents)); $this->assertEquals('Frozen', $documents[0]['name']); @@ -1048,8 +1068,15 @@ public function testFind(Document $document) /** * ORDER BY natural */ - $base = array_reverse(static::getDatabase()->find('movies', [], 25, 0)); - $documents = static::getDatabase()->find('movies', [], 25, 0, [], [Database::ORDER_DESC]); + $base = array_reverse(static::getDatabase()->find('movies', [ + Query::limit(25), + Query::offset(0), + ])); + $documents = static::getDatabase()->find('movies', [ + Query::limit(25), + Query::offset(0), + Query::orderDesc(''), + ]); $this->assertEquals(6, count($documents)); $this->assertEquals($base[0]['name'], $documents[0]['name']); @@ -1062,7 +1089,12 @@ public function testFind(Document $document) /** * ORDER BY - Multiple attributes */ - $documents = static::getDatabase()->find('movies', [], 25, 0, ['price', 'name'], [Database::ORDER_DESC, Database::ORDER_DESC]); + $documents = static::getDatabase()->find('movies', [ + Query::limit(25), + Query::offset(0), + Query::orderDesc('price'), + Query::orderDesc('name') + ]); $this->assertEquals(6, count($documents)); $this->assertEquals('Frozen II', $documents[0]['name']); @@ -1075,237 +1107,488 @@ public function testFind(Document $document) /** * ORDER BY - After */ - $movies = static::getDatabase()->find('movies', [], 25, 0, [], []); + $movies = static::getDatabase()->find('movies', [ + Query::limit(25), + Query::offset(0), + ]); - $documents = static::getDatabase()->find('movies', [], 2, 0, [], [], $movies[1]); + $documents = static::getDatabase()->find('movies', [ + Query::limit(2), + Query::offset(0), + Query::cursorAfter($movies[1]) + ]); $this->assertEquals(2, count($documents)); $this->assertEquals($movies[2]['name'], $documents[0]['name']); $this->assertEquals($movies[3]['name'], $documents[1]['name']); - $documents = static::getDatabase()->find('movies', [], 2, 0, [], [], $movies[3]); + $documents = static::getDatabase()->find('movies', [ + Query::limit(2), + Query::offset(0), + Query::cursorAfter($movies[3]) + ]); $this->assertEquals(2, count($documents)); $this->assertEquals($movies[4]['name'], $documents[0]['name']); $this->assertEquals($movies[5]['name'], $documents[1]['name']); - $documents = static::getDatabase()->find('movies', [], 2, 0, [], [], $movies[4]); + $documents = static::getDatabase()->find('movies', [ + Query::limit(2), + Query::offset(0), + Query::cursorAfter($movies[4]) + ]); $this->assertEquals(1, count($documents)); $this->assertEquals($movies[5]['name'], $documents[0]['name']); - $documents = static::getDatabase()->find('movies', [], 2, 0, [], [], $movies[5]); + $documents = static::getDatabase()->find('movies', [ + Query::limit(2), + Query::offset(0), + Query::cursorAfter($movies[5]) + ]); $this->assertEmpty(count($documents)); /** * ORDER BY - Before */ - $movies = static::getDatabase()->find('movies', [], 25, 0, [], []); + $movies = static::getDatabase()->find('movies', [ + Query::limit(25), + Query::offset(0), + ]); - $documents = static::getDatabase()->find('movies', [], 2, 0, [], [], $movies[5], Database::CURSOR_BEFORE); + $documents = static::getDatabase()->find('movies', [ + Query::limit(2), + Query::offset(0), + Query::cursorBefore($movies[5]) + ]); $this->assertEquals(2, count($documents)); $this->assertEquals($movies[3]['name'], $documents[0]['name']); $this->assertEquals($movies[4]['name'], $documents[1]['name']); - $documents = static::getDatabase()->find('movies', [], 2, 0, [], [], $movies[3], Database::CURSOR_BEFORE); + $documents = static::getDatabase()->find('movies', [ + Query::limit(2), + Query::offset(0), + Query::cursorBefore($movies[3]) + ]); $this->assertEquals(2, count($documents)); $this->assertEquals($movies[1]['name'], $documents[0]['name']); $this->assertEquals($movies[2]['name'], $documents[1]['name']); - $documents = static::getDatabase()->find('movies', [], 2, 0, [], [], $movies[2], Database::CURSOR_BEFORE); + $documents = static::getDatabase()->find('movies', [ + Query::limit(2), + Query::offset(0), + Query::cursorBefore($movies[2]) + ]); $this->assertEquals(2, count($documents)); $this->assertEquals($movies[0]['name'], $documents[0]['name']); $this->assertEquals($movies[1]['name'], $documents[1]['name']); - $documents = static::getDatabase()->find('movies', [], 2, 0, [], [], $movies[1], Database::CURSOR_BEFORE); + $documents = static::getDatabase()->find('movies', [ + Query::limit(2), + Query::offset(0), + Query::cursorBefore($movies[1]) + ]); $this->assertEquals(1, count($documents)); $this->assertEquals($movies[0]['name'], $documents[0]['name']); - $documents = static::getDatabase()->find('movies', [], 2, 0, [], [], $movies[0], Database::CURSOR_BEFORE); + $documents = static::getDatabase()->find('movies', [ + Query::limit(2), + Query::offset(0), + Query::cursorBefore($movies[0]) + ]); $this->assertEmpty(count($documents)); /** * ORDER BY - After by natural order */ - $movies = array_reverse(static::getDatabase()->find('movies', [], 25, 0, [], [])); + $movies = array_reverse(static::getDatabase()->find('movies', [ + Query::limit(25), + Query::offset(0), + ])); - $documents = static::getDatabase()->find('movies', [], 2, 0, [], [Database::ORDER_DESC], $movies[1]); + $documents = static::getDatabase()->find('movies', [ + Query::limit(2), + Query::offset(0), + Query::orderDesc(''), + Query::cursorAfter($movies[1]) + ]); $this->assertEquals(2, count($documents)); $this->assertEquals($movies[2]['name'], $documents[0]['name']); $this->assertEquals($movies[3]['name'], $documents[1]['name']); - $documents = static::getDatabase()->find('movies', [], 2, 0, [], [Database::ORDER_DESC], $movies[3]); + $documents = static::getDatabase()->find('movies', [ + Query::limit(2), + Query::offset(0), + Query::orderDesc(''), + Query::cursorAfter($movies[3]) + ]); $this->assertEquals(2, count($documents)); $this->assertEquals($movies[4]['name'], $documents[0]['name']); $this->assertEquals($movies[5]['name'], $documents[1]['name']); - $documents = static::getDatabase()->find('movies', [], 2, 0, [], [Database::ORDER_DESC], $movies[4]); + $documents = static::getDatabase()->find('movies', [ + Query::limit(2), + Query::offset(0), + Query::orderDesc(''), + Query::cursorAfter($movies[4]) + ]); $this->assertEquals(1, count($documents)); $this->assertEquals($movies[5]['name'], $documents[0]['name']); - $documents = static::getDatabase()->find('movies', [], 2, 0, [], [Database::ORDER_DESC], $movies[5]); + $documents = static::getDatabase()->find('movies', [ + Query::limit(2), + Query::offset(0), + Query::orderDesc(''), + Query::cursorAfter($movies[5]) + ]); $this->assertEmpty(count($documents)); /** * ORDER BY - Before by natural order */ - $movies = static::getDatabase()->find('movies', [], 25, 0, [], [Database::ORDER_DESC]); + $movies = static::getDatabase()->find('movies', [ + Query::limit(25), + Query::offset(0), + Query::orderDesc(''), + ]); - $documents = static::getDatabase()->find('movies', [], 2, 0, [], [Database::ORDER_DESC], $movies[5], Database::CURSOR_BEFORE); + $documents = static::getDatabase()->find('movies', [ + Query::limit(2), + Query::offset(0), + Query::orderDesc(''), + Query::cursorBefore($movies[5]) + ]); $this->assertEquals(2, count($documents)); $this->assertEquals($movies[3]['name'], $documents[0]['name']); $this->assertEquals($movies[4]['name'], $documents[1]['name']); - $documents = static::getDatabase()->find('movies', [], 2, 0, [], [Database::ORDER_DESC], $movies[3], Database::CURSOR_BEFORE); + $documents = static::getDatabase()->find('movies', [ + Query::limit(2), + Query::offset(0), + Query::orderDesc(''), + Query::cursorBefore($movies[3]) + ]); $this->assertEquals(2, count($documents)); $this->assertEquals($movies[1]['name'], $documents[0]['name']); $this->assertEquals($movies[2]['name'], $documents[1]['name']); - $documents = static::getDatabase()->find('movies', [], 2, 0, [], [Database::ORDER_DESC], $movies[2], Database::CURSOR_BEFORE); + $documents = static::getDatabase()->find('movies', [ + Query::limit(2), + Query::offset(0), + Query::orderDesc(''), + Query::cursorBefore($movies[2]) + ]); $this->assertEquals(2, count($documents)); $this->assertEquals($movies[0]['name'], $documents[0]['name']); $this->assertEquals($movies[1]['name'], $documents[1]['name']); - $documents = static::getDatabase()->find('movies', [], 2, 0, [], [Database::ORDER_DESC], $movies[1], Database::CURSOR_BEFORE); + $documents = static::getDatabase()->find('movies', [ + Query::limit(2), + Query::offset(0), + Query::orderDesc(''), + Query::cursorBefore($movies[1]) + ]); $this->assertEquals(1, count($documents)); $this->assertEquals($movies[0]['name'], $documents[0]['name']); - $documents = static::getDatabase()->find('movies', [], 2, 0, [], [Database::ORDER_DESC], $movies[0], Database::CURSOR_BEFORE); + $documents = static::getDatabase()->find('movies', [ + Query::limit(2), + Query::offset(0), + Query::orderDesc(''), + Query::cursorBefore($movies[0]) + ]); $this->assertEmpty(count($documents)); /** * ORDER BY - Single Attribute After */ - $movies = static::getDatabase()->find('movies', [], 25, 0, ['year'], [Database::ORDER_DESC]); + $movies = static::getDatabase()->find('movies', [ + Query::limit(25), + Query::offset(0), + Query::orderDesc('year') + ]); - $documents = static::getDatabase()->find('movies', [], 2, 0, ['year'], [Database::ORDER_DESC], $movies[1]); + $documents = static::getDatabase()->find('movies', [ + Query::limit(2), + Query::offset(0), + Query::orderDesc('year'), + Query::cursorAfter($movies[1]) + ]); $this->assertEquals(2, count($documents)); $this->assertEquals($movies[2]['name'], $documents[0]['name']); $this->assertEquals($movies[3]['name'], $documents[1]['name']); - $documents = static::getDatabase()->find('movies', [], 2, 0, ['year'], [Database::ORDER_DESC], $movies[3]); + $documents = static::getDatabase()->find('movies', [ + Query::limit(2), + Query::offset(0), + Query::orderDesc('year'), + Query::cursorAfter($movies[3]) + ]); $this->assertEquals(2, count($documents)); $this->assertEquals($movies[4]['name'], $documents[0]['name']); $this->assertEquals($movies[5]['name'], $documents[1]['name']); - $documents = static::getDatabase()->find('movies', [], 2, 0, ['year'], [Database::ORDER_DESC], $movies[4]); + $documents = static::getDatabase()->find('movies', [ + Query::limit(2), + Query::offset(0), + Query::orderDesc('year'), + Query::cursorAfter($movies[4]) + ]); $this->assertEquals(1, count($documents)); $this->assertEquals($movies[5]['name'], $documents[0]['name']); - $documents = static::getDatabase()->find('movies', [], 2, 0, ['year'], [Database::ORDER_DESC], $movies[5]); + $documents = static::getDatabase()->find('movies', [ + Query::limit(2), + Query::offset(0), + Query::orderDesc('year'), + Query::cursorAfter($movies[5]) + ]); $this->assertEmpty(count($documents)); /** * ORDER BY - Single Attribute Before */ - $movies = static::getDatabase()->find('movies', [], 25, 0, ['year'], [Database::ORDER_DESC]); + $movies = static::getDatabase()->find('movies', [ + Query::limit(25), + Query::offset(0), + Query::orderDesc('year') + ]); - $documents = static::getDatabase()->find('movies', [], 2, 0, ['year'], [Database::ORDER_DESC], $movies[5], Database::CURSOR_BEFORE); + $documents = static::getDatabase()->find('movies', [ + Query::limit(2), + Query::offset(0), + Query::orderDesc('year'), + Query::cursorBefore($movies[5]) + ]); $this->assertEquals(2, count($documents)); $this->assertEquals($movies[3]['name'], $documents[0]['name']); $this->assertEquals($movies[4]['name'], $documents[1]['name']); - $documents = static::getDatabase()->find('movies', [], 2, 0, ['year'], [Database::ORDER_DESC], $movies[3], Database::CURSOR_BEFORE); + $documents = static::getDatabase()->find('movies', [ + Query::limit(2), + Query::offset(0), + Query::orderDesc('year'), + Query::cursorBefore($movies[3]) + ]); $this->assertEquals(2, count($documents)); $this->assertEquals($movies[1]['name'], $documents[0]['name']); $this->assertEquals($movies[2]['name'], $documents[1]['name']); - $documents = static::getDatabase()->find('movies', [], 2, 0, ['year'], [Database::ORDER_DESC], $movies[2], Database::CURSOR_BEFORE); + $documents = static::getDatabase()->find('movies', [ + Query::limit(2), + Query::offset(0), + Query::orderDesc('year'), + Query::cursorBefore($movies[2]) + ]); $this->assertEquals(2, count($documents)); $this->assertEquals($movies[0]['name'], $documents[0]['name']); $this->assertEquals($movies[1]['name'], $documents[1]['name']); - $documents = static::getDatabase()->find('movies', [], 2, 0, ['year'], [Database::ORDER_DESC], $movies[1], Database::CURSOR_BEFORE); + $documents = static::getDatabase()->find('movies', [ + Query::limit(2), + Query::offset(0), + Query::orderDesc('year'), + Query::cursorBefore($movies[1]) + ]); $this->assertEquals(1, count($documents)); $this->assertEquals($movies[0]['name'], $documents[0]['name']); - $documents = static::getDatabase()->find('movies', [], 2, 0, ['year'], [Database::ORDER_DESC], $movies[0], Database::CURSOR_BEFORE); + $documents = static::getDatabase()->find('movies', [ + Query::limit(2), + Query::offset(0), + Query::orderDesc('year'), + Query::cursorBefore($movies[0]) + ]); $this->assertEmpty(count($documents)); /** * ORDER BY - Multiple Attribute After */ - $movies = static::getDatabase()->find('movies', [], 25, 0, ['price', 'year'], [Database::ORDER_DESC, Database::ORDER_ASC]); + $movies = static::getDatabase()->find('movies', [ + Query::limit(25), + Query::offset(0), + Query::orderDesc('price'), + Query::orderAsc('year') + ]); - $documents = static::getDatabase()->find('movies', [], 2, 0, ['price', 'year'], [Database::ORDER_DESC, Database::ORDER_ASC], $movies[1]); + $documents = static::getDatabase()->find('movies', [ + Query::limit(2), + Query::offset(0), + Query::orderDesc('price'), + Query::orderAsc('year'), + Query::cursorAfter($movies[1]) + ]); $this->assertEquals(2, count($documents)); $this->assertEquals($movies[2]['name'], $documents[0]['name']); $this->assertEquals($movies[3]['name'], $documents[1]['name']); - $documents = static::getDatabase()->find('movies', [], 2, 0, ['price', 'year'], [Database::ORDER_DESC, Database::ORDER_ASC], $movies[3]); + $documents = static::getDatabase()->find('movies', [ + Query::limit(2), + Query::offset(0), + Query::orderDesc('price'), + Query::orderAsc('year'), + Query::cursorAfter($movies[3]) + ]); $this->assertEquals(2, count($documents)); $this->assertEquals($movies[4]['name'], $documents[0]['name']); $this->assertEquals($movies[5]['name'], $documents[1]['name']); - $documents = static::getDatabase()->find('movies', [], 2, 0, ['price', 'year'], [Database::ORDER_DESC, Database::ORDER_ASC], $movies[4]); + $documents = static::getDatabase()->find('movies', [ + Query::limit(2), + Query::offset(0), + Query::orderDesc('price'), + Query::orderAsc('year'), + Query::cursorAfter($movies[4]) + ]); $this->assertEquals(1, count($documents)); $this->assertEquals($movies[5]['name'], $documents[0]['name']); - $documents = static::getDatabase()->find('movies', [], 2, 0, ['price', 'year'], [Database::ORDER_DESC, Database::ORDER_ASC], $movies[5]); + $documents = static::getDatabase()->find('movies', [ + Query::limit(2), + Query::offset(0), + Query::orderDesc('price'), + Query::orderAsc('year'), + Query::cursorAfter($movies[5]) + ]); $this->assertEmpty(count($documents)); /** * ORDER BY - Multiple Attribute Before */ - $movies = static::getDatabase()->find('movies', [], 25, 0, ['price', 'year'], [Database::ORDER_DESC, Database::ORDER_ASC]); + $movies = static::getDatabase()->find('movies', [ + Query::limit(25), + Query::offset(0), + Query::orderDesc('price'), + Query::orderAsc('year') + ]); - $documents = static::getDatabase()->find('movies', [], 2, 0, ['price', 'year'], [Database::ORDER_DESC, Database::ORDER_ASC], $movies[5], Database::CURSOR_BEFORE); + $documents = static::getDatabase()->find('movies', [ + Query::limit(2), + Query::offset(0), + Query::orderDesc('price'), + Query::orderAsc('year'), + Query::cursorBefore($movies[5]) + ]); $this->assertEquals(2, count($documents)); $this->assertEquals($movies[3]['name'], $documents[0]['name']); $this->assertEquals($movies[4]['name'], $documents[1]['name']); - $documents = static::getDatabase()->find('movies', [], 2, 0, ['price', 'year'], [Database::ORDER_DESC, Database::ORDER_ASC], $movies[4], Database::CURSOR_BEFORE); + $documents = static::getDatabase()->find('movies', [ + Query::limit(2), + Query::offset(0), + Query::orderDesc('price'), + Query::orderAsc('year'), + Query::cursorBefore($movies[4]) + ]); $this->assertEquals(2, count($documents)); $this->assertEquals($movies[2]['name'], $documents[0]['name']); $this->assertEquals($movies[3]['name'], $documents[1]['name']); - $documents = static::getDatabase()->find('movies', [], 2, 0, ['price', 'year'], [Database::ORDER_DESC, Database::ORDER_ASC], $movies[2], Database::CURSOR_BEFORE); + $documents = static::getDatabase()->find('movies', [ + Query::limit(2), + Query::offset(0), + Query::orderDesc('price'), + Query::orderAsc('year'), + Query::cursorBefore($movies[2]) + ]); $this->assertEquals(2, count($documents)); $this->assertEquals($movies[0]['name'], $documents[0]['name']); $this->assertEquals($movies[1]['name'], $documents[1]['name']); - $documents = static::getDatabase()->find('movies', [], 2, 0, ['price', 'year'], [Database::ORDER_DESC, Database::ORDER_ASC], $movies[1], Database::CURSOR_BEFORE); + $documents = static::getDatabase()->find('movies', [ + Query::limit(2), + Query::offset(0), + Query::orderDesc('price'), + Query::orderAsc('year'), + Query::cursorBefore($movies[1]) + ]); $this->assertEquals(1, count($documents)); $this->assertEquals($movies[0]['name'], $documents[0]['name']); - $documents = static::getDatabase()->find('movies', [], 2, 0, ['price', 'year'], [Database::ORDER_DESC, Database::ORDER_ASC], $movies[0], Database::CURSOR_BEFORE); + $documents = static::getDatabase()->find('movies', [ + Query::limit(2), + Query::offset(0), + Query::orderDesc('price'), + Query::orderAsc('year'), + Query::cursorBefore($movies[0]) + ]); $this->assertEmpty(count($documents)); /** * ORDER BY + CURSOR */ - $documentsTest = static::getDatabase()->find('movies', [], 2, 0, ['price'], [Database::ORDER_DESC]); - $documents = static::getDatabase()->find('movies', [], 1, 0, ['price'], [Database::ORDER_DESC], $documentsTest[0], Database::CURSOR_AFTER); + $documentsTest = static::getDatabase()->find('movies', [ + Query::limit(2), + Query::offset(0), + Query::orderDesc('price'), + ]); + $documents = static::getDatabase()->find('movies', [ + Query::limit(1), + Query::offset(0), + Query::orderDesc('price'), + Query::cursorAfter($documentsTest[0]) + ]); $this->assertEquals($documentsTest[1]['$id'], $documents[0]['$id']); /** * ORDER BY ID + CURSOR */ - $documentsTest = static::getDatabase()->find('movies', [], 2, 0, ['$id'], [Database::ORDER_DESC]); - $documents = static::getDatabase()->find('movies', [], 1, 0, ['$id'], [Database::ORDER_DESC], $documentsTest[0], Database::CURSOR_AFTER); + $documentsTest = static::getDatabase()->find('movies', [ + Query::limit(2), + Query::offset(0), + Query::orderDesc('$id'), + ]); + $documents = static::getDatabase()->find('movies', [ + Query::limit(1), + Query::offset(0), + Query::orderDesc('$id'), + Query::cursorAfter($documentsTest[0]) + ]); $this->assertEquals($documentsTest[1]['$id'], $documents[0]['$id']); /** * ORDER BY CREATE DATE + CURSOR */ - $documentsTest = static::getDatabase()->find('movies', [], 2, 0, ['$createdAt'], [Database::ORDER_DESC]); - $documents = static::getDatabase()->find('movies', [], 1, 0, ['$createdAt'], [Database::ORDER_DESC], $documentsTest[0], Database::CURSOR_AFTER); + $documentsTest = static::getDatabase()->find('movies', [ + Query::limit(2), + Query::offset(0), + Query::orderDesc('$createdAt'), + ]); + $documents = static::getDatabase()->find('movies', [ + Query::limit(1), + Query::offset(0), + Query::orderDesc('$createdAt'), + Query::cursorAfter($documentsTest[0]) + ]); $this->assertEquals($documentsTest[1]['$id'], $documents[0]['$id']); /** * ORDER BY UPDATE DATE + CURSOR */ - $documentsTest = static::getDatabase()->find('movies', [], 2, 0, ['$updatedAt'], [Database::ORDER_DESC]); - $documents = static::getDatabase()->find('movies', [], 1, 0, ['$updatedAt'], [Database::ORDER_DESC], $documentsTest[0], Database::CURSOR_AFTER); + $documentsTest = static::getDatabase()->find('movies', [ + Query::limit(2), + Query::offset(0), + Query::orderDesc('$updatedAt'), + ]); + $documents = static::getDatabase()->find('movies', [ + Query::limit(1), + Query::offset(0), + Query::orderDesc('$updatedAt'), + Query::cursorAfter($documentsTest[0]) + ]); $this->assertEquals($documentsTest[1]['$id'], $documents[0]['$id']); /** * Limit */ - $documents = static::getDatabase()->find('movies', [], 4, 0, ['name']); + $documents = static::getDatabase()->find('movies', [ + Query::limit(4), + Query::offset(0), + Query::orderAsc('name') + ]); $this->assertEquals(4, count($documents)); $this->assertEquals('Captain America: The First Avenger', $documents[0]['name']); @@ -1316,7 +1599,11 @@ public function testFind(Document $document) /** * Limit + Offset */ - $documents = static::getDatabase()->find('movies', [], 4, 2, ['name']); + $documents = static::getDatabase()->find('movies', [ + Query::limit(4), + Query::offset(2), + Query::orderAsc('name') + ]); $this->assertEquals(4, count($documents)); $this->assertEquals('Frozen', $documents[0]['name']); @@ -1328,8 +1615,8 @@ public function testFind(Document $document) * Test that OR queries are handled correctly */ $documents = static::getDatabase()->find('movies', [ - new Query(Query::TYPE_EQUAL, 'director', ['TBD', 'Joe Johnston']), - new Query(Query::TYPE_EQUAL, 'year', [2025]), + Query::equal('director', ['TBD', 'Joe Johnston']), + Query::equal('year', [2025]), ]); $this->assertEquals(1, count($documents)); @@ -1342,7 +1629,11 @@ public function testFind(Document $document) ]); $this->expectException(Exception::class); - static::getDatabase()->find('movies', [], 2, 0, [], [], $document); + static::getDatabase()->find('movies', [ + Query::limit(2), + Query::offset(0), + Query::cursorAfter($document) + ]); } /** @@ -1364,7 +1655,7 @@ public function testCount() { $count = static::getDatabase()->count('movies'); $this->assertEquals(6, $count); - $count = static::getDatabase()->count('movies', [new Query(Query::TYPE_EQUAL, 'year', [2019]),]); + $count = static::getDatabase()->count('movies', [Query::equal('year', [2019]),]); $this->assertEquals(2, $count); Authorization::disable(); @@ -1382,8 +1673,8 @@ public function testCount() */ Authorization::disable(); $count = static::getDatabase()->count('movies', [ - new Query(Query::TYPE_EQUAL, 'director', ['TBD', 'Joe Johnston']), - new Query(Query::TYPE_EQUAL, 'year', [2025]), + Query::equal('director', ['TBD', 'Joe Johnston']), + Query::equal('year', [2025]), ]); $this->assertEquals(1, $count); Authorization::reset(); @@ -1395,26 +1686,26 @@ public function testCount() public function testSum() { Authorization::setRole('userx'); - $sum = static::getDatabase()->sum('movies', 'year', [new Query(Query::TYPE_EQUAL, 'year', [2019]),]); + $sum = static::getDatabase()->sum('movies', 'year', [Query::equal('year', [2019]),]); $this->assertEquals(2019 + 2019, $sum); $sum = static::getDatabase()->sum('movies', 'year'); $this->assertEquals(2013 + 2019 + 2011 + 2019 + 2025 + 2026, $sum); - $sum = static::getDatabase()->sum('movies', 'price', [new Query(Query::TYPE_EQUAL, 'year', [2019]),]); + $sum = static::getDatabase()->sum('movies', 'price', [Query::equal('year', [2019]),]); $this->assertEquals(round(39.50 + 25.99, 2), round($sum, 2)); - $sum = static::getDatabase()->sum('movies', 'price', [new Query(Query::TYPE_EQUAL, 'year', [2019]),]); + $sum = static::getDatabase()->sum('movies', 'price', [Query::equal('year', [2019]),]); $this->assertEquals(round(39.50 + 25.99, 2), round($sum, 2)); - $sum = static::getDatabase()->sum('movies', 'year', [new Query(Query::TYPE_EQUAL, 'year', [2019])], 1); + $sum = static::getDatabase()->sum('movies', 'year', [Query::equal('year', [2019])], 1); $this->assertEquals(2019, $sum); Authorization::unsetRole('userx'); - $sum = static::getDatabase()->sum('movies', 'year', [new Query(Query::TYPE_EQUAL, 'year', [2019]),]); + $sum = static::getDatabase()->sum('movies', 'year', [Query::equal('year', [2019]),]); $this->assertEquals(2019 + 2019, $sum); $sum = static::getDatabase()->sum('movies', 'year'); $this->assertEquals(2013 + 2019 + 2011 + 2019 + 2025, $sum); - $sum = static::getDatabase()->sum('movies', 'price', [new Query(Query::TYPE_EQUAL, 'year', [2019]),]); + $sum = static::getDatabase()->sum('movies', 'price', [Query::equal('year', [2019]),]); $this->assertEquals(round(39.50 + 25.99, 2), round($sum, 2)); - $sum = static::getDatabase()->sum('movies', 'price', [new Query(Query::TYPE_EQUAL, 'year', [2019]),]); + $sum = static::getDatabase()->sum('movies', 'price', [Query::equal('year', [2019]),]); $this->assertEquals(round(39.50 + 25.99, 2), round($sum, 2)); } @@ -2370,8 +2661,8 @@ public function testCreateDatetime() $this->assertEquals(false, DateTime::isValid($document->getAttribute('date2'))); $documents = static::getDatabase()->find('datetime', [ - new Query(Query::TYPE_GREATER, 'date', ['1975-12-06 10:00:00+01:00']), - new Query(Query::TYPE_LESSER, 'date', ['2030-12-06 10:00:00-01:00']), + Query::greaterThan('date', '1975-12-06 10:00:00+01:00'), + Query::lessThan('date', '2030-12-06 10:00:00-01:00'), ]); if (in_array(static::getAdapterName(), ['mysql', 'mariadb'])) { // todo: fix in mongo @@ -2468,11 +2759,13 @@ public function testReservedKeywords() $this->assertEquals('reservedKeyDocument', $documents[0]->getId()); $this->assertEquals('Reserved:' . $keyword, $documents[0]->getAttribute($keyword)); - $documents = $database->find($collectionName, [new Query(Query::TYPE_EQUAL, $keyword, ["Reserved:${keyword}"])]); + $documents = $database->find($collectionName, [Query::equal($keyword, ["Reserved:${keyword}"])]); $this->assertCount(1, $documents); $this->assertEquals('reservedKeyDocument', $documents[0]->getId()); - $documents = $database->find($collectionName, orderAttributes: [$keyword], orderTypes: ['DESC']); + $documents = $database->find($collectionName, [ + Query::orderDesc($keyword) + ]); $this->assertCount(1, $documents); $this->assertEquals('reservedKeyDocument', $documents[0]->getId());