Passed
Branch dev (f56f10)
by Wilmer
04:41 queued 01:34
created

DMLQueryBuilder   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 47
dl 0
loc 104
rs 10
c 0
b 0
f 0
wmc 24

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A update() 0 11 2
A resetSequence() 0 3 1
A insert() 0 8 3
A selectExists() 0 3 1
C batchInsert() 0 48 13
A delete() 0 6 2
A upsert() 0 7 1
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, Query|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 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

86
    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

86
    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...
87
    {
88
        throw new NotSupportedException(static::class . ' does not support resetting sequence.');
89
    }
90
91
    public function selectExists(string $rawSql): string
92
    {
93
        return 'SELECT EXISTS(' . $rawSql . ')';
94
    }
95
96
    public function update(string $table, array $columns, array|string $condition, array &$params = []): string
97
    {
98
        /**
99
         * @var array $lines
100
         * @var array $params
101
         */
102
        [$lines, $params] = $this->queryBuilder->prepareUpdateSets($table, $columns, $params);
103
        $sql = 'UPDATE ' . $this->queryBuilder->quoter()->quoteTableName($table) . ' SET ' . implode(', ', $lines);
104
        $where = $this->queryBuilder->buildWhere($condition, $params);
105
106
        return $where === '' ? $sql : $sql . ' ' . $where;
107
    }
108
109
    public function upsert(
110
        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

110
        /** @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...
111
        Query|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

111
        /** @scrutinizer ignore-unused */ Query|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...
112
        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

112
        /** @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...
113
        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

113
        /** @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...
114
    ): string {
115
        throw new NotSupportedException(static::class . ' does not support upsert.');
116
    }
117
}
118