File: PostgresResultWrapper.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (100 lines) | stat: -rw-r--r-- 2,431 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php

namespace Wikimedia\Rdbms;

use PgSql\Connection;
use PgSql\Result;

/**
 * Result wrapper for PostgreSQL database results.
 *
 * @since 1.37
 */
class PostgresResultWrapper extends ResultWrapper {
	/** @var DatabasePostgres */
	private $db;
	private Connection $handle;
	private Result $result;

	/**
	 * @internal
	 */
	public function __construct( DatabasePostgres $db, Connection $handle, Result $result ) {
		$this->db = $db;
		$this->handle = $handle;
		$this->result = $result;
	}

	protected function doNumRows() {
		return pg_num_rows( $this->result );
	}

	protected function doFetchObject() {
		// pg_fetch_object may raise a warning after a seek to an invalid offset
		// phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
		$row = @pg_fetch_object( $this->result );
		// Map boolean values (T352229)
		if ( is_object( $row ) ) {
			$numFields = pg_num_fields( $this->result );
			for ( $i = 0; $i < $numFields; $i++ ) {
				if ( pg_field_type( $this->result, $i ) === 'bool' ) {
					$name = pg_field_name( $this->result, $i );
					$row->$name = $this->convertBoolean( $row->$name );
				}
			}
		}
		return $row;
	}

	protected function doFetchRow() {
		// phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
		$row = @pg_fetch_array( $this->result );
		// Map boolean values (T352229)
		if ( is_array( $row ) ) {
			$numFields = pg_num_fields( $this->result );
			for ( $i = 0; $i < $numFields; $i++ ) {
				if ( pg_field_type( $this->result, $i ) === 'bool' ) {
					$name = pg_field_name( $this->result, $i );
					$row[$i] = $this->convertBoolean( $row[$i] );
					$row[$name] = $this->convertBoolean( $row[$name] );
				}
			}
		}
		return $row;
	}

	/**
	 * Convert a boolean value from the database to the string '0' or '1' for
	 * compatibility with MySQL.
	 *
	 * @param mixed $value
	 * @return mixed
	 */
	private function convertBoolean( $value ) {
		if ( $value === 't' ) {
			return '1';
		} elseif ( $value === 'f' ) {
			return '0';
		} else {
			// Just pass through values that are not 't' or 'f'
			return $value;
		}
	}

	protected function doSeek( $pos ) {
		pg_result_seek( $this->result, $pos );
	}

	protected function doFree() {
		return pg_free_result( $this->result );
	}

	protected function doGetFieldNames() {
		$names = [];
		$n = pg_num_fields( $this->result );
		for ( $i = 0; $i < $n; $i++ ) {
			$names[] = pg_field_name( $this->result, $i );
		}
		return $names;
	}
}