Test Failed
Push — add-command-show-databases ( aec5fe )
by Wilmer
12:48 queued 04:13
created

CommandPDO::showDatabases()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 7
rs 10
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 CommandPDO extends AbstractCommandPDO
27
{
28
    public function insertWithReturningPks(string $table, array $columns): bool|array
29
    {
30
        $params = [];
31
        $sql = $this->getQueryBuilder()->insert($table, $columns, $params);
32
33
        $tableSchema = $this->db->getSchema()->getTableSchema($table);
34
35
        $returnColumns = $tableSchema?->getPrimaryKey() ?? [];
36
        $columnSchemas = $tableSchema?->getColumns() ?? [];
37
38
        $returnParams = [];
39
        $returning = [];
40
41
        foreach ($returnColumns as $name) {
42
            /** @noRector \Rector\Php71\Rector\FuncCall\CountOnNullRector */
43
            $phName = AbstractQueryBuilder::PARAM_PREFIX . (count($params) + count($returnParams));
44
45
            $returnParams[$phName] = [
46
                'column' => $name,
47
                'value' => '',
48
            ];
49
50
            if (!isset($columnSchemas[$name]) || $columnSchemas[$name]->getPhpType() !== SchemaInterface::PHP_TYPE_INTEGER) {
51
                $returnParams[$phName]['dataType'] = PDO::PARAM_STR;
52
            } else {
53
                $returnParams[$phName]['dataType'] = PDO::PARAM_INT;
54
            }
55
56
            $returnParams[$phName]['size'] = $columnSchemas[$name]->getSize() ?? -1;
57
58
            $returning[] = $this->db->getQuoter()->quoteColumnName($name);
59
        }
60
61
        $sql .= ' RETURNING ' . implode(', ', $returning) . ' INTO ' . implode(', ', array_keys($returnParams));
62
63
        $this->setSql($sql)->bindValues($params);
64
        $this->prepare(false);
65
66
        /** @psalm-var array<string, array{column: string, value: mixed, dataType: int, size: int}> $returnParams */
67
        foreach ($returnParams as $name => &$value) {
68
            $this->bindParam($name, $value['value'], $value['dataType'], $value['size']);
69
        }
70
71
        unset($value);
72
73
        if (!$this->execute()) {
74
            return false;
75
        }
76
77
        $result = [];
78
79
        foreach ($returnParams as $value) {
80
            /** @psalm-var mixed */
81
            $result[$value['column']] = $value['value'];
82
        }
83
84
        return $result;
85
    }
86
87
    public function showDatabases(): array
88
    {
89
        $sql = <<<SQL
90
        SELECT PDB_NAME FROM dba_pdbs WHERE PDB_NAME NOT IN ('PDB\$SEED', 'PDB\$ROOT', 'ORCLPDB1', 'XEPDB1')
91
        SQL;
92
93
        return $this->setSql($sql)->queryColumn();
94
    }
95
96
    protected function getQueryBuilder(): QueryBuilderInterface
97
    {
98
        return $this->db->getQueryBuilder();
99
    }
100
101
    protected function bindPendingParams(): void
102
    {
103
        $paramsPassedByReference = [];
104
105
        $params = $this->params;
106
107
        foreach ($params as $name => $value) {
108
            if (PDO::PARAM_STR === $value->getType()) {
109
                /** @var mixed */
110
                $paramsPassedByReference[$name] = $value->getValue();
111
                $this->pdoStatement?->bindParam(
112
                    $name,
113
                    $paramsPassedByReference[$name],
114
                    $value->getType(),
115
                    strlen((string) $value->getValue())
116
                );
117
            } else {
118
                $this->pdoStatement?->bindValue($name, $value->getValue(), $value->getType());
119
            }
120
        }
121
    }
122
123
    /**
124
     * @psalm-suppress UnusedClosureParam
125
     *
126
     * @throws Throwable
127
     */
128
    protected function internalExecute(?string $rawSql): void
129
    {
130
        $attempt = 0;
131
132
        while (true) {
133
            try {
134
                if (
135
                    ++$attempt === 1
136
                    && $this->isolationLevel !== null
137
                    && $this->db->getTransaction() === null
138
                ) {
139
                    $this->db->transaction(
140
                        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
                        $this->isolationLevel
142
                    );
143
                } else {
144
                    $this->pdoStatement?->execute();
145
                }
146
                break;
147
            } catch (PDOException $e) {
148
                $rawSql = $rawSql ?: $this->getRawSql();
149
                $e = (new ConvertException($e, $rawSql))->run();
150
151
                if ($this->retryHandler === null || !($this->retryHandler)($e, $attempt)) {
152
                    throw $e;
153
                }
154
            }
155
        }
156
    }
157
}
158