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\SQLServer\Query; |
13
|
|
|
|
14
|
|
|
use Cycle\Database\Driver\DriverInterface; |
15
|
|
|
use Cycle\Database\Driver\SQLServer\SQLServerDriver; |
16
|
|
|
use Cycle\Database\Exception\BuilderException; |
17
|
|
|
use Cycle\Database\Exception\ReadonlyConnectionException; |
18
|
|
|
use Cycle\Database\Injection\FragmentInterface; |
19
|
|
|
use Cycle\Database\Query\QueryParameters; |
20
|
|
|
use Cycle\Database\Query\ReturningInterface; |
21
|
|
|
use Cycle\Database\Query\UpsertQuery; |
22
|
|
|
use Cycle\Database\Query\QueryInterface; |
23
|
|
|
use Cycle\Database\StatementInterface; |
24
|
|
|
|
25
|
|
|
class SQLServerUpsertQuery extends UpsertQuery implements ReturningInterface |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var SQLServerDriver|null |
29
|
|
|
*/ |
30
|
|
|
protected ?DriverInterface $driver = null; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var list<FragmentInterface|non-empty-string> |
|
|
|
|
34
|
|
|
*/ |
35
|
|
|
protected array $returningColumns = []; |
36
|
|
|
|
37
|
|
|
public function withDriver(DriverInterface $driver, ?string $prefix = null): QueryInterface |
38
|
|
|
{ |
39
|
|
|
$driver instanceof SQLServerDriver or throw new BuilderException( |
40
|
|
|
'SQLServer UpsertQuery can be used only with SQLServer driver', |
41
|
|
|
); |
42
|
|
|
|
43
|
|
|
return parent::withDriver($driver, $prefix); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function returning(string|FragmentInterface ...$columns): self |
47
|
|
|
{ |
48
|
|
|
$columns === [] and throw new BuilderException('RETURNING clause should contain at least 1 column.'); |
49
|
|
|
|
50
|
|
|
$this->returningColumns = \array_values($columns); |
|
|
|
|
51
|
|
|
|
52
|
|
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function run(): mixed |
56
|
|
|
{ |
57
|
|
|
if ($this->returningColumns === []) { |
58
|
|
|
return parent::run(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$params = new QueryParameters(); |
62
|
|
|
$queryString = $this->sqlStatement($params); |
63
|
|
|
|
64
|
|
|
$this->driver->isReadonly() and throw ReadonlyConnectionException::onWriteStatementExecution(); |
|
|
|
|
65
|
|
|
|
66
|
|
|
$result = $this->driver->query($queryString, $params->getParameters()); |
67
|
|
|
|
68
|
|
|
try { |
69
|
|
|
if (\count($this->returningColumns) === 1) { |
70
|
|
|
return $result->fetchColumn(); |
71
|
|
|
} |
72
|
|
|
return $result->fetch(StatementInterface::FETCH_ASSOC); |
73
|
|
|
} finally { |
74
|
|
|
$result->close(); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function getTokens(): array |
79
|
|
|
{ |
80
|
|
|
return parent::getTokens() + [ |
81
|
|
|
'return' => $this->returningColumns, |
82
|
|
|
]; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
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