1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Cycle ORM package. |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Cycle\Database\Driver\Postgres\Query; |
13
|
|
|
|
14
|
|
|
use Cycle\Database\Driver\DriverInterface; |
15
|
|
|
use Cycle\Database\Driver\Postgres\PostgresDriver; |
16
|
|
|
use Cycle\Database\Exception\BuilderException; |
17
|
|
|
use Cycle\Database\Exception\ReadonlyConnectionException; |
18
|
|
|
use Cycle\Database\Injection\FragmentInterface; |
19
|
|
|
use Cycle\Database\Query\ReturningInterface; |
20
|
|
|
use Cycle\Database\Query\QueryInterface; |
21
|
|
|
use Cycle\Database\Query\QueryParameters; |
22
|
|
|
use Cycle\Database\Query\UpsertQuery; |
23
|
|
|
use Cycle\Database\StatementInterface; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Postgres driver requires a slightly different way to handle last insert id. |
27
|
|
|
*/ |
28
|
|
|
class PostgresUpsertQuery extends UpsertQuery implements ReturningInterface |
29
|
|
|
{ |
30
|
|
|
/** @var PostgresDriver|null */ |
31
|
|
|
protected ?DriverInterface $driver = null; |
32
|
|
|
|
33
|
|
|
/** @deprecated */ |
34
|
|
|
protected string|FragmentInterface|null $returning = null; |
35
|
|
|
|
36
|
|
|
/** @var list<FragmentInterface|non-empty-string> */ |
|
|
|
|
37
|
|
|
protected array $returningColumns = []; |
38
|
|
|
|
39
|
|
|
public function withDriver(DriverInterface $driver, ?string $prefix = null): QueryInterface |
40
|
|
|
{ |
41
|
|
|
$driver instanceof PostgresDriver or throw new BuilderException( |
42
|
|
|
'Postgres UpsertQuery can be used only with Postgres driver', |
43
|
|
|
); |
44
|
|
|
|
45
|
|
|
return parent::withDriver($driver, $prefix); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Set returning column. If not set, the driver will detect PK automatically. |
50
|
|
|
*/ |
51
|
|
|
public function returning(string|FragmentInterface ...$columns): self |
52
|
|
|
{ |
53
|
|
|
$columns === [] and throw new BuilderException('RETURNING clause should contain at least 1 column.'); |
54
|
|
|
|
55
|
|
|
$this->returning = \count($columns) === 1 ? \reset($columns) : null; |
|
|
|
|
56
|
|
|
|
57
|
|
|
$this->returningColumns = \array_values($columns); |
|
|
|
|
58
|
|
|
|
59
|
|
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function run(): mixed |
63
|
|
|
{ |
64
|
|
|
$params = new QueryParameters(); |
65
|
|
|
$queryString = $this->sqlStatement($params); |
66
|
|
|
|
67
|
|
|
$this->driver->isReadonly() and throw ReadonlyConnectionException::onWriteStatementExecution(); |
|
|
|
|
68
|
|
|
|
69
|
|
|
$result = $this->driver->query($queryString, $params->getParameters()); |
70
|
|
|
|
71
|
|
|
try { |
72
|
|
|
if ($this->returningColumns !== []) { |
73
|
|
|
if (\count($this->returningColumns) === 1) { |
74
|
|
|
return $result->fetchColumn(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $result->fetch(StatementInterface::FETCH_ASSOC); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
// Return PK if no RETURNING clause is set |
81
|
|
|
if ($this->getPrimaryKey() !== null) { |
82
|
|
|
return $result->fetchColumn(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return null; |
86
|
|
|
} finally { |
87
|
|
|
$result->close(); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function getTokens(): array |
92
|
|
|
{ |
93
|
|
|
return parent::getTokens() + [ |
94
|
|
|
'return' => $this->returningColumns !== [] ? $this->returningColumns : (array) $this->getPrimaryKey(), |
95
|
|
|
]; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
private function getPrimaryKey(): ?string |
99
|
|
|
{ |
100
|
|
|
try { |
101
|
|
|
return $this->driver?->getPrimaryKey($this->prefix, $this->table); |
|
|
|
|
102
|
|
|
} catch (\Throwable) { |
103
|
|
|
return null; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
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