Passed
Push — master ( a9e03f...2aa099 )
by Wilmer
03:08
created

CommandPDO::queryBuilder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
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\Mysql;
6
7
use PDOException;
8
use Yiisoft\Db\Driver\PDO\CommandPDO as AbstractCommandPDO;
9
use Yiisoft\Db\Exception\ConvertException;
10
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
11
use Yiisoft\Db\Schema\SchemaInterface;
12
13
final class CommandPDO extends AbstractCommandPDO
14
{
15
    /**
16
     * @inheritDoc
17
     */
18 1
    public function insertEx(string $table, array $columns): bool|array
19
    {
20 1
        $params = [];
21 1
        $sql = $this->db->getQueryBuilder()->insertEx($table, $columns, $params);
22 1
        $this->setSql($sql)->bindValues($params);
23
24 1
        if (!$this->execute()) {
25
            return false;
26
        }
27
28 1
        $tableSchema = $this->db->getSchema()->getTableSchema($table);
29 1
        $tablePrimaryKeys = $tableSchema?->getPrimaryKey() ?? [];
30
31 1
        $result = [];
32 1
        foreach ($tablePrimaryKeys as $name) {
33 1
            if ($tableSchema?->getColumn($name)?->isAutoIncrement()) {
34 1
                $result[$name] = $this->db->getLastInsertID((string) $tableSchema?->getSequenceName());
35 1
                continue;
36
            }
37
38
            /** @var mixed */
39
            $result[$name] = $columns[$name] ?? $tableSchema?->getColumn($name)?->getDefaultValue();
40
        }
41
42 1
        return $result;
43
    }
44
45 188
    public function queryBuilder(): QueryBuilderInterface
46
    {
47 188
        return $this->db->getQueryBuilder();
48
    }
49
50
    public function schema(): SchemaInterface
51
    {
52
        return $this->db->getSchema();
53
    }
54
55 177
    protected function internalExecute(string|null $rawSql): void
56
    {
57 177
        $attempt = 0;
58
59 177
        while (true) {
60
            try {
61
                if (
62 177
                    ++$attempt === 1
63 177
                    && $this->isolationLevel !== null
64 177
                    && $this->db->getTransaction() === null
65
                ) {
66
                    $this->db->transaction(
67
                        fn (string|null $rawSql) => $this->internalExecute($rawSql),
68
                        $this->isolationLevel,
69
                    );
70
                } else {
71 177
                    $this->pdoStatement?->execute();
72
                }
73 175
                break;
74 22
            } catch (PDOException $e) {
75 22
                $rawSql = $rawSql ?: $this->getRawSql();
76 22
                $e = (new ConvertException($e, $rawSql))->run();
77
78 22
                if ($this->retryHandler === null || !($this->retryHandler)($e, $attempt)) {
79 22
                    throw $e;
80
                }
81
            }
82
        }
83
    }
84
}
85