|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Mssql; |
|
6
|
|
|
|
|
7
|
|
|
use PDOException; |
|
8
|
|
|
use Yiisoft\Db\Driver\PDO\CommandPDO as AbstractCommandPDO; |
|
9
|
|
|
use Yiisoft\Db\Exception\ConvertException; |
|
10
|
|
|
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface; |
|
11
|
|
|
use Yiisoft\Db\Schema\SchemaInterface; |
|
12
|
|
|
|
|
13
|
|
|
use function is_array; |
|
14
|
|
|
|
|
15
|
|
|
final class CommandPDO extends AbstractCommandPDO |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @inheritDoc |
|
19
|
|
|
*/ |
|
20
|
2 |
|
public function insertEx(string $table, array $columns): bool|array |
|
21
|
|
|
{ |
|
22
|
2 |
|
$params = []; |
|
23
|
2 |
|
$sql = $this->queryBuilder()->insertEx($table, $columns, $params); |
|
24
|
|
|
|
|
25
|
2 |
|
$this->setSql($sql)->bindValues($params); |
|
26
|
2 |
|
$this->prepare(false); |
|
27
|
|
|
|
|
28
|
|
|
/** @psalm-var array|bool */ |
|
29
|
2 |
|
$result = $this->queryOne(); |
|
30
|
|
|
|
|
31
|
2 |
|
return is_array($result) ? $result : false; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
223 |
|
public function queryBuilder(): QueryBuilderInterface |
|
35
|
|
|
{ |
|
36
|
223 |
|
return $this->db->getQueryBuilder(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function schema(): SchemaInterface |
|
40
|
|
|
{ |
|
41
|
|
|
return $this->db->getSchema(); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
212 |
|
protected function internalExecute(?string $rawSql): void |
|
45
|
|
|
{ |
|
46
|
212 |
|
$attempt = 0; |
|
47
|
|
|
|
|
48
|
212 |
|
while (true) { |
|
49
|
|
|
try { |
|
50
|
|
|
if ( |
|
51
|
212 |
|
++$attempt === 1 |
|
52
|
212 |
|
&& $this->isolationLevel !== null |
|
53
|
212 |
|
&& $this->db->getTransaction() === null |
|
54
|
|
|
) { |
|
55
|
|
|
$this->db->transaction( |
|
56
|
|
|
fn (string $rawSql) => $this->internalExecute($rawSql), |
|
57
|
|
|
$this->isolationLevel |
|
58
|
|
|
); |
|
59
|
|
|
} else { |
|
60
|
212 |
|
$this->pdoStatement?->execute(); |
|
61
|
|
|
} |
|
62
|
211 |
|
break; |
|
63
|
5 |
|
} catch (PDOException $e) { |
|
64
|
5 |
|
$rawSql = $rawSql ?: $this->getRawSql(); |
|
65
|
5 |
|
$e = (new ConvertException($e, $rawSql))->run(); |
|
66
|
|
|
|
|
67
|
5 |
|
if ($this->retryHandler === null || !($this->retryHandler)($e, $attempt)) { |
|
68
|
5 |
|
throw $e; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|