|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Phossa Project |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 5.4 |
|
6
|
|
|
* |
|
7
|
|
|
* @category Library |
|
8
|
|
|
* @package Phossa2\Db |
|
9
|
|
|
* @copyright Copyright (c) 2016 phossa.com |
|
10
|
|
|
* @license http://mit-license.org/ MIT License |
|
11
|
|
|
* @link http://www.phossa.com/ |
|
12
|
|
|
*/ |
|
13
|
|
|
/*# declare(strict_types=1); */ |
|
14
|
|
|
|
|
15
|
|
|
namespace Phossa2\Db\Driver\Mysqli; |
|
16
|
|
|
|
|
17
|
|
|
use Phossa2\Db\Driver\ResultAbstract; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Result |
|
21
|
|
|
* |
|
22
|
|
|
* @package Phossa2\Db |
|
23
|
|
|
* @author Hong Zhang <[email protected]> |
|
24
|
|
|
* @see ResultAbstract |
|
25
|
|
|
* @version 2.0.0 |
|
26
|
|
|
* @since 2.0.0 added |
|
27
|
|
|
*/ |
|
28
|
|
|
class Result extends ResultAbstract |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* the column names |
|
32
|
|
|
* |
|
33
|
|
|
* @var array |
|
34
|
|
|
* @access protected |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $cols; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* the column values |
|
40
|
|
|
* |
|
41
|
|
|
* @var array |
|
42
|
|
|
* @access protected |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $vals; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var \mysqli_stmt |
|
48
|
|
|
* @access protected |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $statement; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Invoke to set statement |
|
54
|
|
|
* |
|
55
|
|
|
* @param \mysqli_stmt $statement |
|
56
|
|
|
* @return $this |
|
57
|
|
|
* @access public |
|
58
|
|
|
*/ |
|
59
|
|
|
public function __invoke(\mysqli_stmt $statement) |
|
60
|
|
|
{ |
|
61
|
|
|
$this->statement = $statement; |
|
62
|
|
|
return $this; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* {@inheritDoc} |
|
67
|
|
|
*/ |
|
68
|
|
|
public function fieldCount()/*# : int */ |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->statement->field_count; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* {@inheritDoc} |
|
75
|
|
|
*/ |
|
76
|
|
|
public function rowCount()/*# : int */ |
|
77
|
|
|
{ |
|
78
|
|
|
return $this->statement->num_rows; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* {@inheritDoc} |
|
83
|
|
|
*/ |
|
84
|
|
|
public function affectedRows()/*# : int */ |
|
85
|
|
|
{ |
|
86
|
|
|
return $this->statement->affected_rows; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* {@inheritDoc} |
|
91
|
|
|
*/ |
|
92
|
|
|
public function close() |
|
93
|
|
|
{ |
|
94
|
|
|
if ($this->statement) { |
|
95
|
|
|
$this->statement->free_result(); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* {@inheritDoc} |
|
101
|
|
|
*/ |
|
102
|
|
|
protected function realFetchAll()/*# : array */ |
|
103
|
|
|
{ |
|
104
|
|
|
return $this->realFetchRow(100000); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* {@inheritDoc} |
|
109
|
|
|
*/ |
|
110
|
|
|
protected function realFetchRow($rowCount)/*# : array */ |
|
111
|
|
|
{ |
|
112
|
|
|
$count = 0; |
|
113
|
|
|
$result = []; |
|
114
|
|
|
while ($count++ < $rowCount) { |
|
115
|
|
|
$row = $this->getOneRow(); |
|
116
|
|
|
if (is_array($row)) { |
|
117
|
|
|
$result[] = $row; |
|
118
|
|
|
} else { |
|
119
|
|
|
break; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
return $result; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Get one row of data |
|
127
|
|
|
* |
|
128
|
|
|
* @return array|false |
|
129
|
|
|
* @access protected |
|
130
|
|
|
*/ |
|
131
|
|
|
protected function getOneRow() |
|
132
|
|
|
{ |
|
133
|
|
|
if ($this->bindResult()) { |
|
134
|
|
|
if ($this->statement->fetch()) { |
|
135
|
|
|
$row = []; |
|
136
|
|
|
foreach ($this->cols as $i => $col) { |
|
137
|
|
|
$row[$col] = $this->vals[$i]; |
|
138
|
|
|
} |
|
139
|
|
|
return $row; |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
return false; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Bind results |
|
147
|
|
|
* |
|
148
|
|
|
* @return bool |
|
149
|
|
|
* @access protected |
|
150
|
|
|
*/ |
|
151
|
|
|
protected function bindResult()/*# : bool */ |
|
152
|
|
|
{ |
|
153
|
|
|
// get fields first |
|
154
|
|
|
if (!$this->getFields()) { |
|
155
|
|
|
return false; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
// bind values |
|
159
|
|
|
if (null === $this->vals) { |
|
160
|
|
|
$this->vals = array_fill(0, count($this->cols), null); |
|
161
|
|
|
|
|
162
|
|
|
$refs = []; |
|
163
|
|
|
foreach ($this->vals as $i => &$f) { |
|
164
|
|
|
$refs[$i] = &$f; |
|
165
|
|
|
} |
|
166
|
|
|
call_user_func_array([$this->statement, 'bind_result'], $refs); |
|
167
|
|
|
} |
|
168
|
|
|
return true; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Get fields first |
|
173
|
|
|
* |
|
174
|
|
|
* @return bool |
|
175
|
|
|
* @access protected |
|
176
|
|
|
*/ |
|
177
|
|
|
protected function getFields()/*# : bool */ |
|
178
|
|
|
{ |
|
179
|
|
|
if (null === $this->cols) { |
|
180
|
|
|
$result = $this->statement->result_metadata(); |
|
181
|
|
|
if (false === $result) { |
|
182
|
|
|
return false; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
$this->cols = []; |
|
186
|
|
|
|
|
187
|
|
|
// set column name |
|
188
|
|
|
foreach ($result->fetch_fields() as $col) { |
|
189
|
|
|
$this->cols[] = $col->name; |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
return true; |
|
193
|
|
|
} |
|
194
|
|
|
} |
|
195
|
|
|
|