1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Pomm's Foundation package. |
4
|
|
|
* |
5
|
|
|
* (c) 2014 - 2017 Grégoire HUBERT <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace PommProject\Foundation\Session; |
11
|
|
|
|
12
|
|
|
use PommProject\Foundation\Exception\FoundationException; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* ResultHandler |
16
|
|
|
* |
17
|
|
|
* Wrap a PostgreSQL query result resource. |
18
|
|
|
* |
19
|
|
|
* @package Foundation |
20
|
|
|
* @copyright 2014 - 2017 Grégoire HUBERT |
21
|
|
|
* @author Grégoire HUBERT |
22
|
|
|
* @license X11 {@link http://opensource.org/licenses/mit-license.php} |
23
|
|
|
*/ |
24
|
|
|
class ResultHandler |
25
|
|
|
{ |
26
|
|
|
protected $handler; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* __construct |
30
|
|
|
* |
31
|
|
|
* Constructor |
32
|
|
|
* |
33
|
|
|
* @param resource $result_resource |
34
|
|
|
*/ |
35
|
|
|
public function __construct($result_resource) |
36
|
|
|
{ |
37
|
|
|
if (!is_resource($result_resource) || get_resource_type($result_resource) !== 'pgsql result') { |
38
|
|
|
throw new \InvalidArgumentException(sprintf( |
39
|
|
|
"Given handler is not a resource of a pgsql result ('%s' given).", |
40
|
|
|
gettype($result_resource) |
41
|
|
|
)); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$this->handler = $result_resource; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* __destruct |
49
|
|
|
* |
50
|
|
|
* Call free() if handler is set. |
51
|
|
|
* |
52
|
|
|
* @return void |
53
|
|
|
*/ |
54
|
|
|
public function __destruct() |
55
|
|
|
{ |
56
|
|
|
if ($this->handler !== null) { |
57
|
|
|
$this->free(); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* free |
63
|
|
|
* |
64
|
|
|
* Free a result from memory. |
65
|
|
|
* |
66
|
|
|
* @return ResultHandler $this |
67
|
|
|
*/ |
68
|
|
|
public function free() |
69
|
|
|
{ |
70
|
|
|
@pg_free_result($this->handler); |
71
|
|
|
$this->handler = null; |
72
|
|
|
|
73
|
|
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* fetchRow |
78
|
|
|
* |
79
|
|
|
* Fetch a row as associative array. Index starts from 0. |
80
|
|
|
* |
81
|
|
|
* @param int $index |
82
|
|
|
* @throws \OutOfBoundsException if $index out of bounds. |
83
|
|
|
* @return array |
84
|
|
|
*/ |
85
|
|
|
public function fetchRow($index) |
86
|
|
|
{ |
87
|
|
|
$values = @pg_fetch_assoc($this->handler, $index); |
88
|
|
|
|
89
|
|
|
if ($values === false) { |
90
|
|
|
throw new \OutOfBoundsException(sprintf("Cannot jump to non existing row %d.", $index)); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $values; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* countFields |
98
|
|
|
* |
99
|
|
|
* Return the number of fields of a result. |
100
|
|
|
* |
101
|
|
|
* @return int long |
102
|
|
|
*/ |
103
|
|
|
public function countFields() |
104
|
|
|
{ |
105
|
|
|
return pg_num_fields($this->handler); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* countRows |
110
|
|
|
* |
111
|
|
|
* Return the number of rows in a result. |
112
|
|
|
* |
113
|
|
|
* @return int long |
114
|
|
|
*/ |
115
|
|
|
public function countRows() |
116
|
|
|
{ |
117
|
|
|
return pg_num_rows($this->handler); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* countAffectedRows |
122
|
|
|
* |
123
|
|
|
* Return the number of affected rows in a result. |
124
|
|
|
* |
125
|
|
|
* @access public |
126
|
|
|
* @return int long |
127
|
|
|
*/ |
128
|
|
|
public function countAffectedRows() |
129
|
|
|
{ |
130
|
|
|
return pg_affected_rows($this->handler); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* getFieldNames |
135
|
|
|
* |
136
|
|
|
* Return an array with the field names of a result. |
137
|
|
|
* |
138
|
|
|
* @return array |
139
|
|
|
*/ |
140
|
|
|
public function getFieldNames() |
141
|
|
|
{ |
142
|
|
|
$names = []; |
143
|
|
|
|
144
|
|
|
for ($i = 0; $i < $this->countFields(); $i++) { |
145
|
|
|
$names[] = $this->getFieldName($i); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return $names; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* getFieldType |
153
|
|
|
* |
154
|
|
|
* Return the associated type of a field. |
155
|
|
|
* |
156
|
|
|
* @param int $name |
157
|
|
|
* @return string |
158
|
|
|
*/ |
159
|
|
|
public function getFieldType($name) |
160
|
|
|
{ |
161
|
|
|
$type = pg_field_type($this->handler, $this->getFieldNumber($name)); |
162
|
|
|
|
163
|
|
|
return $type !== 'unknown' ? $type : null; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* getFieldName |
168
|
|
|
* |
169
|
|
|
* Return the name from a field number. |
170
|
|
|
* |
171
|
|
|
* @param int $field_no |
172
|
|
|
* @return string |
173
|
|
|
* @throws \InvalidArgumentException |
174
|
|
|
*/ |
175
|
|
|
public function getFieldName($field_no) |
176
|
|
|
{ |
177
|
|
|
if (!is_int($field_no)) { |
178
|
|
|
throw new \InvalidArgumentException(sprintf( |
179
|
|
|
"getFieldType::field_no = '%s' is not an integer.\n", |
180
|
|
|
$field_no |
181
|
|
|
)); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
return pg_field_name($this->handler, $field_no); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* getFieldNumber |
189
|
|
|
* |
190
|
|
|
* Return the field index from its name. |
191
|
|
|
* |
192
|
|
|
* @param string $name |
193
|
|
|
* @return int long |
194
|
|
|
*/ |
195
|
|
|
protected function getFieldNumber($name) |
196
|
|
|
{ |
197
|
|
|
$no = pg_field_num($this->handler, sprintf('"%s"', $name)); |
198
|
|
|
|
199
|
|
|
if ($no === -1) { |
200
|
|
|
throw new \InvalidArgumentException( |
201
|
|
|
sprintf( |
202
|
|
|
"Could not find field name '%s'. Available fields are {%s}.", |
203
|
|
|
$name, |
204
|
|
|
join(', ', array_keys(pg_fetch_assoc($this->handler))) |
205
|
|
|
) |
206
|
|
|
); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
return $no; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* fetchColumn |
214
|
|
|
* |
215
|
|
|
* Fetch a column from a result. |
216
|
|
|
* |
217
|
|
|
* @param string $name |
218
|
|
|
* @return array |
219
|
|
|
*/ |
220
|
|
|
public function fetchColumn($name) |
221
|
|
|
{ |
222
|
|
|
return pg_fetch_all_columns($this->handler, $this->getFieldNumber($name)); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* fieldExist |
227
|
|
|
* |
228
|
|
|
* Check if a field exist or not. |
229
|
|
|
* |
230
|
|
|
* @param mixed $name |
231
|
|
|
* @return bool |
232
|
|
|
*/ |
233
|
|
|
public function fieldExist($name) |
234
|
|
|
{ |
235
|
|
|
return (bool) (pg_field_num($this->handler, $name) > -1); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* getTypeOid |
240
|
|
|
* |
241
|
|
|
* Return the type oid of the given field. |
242
|
|
|
* |
243
|
|
|
* @param string $name |
244
|
|
|
* @throws FoundationException on error |
245
|
|
|
* @return int |
246
|
|
|
*/ |
247
|
|
|
public function getTypeOid($name) |
248
|
|
|
{ |
249
|
|
|
$ret = pg_field_type_oid($this->handler, $name); |
250
|
|
|
|
251
|
|
|
if ($ret === false) { |
252
|
|
|
throw new FoundationException( |
253
|
|
|
sprintf( |
254
|
|
|
"Error while fetching type oid for field '%s'.", |
255
|
|
|
$name |
256
|
|
|
) |
257
|
|
|
); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
return $ret; |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
|