Test Failed
Pull Request — dev (#78)
by Def
22:38 queued 02:57
created

CommandPDO   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 59
dl 0
loc 112
rs 10
c 0
b 0
f 0
wmc 21

5 Methods

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