Skip to content

Commit 3e75ae0

Browse files
authored
Fix duplicate column names in MySQL proxy results (#628)
## What? Closes #627 Fixes an issue where the MySQL proxy returns incorrect values when a query has columns with the same name. For example: ```sql SELECT 1 AS x, 2 AS x; ``` MySQL returns `1, 2`, but the proxy returns `2, 2`. This also affects joins with columns that have the same name. ## Why? `SQLite_Adapter::handle_query()` used `PDO::FETCH_OBJ`, which overwrites values when column names are the same. `MySQL_Protocol::build_row_packet()` then reads values by column name, causing both columns to return the same value. ## How? - Changed `SQLite_Adapter` to use `PDO::FETCH_NUM`. - Updated `MySQL_Protocol::build_row_packet()` to read values by column position instead of name. - Updated the method to accept `array $row` instead of `object $row`. Direct callers must now pass numeric arrays. - Added regression tests for duplicate column names and null values. - Updated existing test mocks to use arrays. ## Use of AI Tools **AI assistance:** Yes **Tool:** Codex **Used for:** Investigating the issue, implementing the fix, adding tests, and drafting the PR description. I manually reviewed and verified the changes. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit * **Bug Fixes** * Query results now preserve separate values when multiple columns share the same name, including columns returned from joined queries. * SQL `NULL` values are returned as `null` instead of being confused with other values. * Large query results continue to return blob values correctly. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 3dd5b35 commit 3e75ae0

4 files changed

Lines changed: 20 additions & 6 deletions

File tree

‎packages/mysql-proxy/src/Adapter/class-sqlite-adapter.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function handle_query( string $query ): MySQL_Result {
2828
$rows = array();
2929

3030
try {
31-
$statement = $this->sqlite_driver->query( $query, PDO::FETCH_OBJ );
31+
$statement = $this->sqlite_driver->query( $query, PDO::FETCH_NUM );
3232
$last_insert_id = (int) $this->sqlite_driver->lastInsertId();
3333
if ( $statement->columnCount() > 0 ) {
3434
$rows = $statement->fetchAll();

‎packages/mysql-proxy/src/class-mysql-protocol.php‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -501,13 +501,13 @@ public static function build_column_definition_packet( int $sequence_id, array $
501501
*
502502
* @param int $sequence_id The sequence ID of the packet.
503503
* @param array $columns The columns.
504-
* @param object $row The row.
504+
* @param array $row The row values, indexed by column position.
505505
* @return string The row packet.
506506
*/
507-
public static function build_row_packet( int $sequence_id, array $columns, object $row ): string {
507+
public static function build_row_packet( int $sequence_id, array $columns, array $row ): string {
508508
$payload = '';
509-
foreach ( $columns as $column ) {
510-
$value = $row->{$column['name']} ?? null;
509+
foreach ( $columns as $i => $column ) {
510+
$value = $row[ $i ] ?? null;
511511
if ( null === $value ) {
512512
$payload .= "\xfb"; // NULL value
513513
} else {

‎packages/mysql-proxy/tests/WP_MySQL_Proxy_Limits_Test.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public function test_client_that_stops_reading_does_not_block_the_proxy(): void
160160
'flags' => 0,
161161
'decimals' => 0,
162162
);
163-
$rows = array_fill( 0, 128, (object) array( 'v' => str_repeat( 'x', 65536 ) ) );
163+
$rows = array_fill( 0, 128, array( str_repeat( 'x', 65536 ) ) );
164164
$adapter = $this->createMock( Adapter::class );
165165
$adapter->expects( $this->once() )->method( 'handle_query' )->willReturn( MySQL_Result::from_data( 0, 0, array( $column ), $rows ) );
166166
$proxy = $this->proxy( $adapter );

‎packages/mysql-proxy/tests/WP_MySQL_Proxy_PDO_Test.php‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,20 @@ public function test_query(): void {
4747
);
4848
}
4949

50+
public function test_duplicate_column_names(): void {
51+
$result = $this->pdo->query( "SELECT '1' AS x, '2' AS x" );
52+
$this->assertSame( 2, $result->columnCount() );
53+
$this->assertSame( array( array( '1', '2' ) ), $result->fetchAll( PDO::FETCH_NUM ) );
54+
55+
$result = $this->pdo->query( "SELECT a.id, b.id FROM (SELECT '1' AS id) a JOIN (SELECT '2' AS id) b ON 1=1" );
56+
$this->assertSame( array( array( '1', '2' ) ), $result->fetchAll( PDO::FETCH_NUM ) );
57+
}
58+
59+
public function test_null_values(): void {
60+
$result = $this->pdo->query( "SELECT NULL AS a, 'b' AS b, NULL AS c" );
61+
$this->assertSame( array( array( null, 'b', null ) ), $result->fetchAll( PDO::FETCH_NUM ) );
62+
}
63+
5064
public function test_prepared_statement(): void {
5165
$this->pdo->exec( 'CREATE TABLE t (id INT PRIMARY KEY, name TEXT)' );
5266
$this->pdo->exec( 'INSERT INTO t (id, name) VALUES (123, "abc"), (456, "def")' );

0 commit comments

Comments
 (0)