2013-06-21 10:32:08 +12:00
|
|
|
<?php
|
|
|
|
|
2016-06-15 16:03:16 +12:00
|
|
|
namespace SilverStripe\ORM\Connect;
|
|
|
|
|
2013-06-21 10:32:08 +12:00
|
|
|
/**
|
|
|
|
* A result-set from a MySQL database (using MySQLiConnector)
|
2018-11-11 13:54:03 +13:00
|
|
|
* Note that this class is only used for the results of non-prepared statements
|
2013-06-21 10:32:08 +12:00
|
|
|
*/
|
2016-11-29 12:31:16 +13:00
|
|
|
class MySQLQuery extends Query
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The internal MySQL handle that points to the result set.
|
|
|
|
* Select queries will have mysqli_result as a value.
|
|
|
|
* Non-select queries will not
|
|
|
|
*
|
|
|
|
* @var mixed
|
|
|
|
*/
|
|
|
|
protected $handle;
|
|
|
|
|
2018-11-11 13:54:03 +13:00
|
|
|
/**
|
|
|
|
* Metadata about the columns of this query
|
|
|
|
*/
|
|
|
|
protected $columns;
|
|
|
|
|
2016-11-29 12:31:16 +13:00
|
|
|
/**
|
|
|
|
* Hook the result-set given into a Query class, suitable for use by SilverStripe.
|
|
|
|
*
|
|
|
|
* @param MySQLiConnector $database The database object that created this query.
|
|
|
|
* @param mixed $handle the internal mysql handle that is points to the resultset.
|
|
|
|
* Non-mysqli_result values could be given for non-select queries (e.g. true)
|
|
|
|
*/
|
|
|
|
public function __construct($database, $handle)
|
|
|
|
{
|
|
|
|
$this->handle = $handle;
|
2018-11-11 13:54:03 +13:00
|
|
|
if (is_object($this->handle)) {
|
|
|
|
$this->columns = $this->handle->fetch_fields();
|
|
|
|
}
|
2016-11-29 12:31:16 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
public function __destruct()
|
|
|
|
{
|
|
|
|
if (is_object($this->handle)) {
|
|
|
|
$this->handle->free();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-02 10:58:37 +12:00
|
|
|
public function seek($row)
|
2016-11-29 12:31:16 +13:00
|
|
|
{
|
|
|
|
if (is_object($this->handle)) {
|
2022-09-02 10:58:37 +12:00
|
|
|
// Fix for https://github.com/silverstripe/silverstripe-framework/issues/9097 without breaking the seek() API
|
|
|
|
$this->handle->data_seek($row);
|
|
|
|
$result = $this->nextRecord();
|
|
|
|
$this->handle->data_seek($row);
|
|
|
|
return $result;
|
2016-11-29 12:31:16 +13:00
|
|
|
}
|
2022-09-02 10:58:37 +12:00
|
|
|
return null;
|
2016-11-29 12:31:16 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
public function numRecords()
|
|
|
|
{
|
|
|
|
if (is_object($this->handle)) {
|
|
|
|
return $this->handle->num_rows;
|
|
|
|
}
|
2018-03-17 15:05:40 +00:00
|
|
|
return null;
|
2016-11-29 12:31:16 +13:00
|
|
|
}
|
2022-09-02 10:58:37 +12:00
|
|
|
|
|
|
|
public function nextRecord()
|
|
|
|
{
|
|
|
|
$floatTypes = [MYSQLI_TYPE_FLOAT, MYSQLI_TYPE_DOUBLE, MYSQLI_TYPE_DECIMAL, MYSQLI_TYPE_NEWDECIMAL];
|
|
|
|
|
|
|
|
if (is_object($this->handle) && ($row = $this->handle->fetch_array(MYSQLI_NUM))) {
|
|
|
|
$data = [];
|
|
|
|
foreach ($row as $i => $value) {
|
|
|
|
if (!isset($this->columns[$i])) {
|
|
|
|
throw new DatabaseException("Can't get metadata for column $i");
|
|
|
|
}
|
|
|
|
if (in_array($this->columns[$i]->type, $floatTypes ?? [])) {
|
|
|
|
$value = (float)$value;
|
|
|
|
}
|
|
|
|
$data[$this->columns[$i]->name] = $value;
|
|
|
|
}
|
|
|
|
return $data;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2013-06-21 10:32:08 +12:00
|
|
|
}
|