20250113#1
This commit is contained in:
78
src/Mysql/MysqlResultData.php
Normal file
78
src/Mysql/MysqlResultData.php
Normal file
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
namespace Rmphp\Storage\Mysql;
|
||||
|
||||
|
||||
class MysqlResultData {
|
||||
|
||||
private ?\mysqli_result $result;
|
||||
private array $arrayData = [];
|
||||
public int $count;
|
||||
public string $hex = "";
|
||||
|
||||
/**
|
||||
* MysqlDataObject constructor.
|
||||
* @param \mysqli_result|null $result
|
||||
*/
|
||||
public function __construct(\mysqli_result $result = null) {
|
||||
$this->result = $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isResult() : bool {
|
||||
return isset($this->result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \mysqli_result|null
|
||||
*/
|
||||
public function getMysqlResult() : ?\mysqli_result {
|
||||
return $this->result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return iterable
|
||||
*/
|
||||
public function fetch(): iterable {
|
||||
if(!empty($this->arrayData)) return $this->arrayData;
|
||||
if(!$this->result) return [];
|
||||
return $this->generator();
|
||||
}
|
||||
|
||||
|
||||
public function fetchOne(int $index = 0) : array {
|
||||
if(!$this->result) return [];
|
||||
$this->result->data_seek($index);
|
||||
return $this->result->fetch_assoc();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getData() : iterable {
|
||||
if(!empty($this->arrayData)) return $this->arrayData;
|
||||
if(!$this->result) return [];
|
||||
$this->result->data_seek(0);
|
||||
while ($row = $this->result->fetch_assoc()) {
|
||||
$this->arrayData[] = $row;
|
||||
}
|
||||
return $this->arrayData;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return iterable
|
||||
*/
|
||||
private function generator() : iterable {
|
||||
$this->result->data_seek(0);
|
||||
while ($row = $this->result->fetch_assoc()) {
|
||||
yield $row;
|
||||
}
|
||||
}
|
||||
|
||||
public function __destruct() {
|
||||
if($this->result) $this->result->close();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user