1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Oracle; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use JsonException; |
9
|
|
|
use Yiisoft\Db\Constraint\Constraint; |
10
|
|
|
use Yiisoft\Db\Exception\Exception; |
11
|
|
|
use Yiisoft\Db\Exception\InvalidConfigException; |
12
|
|
|
use Yiisoft\Db\Exception\NotSupportedException; |
13
|
|
|
use Yiisoft\Db\Expression\Expression; |
14
|
|
|
use Yiisoft\Db\Query\DMLQueryBuilder as AbstractDMLQueryBuilder; |
|
|
|
|
15
|
|
|
use Yiisoft\Db\Query\QueryBuilderInterface; |
|
|
|
|
16
|
|
|
|
17
|
|
|
final class DMLQueryBuilder extends AbstractDMLQueryBuilder |
18
|
|
|
{ |
19
|
|
|
public function __construct(private QueryBuilderInterface $queryBuilder) |
20
|
|
|
{ |
21
|
|
|
parent::__construct($queryBuilder); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @link https://docs.oracle.com/cd/B28359_01/server.111/b28286/statements_9016.htm#SQLRF01606 |
26
|
|
|
* |
27
|
|
|
* @param string $table |
28
|
|
|
* @param $insertColumns |
29
|
|
|
* @param $updateColumns |
30
|
|
|
* @param array $params |
31
|
|
|
* |
32
|
|
|
* @throws Exception|InvalidArgumentException|InvalidConfigException|JsonException|NotSupportedException |
33
|
|
|
* |
34
|
|
|
* @return string |
35
|
|
|
*/ |
36
|
|
|
public function upsert(string $table, $insertColumns, $updateColumns, array &$params = []): string |
37
|
|
|
{ |
38
|
|
|
$constraints = []; |
39
|
|
|
|
40
|
|
|
/** @var Constraint[] $constraints */ |
41
|
|
|
[$uniqueNames, $insertNames, $updateNames] = $this->queryBuilder->prepareUpsertColumns( |
42
|
|
|
$table, |
43
|
|
|
$insertColumns, |
44
|
|
|
$updateColumns, |
45
|
|
|
$constraints |
46
|
|
|
); |
47
|
|
|
|
48
|
|
|
if (empty($uniqueNames)) { |
49
|
|
|
return $this->insert($table, $insertColumns, $params); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
if ($updateNames === []) { |
53
|
|
|
/** there are no columns to update */ |
54
|
|
|
$updateColumns = false; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$onCondition = ['or']; |
58
|
|
|
$quotedTableName = $this->queryBuilder->quoter()->quoteTableName($table); |
59
|
|
|
|
60
|
|
|
foreach ($constraints as $constraint) { |
61
|
|
|
$constraintCondition = ['and']; |
62
|
|
|
foreach ($constraint->getColumnNames() as $name) { |
63
|
|
|
$quotedName = $this->queryBuilder->quoter()->quoteColumnName($name); |
64
|
|
|
$constraintCondition[] = "$quotedTableName.$quotedName=\"EXCLUDED\".$quotedName"; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$onCondition[] = $constraintCondition; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$on = $this->queryBuilder->buildCondition($onCondition, $params); |
71
|
|
|
[, $placeholders, $values, $params] = $this->queryBuilder->prepareInsertValues($table, $insertColumns, $params); |
72
|
|
|
|
73
|
|
|
if (!empty($placeholders)) { |
74
|
|
|
$usingSelectValues = []; |
75
|
|
|
foreach ($insertNames as $index => $name) { |
76
|
|
|
$usingSelectValues[$name] = new Expression($placeholders[$index]); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** @psalm-suppress UndefinedInterfaceMethod */ |
80
|
|
|
$usingSubQuery = $this->queryBuilder->query()->select($usingSelectValues)->from('DUAL'); |
81
|
|
|
[$usingValues, $params] = $this->queryBuilder->build($usingSubQuery, $params); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$insertValues = []; |
85
|
|
|
$mergeSql = 'MERGE INTO ' |
86
|
|
|
. $this->queryBuilder->quoter()->quoteTableName($table) |
87
|
|
|
. ' ' |
88
|
|
|
. 'USING (' . ($usingValues ?? ltrim($values, ' ')) |
89
|
|
|
. ') "EXCLUDED" ' |
90
|
|
|
. "ON ($on)"; |
91
|
|
|
|
92
|
|
|
foreach ($insertNames as $name) { |
93
|
|
|
$quotedName = $this->queryBuilder->quoter()->quoteColumnName($name); |
94
|
|
|
|
95
|
|
|
if (strrpos($quotedName, '.') === false) { |
96
|
|
|
$quotedName = '"EXCLUDED".' . $quotedName; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$insertValues[] = $quotedName; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$insertSql = 'INSERT (' . implode(', ', $insertNames) . ')' . ' VALUES (' . implode(', ', $insertValues) . ')'; |
103
|
|
|
|
104
|
|
|
if ($updateColumns === false) { |
105
|
|
|
return "$mergeSql WHEN NOT MATCHED THEN $insertSql"; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
if ($updateColumns === true) { |
109
|
|
|
$updateColumns = []; |
110
|
|
|
foreach ($updateNames as $name) { |
111
|
|
|
$quotedName = $this->queryBuilder->quoter()->quoteColumnName($name); |
112
|
|
|
|
113
|
|
|
if (strrpos($quotedName, '.') === false) { |
114
|
|
|
$quotedName = '"EXCLUDED".' . $quotedName; |
115
|
|
|
} |
116
|
|
|
$updateColumns[$name] = new Expression($quotedName); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
[$updates, $params] = $this->queryBuilder->prepareUpdateSets($table, $updateColumns, $params); |
121
|
|
|
$updateSql = 'UPDATE SET ' . implode(', ', $updates); |
122
|
|
|
|
123
|
|
|
return "$mergeSql WHEN MATCHED THEN $updateSql WHEN NOT MATCHED THEN $insertSql"; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
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