MediaWiki REL1_37
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( DatabaseMysqli $db, mysqli_result $result ) {
20 $this->db = $db;
21 $this->result = $result;
22 }
23
24 protected function doNumRows() {
25 // We are not checking for any errors here, since
26 // there are no errors mysql_num_rows can cause.
27 // See https://dev.mysql.com/doc/refman/5.7/en/mysql-fetch-row.html.
28 // See https://phabricator.wikimedia.org/T44430
29 return $this->result->num_rows;
30 }
31
32 private function checkFetchError() {
33 $errno = $this->db->lastErrno();
34 // Unfortunately, mysql_fetch_array does not reset the last errno.
35 // Only check for CR_SERVER_LOST and CR_UNKNOWN_ERROR, as
36 // these are the only errors mysql_fetch_array can cause.
37 // See https://dev.mysql.com/doc/refman/5.7/en/mysql-fetch-row.html.
38 if ( $errno == 2000 || $errno == 2013 ) {
39 throw new DBUnexpectedError(
40 $this->db,
41 'Error in fetchRow(): ' . htmlspecialchars( $this->db->lastError() )
42 );
43 }
44 }
45
46 protected function doFetchObject() {
47 $object = $this->result->fetch_object();
48 $this->checkFetchError();
49 if ( $object === null ) {
50 return false;
51 }
52 return $object;
53 }
54
55 protected function doFetchRow() {
56 $array = $this->result->fetch_array();
57 $this->checkFetchError();
58 if ( $array === null ) {
59 return false;
60 }
61 return $array;
62 }
63
64 protected function doSeek( $pos ) {
65 $this->result->data_seek( $pos );
66 }
67
75 public function getInternalResult() {
76 return $this->result;
77 }
78
79 protected function doFree() {
80 $this->result = null;
81 }
82
83 protected function doGetFieldNames() {
84 $names = [];
85 foreach ( $this->result->fetch_fields() as $fieldInfo ) {
86 $names[] = $fieldInfo->name;
87 }
88 return $names;
89 }
90
99 public function getInternalFieldInfo( $fieldName ) {
100 for ( $i = 0; $i < $this->result->field_count; $i++ ) {
101 $meta = $this->result->fetch_field_direct( $i );
102 if ( $fieldName == $meta->name ) {
103 // Add missing properties to result (using flags property)
104 // which will be part of function mysql-fetch-field for backward compatibility
105 $meta->not_null = $meta->flags & MYSQLI_NOT_NULL_FLAG;
106 $meta->primary_key = $meta->flags & MYSQLI_PRI_KEY_FLAG;
107 $meta->unique_key = $meta->flags & MYSQLI_UNIQUE_KEY_FLAG;
108 $meta->multiple_key = $meta->flags & MYSQLI_MULTIPLE_KEY_FLAG;
109 $meta->binary = $meta->flags & MYSQLI_BINARY_FLAG;
110 $meta->numeric = $meta->flags & MYSQLI_NUM_FLAG;
111 $meta->blob = $meta->flags & MYSQLI_BLOB_FLAG;
112 $meta->unsigned = $meta->flags & MYSQLI_UNSIGNED_FLAG;
113 $meta->zerofill = $meta->flags & MYSQLI_ZEROFILL_FLAG;
114 return new MySQLField( $meta );
115 }
116 }
117 return false;
118 }
119}
Database abstraction object for PHP extension mysqli.
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.
getInternalResult()
Get the underlying result object or array.
__construct(DatabaseMysqli $db, mysqli_result $result)
doNumRows()
Get the number of rows in the result set.
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.
doSeek( $pos)
Modify the current cursor position to the row with the specified offset.
Result wrapper for grabbing data queried from an IDatabase object.