Passed
Branch psalm-3 (d5d890)
by Wilmer
02:56
created

DMLQueryBuilder::insertEx()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Query;
6
7
use Generator;
8
use Yiisoft\Db\Exception\NotSupportedException;
9
use Yiisoft\Db\Expression\ExpressionInterface;
10
use Yiisoft\Strings\NumericHelper;
11
12
abstract class DMLQueryBuilder
13
{
14
    public function __construct(private QueryBuilderInterface $queryBuilder)
0 ignored issues
show
Bug introduced by
The type Yiisoft\Db\Query\QueryBuilderInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
    {
16
    }
17
18
    public function batchInsert(string $table, array $columns, iterable|Generator $rows, array &$params = []): string
19
    {
20
        if (empty($rows)) {
21
            return '';
22
        }
23
24
        if (($tableSchema = $this->queryBuilder->schema()->getTableSchema($table)) !== null) {
25
            $columnSchemas = $tableSchema->getColumns();
26
        } else {
27
            $columnSchemas = [];
28
        }
29
30
        $values = [];
31
32
        foreach ($rows as $row) {
33
            $vs = [];
34
            foreach ($row as $i => $value) {
35
                if (isset($columns[$i], $columnSchemas[$columns[$i]])) {
36
                    $value = $columnSchemas[$columns[$i]]->dbTypecast($value);
37
                }
38
                if (is_string($value)) {
39
                    $value = $this->queryBuilder->quoter()->quoteValue($value);
40
                } elseif (is_float($value)) {
41
                    /* ensure type cast always has . as decimal separator in all locales */
42
                    $value = NumericHelper::normalize((string) $value);
43
                } elseif ($value === false) {
44
                    $value = 0;
45
                } elseif ($value === null) {
46
                    $value = 'NULL';
47
                } elseif ($value instanceof ExpressionInterface) {
48
                    $value = $this->queryBuilder->buildExpression($value, $params);
49
                }
50
                $vs[] = $value;
51
            }
52
            $values[] = '(' . implode(', ', $vs) . ')';
53
        }
54
55
        if (empty($values)) {
56
            return '';
57
        }
58
59
        foreach ($columns as $i => $name) {
60
            $columns[$i] = $this->queryBuilder->quoter()->quoteColumnName($name);
61
        }
62
63
        return 'INSERT INTO '
64
            . $this->queryBuilder->quoter()->quoteTableName($table)
65
            . ' (' . implode(', ', $columns) . ') VALUES ' . implode(', ', $values);
66
    }
67
68
    public function delete(string $table, array|string $condition, array &$params): string
69
    {
70
        $sql = 'DELETE FROM ' . $this->queryBuilder->quoter()->quoteTableName($table);
71
        $where = $this->queryBuilder->buildWhere($condition, $params);
72
73
        return $where === '' ? $sql : $sql . ' ' . $where;
74
    }
75
76
    public function insert(string $table, QueryInterface|array $columns, array &$params = []): string
77
    {
78
        [$names, $placeholders, $values, $params] = $this->queryBuilder->prepareInsertValues($table, $columns, $params);
79
80
        return 'INSERT INTO '
81
            . $this->queryBuilder->quoter()->quoteTableName($table)
82
            . (!empty($names) ? ' (' . implode(', ', $names) . ')' : '')
83
            . (!empty($placeholders) ? ' VALUES (' . implode(', ', $placeholders) . ')' : $values);
84
    }
85
86
    public function insertEx(string $table, QueryInterface|array $columns, array &$params = []): string
87
    {
88
        return $this->insert($table, $columns, $params);
89
    }
90
91
    public function resetSequence(string $tableName, array|int|string|null $value = null): string
0 ignored issues
show
Unused Code introduced by
The parameter $value 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

91
    public function resetSequence(string $tableName, /** @scrutinizer ignore-unused */ array|int|string|null $value = null): string

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...
Unused Code introduced by
The parameter $tableName 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

91
    public function resetSequence(/** @scrutinizer ignore-unused */ string $tableName, array|int|string|null $value = null): string

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...
92
    {
93
        throw new NotSupportedException(static::class . ' does not support resetting sequence.');
94
    }
95
96
    public function selectExists(string $rawSql): string
97
    {
98
        return 'SELECT EXISTS(' . $rawSql . ')';
99
    }
100
101
    public function update(string $table, array $columns, array|string $condition, array &$params = []): string
102
    {
103
        /**
104
         * @var array $lines
105
         * @var array $params
106
         */
107
        [$lines, $params] = $this->queryBuilder->prepareUpdateSets($table, $columns, $params);
108
        $sql = 'UPDATE ' . $this->queryBuilder->quoter()->quoteTableName($table) . ' SET ' . implode(', ', $lines);
109
        $where = $this->queryBuilder->buildWhere($condition, $params);
110
111
        return $where === '' ? $sql : $sql . ' ' . $where;
112
    }
113
114
    public function upsert(
115
        string $table,
0 ignored issues
show
Unused Code introduced by
The parameter $table 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

115
        /** @scrutinizer ignore-unused */ string $table,

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...
116
        QueryInterface|array $insertColumns,
0 ignored issues
show
Unused Code introduced by
The parameter $insertColumns 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

116
        /** @scrutinizer ignore-unused */ QueryInterface|array $insertColumns,

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...
117
        bool|array $updateColumns,
0 ignored issues
show
Unused Code introduced by
The parameter $updateColumns 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

117
        /** @scrutinizer ignore-unused */ bool|array $updateColumns,

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...
118
        array &$params
0 ignored issues
show
Unused Code introduced by
The parameter $params 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

118
        /** @scrutinizer ignore-unused */ array &$params

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...
119
    ): string {
120
        throw new NotSupportedException(static::class . ' does not support upsert.');
121
    }
122
}
123