1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Mysql; |
6
|
|
|
|
7
|
|
|
use PDOException; |
8
|
|
|
use Throwable; |
9
|
|
|
use Yiisoft\Db\Driver\PDO\CommandPDO as AbstractCommandPDO; |
10
|
|
|
use Yiisoft\Db\Driver\PDO\ConnectionPDOInterface; |
11
|
|
|
use Yiisoft\Db\Exception\ConvertException; |
12
|
|
|
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface; |
13
|
|
|
|
14
|
|
|
final class CommandPDO extends AbstractCommandPDO |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @inheritDoc |
18
|
|
|
*/ |
19
|
1 |
|
public function insertEx(string $table, array $columns): bool|array |
20
|
|
|
{ |
21
|
1 |
|
$params = []; |
22
|
1 |
|
$sql = $this->db->getQueryBuilder()->insertEx($table, $columns, $params); |
23
|
1 |
|
$this->setSql($sql)->bindValues($params); |
24
|
|
|
|
25
|
1 |
|
if (!$this->execute()) { |
26
|
|
|
return false; |
27
|
|
|
} |
28
|
|
|
|
29
|
1 |
|
$tableSchema = $this->db->getSchema()->getTableSchema($table); |
30
|
1 |
|
$tablePrimaryKeys = $tableSchema?->getPrimaryKey() ?? []; |
31
|
|
|
|
32
|
1 |
|
$result = []; |
33
|
1 |
|
foreach ($tablePrimaryKeys as $name) { |
34
|
1 |
|
if ($tableSchema?->getColumn($name)?->isAutoIncrement()) { |
35
|
1 |
|
$result[$name] = $this->db->getLastInsertID((string) $tableSchema?->getSequenceName()); |
36
|
1 |
|
continue; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** @psalm-var mixed */ |
40
|
|
|
$result[$name] = $columns[$name] ?? $tableSchema?->getColumn($name)?->getDefaultValue(); |
41
|
|
|
} |
42
|
|
|
|
43
|
1 |
|
return $result; |
44
|
|
|
} |
45
|
|
|
|
46
|
221 |
|
public function queryBuilder(): QueryBuilderInterface |
47
|
|
|
{ |
48
|
221 |
|
return $this->db->getQueryBuilder(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @psalm-suppress UnusedClosureParam |
53
|
|
|
* |
54
|
|
|
* @throws Throwable |
55
|
|
|
*/ |
56
|
209 |
|
protected function internalExecute(string|null $rawSql): void |
57
|
|
|
{ |
58
|
209 |
|
$attempt = 0; |
59
|
|
|
|
60
|
209 |
|
while (true) { |
61
|
|
|
try { |
62
|
|
|
if ( |
63
|
|
|
++$attempt === 1 |
64
|
209 |
|
&& $this->isolationLevel !== null |
65
|
209 |
|
&& $this->db->getTransaction() === null |
66
|
|
|
) { |
67
|
1 |
|
$this->db->transaction( |
68
|
1 |
|
fn (ConnectionPDOInterface $db) => $this->internalExecute($rawSql), |
|
|
|
|
69
|
1 |
|
$this->isolationLevel, |
70
|
|
|
); |
71
|
|
|
} else { |
72
|
209 |
|
$this->pdoStatement?->execute(); |
73
|
|
|
} |
74
|
201 |
|
break; |
75
|
33 |
|
} catch (PDOException $e) { |
76
|
33 |
|
$rawSql = $rawSql ?: $this->getRawSql(); |
77
|
33 |
|
$e = (new ConvertException($e, $rawSql))->run(); |
78
|
|
|
|
79
|
33 |
|
if ($this->retryHandler === null || !($this->retryHandler)($e, $attempt)) { |
80
|
33 |
|
throw $e; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.