Command::showDatabases()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 1
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 Yiisoft\Db\Driver\Pdo\AbstractPdoCommand;
9
use Yiisoft\Db\QueryBuilder\AbstractQueryBuilder;
10
use Yiisoft\Db\Schema\SchemaInterface;
11
12
use function array_keys;
13
use function count;
14
use function implode;
15
use function strlen;
16
17
/**
18
 * Implements a database command that can be executed against a PDO (PHP Data Object) database connection for Oracle
19
 * Server.
20
 */
21
final class Command extends AbstractPdoCommand
22
{
23 7
    public function insertWithReturningPks(string $table, array $columns): bool|array
24
    {
25 7
        $tableSchema = $this->db->getSchema()->getTableSchema($table);
26 7
        $returnColumns = $tableSchema?->getPrimaryKey() ?? [];
27
28 7
        if ($returnColumns === []) {
29 1
            if ($this->insert($table, $columns)->execute() === 0) {
30
                return false;
31
            }
32
33 1
            return [];
34
        }
35
36 6
        $params = [];
37 6
        $sql = $this->getQueryBuilder()->insert($table, $columns, $params);
38
39 6
        $columnSchemas = $tableSchema?->getColumns() ?? [];
40 6
        $returnParams = [];
41 6
        $returning = [];
42
43 6
        foreach ($returnColumns as $name) {
44 6
            $phName = AbstractQueryBuilder::PARAM_PREFIX . (count($params) + count($returnParams));
45
46 6
            $returnParams[$phName] = [
47 6
                'column' => $name,
48 6
                'value' => '',
49 6
            ];
50
51 6
            if (!isset($columnSchemas[$name]) || $columnSchemas[$name]->getPhpType() !== SchemaInterface::PHP_TYPE_INTEGER) {
52 1
                $returnParams[$phName]['dataType'] = PDO::PARAM_STR;
53
            } else {
54 5
                $returnParams[$phName]['dataType'] = PDO::PARAM_INT;
55
            }
56
57 6
            $returnParams[$phName]['size'] = $columnSchemas[$name]->getSize() ?? -1;
58
59 6
            $returning[] = $this->db->getQuoter()->quoteColumnName($name);
60
        }
61
62 6
        $sql .= ' RETURNING ' . implode(', ', $returning) . ' INTO ' . implode(', ', array_keys($returnParams));
63
64 6
        $this->setSql($sql)->bindValues($params);
65 6
        $this->prepare(false);
66
67
        /** @psalm-var array<string, array{column: string, value: mixed, dataType: int, size: int}> $returnParams */
68 6
        foreach ($returnParams as $name => &$value) {
69 6
            $this->bindParam($name, $value['value'], $value['dataType'], $value['size']);
70
        }
71
72 6
        unset($value);
73
74 6
        if (!$this->execute()) {
75
            return false;
76
        }
77
78 6
        $result = [];
79
80 6
        foreach ($returnParams as $value) {
81
            /** @psalm-var mixed */
82 6
            $result[$value['column']] = $value['value'];
83
        }
84
85 6
        return $result;
86
    }
87
88 1
    public function showDatabases(): array
89
    {
90 1
        $sql = <<<SQL
91
        SELECT PDB_NAME FROM DBA_PDBS WHERE PDB_NAME NOT IN ('PDB\$SEED', 'PDB\$ROOT', 'ORCLPDB1', 'XEPDB1')
92 1
        SQL;
93
94 1
        return $this->setSql($sql)->queryColumn();
95
    }
96
97 276
    protected function bindPendingParams(): void
98
    {
99 276
        $paramsPassedByReference = [];
100
101 276
        $params = $this->params;
102
103 276
        foreach ($params as $name => $value) {
104 236
            if (PDO::PARAM_STR === $value->getType()) {
105
                /** @var mixed */
106 232
                $paramsPassedByReference[$name] = $value->getValue();
107 232
                $this->pdoStatement?->bindParam(
108 232
                    $name,
109 232
                    $paramsPassedByReference[$name],
110 232
                    $value->getType(),
111 232
                    strlen((string) $value->getValue())
112 232
                );
113
            } else {
114 49
                $this->pdoStatement?->bindValue($name, $value->getValue(), $value->getType());
115
            }
116
        }
117
    }
118
}
119