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