Passed
Push — master ( d70a5d...abf1ec )
by Wilmer
10:23 queued 03:57
created

Command::internalExecute()   B

Complexity

Conditions 9
Paths 9

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 9

Importance

Changes 0
Metric Value
cc 9
eloc 18
nc 9
nop 1
dl 0
loc 25
ccs 17
cts 17
cp 1
crap 9
rs 8.0555
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Oracle;
6
7
use PDO;
8
use PDOException;
9
use Throwable;
10
use Yiisoft\Db\Driver\PDO\AbstractCommandPDO;
11
use Yiisoft\Db\Driver\PDO\ConnectionPDOInterface;
12
use Yiisoft\Db\Exception\ConvertException;
13
use Yiisoft\Db\QueryBuilder\AbstractQueryBuilder;
14
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
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
 * Implements a database command that can be executed against a PDO (PHP Data Object) database connection for Oracle
24
 * Server.
25
 */
26
final class Command extends AbstractCommandPDO
27
{
28 5
    public function insertWithReturningPks(string $table, array $columns): bool|array
29
    {
30 5
        $params = [];
31 5
        $sql = $this->getQueryBuilder()->insert($table, $columns, $params);
32
33 5
        $tableSchema = $this->db->getSchema()->getTableSchema($table);
34
35 5
        $returnColumns = $tableSchema?->getPrimaryKey() ?? [];
36 5
        $columnSchemas = $tableSchema?->getColumns() ?? [];
37
38 5
        $returnParams = [];
39 5
        $returning = [];
40
41 5
        foreach ($returnColumns as $name) {
42
            /** @noRector \Rector\Php71\Rector\FuncCall\CountOnNullRector */
43 5
            $phName = AbstractQueryBuilder::PARAM_PREFIX . (count($params) + count($returnParams));
44
45 5
            $returnParams[$phName] = [
46 5
                'column' => $name,
47 5
                'value' => '',
48 5
            ];
49
50 5
            if (!isset($columnSchemas[$name]) || $columnSchemas[$name]->getPhpType() !== SchemaInterface::PHP_TYPE_INTEGER) {
51 1
                $returnParams[$phName]['dataType'] = PDO::PARAM_STR;
52
            } else {
53 4
                $returnParams[$phName]['dataType'] = PDO::PARAM_INT;
54
            }
55
56 5
            $returnParams[$phName]['size'] = $columnSchemas[$name]->getSize() ?? -1;
57
58 5
            $returning[] = $this->db->getQuoter()->quoteColumnName($name);
59
        }
60
61 5
        $sql .= ' RETURNING ' . implode(', ', $returning) . ' INTO ' . implode(', ', array_keys($returnParams));
62
63 5
        $this->setSql($sql)->bindValues($params);
64 5
        $this->prepare(false);
65
66
        /** @psalm-var array<string, array{column: string, value: mixed, dataType: int, size: int}> $returnParams */
67 5
        foreach ($returnParams as $name => &$value) {
68 5
            $this->bindParam($name, $value['value'], $value['dataType'], $value['size']);
69
        }
70
71 5
        unset($value);
72
73 5
        if (!$this->execute()) {
74
            return false;
75
        }
76
77 5
        $result = [];
78
79 5
        foreach ($returnParams as $value) {
80
            /** @psalm-var mixed */
81 5
            $result[$value['column']] = $value['value'];
82
        }
83
84 5
        return $result;
85
    }
86
87 1
    public function showDatabases(): array
88
    {
89 1
        $sql = <<<SQL
90
        SELECT PDB_NAME FROM DBA_PDBS WHERE PDB_NAME NOT IN ('PDB\$SEED', 'PDB\$ROOT', 'ORCLPDB1', 'XEPDB1')
91 1
        SQL;
92
93 1
        return $this->setSql($sql)->queryColumn();
94
    }
95
96 264
    protected function getQueryBuilder(): QueryBuilderInterface
97
    {
98 264
        return $this->db->getQueryBuilder();
99
    }
100
101 241
    protected function bindPendingParams(): void
102
    {
103 241
        $paramsPassedByReference = [];
104
105 241
        $params = $this->params;
106
107 241
        foreach ($params as $name => $value) {
108 203
            if (PDO::PARAM_STR === $value->getType()) {
109
                /** @var mixed */
110 200
                $paramsPassedByReference[$name] = $value->getValue();
111 200
                $this->pdoStatement?->bindParam(
112 200
                    $name,
113 200
                    $paramsPassedByReference[$name],
114 200
                    $value->getType(),
115 200
                    strlen((string) $value->getValue())
116 200
                );
117
            } else {
118 33
                $this->pdoStatement?->bindValue($name, $value->getValue(), $value->getType());
119
            }
120
        }
121
    }
122
123
    /**
124
     * @psalm-suppress UnusedClosureParam
125
     *
126
     * @throws Throwable
127
     */
128 240
    protected function internalExecute(?string $rawSql): void
129
    {
130 240
        $attempt = 0;
131
132 240
        while (true) {
133
            try {
134
                if (
135 240
                    ++$attempt === 1
136 240
                    && $this->isolationLevel !== null
137 240
                    && $this->db->getTransaction() === null
138
                ) {
139 1
                    $this->db->transaction(
140 1
                        fn (ConnectionPDOInterface $db) => $this->internalExecute($rawSql),
0 ignored issues
show
Unused Code introduced by
The parameter $db is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

140
                        fn (/** @scrutinizer ignore-unused */ ConnectionPDOInterface $db) => $this->internalExecute($rawSql),

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
141 1
                        $this->isolationLevel
142 1
                    );
143
                } else {
144 240
                    $this->pdoStatement?->execute();
145
                }
146 240
                break;
147 7
            } catch (PDOException $e) {
148 7
                $rawSql = $rawSql ?: $this->getRawSql();
149 7
                $e = (new ConvertException($e, $rawSql))->run();
150
151 7
                if ($this->retryHandler === null || !($this->retryHandler)($e, $attempt)) {
152 7
                    throw $e;
153
                }
154
            }
155
        }
156
    }
157
}
158