Completed
Push — 2.0 ( 143803...4e64fa )
by grégoire
08:42 queued 04:22
created

ResultHandler::countRows()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

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