-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathAbstractConnection.php
More file actions
129 lines (107 loc) · 3.73 KB
/
AbstractConnection.php
File metadata and controls
129 lines (107 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
declare(strict_types=1);
namespace Yiisoft\Db\Connection;
use Closure;
use Throwable;
use Yiisoft\Db\Query\BatchQueryResult;
use Yiisoft\Db\Query\BatchQueryResultInterface;
use Yiisoft\Db\Query\QueryInterface;
use Yiisoft\Db\Schema\TableSchemaInterface;
use Yiisoft\Db\Transaction\TransactionInterface;
/**
* Represents a connection to a database.
*
* It provides methods for interacting with the database, such as executing SQL queries and performing data
* manipulation.
*/
abstract class AbstractConnection implements ConnectionInterface
{
protected TransactionInterface|null $transaction = null;
private bool $enableSavepoint = true;
private string $tablePrefix = '';
public function beginTransaction(?string $isolationLevel = null): TransactionInterface
{
$this->open();
$this->transaction = $this->getTransaction();
if ($this->transaction === null) {
$this->transaction = $this->createTransaction();
}
$this->transaction->begin($isolationLevel);
return $this->transaction;
}
public function createBatchQueryResult(QueryInterface $query): BatchQueryResultInterface
{
return new BatchQueryResult($query);
}
public function getTablePrefix(): string
{
return $this->tablePrefix;
}
public function getTableSchema(string $name, bool $refresh = false): TableSchemaInterface|null
{
return $this->getSchema()->getTableSchema($name, $refresh);
}
public function getTransaction(): TransactionInterface|null
{
return $this->transaction && $this->transaction->isActive() ? $this->transaction : null;
}
public function isSavepointEnabled(): bool
{
return $this->enableSavepoint;
}
public function setEnableSavepoint(bool $value): void
{
$this->enableSavepoint = $value;
}
public function setTablePrefix(string $value): void
{
$this->tablePrefix = $value;
}
public function transaction(Closure $closure, ?string $isolationLevel = null): mixed
{
$transaction = $this->beginTransaction($isolationLevel);
$level = $transaction->getLevel();
try {
/** @psalm-var mixed $result */
$result = $closure($this);
if ($transaction->isActive() && $transaction->getLevel() === $level) {
$transaction->commit();
}
} catch (Throwable $e) {
$this->rollbackTransactionOnLevel($transaction, $level);
throw $e;
}
return $result;
}
/**
* Rolls back given {@see TransactionInterface} an object if it's still active and level match.
*
* Sometimes, rollback can fail, so this method is fail-safe.
*
* @param TransactionInterface $transaction TransactionInterface object given from {@see beginTransaction()}.
* @param int $level TransactionInterface level just after {@see beginTransaction()} call.
*
* @throws Throwable If transaction wasn't rolled back.
*/
protected function rollbackTransactionOnLevel(TransactionInterface $transaction, int $level): void
{
if ($transaction->isActive() && $transaction->getLevel() === $level) {
/**
* @link https://github.com/yiisoft/yii2/pull/13347
*/
try {
$transaction->rollBack();
} catch (Throwable) {
/** hide this exception to be able to continue throwing original exception outside */
}
}
}
public function getParametersLimit(): int
{
// Must be overridden in a DBMS package which has a limit
if ($this->getDriverName() === 'pgsql') {
return 65535;
}
return 0;
}
}