Completed
Push — 2.10 ( 39b76b...400f60 )
by Sergei
30s queued 15s
created

DB2Statement::bindValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1.037

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 3
cp 0.6667
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 3
crap 1.037
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 PDO;
11
use ReflectionClass;
12
use ReflectionObject;
13
use ReflectionProperty;
14
use stdClass;
15
use const CASE_LOWER;
16
use const DB2_BINARY;
17
use const DB2_CHAR;
18
use const DB2_LONG;
19
use const DB2_PARAM_FILE;
20
use const DB2_PARAM_IN;
21
use function array_change_key_case;
22
use function db2_bind_param;
23
use function db2_execute;
24
use function db2_fetch_array;
25
use function db2_fetch_assoc;
26
use function db2_fetch_both;
27
use function db2_fetch_object;
28
use function db2_free_result;
29
use function db2_num_fields;
30
use function db2_num_rows;
31
use function db2_stmt_error;
32
use function db2_stmt_errormsg;
33
use function error_get_last;
34
use function fclose;
35
use function func_get_args;
36
use function func_num_args;
37
use function fwrite;
38
use function gettype;
39
use function is_object;
40
use function is_resource;
41
use function is_string;
42
use function ksort;
43
use function sprintf;
44
use function stream_copy_to_stream;
45
use function stream_get_meta_data;
46
use function strtolower;
47
use function tmpfile;
48
49
class DB2Statement implements IteratorAggregate, Statement
50
{
51
    /** @var resource */
52
    private $stmt;
53
54
    /** @var mixed[] */
55
    private $bindParam = [];
56
57
    /**
58
     * Map of LOB parameter positions to the tuples containing reference to the variable bound to the driver statement
59
     * and the temporary file handle bound to the underlying statement
60
     *
61
     * @var mixed[][]
62
     */
63
    private $lobs = [];
64
65
    /** @var string Name of the default class to instantiate when fetching class instances. */
66
    private $defaultFetchClass = '\stdClass';
67
68
    /** @var mixed[] Constructor arguments for the default class to instantiate when fetching class instances. */
69
    private $defaultFetchClassCtorArgs = [];
70
71
    /** @var int */
72
    private $defaultFetchMode = FetchMode::MIXED;
73
74
    /**
75
     * Indicates whether the statement is in the state when fetching results is possible
76
     *
77
     * @var bool
78
     */
79
    private $result = false;
80
81
    /**
82
     * @param resource $stmt
83
     */
84 220
    public function __construct($stmt)
85
    {
86 220
        $this->stmt = $stmt;
87 220
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92 218
    public function bindValue($param, $value, $type = ParameterType::STRING)
93
    {
94 218
        return $this->bindParam($param, $value, $type);
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100 218
    public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null)
101
    {
102 218
        switch ($type) {
103
            case ParameterType::INTEGER:
104 218
                $this->bind($column, $variable, DB2_PARAM_IN, DB2_LONG);
105 218
                break;
106
107
            case ParameterType::LARGE_OBJECT:
108 218
                if (isset($this->lobs[$column])) {
109
                    [, $handle] = $this->lobs[$column];
110
                    fclose($handle);
111
                }
112
113 218
                $handle = $this->createTemporaryFile();
114 218
                $path   = stream_get_meta_data($handle)['uri'];
115
116 218
                $this->bind($column, $path, DB2_PARAM_FILE, DB2_BINARY);
117
118 218
                $this->lobs[$column] = [&$variable, $handle];
119 218
                break;
120
121
            default:
122 218
                $this->bind($column, $variable, DB2_PARAM_IN, DB2_CHAR);
123 218
                break;
124
        }
125
126 218
        return true;
127
    }
128
129
    /**
130
     * @param int   $position Parameter position
131
     * @param mixed $variable
132
     *
133
     * @throws DB2Exception
134
     */
135 218
    private function bind($position, &$variable, int $parameterType, int $dataType) : void
136
    {
137 218
        $this->bindParam[$position] =& $variable;
138
139 218
        if (! db2_bind_param($this->stmt, $position, 'variable', $parameterType, $dataType)) {
140
            throw new DB2Exception(db2_stmt_errormsg());
141
        }
142 218
    }
143
144
    /**
145
     * {@inheritdoc}
146
     */
147 122
    public function closeCursor()
148
    {
149 122
        $this->bindParam = [];
150
151 122
        if (! db2_free_result($this->stmt)) {
152
            return false;
153
        }
154
155 122
        $this->result = false;
156
157 122
        return true;
158
    }
159
160
    /**
161
     * {@inheritdoc}
162
     */
163 122
    public function columnCount()
164
    {
165 122
        return db2_num_fields($this->stmt) ?: 0;
166
    }
167
168
    /**
169
     * {@inheritdoc}
170
     */
171
    public function errorCode()
172
    {
173
        return db2_stmt_error();
174
    }
175
176
    /**
177
     * {@inheritdoc}
178
     */
179
    public function errorInfo()
180
    {
181
        return [
182
            db2_stmt_errormsg(),
183
            db2_stmt_error(),
184
        ];
185
    }
186
187
    /**
188
     * {@inheritdoc}
189
     */
190 220
    public function execute($params = null)
191
    {
192 220
        if ($params === null) {
193 220
            ksort($this->bindParam);
194
195 220
            $params = [];
196
197 220
            foreach ($this->bindParam as $column => $value) {
198 218
                $params[] = $value;
199
            }
200
        }
201
202 220
        foreach ($this->lobs as [$source, $target]) {
203 218
            if (is_resource($source)) {
204 217
                $this->copyStreamToStream($source, $target);
205
206 217
                continue;
207
            }
208
209 218
            $this->writeStringToStream($source, $target);
210
        }
211
212 220
        $retval = db2_execute($this->stmt, $params);
213
214 220
        foreach ($this->lobs as [, $handle]) {
215 218
            fclose($handle);
216
        }
217
218 220
        $this->lobs = [];
219
220 220
        if ($retval === false) {
221
            throw new DB2Exception(db2_stmt_errormsg());
222
        }
223
224 220
        $this->result = true;
225
226 220
        return $retval;
227
    }
228
229
    /**
230
     * {@inheritdoc}
231
     */
232 220
    public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
233
    {
234 220
        $this->defaultFetchMode          = $fetchMode;
235 220
        $this->defaultFetchClass         = $arg2 ?: $this->defaultFetchClass;
236 220
        $this->defaultFetchClassCtorArgs = $arg3 ? (array) $arg3 : $this->defaultFetchClassCtorArgs;
237
238 220
        return true;
239
    }
240
241
    /**
242
     * {@inheritdoc}
243
     */
244 220
    public function getIterator()
245
    {
246 220
        return new StatementIterator($this);
247
    }
248
249
    /**
250
     * {@inheritdoc}
251
     */
252 220
    public function fetch($fetchMode = null, $cursorOrientation = PDO::FETCH_ORI_NEXT, $cursorOffset = 0)
253
    {
254
        // do not try fetching from the statement if it's not expected to contain result
255
        // in order to prevent exceptional situation
256 220
        if (! $this->result) {
257 47
            return false;
258
        }
259
260 220
        $fetchMode = $fetchMode ?: $this->defaultFetchMode;
261 220
        switch ($fetchMode) {
262
            case FetchMode::COLUMN:
263 36
                return $this->fetchColumn();
264
265
            case FetchMode::MIXED:
266 205
                return db2_fetch_both($this->stmt);
267
268
            case FetchMode::ASSOCIATIVE:
269 220
                return db2_fetch_assoc($this->stmt);
270
271
            case FetchMode::CUSTOM_OBJECT:
272 160
                $className = $this->defaultFetchClass;
273 160
                $ctorArgs  = $this->defaultFetchClassCtorArgs;
274
275 160
                if (func_num_args() >= 2) {
276 160
                    $args      = func_get_args();
277 160
                    $className = $args[1];
278 160
                    $ctorArgs  = $args[2] ?? [];
279
                }
280
281 160
                $result = db2_fetch_object($this->stmt);
282
283 160
                if ($result instanceof stdClass) {
284 160
                    $result = $this->castObject($result, $className, $ctorArgs);
285
                }
286
287 160
                return $result;
288
289
            case FetchMode::NUMERIC:
290 217
                return db2_fetch_array($this->stmt);
291
292
            case FetchMode::STANDARD_OBJECT:
293 161
                return db2_fetch_object($this->stmt);
294
295
            default:
296
                throw new DB2Exception('Given Fetch-Style ' . $fetchMode . ' is not supported.');
297
        }
298
    }
299
300
    /**
301
     * {@inheritdoc}
302
     */
303 220
    public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
304
    {
305 220
        $rows = [];
306
307 220
        switch ($fetchMode) {
308
            case FetchMode::CUSTOM_OBJECT:
309 160
                while (($row = $this->fetch(...func_get_args())) !== false) {
310 160
                    $rows[] = $row;
311
                }
312 160
                break;
313
            case FetchMode::COLUMN:
314 217
                while (($row = $this->fetchColumn()) !== false) {
315 217
                    $rows[] = $row;
316
                }
317 217
                break;
318
            default:
319 220
                while (($row = $this->fetch($fetchMode)) !== false) {
320 206
                    $rows[] = $row;
321
                }
322
        }
323
324 220
        return $rows;
325
    }
326
327
    /**
328
     * {@inheritdoc}
329
     */
330 217
    public function fetchColumn($columnIndex = 0)
331
    {
332 217
        $row = $this->fetch(FetchMode::NUMERIC);
333
334 217
        if ($row === false) {
335 217
            return false;
336
        }
337
338 217
        return $row[$columnIndex] ?? null;
339
    }
340
341
    /**
342
     * {@inheritdoc}
343
     */
344 218
    public function rowCount()
345
    {
346 218
        return @db2_num_rows($this->stmt) ? : 0;
347
    }
348
349
    /**
350
     * Casts a stdClass object to the given class name mapping its' properties.
351
     *
352
     * @param stdClass      $sourceObject     Object to cast from.
353
     * @param string|object $destinationClass Name of the class or class instance to cast to.
354
     * @param mixed[]       $ctorArgs         Arguments to use for constructing the destination class instance.
355
     *
356
     * @return object
357
     *
358
     * @throws DB2Exception
359
     */
360 160
    private function castObject(stdClass $sourceObject, $destinationClass, array $ctorArgs = [])
361
    {
362 160
        if (! is_string($destinationClass)) {
363
            if (! is_object($destinationClass)) {
364
                throw new DB2Exception(sprintf(
365
                    'Destination class has to be of type string or object, %s given.',
366
                    gettype($destinationClass)
367
                ));
368
            }
369
        } else {
370 160
            $destinationClass = new ReflectionClass($destinationClass);
371 160
            $destinationClass = $destinationClass->newInstanceArgs($ctorArgs);
372
        }
373
374 160
        $sourceReflection           = new ReflectionObject($sourceObject);
375 160
        $destinationClassReflection = new ReflectionObject($destinationClass);
376
        /** @var ReflectionProperty[] $destinationProperties */
377 160
        $destinationProperties = array_change_key_case($destinationClassReflection->getProperties(), CASE_LOWER);
378
379 160
        foreach ($sourceReflection->getProperties() as $sourceProperty) {
380 160
            $sourceProperty->setAccessible(true);
381
382 160
            $name  = $sourceProperty->getName();
383 160
            $value = $sourceProperty->getValue($sourceObject);
384
385
            // Try to find a case-matching property.
386 160
            if ($destinationClassReflection->hasProperty($name)) {
387
                $destinationProperty = $destinationClassReflection->getProperty($name);
388
389
                $destinationProperty->setAccessible(true);
390
                $destinationProperty->setValue($destinationClass, $value);
391
392
                continue;
393
            }
394
395 160
            $name = strtolower($name);
396
397
            // Try to find a property without matching case.
398
            // Fallback for the driver returning either all uppercase or all lowercase column names.
399 160
            if (isset($destinationProperties[$name])) {
400
                $destinationProperty = $destinationProperties[$name];
401
402
                $destinationProperty->setAccessible(true);
403
                $destinationProperty->setValue($destinationClass, $value);
404
405
                continue;
406
            }
407
408 160
            $destinationClass->$name = $value;
409
        }
410
411 160
        return $destinationClass;
412
    }
413
414
    /**
415
     * @return resource
416
     *
417
     * @throws DB2Exception
418
     */
419 218
    private function createTemporaryFile()
420
    {
421 218
        $handle = @tmpfile();
422
423 218
        if ($handle === false) {
424
            throw new DB2Exception('Could not create temporary file: ' . error_get_last()['message']);
425
        }
426
427 218
        return $handle;
428
    }
429
430
    /**
431
     * @param resource $source
432
     * @param resource $target
433
     *
434
     * @throws DB2Exception
435
     */
436 217
    private function copyStreamToStream($source, $target) : void
437
    {
438 217
        if (@stream_copy_to_stream($source, $target) === false) {
439
            throw new DB2Exception('Could not copy source stream to temporary file: ' . error_get_last()['message']);
440
        }
441 217
    }
442
443
    /**
444
     * @param resource $target
445
     *
446
     * @throws DB2Exception
447
     */
448 218
    private function writeStringToStream(string $string, $target) : void
449
    {
450 218
        if (@fwrite($target, $string) === false) {
451
            throw new DB2Exception('Could not write string to temporary file: ' . error_get_last()['message']);
452
        }
453 218
    }
454
}
455