|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\ORM\Connect; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* A result-set from a MySQL database (using MySQLiConnector) |
|
7
|
|
|
* Note that this class is only used for the results of non-prepared statements |
|
8
|
|
|
*/ |
|
9
|
|
|
class MySQLQuery extends Query |
|
10
|
|
|
{ |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* The internal MySQL handle that points to the result set. |
|
14
|
|
|
* Select queries will have mysqli_result as a value. |
|
15
|
|
|
* Non-select queries will not |
|
16
|
|
|
* |
|
17
|
|
|
* @var mixed |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $handle; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Metadata about the columns of this query |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $columns; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Hook the result-set given into a Query class, suitable for use by SilverStripe. |
|
28
|
|
|
* |
|
29
|
|
|
* @param MySQLiConnector $database The database object that created this query. |
|
30
|
|
|
* @param mixed $handle the internal mysql handle that is points to the resultset. |
|
31
|
|
|
* Non-mysqli_result values could be given for non-select queries (e.g. true) |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct($database, $handle) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->handle = $handle; |
|
36
|
|
|
if (is_object($this->handle)) { |
|
37
|
|
|
$this->columns = $this->handle->fetch_fields(); |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function __destruct() |
|
42
|
|
|
{ |
|
43
|
|
|
if (is_object($this->handle)) { |
|
44
|
|
|
$this->handle->free(); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function getIterator() |
|
49
|
|
|
{ |
|
50
|
|
|
if (is_object($this->handle)) { |
|
51
|
|
|
while ($data = $this->nextRecord()) { |
|
52
|
|
|
yield $data; |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function numRecords() |
|
58
|
|
|
{ |
|
59
|
|
|
if (is_object($this->handle)) { |
|
60
|
|
|
return $this->handle->num_rows; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return null; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function nextRecord() |
|
67
|
|
|
{ |
|
68
|
|
|
$floatTypes = [MYSQLI_TYPE_FLOAT, MYSQLI_TYPE_DOUBLE, MYSQLI_TYPE_DECIMAL, MYSQLI_TYPE_NEWDECIMAL]; |
|
69
|
|
|
|
|
70
|
|
|
if (is_object($this->handle) && ($row = $this->handle->fetch_array(MYSQLI_NUM))) { |
|
71
|
|
|
$data = []; |
|
72
|
|
|
foreach ($row as $i => $value) { |
|
73
|
|
|
if (!isset($this->columns[$i])) { |
|
74
|
|
|
throw new DatabaseException("Can't get metadata for column $i"); |
|
75
|
|
|
} |
|
76
|
|
|
if (in_array($this->columns[$i]->type, $floatTypes)) { |
|
77
|
|
|
$value = (float)$value; |
|
78
|
|
|
} |
|
79
|
|
|
$data[$this->columns[$i]->name] = $value; |
|
80
|
|
|
} |
|
81
|
|
|
return $data; |
|
82
|
|
|
} else { |
|
83
|
|
|
return false; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|