Passed
Push — move-escapesql-drivers ( 8474c4 )
by Wilmer
44:23 queued 09:58
created

CommandPDO::bindPendingParams()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 3
eloc 12
c 1
b 1
f 0
nc 3
nop 0
dl 0
loc 18
ccs 11
cts 11
cp 1
crap 3
rs 9.8666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Oracle;
6
7
use PDO;
8
use PDOException;
9
use Yiisoft\Db\Driver\PDO\CommandPDO as AbstractCommandPDO;
10
use Yiisoft\Db\Exception\ConvertException;
11
use Yiisoft\Db\QueryBuilder\QueryBuilder;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Yiisoft\Db\Oracle\QueryBuilder. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
12
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
13
use Yiisoft\Db\Schema\Schema;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Yiisoft\Db\Oracle\Schema. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
14
use Yiisoft\Db\Schema\SchemaInterface;
15
16
use function array_keys;
17
use function count;
18
use function implode;
19
use function strlen;
20
21
/**
22
 * Command represents an Oracle SQL statement to be executed against a database.
23
 */
24
final class CommandPDO extends AbstractCommandPDO
25
{
26 188
    public function queryBuilder(): QueryBuilderInterface
27
    {
28 188
        return $this->db->getQueryBuilder();
29
    }
30
31
    public function schema(): SchemaInterface
32
    {
33
        return $this->db->getSchema();
34
    }
35
36 1
    public function insertEx(string $table, array $columns): bool|array
37
    {
38 1
        $params = [];
39 1
        $sql = $this->queryBuilder()->insertEx($table, $columns, $params);
40
41 1
        $tableSchema = $this->db->getSchema()->getTableSchema($table);
42
43 1
        $returnColumns = $tableSchema?->getPrimaryKey() ?? [];
44 1
        $columnSchemas = $tableSchema?->getColumns() ?? [];
45
46 1
        $returnParams = [];
47 1
        $returning = [];
48 1
        foreach ($returnColumns as $name) {
49
            /** @noRector \Rector\Php71\Rector\FuncCall\CountOnNullRector */
50 1
            $phName = QueryBuilder::PARAM_PREFIX . (count($params) + count($returnParams));
51
52 1
            $returnParams[$phName] = [
53
                'column' => $name,
54
                'value' => '',
55
            ];
56
57 1
            if (!isset($columnSchemas[$name]) || $columnSchemas[$name]->getPhpType() !== Schema::PHP_TYPE_INTEGER) {
58
                $returnParams[$phName]['dataType'] = PDO::PARAM_STR;
59
            } else {
60 1
                $returnParams[$phName]['dataType'] = PDO::PARAM_INT;
61
            }
62
63 1
            $returnParams[$phName]['size'] = $columnSchemas[$name]->getSize() ?? -1;
64
65 1
            $returning[] = $this->db->getQuoter()->quoteColumnName($name);
66
        }
67
68 1
        $sql .= ' RETURNING ' . implode(', ', $returning) . ' INTO ' . implode(', ', array_keys($returnParams));
69
70 1
        $this->setSql($sql)->bindValues($params);
71 1
        $this->prepare(false);
72
73
        /** @psalm-var array<string, array{column: string, value: mixed, dataType: int, size: int}> $returnParams */
74 1
        foreach ($returnParams as $name => &$value) {
75 1
            $this->bindParam($name, $value['value'], $value['dataType'], $value['size']);
76
        }
77
78 1
        if (!$this->execute()) {
79
            return false;
80
        }
81
82 1
        $result = [];
83
84 1
        foreach ($returnParams as $value) {
85
            /** @var mixed */
86 1
            $result[$value['column']] = $value['value'];
87
        }
88
89 1
        return $result;
90
    }
91
92 178
    protected function bindPendingParams(): void
93
    {
94 178
        $paramsPassedByReference = [];
95
96 178
        $params = $this->params;
97
98 178
        foreach ($params as $name => $value) {
99 153
            if (PDO::PARAM_STR === $value->getType()) {
100
                /** @var mixed */
101 147
                $paramsPassedByReference[$name] = $value->getValue();
102 147
                $this->pdoStatement?->bindParam(
103
                    $name,
104 147
                    $paramsPassedByReference[$name],
105 147
                    $value->getType(),
106 147
                    strlen((string) $value->getValue())
107
                );
108
            } else {
109 28
                $this->pdoStatement?->bindValue($name, $value->getValue(), $value->getType());
110
            }
111
        }
112
    }
113
114 177
    protected function internalExecute(?string $rawSql): void
115
    {
116 177
        $attempt = 0;
117
118 177
        while (true) {
119
            try {
120
                if (
121
                    ++$attempt === 1
122 177
                    && $this->isolationLevel !== null
123 177
                    && $this->db->getTransaction() === null
124
                ) {
125
                    $this->db->transaction(fn (string $rawSql) => $this->internalExecute($rawSql), $this->isolationLevel);
126
                } else {
127 177
                    $this->pdoStatement?->execute();
128
                }
129 177
                break;
130 6
            } catch (PDOException $e) {
131 6
                $rawSql = $rawSql ?: $this->getRawSql();
132 6
                $e = (new ConvertException($e, $rawSql))->run();
133
134 6
                if ($this->retryHandler === null || !($this->retryHandler)($e, $attempt)) {
135 6
                    throw $e;
136
                }
137
            }
138
        }
139
    }
140
}
141