Passed
Push — enable-bc-test ( 01c074 )
by Sergei
32:52 queued 15:42
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 2
Bugs 0 Features 0
Metric Value
cc 9
eloc 18
c 2
b 0
f 0
nc 9
nop 1
dl 0
loc 25
ccs 17
cts 17
cp 1
crap 9
rs 8.0555
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 5
    public function insertWithReturningPks(string $table, array $columns): bool|array
24
    {
25 5
        $params = [];
26 5
        $sql = $this->getQueryBuilder()->insert($table, $columns, $params);
27
28 5
        $tableSchema = $this->db->getSchema()->getTableSchema($table);
29
30 5
        $returnColumns = $tableSchema?->getPrimaryKey() ?? [];
31 5
        $columnSchemas = $tableSchema?->getColumns() ?? [];
32
33 5
        $returnParams = [];
34 5
        $returning = [];
35
36 5
        foreach ($returnColumns as $name) {
37 5
            $phName = AbstractQueryBuilder::PARAM_PREFIX . (count($params) + count($returnParams));
38
39 5
            $returnParams[$phName] = [
40 5
                'column' => $name,
41 5
                'value' => '',
42 5
            ];
43
44 5
            if (!isset($columnSchemas[$name]) || $columnSchemas[$name]->getPhpType() !== SchemaInterface::PHP_TYPE_INTEGER) {
45 1
                $returnParams[$phName]['dataType'] = PDO::PARAM_STR;
46
            } else {
47 4
                $returnParams[$phName]['dataType'] = PDO::PARAM_INT;
48
            }
49
50 5
            $returnParams[$phName]['size'] = $columnSchemas[$name]->getSize() ?? -1;
51
52 5
            $returning[] = $this->db->getQuoter()->quoteColumnName($name);
53
        }
54
55 5
        $sql .= ' RETURNING ' . implode(', ', $returning) . ' INTO ' . implode(', ', array_keys($returnParams));
56
57 5
        $this->setSql($sql)->bindValues($params);
58 5
        $this->prepare(false);
59
60
        /** @psalm-var array<string, array{column: string, value: mixed, dataType: int, size: int}> $returnParams */
61 5
        foreach ($returnParams as $name => &$value) {
62 5
            $this->bindParam($name, $value['value'], $value['dataType'], $value['size']);
63
        }
64
65 5
        unset($value);
66
67 5
        if (!$this->execute()) {
68
            return false;
69
        }
70
71 5
        $result = [];
72
73 5
        foreach ($returnParams as $value) {
74
            /** @psalm-var mixed */
75 5
            $result[$value['column']] = $value['value'];
76
        }
77
78 5
        return $result;
79
    }
80
81 1
    public function showDatabases(): array
82
    {
83 1
        $sql = <<<SQL
84
        SELECT PDB_NAME FROM DBA_PDBS WHERE PDB_NAME NOT IN ('PDB\$SEED', 'PDB\$ROOT', 'ORCLPDB1', 'XEPDB1')
85 1
        SQL;
86
87 1
        return $this->setSql($sql)->queryColumn();
88
    }
89
90 247
    protected function bindPendingParams(): void
91
    {
92 247
        $paramsPassedByReference = [];
93
94 247
        $params = $this->params;
95
96 247
        foreach ($params as $name => $value) {
97 208
            if (PDO::PARAM_STR === $value->getType()) {
98
                /** @var mixed */
99 204
                $paramsPassedByReference[$name] = $value->getValue();
100 204
                $this->pdoStatement?->bindParam(
101 204
                    $name,
102 204
                    $paramsPassedByReference[$name],
103 204
                    $value->getType(),
104 204
                    strlen((string) $value->getValue())
105 204
                );
106
            } else {
107 38
                $this->pdoStatement?->bindValue($name, $value->getValue(), $value->getType());
108
            }
109
        }
110
    }
111
}
112