-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpsertSelect.php
More file actions
45 lines (35 loc) · 1.54 KB
/
UpsertSelect.php
File metadata and controls
45 lines (35 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
namespace Utopia\Query\Builder\Trait;
use Utopia\Query\Builder\Statement;
use Utopia\Query\Exception\ValidationException;
trait UpsertSelect
{
public function upsertSelect(): Statement
{
$this->bindings = [];
$this->validateTable();
if ($this->insertSelectSource === null) {
throw new ValidationException('No SELECT source specified. Call fromSelect() before upsertSelect().');
}
if (empty($this->insertSelectColumns)) {
throw new ValidationException('No columns specified. Call fromSelect() with columns before upsertSelect().');
}
if (empty($this->conflictKeys)) {
throw new ValidationException('No conflict keys specified. Call onConflict() before upsertSelect().');
}
if (empty($this->conflictUpdateColumns)) {
throw new ValidationException('No conflict update columns specified. Call onConflict() with update columns before upsertSelect().');
}
$wrappedColumns = \array_map(
fn (string $col): string => $this->resolveAndWrap($col),
$this->insertSelectColumns
);
$sourceResult = $this->insertSelectSource->build();
$sql = 'INSERT INTO ' . $this->quote($this->table)
. ' (' . \implode(', ', $wrappedColumns) . ')'
. ' ' . $sourceResult->query;
$this->addBindings($sourceResult->bindings);
$sql .= ' ' . $this->compileConflictClause();
return new Statement($sql, $this->bindings, executor: $this->executor);
}
}