MediaWiki master
MysqliResultWrapper.php
Go to the documentation of this file.
1<?php
2
3namespace Wikimedia\Rdbms;
4
5use mysqli_result;
6
9 private $db;
10
12 private $result;
13
19 public function __construct( DatabaseMySQL $db, mysqli_result $result ) {
20 $this->db = $db;
21 $this->result = $result;
22 }
23
25 protected function doNumRows() {
26 // We are not checking for any errors here, since
27 // there are no errors mysql_num_rows can cause.
28 // See https://dev.mysql.com/doc/refman/5.7/en/mysql-fetch-row.html.
29 // See https://phabricator.wikimedia.org/T44430
30 return $this->result->num_rows;
31 }
32
33 private function checkFetchError() {
34 $errno = $this->db->lastErrno();
35 // Unfortunately, mysql_fetch_array does not reset the last errno.
36 // Only check for CR_SERVER_LOST and CR_UNKNOWN_ERROR, as
37 // these are the only errors mysql_fetch_array can cause.
38 // See https://dev.mysql.com/doc/refman/5.7/en/mysql-fetch-row.html.
39 if ( $errno == 2000 || $errno == 2013 ) {
40 throw new DBUnexpectedError(
41 $this->db,
42 'Error in fetchRow(): ' . htmlspecialchars( $this->db->lastError() )
43 );
44 }
45 }
46
48 protected function doFetchObject() {
49 $object = $this->result->fetch_object();
50 $this->checkFetchError();
51 if ( $object === null ) {
52 return false;
53 }
54 return $object;
55 }
56
58 protected function doFetchRow() {
59 $array = $this->result->fetch_array();
60 $this->checkFetchError();
61 if ( $array === null ) {
62 return false;
63 }
64 return $array;
65 }
66
68 protected function doSeek( $pos ) {
69 $this->result->data_seek( $pos );
70 }
71
73 protected function doFree() {
74 $this->result = null;
75 }
76
78 protected function doGetFieldNames() {
79 $names = [];
80 foreach ( $this->result->fetch_fields() as $fieldInfo ) {
81 $names[] = $fieldInfo->name;
82 }
83 return $names;
84 }
85
93 public function getInternalFieldInfo( $fieldName ) {
94 for ( $i = 0; $i < $this->result->field_count; $i++ ) {
95 $meta = $this->result->fetch_field_direct( $i );
96 if ( $fieldName == $meta->name ) {
97 // Add missing properties to result (using flags property)
98 // which will be part of function mysql-fetch-field for backward compatibility
99 $meta->not_null = $meta->flags & MYSQLI_NOT_NULL_FLAG;
100 $meta->primary_key = $meta->flags & MYSQLI_PRI_KEY_FLAG;
101 $meta->unique_key = $meta->flags & MYSQLI_UNIQUE_KEY_FLAG;
102 $meta->multiple_key = $meta->flags & MYSQLI_MULTIPLE_KEY_FLAG;
103 $meta->binary = $meta->flags & MYSQLI_BINARY_FLAG;
104 $meta->numeric = $meta->flags & MYSQLI_NUM_FLAG;
105 $meta->blob = $meta->flags & MYSQLI_BLOB_FLAG;
106 $meta->unsigned = $meta->flags & MYSQLI_UNSIGNED_FLAG;
107 $meta->zerofill = $meta->flags & MYSQLI_ZEROFILL_FLAG;
108 return new MySQLField( $meta );
109 }
110 }
111 return false;
112 }
113}
doFree()
Free underlying data.It is not necessary to do anything.
doFetchObject()
Get the next row as a stdClass object, or false if iteration has proceeded past the end....
getInternalFieldInfo( $fieldName)
Get information about a field in the result set.
doNumRows()
Get the number of rows in the result set.1.37 int
__construct(DatabaseMySQL $db, mysqli_result $result)
doFetchRow()
Get the next row as an array containing the data duplicated, once with string keys and once with nume...
doGetFieldNames()
Get the field names in the result set.string[]
doSeek( $pos)
Modify the current cursor position to the row with the specified offset.If $pos is out of bounds,...
Result wrapper for grabbing data queried from an IDatabase object.