From 6f858267b9fcd5354b59fbf9cd2d001e0a6cff27 Mon Sep 17 00:00:00 2001 From: fogelito Date: Tue, 5 May 2026 15:27:50 +0300 Subject: [PATCH 1/2] Fix SQL query to --- composer.lock | 54 ++++++++++++++++++------------------ src/Database/Adapter/SQL.php | 21 ++++++++++++-- 2 files changed, 46 insertions(+), 29 deletions(-) diff --git a/composer.lock b/composer.lock index e7055b8e2..2d3ea74b6 100644 --- a/composer.lock +++ b/composer.lock @@ -1383,16 +1383,16 @@ }, { "name": "symfony/http-client", - "version": "v7.4.8", + "version": "v7.4.9", "source": { "type": "git", "url": "https://github.com/symfony/http-client.git", - "reference": "01933e626c3de76bea1e22641e205e78f6a34342" + "reference": "7e941c6abf4e3bf7dca160bf0e11ef36a9f832f6" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/http-client/zipball/01933e626c3de76bea1e22641e205e78f6a34342", - "reference": "01933e626c3de76bea1e22641e205e78f6a34342", + "url": "https://api.github.com/repos/symfony/http-client/zipball/7e941c6abf4e3bf7dca160bf0e11ef36a9f832f6", + "reference": "7e941c6abf4e3bf7dca160bf0e11ef36a9f832f6", "shasum": "" }, "require": { @@ -1460,7 +1460,7 @@ "http" ], "support": { - "source": "https://github.com/symfony/http-client/tree/v7.4.8" + "source": "https://github.com/symfony/http-client/tree/v7.4.9" }, "funding": [ { @@ -1480,7 +1480,7 @@ "type": "tidelift" } ], - "time": "2026-03-30T12:55:43+00:00" + "time": "2026-04-29T13:25:15+00:00" }, { "name": "symfony/http-client-contracts", @@ -1562,7 +1562,7 @@ }, { "name": "symfony/polyfill-mbstring", - "version": "v1.36.0", + "version": "v1.37.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-mbstring.git", @@ -1623,7 +1623,7 @@ "shim" ], "support": { - "source": "https://github.com/symfony/polyfill-mbstring/tree/v1.36.0" + "source": "https://github.com/symfony/polyfill-mbstring/tree/v1.37.0" }, "funding": [ { @@ -1647,7 +1647,7 @@ }, { "name": "symfony/polyfill-php82", - "version": "v1.36.0", + "version": "v1.37.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-php82.git", @@ -1703,7 +1703,7 @@ "shim" ], "support": { - "source": "https://github.com/symfony/polyfill-php82/tree/v1.36.0" + "source": "https://github.com/symfony/polyfill-php82/tree/v1.37.0" }, "funding": [ { @@ -1727,7 +1727,7 @@ }, { "name": "symfony/polyfill-php83", - "version": "v1.36.0", + "version": "v1.37.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-php83.git", @@ -1783,7 +1783,7 @@ "shim" ], "support": { - "source": "https://github.com/symfony/polyfill-php83/tree/v1.36.0" + "source": "https://github.com/symfony/polyfill-php83/tree/v1.37.0" }, "funding": [ { @@ -1807,16 +1807,16 @@ }, { "name": "symfony/polyfill-php85", - "version": "v1.36.0", + "version": "v1.37.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-php85.git", - "reference": "2c408a6bb0313e6001a83628dc5506100474254e" + "reference": "fcfa4973a9917cef23f2e38774da74a2b7d115ee" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-php85/zipball/2c408a6bb0313e6001a83628dc5506100474254e", - "reference": "2c408a6bb0313e6001a83628dc5506100474254e", + "url": "https://api.github.com/repos/symfony/polyfill-php85/zipball/fcfa4973a9917cef23f2e38774da74a2b7d115ee", + "reference": "fcfa4973a9917cef23f2e38774da74a2b7d115ee", "shasum": "" }, "require": { @@ -1863,7 +1863,7 @@ "shim" ], "support": { - "source": "https://github.com/symfony/polyfill-php85/tree/v1.36.0" + "source": "https://github.com/symfony/polyfill-php85/tree/v1.37.0" }, "funding": [ { @@ -1883,7 +1883,7 @@ "type": "tidelift" } ], - "time": "2026-04-10T16:50:15+00:00" + "time": "2026-04-26T13:10:57+00:00" }, { "name": "symfony/service-contracts", @@ -2295,16 +2295,16 @@ }, { "name": "utopia-php/validators", - "version": "0.2.0", + "version": "0.2.2", "source": { "type": "git", "url": "https://github.com/utopia-php/validators.git", - "reference": "30b6030a5b100fc1dff34506e5053759594b2a20" + "reference": "5d7d494e64457cd4eb67fdcfd9481f2c89796aa6" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/validators/zipball/30b6030a5b100fc1dff34506e5053759594b2a20", - "reference": "30b6030a5b100fc1dff34506e5053759594b2a20", + "url": "https://api.github.com/repos/utopia-php/validators/zipball/5d7d494e64457cd4eb67fdcfd9481f2c89796aa6", + "reference": "5d7d494e64457cd4eb67fdcfd9481f2c89796aa6", "shasum": "" }, "require": { @@ -2334,9 +2334,9 @@ ], "support": { "issues": "https://github.com/utopia-php/validators/issues", - "source": "https://github.com/utopia-php/validators/tree/0.2.0" + "source": "https://github.com/utopia-php/validators/tree/0.2.2" }, - "time": "2026-01-13T09:16:51+00:00" + "time": "2026-04-27T16:30:24+00:00" } ], "packages-dev": [ @@ -4581,7 +4581,7 @@ ], "aliases": [], "minimum-stability": "stable", - "stability-flags": {}, + "stability-flags": [], "prefer-stable": false, "prefer-lowest": false, "platform": { @@ -4591,6 +4591,6 @@ "ext-mbstring": "*", "ext-redis": "*" }, - "platform-dev": {}, - "plugin-api-version": "2.9.0" + "platform-dev": [], + "plugin-api-version": "2.6.0" } diff --git a/src/Database/Adapter/SQL.php b/src/Database/Adapter/SQL.php index eba1b3bcf..fffc82726 100644 --- a/src/Database/Adapter/SQL.php +++ b/src/Database/Adapter/SQL.php @@ -3331,7 +3331,14 @@ public function count(Document $collection, array $queries = [], ?int $max = nul ? 'WHERE ' . \implode(' AND ', $where) : ''; - $sql = " + if (\is_null($max)) { + $sql = " + SELECT COUNT(1) as sum + FROM {$this->getSQLTable($name)} AS {$this->quote($alias)} + {$sqlWhere} + "; + } else { + $sql = " SELECT COUNT(1) as sum FROM ( SELECT 1 FROM {$this->getSQLTable($name)} AS {$this->quote($alias)} @@ -3339,6 +3346,7 @@ public function count(Document $collection, array $queries = [], ?int $max = nul {$limit} ) table_count "; + } $sql = $this->trigger(Database::EVENT_DOCUMENT_COUNT, $sql); @@ -3417,7 +3425,14 @@ public function sum(Document $collection, string $attribute, array $queries = [] ? 'WHERE ' . \implode(' AND ', $where) : ''; - $sql = " + if (\is_null($max)) { + $sql = " + SELECT SUM({$this->quote($attribute)}) as sum + FROM {$this->getSQLTable($name)} AS {$this->quote($alias)} + {$sqlWhere} + "; + } else { + $sql = " SELECT SUM({$this->quote($attribute)}) as sum FROM ( SELECT {$this->quote($attribute)} FROM {$this->getSQLTable($name)} AS {$this->quote($alias)} @@ -3425,6 +3440,8 @@ public function sum(Document $collection, string $attribute, array $queries = [] {$limit} ) table_count "; + } + $sql = $this->trigger(Database::EVENT_DOCUMENT_SUM, $sql); From 5bc3e2632e1bef1a1a96d74dc31fb453b6817864 Mon Sep 17 00:00:00 2001 From: fogelito Date: Tue, 5 May 2026 15:55:47 +0300 Subject: [PATCH 2/2] check empty limit --- src/Database/Adapter/SQL.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Database/Adapter/SQL.php b/src/Database/Adapter/SQL.php index fffc82726..7724a455c 100644 --- a/src/Database/Adapter/SQL.php +++ b/src/Database/Adapter/SQL.php @@ -3331,11 +3331,11 @@ public function count(Document $collection, array $queries = [], ?int $max = nul ? 'WHERE ' . \implode(' AND ', $where) : ''; - if (\is_null($max)) { + if (empty($limit)) { $sql = " - SELECT COUNT(1) as sum - FROM {$this->getSQLTable($name)} AS {$this->quote($alias)} - {$sqlWhere} + SELECT COUNT(1) as sum + FROM {$this->getSQLTable($name)} AS {$this->quote($alias)} + {$sqlWhere} "; } else { $sql = " @@ -3425,7 +3425,7 @@ public function sum(Document $collection, string $attribute, array $queries = [] ? 'WHERE ' . \implode(' AND ', $where) : ''; - if (\is_null($max)) { + if (empty($limit)) { $sql = " SELECT SUM({$this->quote($attribute)}) as sum FROM {$this->getSQLTable($name)} AS {$this->quote($alias)}