Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 33 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 1 |
| PostgresResultWrapper | |
0.00% |
0 / 33 |
|
0.00% |
0 / 8 |
306 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| doNumRows | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| doFetchObject | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
20 | |||
| doFetchRow | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
20 | |||
| convertBoolean | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| doSeek | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| doFree | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| doGetFieldNames | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Wikimedia\Rdbms; |
| 4 | |
| 5 | use PgSql\Connection; |
| 6 | use PgSql\Result; |
| 7 | |
| 8 | /** |
| 9 | * Result wrapper for PostgreSQL database results. |
| 10 | * |
| 11 | * @since 1.37 |
| 12 | */ |
| 13 | class PostgresResultWrapper extends ResultWrapper { |
| 14 | /** @var DatabasePostgres */ |
| 15 | private $db; |
| 16 | private Connection $handle; |
| 17 | private Result $result; |
| 18 | |
| 19 | /** |
| 20 | * @internal |
| 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 | /** @inheritDoc */ |
| 29 | protected function doNumRows() { |
| 30 | return pg_num_rows( $this->result ); |
| 31 | } |
| 32 | |
| 33 | /** @inheritDoc */ |
| 34 | protected function doFetchObject() { |
| 35 | // pg_fetch_object may raise a warning after a seek to an invalid offset |
| 36 | // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged |
| 37 | $row = @pg_fetch_object( $this->result ); |
| 38 | // Map boolean values (T352229) |
| 39 | if ( is_object( $row ) ) { |
| 40 | $numFields = pg_num_fields( $this->result ); |
| 41 | for ( $i = 0; $i < $numFields; $i++ ) { |
| 42 | if ( pg_field_type( $this->result, $i ) === 'bool' ) { |
| 43 | $name = pg_field_name( $this->result, $i ); |
| 44 | $row->$name = $this->convertBoolean( $row->$name ); |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | return $row; |
| 49 | } |
| 50 | |
| 51 | /** @inheritDoc */ |
| 52 | protected function doFetchRow() { |
| 53 | // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged |
| 54 | $row = @pg_fetch_array( $this->result ); |
| 55 | // Map boolean values (T352229) |
| 56 | if ( is_array( $row ) ) { |
| 57 | $numFields = pg_num_fields( $this->result ); |
| 58 | for ( $i = 0; $i < $numFields; $i++ ) { |
| 59 | if ( pg_field_type( $this->result, $i ) === 'bool' ) { |
| 60 | $name = pg_field_name( $this->result, $i ); |
| 61 | $row[$i] = $this->convertBoolean( $row[$i] ); |
| 62 | $row[$name] = $this->convertBoolean( $row[$name] ); |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | return $row; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Convert a boolean value from the database to the string '0' or '1' for |
| 71 | * compatibility with MySQL. |
| 72 | * |
| 73 | * @param mixed $value |
| 74 | * @return mixed |
| 75 | */ |
| 76 | private function convertBoolean( $value ) { |
| 77 | if ( $value === 't' ) { |
| 78 | return '1'; |
| 79 | } elseif ( $value === 'f' ) { |
| 80 | return '0'; |
| 81 | } else { |
| 82 | // Just pass through values that are not 't' or 'f' |
| 83 | return $value; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /** @inheritDoc */ |
| 88 | protected function doSeek( $pos ) { |
| 89 | pg_result_seek( $this->result, $pos ); |
| 90 | } |
| 91 | |
| 92 | /** @inheritDoc */ |
| 93 | protected function doFree() { |
| 94 | return pg_free_result( $this->result ); |
| 95 | } |
| 96 | |
| 97 | /** @inheritDoc */ |
| 98 | protected function doGetFieldNames() { |
| 99 | $names = []; |
| 100 | $n = pg_num_fields( $this->result ); |
| 101 | for ( $i = 0; $i < $n; $i++ ) { |
| 102 | $names[] = pg_field_name( $this->result, $i ); |
| 103 | } |
| 104 | return $names; |
| 105 | } |
| 106 | } |