|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Pgsql; |
|
6
|
|
|
|
|
7
|
|
|
use Yiisoft\Db\Exception\InvalidArgumentException; |
|
8
|
|
|
use Yiisoft\Db\Expression\Expression; |
|
9
|
|
|
use Yiisoft\Db\Query\QueryInterface; |
|
10
|
|
|
use Yiisoft\Db\QueryBuilder\AbstractDMLQueryBuilder; |
|
11
|
|
|
|
|
12
|
|
|
use function implode; |
|
13
|
|
|
use function reset; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Implements a DML (Data Manipulation Language) SQL statements for PostgreSQL Server. |
|
17
|
|
|
*/ |
|
18
|
|
|
final class DMLQueryBuilder extends AbstractDMLQueryBuilder |
|
19
|
|
|
{ |
|
20
|
|
|
public function insertWithReturningPks(string $table, QueryInterface|array $columns, array &$params = []): string |
|
21
|
|
|
{ |
|
22
|
|
|
$sql = $this->insert($table, $columns, $params); |
|
23
|
|
|
|
|
24
|
|
|
$tableSchema = $this->schema->getTableSchema($table); |
|
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
$returnColumns = []; |
|
27
|
|
|
if ($tableSchema !== null) { |
|
28
|
|
|
$returnColumns = $tableSchema->getPrimaryKey(); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
if (!empty($returnColumns)) { |
|
32
|
|
|
$returning = []; |
|
33
|
|
|
foreach ($returnColumns as $name) { |
|
34
|
|
|
$returning[] = $this->quoter->quoteColumnName($name); |
|
|
|
|
|
|
35
|
|
|
} |
|
36
|
|
|
$sql .= ' RETURNING ' . implode(', ', $returning); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
return $sql; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function resetSequence(string $tableName, int|string $value = null): string |
|
43
|
|
|
{ |
|
44
|
|
|
$table = $this->schema->getTableSchema($tableName); |
|
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
if ($table !== null && ($sequence = $table->getSequenceName()) !== null) { |
|
47
|
|
|
/** |
|
48
|
|
|
* @link https://www.postgresql.org/docs/8.1/static/functions-sequence.html |
|
49
|
|
|
*/ |
|
50
|
|
|
$sequence = $this->quoter->quoteTableName($sequence); |
|
|
|
|
|
|
51
|
|
|
$tableName = $this->quoter->quoteTableName($tableName); |
|
52
|
|
|
|
|
53
|
|
|
if ($value === null) { |
|
54
|
|
|
$pk = $table->getPrimaryKey(); |
|
55
|
|
|
$key = $this->quoter->quoteColumnName(reset($pk)); |
|
56
|
|
|
$value = "(SELECT COALESCE(MAX($key),0) FROM $tableName)+1"; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return "SELECT SETVAL('$sequence',$value,false)"; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
if ($table === null) { |
|
63
|
|
|
throw new InvalidArgumentException("Table not found: '$tableName'."); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
throw new InvalidArgumentException("There is not sequence associated with table '$tableName'."); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function upsert( |
|
70
|
|
|
string $table, |
|
71
|
|
|
QueryInterface|array $insertColumns, |
|
72
|
|
|
$updateColumns, |
|
73
|
|
|
array &$params = [] |
|
74
|
|
|
): string { |
|
75
|
|
|
$insertSql = $this->insert($table, $insertColumns, $params); |
|
76
|
|
|
|
|
77
|
|
|
/** @psalm-var array $uniqueNames */ |
|
78
|
|
|
[$uniqueNames, , $updateNames] = $this->prepareUpsertColumns( |
|
79
|
|
|
$table, |
|
80
|
|
|
$insertColumns, |
|
81
|
|
|
$updateColumns, |
|
82
|
|
|
); |
|
83
|
|
|
|
|
84
|
|
|
if (empty($uniqueNames)) { |
|
85
|
|
|
return $insertSql; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
if ($updateNames === []) { |
|
|
|
|
|
|
89
|
|
|
/** there are no columns to update */ |
|
90
|
|
|
$updateColumns = false; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
if ($updateColumns === false) { |
|
94
|
|
|
return "$insertSql ON CONFLICT DO NOTHING"; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
if ($updateColumns === true) { |
|
98
|
|
|
$updateColumns = []; |
|
99
|
|
|
|
|
100
|
|
|
/** @psalm-var string $name */ |
|
101
|
|
|
foreach ($updateNames as $name) { |
|
|
|
|
|
|
102
|
|
|
$updateColumns[$name] = new Expression( |
|
103
|
|
|
'EXCLUDED.' . $this->quoter->quoteColumnName($name) |
|
|
|
|
|
|
104
|
|
|
); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @psalm-var array $updateColumns |
|
110
|
|
|
* @psalm-var string[] $uniqueNames |
|
111
|
|
|
* @psalm-var string[] $updates |
|
112
|
|
|
*/ |
|
113
|
|
|
[$updates, $params] = $this->prepareUpdateSets($table, $updateColumns, $params); |
|
114
|
|
|
|
|
115
|
|
|
return $insertSql |
|
116
|
|
|
. ' ON CONFLICT (' . implode(', ', $uniqueNames) . ') DO UPDATE SET ' . implode(', ', $updates); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|