Skip to content

Commit 81d6afa

Browse files
Nandan108claudeJanJakes
authored
Fix FOUND_ROWS() for more than nine rows without SQL_CALC_FOUND_ROWS (#533)
## Summary After a `SELECT` without `SQL_CALC_FOUND_ROWS`, or after a `SHOW` statement, `FOUND_ROWS()` returns only the first digit of the row count: ```sql -- Table "t" has 25 rows. SELECT id FROM t; SELECT FOUND_ROWS(); -- '2' (MySQL: 25) ``` The count of the previous query is read with `fetchColumn()[0]`, which takes the first character of a stringified count. Without stringified fetches, indexing the integer count returns `null`, so the result is `0` and PHP emits a warning. `SELECT SQL_CALC_FOUND_ROWS ...` is not affected; it uses a different path. ## Fix The fetched count is used directly. ## Tests `testFoundRowsWithoutSqlCalcFoundRowsAboveNine` checks `FOUND_ROWS()` after a 12-row `SELECT` and after `SHOW TABLES` with 13 tables, which covers both code paths. Each assertion fails on `trunk`. The existing `testFoundRowsWithoutSqlCalcFoundRows` only uses counts below ten. `packages/mysql-on-sqlite`: 1044 tests pass (PHP 8.3, SQLite 3.45.1); PHPCS is clean. ## Use of AI This change was prepared with Claude Code (Claude Opus 5.5). I reviewed the code, the tests, and this description, and I take responsibility for the change. 🤖 Generated with [Claude Code](https://claude.com/claude-code) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Corrected `FOUND_ROWS()` results for queries returning counts above nine, including when the database returns fetched values as strings. * **Tests** * Expanded coverage for row counts from unrestricted `SELECT` queries and metadata commands, including `SHOW TABLES` with multiple tables. Verified results with both database fetch settings. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Jan Jakeš <jan@jakes.pro>
1 parent ba9d450 commit 81d6afa

2 files changed

Lines changed: 27 additions & 19 deletions

File tree

‎packages/mysql-on-sqlite/src/sqlite/class-wp-mysql-on-sqlite.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5169,12 +5169,12 @@ private function translate_function_call( WP_Parser_Node $node ): string {
51695169
} elseif ( is_string( $found_rows ) ) {
51705170
return (int) $this->execute_sqlite_query(
51715171
sprintf( 'SELECT COUNT(*) FROM (%s)', $found_rows )
5172-
)->fetchColumn()[0];
5172+
)->fetchColumn();
51735173
} elseif ( is_array( $found_rows ) && isset( $found_rows[0] ) ) {
51745174
return (int) $this->execute_sqlite_query(
51755175
sprintf( 'SELECT COUNT(*) FROM (%s)', $found_rows[0] ),
51765176
$found_rows[1] ?? array()
5177-
)->fetchColumn()[0];
5177+
)->fetchColumn();
51785178
} else {
51795179
return 0;
51805180
}

‎packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php‎

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4186,79 +4186,87 @@ public function testCalcFoundRows() {
41864186
);
41874187
}
41884188

