|
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) |
|
|
|
|
|
|
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 |
|
|
|
|
|
|
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, |
|
|
|
|
|
|
111
|
|
|
Query|array $insertColumns, |
|
|
|
|
|
|
112
|
|
|
bool|array $updateColumns, |
|
|
|
|
|
|
113
|
|
|
array &$params |
|
|
|
|
|
|
114
|
|
|
): string { |
|
115
|
|
|
throw new NotSupportedException(static::class . ' does not support upsert.'); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths