Skip to content

Commit

Permalink
feat: add feature flag for limit(0)
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Dec 1, 2023
1 parent 6e10d52 commit c035ef1
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 10 deletions.
8 changes: 8 additions & 0 deletions app/Config/Feature.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,12 @@ class Feature extends BaseConfig
* Use filter execution order in 4.4 or before.
*/
public bool $oldFilterOrder = false;

/**
* Keep the behavior of `limit(0)` in Query Builder in 4.4 or before.
*
* If true, `limit(0)` returns all records. (the behavior in 4.4 or before)
* If false, `limit(0)` returns no records.
*/
public bool $limitZeroAsAll = false;
}
5 changes: 5 additions & 0 deletions system/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use CodeIgniter\I18n\Time;
use CodeIgniter\Pager\Pager;
use CodeIgniter\Validation\ValidationInterface;
use Config\Feature;
use Config\Services;
use InvalidArgumentException;
use ReflectionClass;
Expand Down Expand Up @@ -596,6 +597,10 @@ public function findColumn(string $columnName)
*/
public function findAll(?int $limit = null, int $offset = 0)
{
if (config(Feature::class)->limitZeroAsAll) {
$limit ??= 0;
}

if ($this->tempAllowCallbacks) {
// Call the before event and check for a return
$eventData = $this->trigger('beforeFind', [
Expand Down
18 changes: 16 additions & 2 deletions system/Database/BaseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use CodeIgniter\Database\Exceptions\DatabaseException;
use CodeIgniter\Database\Exceptions\DataException;
use CodeIgniter\Traits\ConditionalTrait;
use Config\Feature;
use InvalidArgumentException;

/**
Expand Down Expand Up @@ -2493,6 +2494,13 @@ protected function _update(string $table, array $values): string
$valStr[] = $key . ' = ' . $val;
}

if (config(Feature::class)->limitZeroAsAll) {
return 'UPDATE ' . $this->compileIgnore('update') . $table . ' SET ' . implode(', ', $valStr)
. $this->compileWhereHaving('QBWhere')
. $this->compileOrderBy()
. ($this->QBLimit ? $this->_limit(' ', true) : '');
}

return 'UPDATE ' . $this->compileIgnore('update') . $table . ' SET ' . implode(', ', $valStr)
. $this->compileWhereHaving('QBWhere')
. $this->compileOrderBy()
Expand Down Expand Up @@ -3028,8 +3036,14 @@ protected function compileSelect($selectOverride = false): string
. $this->compileWhereHaving('QBHaving')
. $this->compileOrderBy();

if ($this->QBLimit !== false || $this->QBOffset) {
$sql = $this->_limit($sql . "\n");
if (config(Feature::class)->limitZeroAsAll) {
if ($this->QBLimit || $this->QBOffset) {
$sql = $this->_limit($sql . "\n");
}
} else {
if ($this->QBLimit !== false || $this->QBOffset) {
$sql = $this->_limit($sql . "\n");
}
}

return $this->unionInjection($sql);
Expand Down
25 changes: 17 additions & 8 deletions system/Database/SQLSRV/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use CodeIgniter\Database\Exceptions\DataException;
use CodeIgniter\Database\RawSql;
use CodeIgniter\Database\ResultInterface;
use Config\Feature;

/**
* Builder for SQLSRV
Expand Down Expand Up @@ -306,12 +307,14 @@ private function addIdentity(string $fullTable, string $insert): string
*/
protected function _limit(string $sql, bool $offsetIgnore = false): string
{
// SQL Server cannot handle `LIMIT 0`.
// DatabaseException:
// [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]The number of
// rows provided for a FETCH clause must be greater then zero.
if ($this->QBLimit === 0) {
return $sql . ' WHERE 1=0 ';
if (! config(Feature::class)->limitZeroAsAll) {
// SQL Server cannot handle `LIMIT 0`.
// DatabaseException:
// [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]The number of
// rows provided for a FETCH clause must be greater then zero.
if ($this->QBLimit === 0) {
return $sql . ' WHERE 1=0 ';
}
}

if (empty($this->QBOrderBy)) {
Expand Down Expand Up @@ -596,8 +599,14 @@ protected function compileSelect($selectOverride = false): string
. $this->compileOrderBy(); // ORDER BY

// LIMIT
if ($this->QBLimit !== false || $this->QBOffset) {
$sql = $this->_limit($sql . "\n");
if (config(Feature::class)->limitZeroAsAll) {
if ($this->QBLimit) {
$sql = $this->_limit($sql . "\n");
}
} else {
if ($this->QBLimit !== false || $this->QBOffset) {
$sql = $this->_limit($sql . "\n");
}
}

return $this->unionInjection($sql);
Expand Down
5 changes: 5 additions & 0 deletions system/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use CodeIgniter\Exceptions\ModelException;
use CodeIgniter\Validation\ValidationInterface;
use Config\Database;
use Config\Feature;
use ReflectionException;

/**
Expand Down Expand Up @@ -228,6 +229,10 @@ protected function doFindColumn(string $columnName)
*/
protected function doFindAll(?int $limit = null, int $offset = 0)
{
if (config(Feature::class)->limitZeroAsAll) {
$limit ??= 0;
}

$builder = $this->builder();

if ($this->tempUseSoftDeletes) {
Expand Down

0 comments on commit c035ef1

Please sign in to comment.