Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 39 |
|
0.00% |
0 / 9 |
CRAP | |
0.00% |
0 / 1 |
| MysqliResultWrapper | |
0.00% |
0 / 39 |
|
0.00% |
0 / 9 |
272 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| doNumRows | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| checkFetchError | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| doFetchObject | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| doFetchRow | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| doSeek | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| doFree | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| doGetFieldNames | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| getInternalFieldInfo | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Wikimedia\Rdbms; |
| 4 | |
| 5 | use mysqli_result; |
| 6 | |
| 7 | class MysqliResultWrapper extends ResultWrapper { |
| 8 | /** @var DatabaseMySQL */ |
| 9 | private $db; |
| 10 | |
| 11 | /** @var mysqli_result|null */ |
| 12 | private $result; |
| 13 | |
| 14 | /** |
| 15 | * @internal |
| 16 | * @param DatabaseMySQL $db |
| 17 | * @param mysqli_result $result |
| 18 | */ |
| 19 | public function __construct( DatabaseMySQL $db, mysqli_result $result ) { |
| 20 | $this->db = $db; |
| 21 | $this->result = $result; |
| 22 | } |
| 23 | |
| 24 | /** @inheritDoc */ |
| 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 | |
| 47 | /** @inheritDoc */ |
| 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 | |
| 57 | /** @inheritDoc */ |
| 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 | |
| 67 | /** @inheritDoc */ |
| 68 | protected function doSeek( $pos ) { |
| 69 | $this->result->data_seek( $pos ); |
| 70 | } |
| 71 | |
| 72 | /** @inheritDoc */ |
| 73 | protected function doFree() { |
| 74 | $this->result = null; |
| 75 | } |
| 76 | |
| 77 | /** @inheritDoc */ |
| 78 | protected function doGetFieldNames() { |
| 79 | $names = []; |
| 80 | foreach ( $this->result->fetch_fields() as $fieldInfo ) { |
| 81 | $names[] = $fieldInfo->name; |
| 82 | } |
| 83 | return $names; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Get information about a field in the result set |
| 88 | * |
| 89 | * @param string $fieldName |
| 90 | * @return bool|MySQLField |
| 91 | * @internal For DatabaseMySQL::fieldInfo() only |
| 92 | */ |
| 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 | } |