Skip to content

Commit

Permalink
Merge pull request #8979 from ducng99/feat/add_foundrows_mysqli_config
Browse files Browse the repository at this point in the history
feat: add `foundRows` option for MySQLi config
  • Loading branch information
kenjis authored Jun 25, 2024
2 parents 24544d6 + 9efac84 commit ab71e4f
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/Config/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class Database extends Config
'failover' => [],
'port' => 3306,
'numberNative' => false,
'foundRows' => false,
'dateFormat' => [
'date' => 'Y-m-d',
'datetime' => 'Y-m-d H:i:s',
Expand Down
6 changes: 6 additions & 0 deletions phpstan-baseline.php
Original file line number Diff line number Diff line change
Expand Up @@ -13339,6 +13339,12 @@
'count' => 2,
'path' => __DIR__ . '/tests/system/Database/Live/MySQLi/NumberNativeTest.php',
];
$ignoreErrors[] = [
// identifier: property.notFound
'message' => '#^Access to an undefined property CodeIgniter\\\\Database\\\\BaseConnection\\:\\:\\$foundRows\\.$#',
'count' => 2,
'path' => __DIR__ . '/tests/system/Database/Live/MySQLi/FoundRowsTest.php',
];
$ignoreErrors[] = [
// identifier: missingType.property
'message' => '#^Property CodeIgniter\\\\Database\\\\Live\\\\MySQLi\\\\NumberNativeTest\\:\\:\\$tests has no type specified\\.$#',
Expand Down
14 changes: 14 additions & 0 deletions system/Database/MySQLi/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@ class Connection extends BaseConnection
*/
public $numberNative = false;

/**
* Use MYSQLI_CLIENT_FOUND_ROWS
*
* Whether affectedRows() should return number of rows found,
* or number of rows changed, after an UPDATE query.
*
* @var bool
*/
public $foundRows = false;

/**
* Connect to the database.
*
Expand Down Expand Up @@ -182,6 +192,10 @@ public function connect(bool $persistent = false)
$clientFlags += MYSQLI_CLIENT_SSL;
}

if ($this->foundRows) {
$clientFlags += MYSQLI_CLIENT_FOUND_ROWS;
}

try {
if ($this->mysqli->real_connect(
$hostname,
Expand Down
166 changes: 166 additions & 0 deletions tests/system/Database/Live/MySQLi/FoundRowsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace CodeIgniter\Database\Live\MySQLi;

use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\DatabaseTestTrait;
use Config\Database;
use PHPUnit\Framework\Attributes\Group;
use Tests\Support\Database\Seeds\CITestSeeder;

/**
* @internal
*/
#[Group('DatabaseLive')]
final class FoundRowsTest extends CIUnitTestCase
{
use DatabaseTestTrait;

/**
* Database config for tests
*
* @var array<string, mixed>
*/
private $tests;

protected $refresh = true;
protected $seed = CITestSeeder::class;

protected function setUp(): void
{
$config = config('Database');

$this->tests = $config->tests;

if ($this->tests['DBDriver'] !== 'MySQLi') {
$this->markTestSkipped('Only MySQLi can complete this test.');
}

parent::setUp();
}

public function testEnableFoundRows(): void
{
$this->tests['foundRows'] = true;

$db1 = Database::connect($this->tests);

$this->assertTrue($db1->foundRows);
}

public function testDisableFoundRows(): void
{
$this->tests['foundRows'] = false;

$db1 = Database::connect($this->tests);

$this->assertFalse($db1->foundRows);
}

public function testAffectedRowsAfterEnableFoundRowsWithNoChange(): void
{
$this->tests['foundRows'] = true;

$db1 = Database::connect($this->tests);

$db1->table('db_user')
->set('country', 'US')
->where('country', 'US')
->update();

$affectedRows = $db1->affectedRows();

$this->assertSame($affectedRows, 2);
}

public function testAffectedRowsAfterDisableFoundRowsWithNoChange(): void
{
$this->tests['foundRows'] = false;

$db1 = Database::connect($this->tests);

$db1->table('db_user')
->set('country', 'US')
->where('country', 'US')
->update();

$affectedRows = $db1->affectedRows();

$this->assertSame($affectedRows, 0);
}

public function testAffectedRowsAfterEnableFoundRowsWithChange(): void
{
$this->tests['foundRows'] = true;

$db1 = Database::connect($this->tests);

$db1->table('db_user')
->set('country', 'NZ')
->where('country', 'US')
->update();

$affectedRows = $db1->affectedRows();

$this->assertSame($affectedRows, 2);
}

public function testAffectedRowsAfterDisableFoundRowsWithChange(): void
{
$this->tests['foundRows'] = false;

$db1 = Database::connect($this->tests);

$db1->table('db_user')
->set('country', 'NZ')
->where('country', 'US')
->update();

$affectedRows = $db1->affectedRows();

$this->assertSame($affectedRows, 2);
}

public function testAffectedRowsAfterEnableFoundRowsWithPartialChange(): void
{
$this->tests['foundRows'] = true;

$db1 = Database::connect($this->tests);

$db1->table('db_user')
->set('name', 'Derek Jones')
->where('country', 'US')
->update();

$affectedRows = $db1->affectedRows();

$this->assertSame($affectedRows, 2);
}

public function testAffectedRowsAfterDisableFoundRowsWithPartialChange(): void
{
$this->tests['foundRows'] = false;

$db1 = Database::connect($this->tests);

$db1->table('db_user')
->set('name', 'Derek Jones')
->where('country', 'US')
->update();

$affectedRows = $db1->affectedRows();

$this->assertSame($affectedRows, 1);
}
}
2 changes: 2 additions & 0 deletions user_guide_src/source/changelogs/v4.6.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ Forge
Others
------

- Added a new configuration ``foundRows`` for MySQLi to use ``MYSQLI_CLIENT_FOUND_ROWS``.

Model
=====

Expand Down
1 change: 1 addition & 0 deletions user_guide_src/source/database/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ Description of Values
To enforce Foreign Key constraint, set this config item to true.
**busyTimeout** (``SQLite3`` only) milliseconds (int) - Sleeps for a specified amount of time when a table is locked.
**numberNative** (``MySQLi`` only) true/false (boolean) - Whether to enable MYSQLI_OPT_INT_AND_FLOAT_NATIVE.
**foundRows** (``MySQLi`` only) true/false (boolean) - Whether to enable MYSQLI_CLIENT_FOUND_ROWS.
**dateFormat** The default date/time formats as PHP's `DateTime format`_.
* ``date`` - date format
* ``datetime`` - date and time format
Expand Down

0 comments on commit ab71e4f

Please sign in to comment.