| 1 |
<?php |
| 2 |
|
| 3 |
namespace Wikimedia\Rdbms; |
| 4 |
|
| 5 |
use mysqli_result; |
| 6 |
|
| 7 |
class MysqliResultWrapper extends ResultWrapper { |
| 8 |
|
| 9 |
private $db; |
| 10 |
|
| 11 |
|
| 12 |
private $result; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
public function __construct( DatabaseMySQL $db, mysqli_result $result ) { |
| 20 |
$this->db = $db; |
| 21 |
$this->result = $result; |
| 22 |
} |
| 23 |
|
| 24 |
protected function doNumRows() { |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
return $this->result->num_rows; |
| 30 |
} |
| 31 |
|
| 32 |
private function checkFetchError() { |
| 33 |
$errno = $this->db->lastErrno(); |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 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 |
|
| 68 |
protected function doFree() { |
| 69 |
$this->result = null; |
| 70 |
} |
| 71 |
|
| 72 |
protected function doGetFieldNames() { |
| 73 |
$names = []; |
| 74 |
foreach ( $this->result->fetch_fields() as $fieldInfo ) { |
| 75 |
$names[] = $fieldInfo->name; |
| 76 |
} |
| 77 |
return $names; |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
public function getInternalFieldInfo( $fieldName ) { |
| 89 |
for ( $i = 0; $i < $this->result->field_count; $i++ ) { |
| 90 |
$meta = $this->result->fetch_field_direct( $i ); |
| 91 |
if ( $fieldName == $meta->name ) { |
| 92 |
|
| 93 |
|
| 94 |
$meta->not_null = $meta->flags & MYSQLI_NOT_NULL_FLAG; |
| 95 |
$meta->primary_key = $meta->flags & MYSQLI_PRI_KEY_FLAG; |
| 96 |
$meta->unique_key = $meta->flags & MYSQLI_UNIQUE_KEY_FLAG; |
| 97 |
$meta->multiple_key = $meta->flags & MYSQLI_MULTIPLE_KEY_FLAG; |
| 98 |
$meta->binary = $meta->flags & MYSQLI_BINARY_FLAG; |
| 99 |
$meta->numeric = $meta->flags & MYSQLI_NUM_FLAG; |
| 100 |
$meta->blob = $meta->flags & MYSQLI_BLOB_FLAG; |
| 101 |
$meta->unsigned = $meta->flags & MYSQLI_UNSIGNED_FLAG; |
| 102 |
$meta->zerofill = $meta->flags & MYSQLI_ZEROFILL_FLAG; |
| 103 |
return new MySQLField( $meta ); |
| 104 |
} |
| 105 |
} |
| 106 |
return false; |
| 107 |
} |
| 108 |
} |