Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add foundRows option for MySQLi config #8979

Merged
merged 6 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
1 change: 1 addition & 0 deletions system/Database/BaseConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
* @property-read string $DSN
* @property-read array|bool $encrypt
* @property-read array $failover
* @property-read bool $foundRows
* @property-read string $hostname
* @property-read Query $lastQuery
* @property-read string $password
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 affected_rows 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
192 changes: 192 additions & 0 deletions tests/system/Database/Live/MySQLi/FoundRowsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* 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;

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

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

protected function setUp(): void
{
parent::setUp();

$config = config('Database');

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

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

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

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

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

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

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

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

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

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

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

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

$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);

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

$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);

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

$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);

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

$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);

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

$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);

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

$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 @@ -121,6 +121,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
Loading