Failed Conditions
Pull Request — 3.0.x (#3980)
by Guilherme
07:55
created

DB2Statement::errorCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Doctrine\DBAL\Driver\IBMDB2;
4
5
use Doctrine\DBAL\Driver\Statement;
6
use Doctrine\DBAL\Driver\StatementIterator;
7
use Doctrine\DBAL\FetchMode;
8
use Doctrine\DBAL\ParameterType;
9
use IteratorAggregate;
10
use function assert;
11
use function db2_bind_param;
12
use function db2_execute;
13
use function db2_fetch_array;
14
use function db2_fetch_assoc;
15
use function db2_fetch_both;
16
use function db2_free_result;
17
use function db2_num_fields;
18
use function db2_num_rows;
19
use function db2_stmt_error;
20
use function db2_stmt_errormsg;
21
use function error_get_last;
22
use function fclose;
23
use function fwrite;
24
use function is_int;
25
use function is_resource;
26
use function ksort;
27
use function stream_copy_to_stream;
28
use function stream_get_meta_data;
29
use function tmpfile;
30
use const DB2_BINARY;
31
use const DB2_CHAR;
32
use const DB2_LONG;
33
use const DB2_PARAM_FILE;
34
use const DB2_PARAM_IN;
35
36
class DB2Statement implements IteratorAggregate, Statement
37
{
38
    /** @var resource */
39
    private $stmt;
40
41
    /** @var mixed[] */
42
    private $bindParam = [];
43
44
    /**
45
     * Map of LOB parameter positions to the tuples containing reference to the variable bound to the driver statement
46
     * and the temporary file handle bound to the underlying statement
47
     *
48
     * @var mixed[][]
49
     */
50
    private $lobs = [];
51
52
    /** @var int */
53
    private $defaultFetchMode = FetchMode::MIXED;
54
55
    /**
56
     * Indicates whether the statement is in the state when fetching results is possible
57
     *
58
     * @var bool
59
     */
60
    private $result = false;
61
62
    /**
63
     * @param resource $stmt
64
     */
65
    public function __construct($stmt)
66
    {
67
        $this->stmt = $stmt;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function bindValue($param, $value, $type = ParameterType::STRING)
74
    {
75
        return $this->bindParam($param, $value, $type);
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null)
82
    {
83
        assert(is_int($column));
84
85
        switch ($type) {
86
            case ParameterType::INTEGER:
87
                $this->bind($column, $variable, DB2_PARAM_IN, DB2_LONG);
88
                break;
89
90
            case ParameterType::LARGE_OBJECT:
91
                if (isset($this->lobs[$column])) {
92
                    [, $handle] = $this->lobs[$column];
93
                    fclose($handle);
94
                }
95
96
                $handle = $this->createTemporaryFile();
97
                $path   = stream_get_meta_data($handle)['uri'];
98
99
                $this->bind($column, $path, DB2_PARAM_FILE, DB2_BINARY);
100
101
                $this->lobs[$column] = [&$variable, $handle];
102
                break;
103
104
            default:
105
                $this->bind($column, $variable, DB2_PARAM_IN, DB2_CHAR);
106
                break;
107
        }
108
109
        return true;
110
    }
111
112
    /**
113
     * @param int   $position Parameter position
114
     * @param mixed $variable
115
     *
116
     * @throws DB2Exception
117
     */
118
    private function bind($position, &$variable, int $parameterType, int $dataType) : void
119
    {
120
        $this->bindParam[$position] =& $variable;
121
122
        if (! db2_bind_param($this->stmt, $position, 'variable', $parameterType, $dataType)) {
123
            throw new DB2Exception(db2_stmt_errormsg());
124
        }
125
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130
    public function closeCursor()
131
    {
132
        $this->bindParam = [];
133
134
        if (! db2_free_result($this->stmt)) {
135
            return false;
136
        }
137
138
        $this->result = false;
139
140
        return true;
141
    }
142
143
    /**
144
     * {@inheritdoc}
145
     */
146
    public function columnCount()
147
    {
148
        $count = db2_num_fields($this->stmt);
149
150
        if ($count !== false) {
151
            return $count;
152
        }
153
154
        return 0;
155
    }
156
157
    /**
158
     * {@inheritdoc}
159
     */
160
    public function errorCode()
161
    {
162
        return db2_stmt_error();
163
    }
164
165
    /**
166
     * {@inheritdoc}
167
     */
168
    public function errorInfo()
169
    {
170
        return [
171
            db2_stmt_errormsg(),
172
            db2_stmt_error(),
173
        ];
174
    }
175
176
    /**
177
     * {@inheritdoc}
178
     */
179
    public function execute($params = null)
180
    {
181
        if ($params === null) {
182
            ksort($this->bindParam);
183
184
            $params = [];
185
186
            foreach ($this->bindParam as $column => $value) {
187
                $params[] = $value;
188
            }
189
        }
190
191
        foreach ($this->lobs as [$source, $target]) {
192
            if (is_resource($source)) {
193
                $this->copyStreamToStream($source, $target);
194
195
                continue;
196
            }
197
198
            $this->writeStringToStream($source, $target);
199
        }
200
201
        $retval = db2_execute($this->stmt, $params);
202
203
        foreach ($this->lobs as [, $handle]) {
204
            fclose($handle);
205
        }
206
207
        $this->lobs = [];
208
209
        if ($retval === false) {
210
            throw new DB2Exception(db2_stmt_errormsg());
211
        }
212
213
        $this->result = true;
214
215
        return $retval;
216
    }
217
218
    /**
219
     * {@inheritdoc}
220
     */
221
    public function setFetchMode($fetchMode)
222
    {
223
        $this->defaultFetchMode = $fetchMode;
224
225
        return true;
226
    }
227
228
    /**
229
     * {@inheritdoc}
230
     */
231
    public function getIterator()
232
    {
233
        return new StatementIterator($this);
234
    }
235
236
    /**
237
     * {@inheritdoc}
238
     */
239
    public function fetch($fetchMode = null)
240
    {
241
        // do not try fetching from the statement if it's not expected to contain result
242
        // in order to prevent exceptional situation
243
        if (! $this->result) {
244
            return false;
245
        }
246
247
        $fetchMode = $fetchMode ?? $this->defaultFetchMode;
248
        switch ($fetchMode) {
249
            case FetchMode::COLUMN:
250
                return $this->fetchColumn();
251
252
            case FetchMode::MIXED:
253
                return db2_fetch_both($this->stmt);
254
255
            case FetchMode::ASSOCIATIVE:
256
                return db2_fetch_assoc($this->stmt);
257
258
            case FetchMode::NUMERIC:
259
                return db2_fetch_array($this->stmt);
260
261
            default:
262
                throw new DB2Exception('Given Fetch-Style ' . $fetchMode . ' is not supported.');
263
        }
264
    }
265
266
    /**
267
     * {@inheritdoc}
268
     */
269
    public function fetchAll($fetchMode = null)
270
    {
271
        $rows = [];
272
273
        switch ($fetchMode) {
274
            case FetchMode::COLUMN:
275
                while (($row = $this->fetchColumn()) !== false) {
276
                    $rows[] = $row;
277
                }
278
279
                break;
280
281
            default:
282
                while (($row = $this->fetch($fetchMode)) !== false) {
283
                    $rows[] = $row;
284
                }
285
        }
286
287
        return $rows;
288
    }
289
290
    /**
291
     * {@inheritdoc}
292
     */
293
    public function fetchColumn()
294
    {
295
        $row = $this->fetch(FetchMode::NUMERIC);
296
297
        if ($row === false) {
298
            return false;
299
        }
300
301
        return $row[0] ?? null;
302
    }
303
304
    public function rowCount() : int
305
    {
306
        return @db2_num_rows($this->stmt);
307
    }
308
309
    /**
310
     * @return resource
311
     *
312
     * @throws DB2Exception
313
     */
314
    private function createTemporaryFile()
315
    {
316
        $handle = @tmpfile();
317
318
        if ($handle === false) {
319
            throw new DB2Exception('Could not create temporary file: ' . error_get_last()['message']);
320
        }
321
322
        return $handle;
323
    }
324
325
    /**
326
     * @param resource $source
327
     * @param resource $target
328
     *
329
     * @throws DB2Exception
330
     */
331
    private function copyStreamToStream($source, $target) : void
332
    {
333
        if (@stream_copy_to_stream($source, $target) === false) {
334
            throw new DB2Exception('Could not copy source stream to temporary file: ' . error_get_last()['message']);
335
        }
336
    }
337
338
    /**
339
     * @param resource $target
340
     *
341
     * @throws DB2Exception
342
     */
343
    private function writeStringToStream(string $string, $target) : void
344
    {
345
        if (@fwrite($target, $string) === false) {
346
            throw new DB2Exception('Could not write string to temporary file: ' . error_get_last()['message']);
347
        }
348
    }
349
}
350