/
MySQLQuery.php
92 lines (80 loc) · 2.56 KB
/
MySQLQuery.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
namespace SilverStripe\ORM\Connect;
use Traversable;
/**
* A result-set from a MySQL database (using MySQLiConnector)
* Note that this class is only used for the results of non-prepared statements
*/
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;
/**
* Metadata about the columns of this query
*/
protected $columns;
/**
* 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;
if (is_object($this->handle)) {
$this->columns = $this->handle->fetch_fields();
}
}
public function __destruct()
{
if (is_object($this->handle)) {
$this->handle->free();
}
}
public function getIterator(): Traversable
{
$floatTypes = [MYSQLI_TYPE_FLOAT, MYSQLI_TYPE_DOUBLE, MYSQLI_TYPE_DECIMAL, MYSQLI_TYPE_NEWDECIMAL];
if (is_object($this->handle)) {
while ($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;
}
yield $data;
}
// Reset so the query can be iterated over again
$this->rewind();
}
}
/**
* Rewind to the first row
*/
public function rewind(): void
{
// Check for the method first since $this->handle is a mixed type
if (is_object($this->handle) && method_exists($this->handle, 'data_seek')) {
$this->handle->data_seek(0);
}
}
public function numRecords()
{
if (is_object($this->handle)) {
return $this->handle->num_rows;
}
return null;
}
}