Merge "Use gendered pronouns in emailuserfooter"
[mediawiki.git] / includes / libs / rdbms / database / resultwrapper / PostgresResultWrapper.php
blob5ed8bf3c6a9fd1dde65963807b95cedd8c09f87b
1 <?php
3 namespace Wikimedia\Rdbms;
5 // Phan insists these are resources until we drop PHP 7.4
6 /* @phan-file-suppress PhanTypeMismatchArgumentInternal */
8 class PostgresResultWrapper extends ResultWrapper {
9 /** @var DatabasePostgres */
10 private $db;
11 /** @var resource */
12 private $handle;
13 /** @var resource */
14 private $result;
16 /**
17 * @internal
18 * @param DatabasePostgres $db
19 * @param resource $handle
20 * @param resource $result
22 public function __construct( DatabasePostgres $db, $handle, $result ) {
23 $this->db = $db;
24 $this->handle = $handle;
25 $this->result = $result;
28 protected function doNumRows() {
29 return pg_num_rows( $this->result );
32 protected function doFetchObject() {
33 // pg_fetch_object may raise a warning after a seek to an invalid offset
34 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
35 $row = @pg_fetch_object( $this->result );
36 // Map boolean values (T352229)
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 );
46 return $row;
49 protected function doFetchRow() {
50 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
51 $row = @pg_fetch_array( $this->result );
52 // Map boolean values (T352229)
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] );
63 return $row;
66 /**
67 * Convert a boolean value from the database to the string '0' or '1' for
68 * compatibility with MySQL.
70 * @param mixed $value
71 * @return mixed
73 private function convertBoolean( $value ) {
74 if ( $value === 't' ) {
75 return '1';
76 } elseif ( $value === 'f' ) {
77 return '0';
78 } else {
79 // Just pass through values that are not 't' or 'f'
80 return $value;
84 protected function doSeek( $pos ) {
85 pg_result_seek( $this->result, $pos );
88 protected function doFree() {
89 return pg_free_result( $this->result );
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 );
98 return $names;