1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\ORM\Connect; |
4
|
|
|
|
5
|
|
|
use mysqli_result; |
6
|
|
|
use mysqli_stmt; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Provides a record-view for mysqli statements |
10
|
|
|
* |
11
|
|
|
* By default streams unbuffered data, but seek(), rewind(), or numRecords() will force the statement to |
12
|
|
|
* buffer itself and sacrifice any potential performance benefit. |
13
|
|
|
*/ |
14
|
|
|
class MySQLStatement extends Query |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The related mysqli statement object if generated using a prepared query |
19
|
|
|
* |
20
|
|
|
* @var mysqli_stmt |
21
|
|
|
*/ |
22
|
|
|
protected $statement; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Metadata result for this statement |
26
|
|
|
* |
27
|
|
|
* @var mysqli_result |
28
|
|
|
*/ |
29
|
|
|
protected $metadata; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Is the statement bound to the current resultset? |
33
|
|
|
* |
34
|
|
|
* @var bool |
35
|
|
|
*/ |
36
|
|
|
protected $bound = false; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* List of column names |
40
|
|
|
* |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
protected $columns = array(); |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* List of bound variables in the current row |
47
|
|
|
* |
48
|
|
|
* @var array |
49
|
|
|
*/ |
50
|
|
|
protected $boundValues = array(); |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Hook the result-set given into a Query class, suitable for use by SilverStripe. |
54
|
|
|
* @param mysqli_stmt $statement The related statement, if present |
55
|
|
|
* @param mysqli_result $metadata The metadata for this statement |
56
|
|
|
*/ |
57
|
|
|
public function __construct($statement, $metadata) |
58
|
|
|
{ |
59
|
|
|
$this->statement = $statement; |
60
|
|
|
$this->metadata = $metadata; |
61
|
|
|
|
62
|
|
|
// Immediately bind and buffer |
63
|
|
|
$this->bind(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function __destruct() |
67
|
|
|
{ |
68
|
|
|
$this->statement->close(); |
69
|
|
|
$this->currentRecord = false; |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Binds this statement to the variables |
74
|
|
|
*/ |
75
|
|
|
protected function bind() |
76
|
|
|
{ |
77
|
|
|
$variables = array(); |
78
|
|
|
|
79
|
|
|
// Bind each field |
80
|
|
|
while ($field = $this->metadata->fetch_field()) { |
81
|
|
|
$this->columns[] = $field->name; |
82
|
|
|
// Note that while boundValues isn't initialised at this point, |
83
|
|
|
// later calls to $this->statement->fetch() Will populate |
84
|
|
|
// $this->boundValues later with the next result. |
85
|
|
|
$variables[] = &$this->boundValues[$field->name]; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$this->bound = true; |
89
|
|
|
$this->metadata->free(); |
90
|
|
|
|
91
|
|
|
// Buffer all results |
92
|
|
|
$this->statement->store_result(); |
93
|
|
|
|
94
|
|
|
call_user_func_array(array($this->statement, 'bind_result'), $variables); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function getIterator() |
98
|
|
|
{ |
99
|
|
|
while ($this->statement->fetch()) { |
100
|
|
|
// Dereferenced row |
101
|
|
|
$row = array(); |
102
|
|
|
foreach ($this->boundValues as $key => $value) { |
103
|
|
|
$row[$key] = $value; |
104
|
|
|
} |
105
|
|
|
yield $row; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function numRecords() |
110
|
|
|
{ |
111
|
|
|
return $this->statement->num_rows(); |
|
|
|
|
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|