1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Pgsql; |
6
|
|
|
|
7
|
|
|
use JsonException; |
8
|
|
|
use PDO; |
9
|
|
|
use Yiisoft\Db\Command\Param; |
10
|
|
|
use Yiisoft\Db\Exception\Exception; |
11
|
|
|
use Yiisoft\Db\Exception\InvalidArgumentException; |
12
|
|
|
use Yiisoft\Db\Exception\InvalidConfigException; |
13
|
|
|
use Yiisoft\Db\Exception\NotSupportedException; |
14
|
|
|
use Yiisoft\Db\Expression\Expression; |
15
|
|
|
use Yiisoft\Db\QueryBuilder\DMLQueryBuilder as AbstractDMLQueryBuilder; |
16
|
|
|
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface; |
17
|
|
|
use Yiisoft\Db\Query\Query; |
18
|
|
|
use Yiisoft\Db\Query\QueryInterface; |
19
|
|
|
use Yiisoft\Db\Schema\QuoterInterface; |
20
|
|
|
use Yiisoft\Db\Schema\Schema; |
|
|
|
|
21
|
|
|
use Yiisoft\Db\Schema\SchemaInterface; |
22
|
|
|
|
23
|
|
|
use function implode; |
24
|
|
|
use function is_array; |
25
|
|
|
use function is_string; |
26
|
|
|
use function reset; |
27
|
|
|
|
28
|
|
|
final class DMLQueryBuilder extends AbstractDMLQueryBuilder |
29
|
|
|
{ |
30
|
|
|
public function __construct( |
31
|
|
|
private QueryBuilderInterface $queryBuilder, |
32
|
|
|
private QuoterInterface $quoter, |
33
|
|
|
private SchemaInterface $schema |
34
|
|
|
) { |
35
|
|
|
parent::__construct($queryBuilder, $quoter, $schema); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function insertEx(string $table, QueryInterface|array $columns, array &$params = []): string |
39
|
|
|
{ |
40
|
|
|
$sql = $this->insert($table, $columns, $params); |
41
|
|
|
|
42
|
|
|
$tableSchema = $this->schema->getTableSchema($table); |
43
|
|
|
|
44
|
|
|
$returnColumns = []; |
45
|
|
|
if ($tableSchema !== null) { |
46
|
|
|
$returnColumns = $tableSchema->getPrimaryKey(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if (!empty($returnColumns)) { |
50
|
|
|
$returning = []; |
51
|
|
|
foreach ($returnColumns as $name) { |
52
|
|
|
$returning[] = $this->quoter->quoteColumnName($name); |
53
|
|
|
} |
54
|
|
|
$sql .= ' RETURNING ' . implode(', ', $returning); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $sql; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Creates an INSERT SQL statement. |
62
|
|
|
* |
63
|
|
|
* For example, |
64
|
|
|
* |
65
|
|
|
* ```php |
66
|
|
|
* $sql = $queryBuilder->insert('user', [ |
67
|
|
|
* 'name' => 'Sam', |
68
|
|
|
* 'age' => 30, |
69
|
|
|
* ], $params); |
70
|
|
|
* ``` |
71
|
|
|
* |
72
|
|
|
* The method will properly escape the table and column names. |
73
|
|
|
* |
74
|
|
|
* @param string $table the table that new rows will be inserted into. |
75
|
|
|
* @param array|QueryInterface $columns the column data (name => value) to be inserted into the table or instance of |
76
|
|
|
* {@see Query} to perform INSERT INTO ... SELECT SQL statement. Passing of {@see Query}. |
77
|
|
|
* @param array $params the binding parameters that will be generated by this method. They should be bound to the |
78
|
|
|
* DB command later. |
79
|
|
|
* |
80
|
|
|
* @throws Exception|InvalidArgumentException|InvalidConfigException|NotSupportedException |
81
|
|
|
* |
82
|
|
|
* @return string the INSERT SQL |
83
|
|
|
* |
84
|
|
|
* @psalm-suppress UndefinedInterfaceMethod |
85
|
|
|
* @psalm-suppress MixedArgument |
86
|
|
|
*/ |
87
|
|
|
public function insert(string $table, QueryInterface|array $columns, array &$params = []): string |
88
|
|
|
{ |
89
|
|
|
return parent::insert($table, $this->normalizeTableRowData($table, $columns), $params); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@see upsert()} implementation for PostgresSQL 9.5 or higher. |
94
|
|
|
* |
95
|
|
|
* @throws Exception|InvalidArgumentException|InvalidConfigException|JsonException|NotSupportedException |
96
|
|
|
*/ |
97
|
|
|
public function newUpsert( |
98
|
|
|
string $table, |
99
|
|
|
QueryInterface|array $insertColumns, |
100
|
|
|
bool|array|QueryInterface $updateColumns, |
101
|
|
|
array &$params = [] |
102
|
|
|
): string { |
103
|
|
|
$insertSql = $this->insert($table, $insertColumns, $params); |
104
|
|
|
|
105
|
|
|
/** @var array $uniqueNames */ |
106
|
|
|
[$uniqueNames, , $updateNames] = $this->prepareUpsertColumns( |
107
|
|
|
$table, |
108
|
|
|
$insertColumns, |
109
|
|
|
$updateColumns, |
110
|
|
|
); |
111
|
|
|
|
112
|
|
|
if (empty($uniqueNames)) { |
113
|
|
|
return $insertSql; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
if ($updateNames === []) { |
|
|
|
|
117
|
|
|
/** there are no columns to update */ |
118
|
|
|
$updateColumns = false; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
if ($updateColumns === false) { |
|
|
|
|
122
|
|
|
return "$insertSql ON CONFLICT DO NOTHING"; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
if ($updateColumns === true) { |
|
|
|
|
126
|
|
|
$updateColumns = []; |
127
|
|
|
|
128
|
|
|
/** @var string $name */ |
129
|
|
|
foreach ($updateNames as $name) { |
130
|
|
|
$updateColumns[$name] = new Expression( |
131
|
|
|
'EXCLUDED.' . $this->quoter->quoteColumnName($name) |
132
|
|
|
); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @var array $updateColumns |
138
|
|
|
* |
139
|
|
|
* @psalm-var string[] $uniqueNames |
140
|
|
|
* @psalm-var string[] $updates |
141
|
|
|
*/ |
142
|
|
|
[$updates, $params] = $this->prepareUpdateSets($table, $updateColumns, $params); |
143
|
|
|
|
144
|
|
|
return $insertSql |
145
|
|
|
. ' ON CONFLICT (' . implode(', ', $uniqueNames) . ') DO UPDATE SET ' . implode(', ', $updates); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Creates a SQL statement for resetting the sequence value of a table's primary key. |
150
|
|
|
* |
151
|
|
|
* The sequence will be reset such that the primary key of the next new row inserted will have the specified value |
152
|
|
|
* or 1. |
153
|
|
|
* |
154
|
|
|
* @param string $tableName the name of the table whose primary key sequence will be reset. |
155
|
|
|
* @param int|string|null $value the value for the primary key of the next new row inserted. If this is not set, the |
156
|
|
|
* next new row's primary key will have a value 1. |
157
|
|
|
* |
158
|
|
|
* @throws Exception|InvalidArgumentException if the table does not exist or there is no sequence |
159
|
|
|
* associated with the table. |
160
|
|
|
* |
161
|
|
|
* @return string the SQL statement for resetting sequence. |
162
|
|
|
*/ |
163
|
|
|
public function resetSequence(string $tableName, int|string $value = null): string |
164
|
|
|
{ |
165
|
|
|
$table = $this->schema->getTableSchema($tableName); |
166
|
|
|
|
167
|
|
|
if ($table !== null && ($sequence = $table->getSequenceName()) !== null) { |
168
|
|
|
/** |
169
|
|
|
* {@see http://www.postgresql.org/docs/8.1/static/functions-sequence.html} |
170
|
|
|
*/ |
171
|
|
|
$sequence = $this->quoter->quoteTableName($sequence); |
172
|
|
|
$tableName = $this->quoter->quoteTableName($tableName); |
173
|
|
|
|
174
|
|
|
if ($value === null) { |
175
|
|
|
$pk = $table->getPrimaryKey(); |
176
|
|
|
$key = $this->quoter->quoteColumnName(reset($pk)); |
177
|
|
|
$value = "(SELECT COALESCE(MAX($key),0) FROM $tableName)+1"; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return "SELECT SETVAL('$sequence',$value,false)"; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
if ($table === null) { |
184
|
|
|
throw new InvalidArgumentException("Table not found: $tableName"); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
throw new InvalidArgumentException("There is not sequence associated with table '$tableName'."); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Creates an UPDATE SQL statement. |
192
|
|
|
* |
193
|
|
|
* For example, |
194
|
|
|
* |
195
|
|
|
* ```php |
196
|
|
|
* $params = []; |
197
|
|
|
* $sql = $queryBuilder->update('user', ['status' => 1], 'age > 30', $params); |
198
|
|
|
* ``` |
199
|
|
|
* |
200
|
|
|
* The method will properly escape the table and column names. |
201
|
|
|
* |
202
|
|
|
* @param string $table the table to be updated. |
203
|
|
|
* @param array $columns the column data (name => value) to be updated. |
204
|
|
|
* @param array|string $condition the condition that will be put in the WHERE part. Please refer to |
205
|
|
|
* {@see Query::where()} on how to specify condition. |
206
|
|
|
* @param array $params the binding parameters that will be modified by this method so that they can be bound to the |
207
|
|
|
* DB command later. |
208
|
|
|
* |
209
|
|
|
* @return string the UPDATE SQL. |
210
|
|
|
* |
211
|
|
|
* @psalm-suppress UndefinedInterfaceMethod |
212
|
|
|
*/ |
213
|
|
|
public function update(string $table, array $columns, array|string $condition, array &$params = []): string |
214
|
|
|
{ |
215
|
|
|
$normalizeTableRowData = $this->normalizeTableRowData($table, $columns); |
216
|
|
|
|
217
|
|
|
return parent::update( |
218
|
|
|
$table, |
219
|
|
|
is_array($normalizeTableRowData) ? $normalizeTableRowData : [], |
|
|
|
|
220
|
|
|
$condition, |
221
|
|
|
$params, |
222
|
|
|
); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Creates an SQL statement to insert rows into a database table if they do not already exist (matching unique |
227
|
|
|
* constraints), or update them if they do. |
228
|
|
|
* |
229
|
|
|
* For example, |
230
|
|
|
* |
231
|
|
|
* ```php |
232
|
|
|
* $sql = $queryBuilder->upsert('pages', [ |
233
|
|
|
* 'name' => 'Front page', |
234
|
|
|
* 'url' => 'http://example.com/', // url is unique |
235
|
|
|
* 'visits' => 0, |
236
|
|
|
* ], [ |
237
|
|
|
* 'visits' => new \Yiisoft\Db\Expression('visits + 1'), |
238
|
|
|
* ], $params); |
239
|
|
|
* ``` |
240
|
|
|
* |
241
|
|
|
* The method will properly escape the table and column names. |
242
|
|
|
* |
243
|
|
|
* @param string $table the table that new rows will be inserted into/updated in. |
244
|
|
|
* @param array|QueryInterface $insertColumns the column data (name => value) to be inserted into the table or |
245
|
|
|
* instance of {@see Query} to perform `INSERT INTO ... SELECT` SQL statement. |
246
|
|
|
* @param array|bool|QueryInterface $updateColumns the column data (name => value) to be updated if they already |
247
|
|
|
* exist. |
248
|
|
|
* If `true` is passed, the column data will be updated to match the insert column data. |
249
|
|
|
* If `false` is passed, no update will be performed if the column data already exists. |
250
|
|
|
* @param array $params the binding parameters that will be generated by this method. |
251
|
|
|
* They should be bound to the DB command later. |
252
|
|
|
* |
253
|
|
|
* @throws Exception|InvalidConfigException|JsonException|NotSupportedException if this is not supported by the |
254
|
|
|
* underlying DBMS. |
255
|
|
|
* |
256
|
|
|
* @return string the resulting SQL. |
257
|
|
|
* |
258
|
|
|
* @link https://www.postgresql.org/docs/9.5/static/sql-insert.html#SQL-ON-CONFLICT |
259
|
|
|
* @link https://stackoverflow.com/questions/1109061/insert-on-duplicate-update-in-postgresql/8702291#8702291 |
260
|
|
|
* |
261
|
|
|
* @psalm-suppress UndefinedInterfaceMethod |
262
|
|
|
*/ |
263
|
|
|
public function upsert( |
264
|
|
|
string $table, |
265
|
|
|
QueryInterface|array $insertColumns, |
266
|
|
|
$updateColumns, |
267
|
|
|
array &$params = [] |
268
|
|
|
): string { |
269
|
|
|
$insertColumns = $this->normalizeTableRowData($table, $insertColumns); |
270
|
|
|
|
271
|
|
|
if (!is_bool($updateColumns)) { |
272
|
|
|
$updateColumns = $this->normalizeTableRowData($table, $updateColumns); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
return $this->newUpsert($table, $insertColumns, $updateColumns, $params); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Normalizes data to be saved into the table, performing extra preparations and type converting, if necessary. |
280
|
|
|
* |
281
|
|
|
* @param string $table the table that data will be saved into. |
282
|
|
|
* @param array|QueryInterface $columns the column data (name => value) to be saved into the table or instance of |
283
|
|
|
* {@see QueryInterface} to perform INSERT INTO ... SELECT SQL statement. Passing of |
284
|
|
|
* {@see QueryInterface}. |
285
|
|
|
* |
286
|
|
|
* @return array|QueryInterface normalized columns. |
287
|
|
|
*/ |
288
|
|
|
private function normalizeTableRowData(string $table, QueryInterface|array $columns): QueryInterface|array |
289
|
|
|
{ |
290
|
|
|
if ($columns instanceof QueryInterface) { |
|
|
|
|
291
|
|
|
return $columns; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
if (($tableSchema = $this->schema->getTableSchema($table)) !== null) { |
295
|
|
|
$columnSchemas = $tableSchema->getColumns(); |
296
|
|
|
/** @var mixed $value */ |
297
|
|
|
foreach ($columns as $name => $value) { |
298
|
|
|
if ( |
299
|
|
|
isset($columnSchemas[$name]) && |
300
|
|
|
$columnSchemas[$name]->getType() === Schema::TYPE_BINARY && |
301
|
|
|
is_string($value) |
302
|
|
|
) { |
303
|
|
|
/** explicitly setup PDO param type for binary column */ |
304
|
|
|
$columns[$name] = new Param($value, PDO::PARAM_LOB); |
305
|
|
|
} |
306
|
|
|
} |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
return $columns; |
310
|
|
|
} |
311
|
|
|
} |
312
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: