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