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, 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 |
|
|
|
|
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, |
|
|
|
|
116
|
|
|
QueryInterface|array $insertColumns, |
|
|
|
|
117
|
|
|
bool|array $updateColumns, |
|
|
|
|
118
|
|
|
array &$params |
|
|
|
|
119
|
|
|
): string { |
120
|
|
|
throw new NotSupportedException(static::class . ' does not support upsert.'); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
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