File: MysqliResultWrapper.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 (108 lines) | stat: -rw-r--r-- 2,995 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
101
102
103
104
105
106
107
108
<?php

namespace Wikimedia\Rdbms;

use mysqli_result;

class MysqliResultWrapper extends ResultWrapper {
	/** @var DatabaseMySQL */
	private $db;

	/** @var mysqli_result|null */
	private $result;

	/**
	 * @internal
	 * @param DatabaseMySQL $db
	 * @param mysqli_result $result
	 */
	public function __construct( DatabaseMySQL $db, mysqli_result $result ) {
		$this->db = $db;
		$this->result = $result;
	}

	protected function doNumRows() {
		// We are not checking for any errors here, since
		// there are no errors mysql_num_rows can cause.
		// See https://dev.mysql.com/doc/refman/5.7/en/mysql-fetch-row.html.
		// See https://phabricator.wikimedia.org/T44430
		return $this->result->num_rows;
	}

	private function checkFetchError() {
		$errno = $this->db->lastErrno();
		// Unfortunately, mysql_fetch_array does not reset the last errno.
		// Only check for CR_SERVER_LOST and CR_UNKNOWN_ERROR, as
		// these are the only errors mysql_fetch_array can cause.
		// See https://dev.mysql.com/doc/refman/5.7/en/mysql-fetch-row.html.
		if ( $errno == 2000 || $errno == 2013 ) {
			throw new DBUnexpectedError(
				$this->db,
				'Error in fetchRow(): ' . htmlspecialchars( $this->db->lastError() )
			);
		}
	}

	protected function doFetchObject() {
		$object = $this->result->fetch_object();
		$this->checkFetchError();
		if ( $object === null ) {
			return false;
		}
		return $object;
	}

	protected function doFetchRow() {
		$array = $this->result->fetch_array();
		$this->checkFetchError();
		if ( $array === null ) {
			return false;
		}
		return $array;
	}

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

	protected function doFree() {
		$this->result = null;
	}

	protected function doGetFieldNames() {
		$names = [];
		foreach ( $this->result->fetch_fields() as $fieldInfo ) {
			$names[] = $fieldInfo->name;
		}
		return $names;
	}

	/**
	 * Get information about a field in the result set
	 *
	 * @param string $fieldName
	 * @return bool|MySQLField
	 * @internal For DatabaseMySQL::fieldInfo() only
	 *
	 */
	public function getInternalFieldInfo( $fieldName ) {
		for ( $i = 0; $i < $this->result->field_count; $i++ ) {
			$meta = $this->result->fetch_field_direct( $i );
			if ( $fieldName == $meta->name ) {
				// Add missing properties to result (using flags property)
				// which will be part of function mysql-fetch-field for backward compatibility
				$meta->not_null = $meta->flags & MYSQLI_NOT_NULL_FLAG;
				$meta->primary_key = $meta->flags & MYSQLI_PRI_KEY_FLAG;
				$meta->unique_key = $meta->flags & MYSQLI_UNIQUE_KEY_FLAG;
				$meta->multiple_key = $meta->flags & MYSQLI_MULTIPLE_KEY_FLAG;
				$meta->binary = $meta->flags & MYSQLI_BINARY_FLAG;
				$meta->numeric = $meta->flags & MYSQLI_NUM_FLAG;
				$meta->blob = $meta->flags & MYSQLI_BLOB_FLAG;
				$meta->unsigned = $meta->flags & MYSQLI_UNSIGNED_FLAG;
				$meta->zerofill = $meta->flags & MYSQLI_ZEROFILL_FLAG;
				return new MySQLField( $meta );
			}
		}
		return false;
	}
}