4189-
public function testFoundRowsWithoutSqlCalcFoundRows(): void {
4189+
/**
4190+
* @dataProvider stringifyFetchesProvider
4191+
*/
4192+
public function testFoundRowsWithoutSqlCalcFoundRows( bool $stringify_fetches ): void {
4193+
$this->engine->setAttribute( PDO::ATTR_STRINGIFY_FETCHES, $stringify_fetches );
4194+
41904195
$this->assertQuery( 'DROP TABLE _dates' );
41914196
$this->assertQuery( 'DROP TABLE _options' );
41924197

41934198
// CREATE TABLE
41944199
$this->assertQuery( 'CREATE TABLE t (id INT PRIMARY KEY, value TEXT, INDEX idx_value (value))' );
41954200
$result = $this->assertQuery( 'SELECT FOUND_ROWS()' );
4196-
$this->assertSame( '0', $result[0]->{'FOUND_ROWS()'} );
4201+
$this->assertEquals( 0, $result[0]->{'FOUND_ROWS()'} );
41974202

41984203
// INSERT
4199-
$this->assertQuery( 'INSERT INTO t (id) VALUES (1), (2), (3)' );
4204+
$this->assertQuery( 'INSERT INTO t (id) VALUES (1), (2), (3), (4), (5), (6), (7), (8), (9), (10), (11), (12)' );
42004205
$result = $this->assertQuery( 'SELECT FOUND_ROWS()' );
4201-
$this->assertSame( '0', $result[0]->{'FOUND_ROWS()'} );
4206+
$this->assertEquals( 0, $result[0]->{'FOUND_ROWS()'} );
42024207

42034208
// SELECT
42044209
$this->assertQuery( 'SELECT * FROM t' );
42054210
$result = $this->assertQuery( 'SELECT FOUND_ROWS()' );
4206-
$this->assertSame( '3', $result[0]->{'FOUND_ROWS()'} );
4211+
$this->assertEquals( 12, $result[0]->{'FOUND_ROWS()'} );
42074212

42084213
// DESCRIBE
42094214
$this->assertQuery( 'DESCRIBE t' );
42104215
$result = $this->assertQuery( 'SELECT FOUND_ROWS()' );
4211-
$this->assertSame( '2', $result[0]->{'FOUND_ROWS()'} );
4216+
$this->assertEquals( 2, $result[0]->{'FOUND_ROWS()'} );
42124217

42134218
// SHOW COLLATION
42144219
$this->assertQuery( 'SHOW COLLATION' );
42154220
$result = $this->assertQuery( 'SELECT FOUND_ROWS()' );
4216-
$this->assertSame( '8', $result[0]->{'FOUND_ROWS()'} );
4221+
$this->assertEquals( 8, $result[0]->{'FOUND_ROWS()'} );
42174222

42184223
// SHOW DATABASES
42194224
$this->assertQuery( 'SHOW DATABASES' );
42204225
$result = $this->assertQuery( 'SELECT FOUND_ROWS()' );
4221-
$this->assertSame( '2', $result[0]->{'FOUND_ROWS()'} );
4226+
$this->assertEquals( 2, $result[0]->{'FOUND_ROWS()'} );
42224227

42234228
// SHOW COLUMNS
42244229
$this->assertQuery( 'SHOW COLUMNS FROM t' );
42254230
$result = $this->assertQuery( 'SELECT FOUND_ROWS()' );
4226-
$this->assertSame( '2', $result[0]->{'FOUND_ROWS()'} );
4231+
$this->assertEquals( 2, $result[0]->{'FOUND_ROWS()'} );
42274232

42284233
// SHOW CREATE TABLE
42294234
$this->assertQuery( 'SHOW CREATE TABLE t' );
42304235
$result = $this->assertQuery( 'SELECT FOUND_ROWS()' );
4231-
$this->assertSame( '1', $result[0]->{'FOUND_ROWS()'} );
4236+
$this->assertEquals( 1, $result[0]->{'FOUND_ROWS()'} );
42324237

42334238
// SHOW CREATE TABLE with non-existent table
42344239
$this->assertQuery( 'SHOW CREATE TABLE non_existent_table' );
42354240
$result = $this->assertQuery( 'SELECT FOUND_ROWS()' );
4236-
$this->assertSame( '0', $result[0]->{'FOUND_ROWS()'} );
4241+
$this->assertEquals( 0, $result[0]->{'FOUND_ROWS()'} );
42374242

42384243
// SHOW INDEX
42394244
$this->assertQuery( 'SHOW INDEX FROM t' );
42404245
$result = $this->assertQuery( 'SELECT FOUND_ROWS()' );
4241-
$this->assertSame( '2', $result[0]->{'FOUND_ROWS()'} );
4246+
$this->assertEquals( 2, $result[0]->{'FOUND_ROWS()'} );
42424247

42434248
// SHOW GRANTS
42444249
$this->assertQuery( 'SHOW GRANTS' );
42454250
$result = $this->assertQuery( 'SELECT FOUND_ROWS()' );
4246-
$this->assertSame( '1', $result[0]->{'FOUND_ROWS()'} );
4251+
$this->assertEquals( 1, $result[0]->{'FOUND_ROWS()'} );
42474252

42484253
// SHOW TABLE STATUS
42494254
$r = $this->assertQuery( 'SHOW TABLE STATUS' );
42504255
$result = $this->assertQuery( 'SELECT FOUND_ROWS()' );
4251-
$this->assertSame( '1', $result[0]->{'FOUND_ROWS()'} );
4256+
$this->assertEquals( 1, $result[0]->{'FOUND_ROWS()'} );
42524257

4253-
// SHOW TABLES
4258+
// SHOW TABLES (with a two-digit count)
4259+
for ( $i = 1; $i <= 10; $i++ ) {
4260+
$this->assertQuery( "CREATE TABLE t$i (id INT)" );
4261+
}
42544262
$this->assertQuery( 'SHOW TABLES' );
42554263
$result = $this->assertQuery( 'SELECT FOUND_ROWS()' );
4256-
$this->assertSame( '1', $result[0]->{'FOUND_ROWS()'} );
4264+
$this->assertEquals( 11, $result[0]->{'FOUND_ROWS()'} );
42574265

42584266
// SHOW VARIABLES
42594267
$this->assertQuery( 'SHOW VARIABLES' );
42604268
$result = $this->assertQuery( 'SELECT FOUND_ROWS()' );
4261-
$this->assertSame( '2', $result[0]->{'FOUND_ROWS()'} );
4269+
$this->assertEquals( 2, $result[0]->{'FOUND_ROWS()'} );
42624270
}
42634271

42644272
public function testComplexSelectBasedOnDates() {

0 commit comments

Comments
 (0)