MediaWiki master
PostgresResultWrapper.php
Go to the documentation of this file.
1<?php
2
3namespace Wikimedia\Rdbms;
4
7 private $db;
9 private $handle;
11 private $result;
12
19 public function __construct( DatabasePostgres $db, $handle, $result ) {
20 $this->db = $db;
21 $this->handle = $handle;
22 $this->result = $result;
23 }
24
25 protected function doNumRows() {
26 return pg_num_rows( $this->result );
27 }
28
29 protected function doFetchObject() {
30 // pg_fetch_object may raise a warning after a seek to an invalid offset
31 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
32 $row = @pg_fetch_object( $this->result );
33 // Map boolean values (T352229)
34 if ( is_object( $row ) ) {
35 $numFields = pg_num_fields( $this->result );
36 for ( $i = 0; $i < $numFields; $i++ ) {
37 if ( pg_field_type( $this->result, $i ) === 'bool' ) {
38 $name = pg_field_name( $this->result, $i );
39 $row->$name = $this->convertBoolean( $row->$name );
40 }
41 }
42 }
43 return $row;
44 }
45
46 protected function doFetchRow() {
47 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
48 $row = @pg_fetch_array( $this->result );
49 // Map boolean values (T352229)
50 if ( is_array( $row ) ) {
51 $numFields = pg_num_fields( $this->result );
52 for ( $i = 0; $i < $numFields; $i++ ) {
53 if ( pg_field_type( $this->result, $i ) === 'bool' ) {
54 $name = pg_field_name( $this->result, $i );
55 $row[$i] = $this->convertBoolean( $row[$i] );
56 $row[$name] = $this->convertBoolean( $row[$name] );
57 }
58 }
59 }
60 return $row;
61 }
62
70 private function convertBoolean( $value ) {
71 if ( $value === 't' ) {
72 return '1';
73 } elseif ( $value === 'f' ) {
74 return '0';
75 } else {
76 // Just pass through values that are not 't' or 'f'
77 return $value;
78 }
79 }
80
81 protected function doSeek( $pos ) {
82 pg_result_seek( $this->result, $pos );
83 }
84
85 protected function doFree() {
86 return pg_free_result( $this->result );
87 }
88
89 protected function doGetFieldNames() {
90 $names = [];
91 $n = pg_num_fields( $this->result );
92 for ( $i = 0; $i < $n; $i++ ) {
93 $names[] = pg_field_name( $this->result, $i );
94 }
95 return $names;
96 }
97}
Postgres database abstraction layer.
__construct(DatabasePostgres $db, $handle, $result)
doFetchObject()
Get the next row as a stdClass object, or false if iteration has proceeded past the end.
doGetFieldNames()
Get the field names in the result set.
doFetchRow()
Get the next row as an array containing the data duplicated, once with string keys and once with nume...
doNumRows()
Get the number of rows in the result set.
doSeek( $pos)
Modify the current cursor position to the row with the specified offset.
Result wrapper for grabbing data queried from an IDatabase object.