diff --git a/application/Espo/Core/Select/SelectBuilder.php b/application/Espo/Core/Select/SelectBuilder.php index a3b69468e7..1378294ece 100644 --- a/application/Espo/Core/Select/SelectBuilder.php +++ b/application/Espo/Core/Select/SelectBuilder.php @@ -468,7 +468,7 @@ class SelectBuilder $this->withDefaultOrder(); } - if ($this->searchParams->getMaxSize() || $this->searchParams->getOffset()) { + if ($this->searchParams->getMaxSize() !== null || $this->searchParams->getOffset() !== null) { $this->createLimitApplier() ->apply( $this->queryBuilder, diff --git a/application/Espo/ORM/QueryComposer/BaseQueryComposer.php b/application/Espo/ORM/QueryComposer/BaseQueryComposer.php index 339131b071..b2535df4ef 100644 --- a/application/Espo/ORM/QueryComposer/BaseQueryComposer.php +++ b/application/Espo/ORM/QueryComposer/BaseQueryComposer.php @@ -3475,7 +3475,7 @@ abstract class BaseQueryComposer implements QueryComposer $sql .= " ORDER BY {$order}"; } - if ($limit) { + if ($limit !== null) { $sql = $this->limit($sql, null, $limit); } @@ -3507,7 +3507,7 @@ abstract class BaseQueryComposer implements QueryComposer $sql .= " ORDER BY {$order}"; } - if ($limit) { + if ($limit !== null) { $sql = $this->limit($sql, null, $limit); } diff --git a/package-lock.json b/package-lock.json index aa5d2fd64c..e4162adc25 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "espocrm", - "version": "7.1.9", + "version": "7.1.10", "lockfileVersion": 2, "requires": true, "packages": { diff --git a/package.json b/package.json index 52dcea0cb0..a4bd920e45 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "espocrm", - "version": "7.1.9", + "version": "7.1.10", "description": "Open-source CRM.", "repository": { "type": "git", diff --git a/tests/unit/Espo/Core/Select/SelectBuilderTest.php b/tests/unit/Espo/Core/Select/SelectBuilderTest.php index fc37c9d671..71fb1a00d2 100644 --- a/tests/unit/Espo/Core/Select/SelectBuilderTest.php +++ b/tests/unit/Espo/Core/Select/SelectBuilderTest.php @@ -282,4 +282,23 @@ class SelectBuilderTest extends \PHPUnit\Framework\TestCase $this->assertEquals($this->entityType, $query->getFrom()); } + + public function testBuildMaxSize0() + { + + $searchParams = SearchParams::create()->withMaxSize(0); + + + $this->limitApplier + ->expects($this->once()) + ->method('apply') + ->with($this->isInstanceOf(QueryBuilder::class), 0, 0); + + $query = $this->selectBuilder + ->from($this->entityType) + ->withSearchParams($searchParams) + ->build(); + + $this->assertEquals($this->entityType, $query->getFrom()); + } } diff --git a/tests/unit/Espo/ORM/MysqlQueryComposerTest.php b/tests/unit/Espo/ORM/MysqlQueryComposerTest.php index e6fc1007fc..dd0b611224 100644 --- a/tests/unit/Espo/ORM/MysqlQueryComposerTest.php +++ b/tests/unit/Espo/ORM/MysqlQueryComposerTest.php @@ -150,7 +150,7 @@ class MysqlQueryComposerTest extends \PHPUnit\Framework\TestCase $this->assertEquals($expectedSql, $sql); } - public function testDeleteWithLimit() + public function testDeleteWithLimit1() { $sql = $this->query->compose(Delete::fromRaw([ 'from' => 'Account', @@ -170,6 +170,26 @@ class MysqlQueryComposerTest extends \PHPUnit\Framework\TestCase $this->assertEquals($expectedSql, $sql); } + public function testDeleteWithLimit0() + { + $sql = $this->query->compose(Delete::fromRaw([ + 'from' => 'Account', + 'whereClause' => [ + 'name' => 'test', + ], + 'orderBy' => 'name', + 'limit' => 0, + ])); + + $expectedSql = + "DELETE FROM `account` " . + "WHERE account.name = 'test' " . + "ORDER BY account.name ASC " . + "LIMIT 0"; + + $this->assertEquals($expectedSql, $sql); + } + public function testDeleteWithAlias() { $query = $this->queryBuilder @@ -206,6 +226,28 @@ class MysqlQueryComposerTest extends \PHPUnit\Framework\TestCase $this->assertEquals($expectedSql, $sql); } + public function testUpdateQuery0() + { + $sql = $this->query->compose(Update::fromRaw([ + 'from' => 'Account', + 'whereClause' => [ + 'name' => 'test', + ], + 'set' => [ + 'deleted' => false, + 'name' => 'hello', + ], + 'limit' => 0, + ])); + + $expectedSql = + "UPDATE `account` " . + "SET account.deleted = 0, account.name = 'hello' ". + "WHERE account.name = 'test' LIMIT 0"; + + $this->assertEquals($expectedSql, $sql); + } + public function testUpdateQuery1() { $sql = $this->query->compose(Update::fromRaw([ @@ -383,6 +425,40 @@ class MysqlQueryComposerTest extends \PHPUnit\Framework\TestCase $this->assertEquals($expectedSql, $sql); } + public function testSelectLimit1() + { + $select = $this->queryBuilder + ->select(['id']) + ->from('Account') + ->limit(1, 0) + ->build(); + + $sql = $this->query->compose($select); + + $expectedSql = + "SELECT account.id AS `id` FROM `account` " . + "WHERE account.deleted = 0 LIMIT 1, 0"; + + $this->assertEquals($expectedSql, $sql); + } + + public function testSelectLimit2() + { + $select = $this->queryBuilder + ->select(['id']) + ->from('Account') + ->limit(null, 0) + ->build(); + + $sql = $this->query->compose($select); + + $expectedSql = + "SELECT account.id AS `id` FROM `account` " . + "WHERE account.deleted = 0 LIMIT 0, 0"; + + $this->assertEquals($expectedSql, $sql); + } + public function testSelectAllColumns1() { $select = $this->queryBuilder @@ -2297,7 +2373,7 @@ class MysqlQueryComposerTest extends \PHPUnit\Framework\TestCase ->all() ->query($q1) ->query($q2) - ->limit(0, 2) + ->limit(0, 0) ->order(1, 'DESC') ->build(); @@ -2308,7 +2384,7 @@ class MysqlQueryComposerTest extends \PHPUnit\Framework\TestCase "UNION ALL ". "(SELECT 'test2' AS `value`) ". "ORDER BY 1 DESC ". - "LIMIT 0, 2"; + "LIMIT 0, 0"; $this->assertEquals($expectedSql, $sql); }