Skip to content

Commit 8d694f2

Browse files
committed
Implement WP_PDO_Proxy_Statement::fetchColumn()
1 parent c087051 commit 8d694f2

2 files changed

Lines changed: 65 additions & 1 deletion

File tree

‎tests/WP_PDO_MySQL_On_SQLite_PDO_API_Tests.php‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,70 @@ public function test_fetch( $query, $mode, $expected ): void {
324324
}
325325
}
326326

327+
public function test_fetch_column(): void {
328+
$query = "
329+
SELECT 1, 'abc', true
330+
UNION ALL
331+
SELECT 2, 'xyz', false
332+
UNION ALL
333+
SELECT 3, null, null
334+
";
335+
336+
// Fetch first column (default).
337+
$stmt = $this->driver->query( $query );
338+
$this->assertSame( '1', $stmt->fetchColumn() );
339+
$this->assertSame( '2', $stmt->fetchColumn() );
340+
$this->assertSame( '3', $stmt->fetchColumn() );
341+
$this->assertFalse( $stmt->fetchColumn() );
342+
343+
// Fetch second column.
344+
$stmt = $this->driver->query( $query );
345+
$this->assertSame( 'abc', $stmt->fetchColumn( 1 ) );
346+
$this->assertSame( 'xyz', $stmt->fetchColumn( 1 ) );
347+
$this->assertNull( $stmt->fetchColumn( 1 ) );
348+
$this->assertFalse( $stmt->fetchColumn( 1 ) );
349+
350+
// Fetch third column.
351+
$stmt = $this->driver->query( $query );
352+
$this->assertSame( '1', $stmt->fetchColumn( 2 ) );
353+
$this->assertSame( '0', $stmt->fetchColumn( 2 ) );
354+
$this->assertNull( $stmt->fetchColumn( 2 ) );
355+
$this->assertFalse( $stmt->fetchColumn( 2 ) );
356+
357+
// Fetch different columns across rows.
358+
$stmt = $this->driver->query( $query );
359+
$this->assertSame( '1', $stmt->fetchColumn( 0 ) );
360+
$this->assertSame( 'xyz', $stmt->fetchColumn( 1 ) );
361+
$this->assertNull( $stmt->fetchColumn( 2 ) );
362+
$this->assertFalse( $stmt->fetchColumn() );
363+
}
364+
365+
public function test_fetch_column_invalid_index(): void {
366+
$stmt = $this->driver->query( "SELECT 1, 'abc', true" );
367+
368+
if ( PHP_VERSION_ID < 80000 ) {
369+
$this->expectException( PDOException::class );
370+
$this->expectExceptionMessage( 'Invalid column index' );
371+
} else {
372+
$this->expectException( ValueError::class );
373+
$this->expectExceptionMessage( 'Invalid column index' );
374+
}
375+
$stmt->fetchColumn( 3 );
376+
}
377+
378+
public function test_fetch_column_negative_index(): void {
379+
$stmt = $this->driver->query( "SELECT 1, 'abc', true" );
380+
381+
if ( PHP_VERSION_ID < 80000 ) {
382+
$this->expectException( PDOException::class );
383+
$this->expectExceptionMessage( 'Invalid column index' );
384+
} else {
385+
$this->expectException( ValueError::class );
386+
$this->expectExceptionMessage( 'Column index must be greater than or equal to 0' );
387+
}
388+
$stmt->fetchColumn( -1 );
389+
}
390+
327391
public function test_attr_default_fetch_mode(): void {
328392
$this->driver->setAttribute( PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_NUM );
329393
$result = $this->driver->query( "SELECT 'a', 'b', 'c'" );

‎wp-includes/sqlite-ast/class-wp-pdo-proxy-statement.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ public function fetch(
196196
*/
197197
#[ReturnTypeWillChange]
198198
public function fetchColumn( $column = 0 ) {
199-
throw new RuntimeException( 'Not implemented' );
199+
return $this->statement->fetchColumn( $column );
200200
}
201201

202202
/**

0 commit comments

Comments
 (0)