| 1 |
<?php |
| 2 |
|
| 3 |
namespace Wikimedia\Rdbms; |
| 4 |
|
| 5 |
use PgSql\Connection; |
| 6 |
use PgSql\Result; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
class PostgresResultWrapper extends ResultWrapper { |
| 14 |
|
| 15 |
private $db; |
| 16 |
private Connection $handle; |
| 17 |
private Result $result; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
public function __construct( DatabasePostgres $db, Connection $handle, Result $result ) { |
| 23 |
$this->db = $db; |
| 24 |
$this->handle = $handle; |
| 25 |
$this->result = $result; |
| 26 |
} |
| 27 |
|
| 28 |
protected function doNumRows() { |
| 29 |
return pg_num_rows( $this->result ); |
| 30 |
} |
| 31 |
|
| 32 |
protected function doFetchObject() { |
| 33 |
|
| 34 |
|
| 35 |
$row = @pg_fetch_object( $this->result ); |
| 36 |
|
| 37 |
if ( is_object( $row ) ) { |
| 38 |
$numFields = pg_num_fields( $this->result ); |
| 39 |
for ( $i = 0; $i < $numFields; $i++ ) { |
| 40 |
if ( pg_field_type( $this->result, $i ) === 'bool' ) { |
| 41 |
$name = pg_field_name( $this->result, $i ); |
| 42 |
$row->$name = $this->convertBoolean( $row->$name ); |
| 43 |
} |
| 44 |
} |
| 45 |
} |
| 46 |
return $row; |
| 47 |
} |
| 48 |
|
| 49 |
protected function doFetchRow() { |
| 50 |
|
| 51 |
$row = @pg_fetch_array( $this->result ); |
| 52 |
|
| 53 |
if ( is_array( $row ) ) { |
| 54 |
$numFields = pg_num_fields( $this->result ); |
| 55 |
for ( $i = 0; $i < $numFields; $i++ ) { |
| 56 |
if ( pg_field_type( $this->result, $i ) === 'bool' ) { |
| 57 |
$name = pg_field_name( $this->result, $i ); |
| 58 |
$row[$i] = $this->convertBoolean( $row[$i] ); |
| 59 |
$row[$name] = $this->convertBoolean( $row[$name] ); |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
return $row; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
private function convertBoolean( $value ) { |
| 74 |
if ( $value === 't' ) { |
| 75 |
return '1'; |
| 76 |
} elseif ( $value === 'f' ) { |
| 77 |
return '0'; |
| 78 |
} else { |
| 79 |
|
| 80 |
return $value; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
protected function doSeek( $pos ) { |
| 85 |
pg_result_seek( $this->result, $pos ); |
| 86 |
} |
| 87 |
|
| 88 |
protected function doFree() { |
| 89 |
return pg_free_result( $this->result ); |
| 90 |
} |
| 91 |
|
| 92 |
protected function doGetFieldNames() { |
| 93 |
$names = []; |
| 94 |
$n = pg_num_fields( $this->result ); |
| 95 |
for ( $i = 0; $i < $n; $i++ ) { |
| 96 |
$names[] = pg_field_name( $this->result, $i ); |
| 97 |
} |
| 98 |
return $names; |
| 99 |
} |
| 100 |
} |