|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Pgsql\PDO; |
|
6
|
|
|
|
|
7
|
|
|
use PDOException; |
|
8
|
|
|
use Yiisoft\Db\Cache\QueryCache; |
|
9
|
|
|
use Yiisoft\Db\Command\Command; |
|
10
|
|
|
use Yiisoft\Db\Connection\ConnectionPDOInterface; |
|
11
|
|
|
use Yiisoft\Db\Exception\ConvertException; |
|
12
|
|
|
use Yiisoft\Db\Exception\Exception; |
|
13
|
|
|
use Yiisoft\Db\Query\QueryBuilderInterface; |
|
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
final class CommandPDOPgsql extends Command |
|
16
|
|
|
{ |
|
17
|
401 |
|
public function __construct(private ConnectionPDOInterface $db, QueryCache $queryCache) |
|
18
|
|
|
{ |
|
19
|
401 |
|
parent::__construct($queryCache); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @inheritDoc |
|
24
|
|
|
*/ |
|
25
|
1 |
|
public function insertEx(string $table, array $columns): bool|array |
|
26
|
|
|
{ |
|
27
|
1 |
|
$params = []; |
|
28
|
1 |
|
$sql = $this->queryBuilder()->insertEx($table, $columns, $params); |
|
29
|
|
|
|
|
30
|
1 |
|
$this->setSql($sql)->bindValues($params); |
|
31
|
1 |
|
$this->prepare(false); |
|
32
|
|
|
|
|
33
|
1 |
|
return $this->queryOne(); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
191 |
|
public function queryBuilder(): QueryBuilderInterface |
|
37
|
|
|
{ |
|
38
|
191 |
|
return $this->db->getQueryBuilder(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
182 |
|
public function prepare(?bool $forRead = null): void |
|
42
|
|
|
{ |
|
43
|
182 |
|
if (isset($this->pdoStatement)) { |
|
44
|
6 |
|
$this->bindPendingParams(); |
|
45
|
|
|
|
|
46
|
6 |
|
return; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
182 |
|
$sql = $this->getSql(); |
|
50
|
|
|
|
|
51
|
182 |
|
if ($this->db->getTransaction()) { |
|
52
|
|
|
/** master is in a transaction. use the same connection. */ |
|
53
|
6 |
|
$forRead = false; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
182 |
|
if ($forRead || ($forRead === null && $this->db->getSchema()->isReadQuery($sql))) { |
|
57
|
177 |
|
$pdo = $this->db->getSlavePdo(); |
|
58
|
|
|
} else { |
|
59
|
65 |
|
$pdo = $this->db->getMasterPdo(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
try { |
|
63
|
182 |
|
$this->pdoStatement = $pdo->prepare($sql); |
|
64
|
182 |
|
$this->bindPendingParams(); |
|
65
|
|
|
} catch (PDOException $e) { |
|
66
|
|
|
$message = $e->getMessage() . "\nFailed to prepare SQL: $sql"; |
|
67
|
|
|
$errorInfo = $e->errorInfo ?? null; |
|
68
|
|
|
|
|
69
|
|
|
throw new Exception($message, $errorInfo, $e); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
2 |
|
protected function getCacheKey(string $method, ?int $fetchMode, string $rawSql): array |
|
74
|
|
|
{ |
|
75
|
|
|
return [ |
|
76
|
|
|
__CLASS__, |
|
77
|
|
|
$method, |
|
78
|
|
|
$fetchMode, |
|
79
|
2 |
|
$this->db->getDriver()->getDsn(), |
|
80
|
2 |
|
$this->db->getDriver()->getUsername(), |
|
81
|
|
|
$rawSql, |
|
82
|
|
|
]; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
181 |
|
protected function internalExecute(?string $rawSql): void |
|
86
|
|
|
{ |
|
87
|
181 |
|
$attempt = 0; |
|
88
|
|
|
|
|
89
|
181 |
|
while (true) { |
|
90
|
|
|
try { |
|
91
|
|
|
if ( |
|
92
|
181 |
|
++$attempt === 1 |
|
93
|
181 |
|
&& $this->isolationLevel !== null |
|
94
|
181 |
|
&& $this->db->getTransaction() === null |
|
95
|
|
|
) { |
|
96
|
|
|
$this->db->transaction(fn ($rawSql) => $this->internalExecute($rawSql), $this->isolationLevel); |
|
97
|
|
|
} else { |
|
98
|
181 |
|
$this->pdoStatement->execute(); |
|
|
|
|
|
|
99
|
|
|
} |
|
100
|
181 |
|
break; |
|
101
|
7 |
|
} catch (\Exception $e) { |
|
102
|
7 |
|
$rawSql = $rawSql ?: $this->getRawSql(); |
|
103
|
7 |
|
$e = (new ConvertException($e, $rawSql))->run(); |
|
104
|
|
|
|
|
105
|
7 |
|
if ($this->retryHandler === null || !($this->retryHandler)($e, $attempt)) { |
|
106
|
7 |
|
throw $e; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
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