This commit is contained in:
xl
2023-05-26 09:20:29 +08:00
parent 49742a08ad
commit c8159194bf
3 changed files with 284 additions and 333 deletions
+53 -46
View File
@@ -61,6 +61,9 @@ class Connection extends Component
private ?PDO $_pdo = null;
private Pool $connections;
/**
* @var string
*/
@@ -82,33 +85,12 @@ class Connection extends Component
*/
public function init(): void
{
$eventProvider = Kiri::getDi()->get(EventProvider::class);
$eventProvider->on(BeginTransaction::class, [$this, 'beginTransaction'], 0);
$eventProvider->on(Rollback::class, [$this, 'rollback'], 0);
$eventProvider->on(Commit::class, [$this, 'commit'], 0);
$this->initConnections();
}
/**
* @return void
* @throws ReflectionException
*/
public function initConnections(): void
{
$connections = Kiri::getDi()->get(Pool::class);
$connections->initConnections($this->cds, $this->pool['max'] ?? 1, $this->gender([
'cds' => $this->cds,
'username' => $this->username,
'password' => $this->password,
'attributes' => $this->attributes,
'connect_timeout' => $this->connect_timeout,
'read_timeout' => $this->read_timeout,
'dbname' => $this->database,
'pool' => $this->pool
]));
$this->connections = Kiri::getDi()->get(Pool::class);
}
@@ -165,8 +147,7 @@ class Connection extends Component
*/
public function getConnection(): PDO
{
$connections = Kiri::getDi()->get(Pool::class);
return $connections->get($this->cds);
return $this->pool()->get($this->cds);
}
@@ -176,10 +157,13 @@ class Connection extends Component
*/
public function beginTransaction(): static
{
$pdo = $this->getTransactionClient();
if ($this->storey == 0) {
/** @var PDO $pdo */
$pdo = Context::get($this->cds);
if ($pdo !== null && !$pdo->inTransaction()) {
$pdo->beginTransaction();
}
}
$this->storey++;
return $this;
}
@@ -191,25 +175,24 @@ class Connection extends Component
*/
public function getTransactionClient(): PDO
{
if (!Db::inTransactionsActive()) {
return $this->getConnection();
}
$pdo = Context::get($this->cds);
if ($pdo === null) {
/** @var PDO $pdo */
$pdo = Context::set($this->cds, $this->getConnection());
$pdo = $this->getConnection();
if ($this->storey > 0 && !$pdo->inTransaction()) {
$pdo->beginTransaction();
}
Context::set($this->cds, $pdo);
}
return $pdo;
}
/**
* @return $this|bool
* @return bool
* @throws Exception
*/
public function inTransaction(): bool|static
public function inTransaction(): bool
{
$pdo = $this->getTransactionClient();
return $pdo->inTransaction();
return $this->storey > 0;
}
/**
@@ -220,12 +203,14 @@ class Connection extends Component
{
$this->storey--;
if ($this->storey == 0) {
$pdo = $this->getTransactionClient();
$pdo = Context::get($this->cds);
if ($pdo === null) {
return;
}
if ($pdo->inTransaction()) {
$pdo->rollback();
}
$connections = Kiri::getDi()->get(Pool::class);
$connections->push($this->cds, $pdo);
$this->pool()->push($this->cds, $pdo);
Context::remove($this->cds);
}
}
@@ -238,12 +223,14 @@ class Connection extends Component
{
$this->storey--;
if ($this->storey == 0) {
$pdo = $this->getTransactionClient();
$pdo = Context::get($this->cds);
if ($pdo === null) {
return;
}
if ($pdo->inTransaction()) {
$pdo->commit();
}
$connections = Kiri::getDi()->get(Pool::class);
$connections->push($this->cds, $pdo);
$this->pool()->push($this->cds, $pdo);
Context::remove($this->cds);
}
}
@@ -269,8 +256,7 @@ class Connection extends Component
*/
public function release(PDO $PDO): void
{
$connections = Kiri::getDi()->get(Pool::class);
$connections->push($this->cds, $PDO);
$this->pool()->push($this->cds, $PDO);
}
@@ -281,8 +267,7 @@ class Connection extends Component
*/
public function clear_connection(): void
{
$connections = Kiri::getDi()->get(Pool::class);
$connections->clean($this->cds);
$this->pool()->clean($this->cds);
}
@@ -291,8 +276,30 @@ class Connection extends Component
*/
public function disconnect(): void
{
$connections = Kiri::getDi()->get(Pool::class);
$connections->clean($this->cds);
$this->pool()->clean($this->cds);
}
/**
* @return Pool
*/
private function pool(): Pool
{
if (!$this->connections->hasChannel($this->cds)) {
$params = [
'cds' => $this->cds,
'username' => $this->username,
'password' => $this->password,
'attributes' => $this->attributes,
'connect_timeout' => $this->connect_timeout,
'read_timeout' => $this->read_timeout,
'dbname' => $this->database,
'pool' => $this->pool
];
$itemCount = $this->pool['max'] ?? 1;
$this->connections->created($this->cds, $itemCount, $this->gender($params));
}
return $this->connections;
}
}
+2 -40
View File
@@ -7,8 +7,6 @@ namespace Database;
use Exception;
use Kiri;
use Kiri\Abstracts\Providers;
use Kiri\Config\ConfigProvider;
use Swoole\Timer;
use Kiri\Di\LocalService;
/**
@@ -22,7 +20,7 @@ class DatabasesProviders extends Providers
/**
* @param LocalService $application
* @return void
* @throws \ReflectionException
* @throws Exception
*/
public function onImport(LocalService $application): void
{
@@ -34,43 +32,7 @@ class DatabasesProviders extends Providers
return;
}
foreach ($databases as $key => $database) {
$application->set('db.' . $key, $this->_settings($database));
}
}
public function start(): void
{
if (!Kiri\Di\Context::inCoroutine()) {
return;
}
Timer::tick(60000, function () {
$databases = \config('databases.connections', []);
if (empty($databases)) {
return;
}
$connection = Kiri::getDi()->get(Kiri\Pool\Pool::class);
foreach ($databases as $database) {
$connection->flush($database['cds'], $database['pool']['min'] ?? 1);
}
});
}
/**
* @return void
* @throws Exception
*/
public function exit(): void
{
Timer::clearAll();
$databases = \config('databases.connections', []);
if (!empty($databases)) {
$connection = Kiri::getDi()->get(Kiri\Pool\Pool::class);
foreach ($databases as $database) {
$connection->clean($database['cds']);
}
$application->set('db.' . $key, Kiri::createObject($this->_settings($database)));
}
}
+7 -25
View File
@@ -10,16 +10,14 @@ declare(strict_types=1);
namespace Database;
use Closure;
use Database\Affair\BeginTransaction;
use Database\Affair\Commit;
use Database\Affair\Rollback;
use Database\Traits\QueryTrait;
use Exception;
use Kiri\Di\Context;
use Kiri\Events\EventDispatch;
use Kiri\Exception\ConfigException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use ReflectionException;
/**
* Class Db
@@ -32,21 +30,12 @@ class Db implements ISqlBuilder
private static bool $_inTransaction = false;
/**
* @return bool
*/
public static function inTransactionsActive(): bool
{
return Context::exists('transactions::status') && Context::get('transactions::status') === true;
}
/**
* @return void
*/
public static function beginTransaction(): void
{
Context::set('transactions::status', true);
fire(new BeginTransaction());
}
@@ -78,27 +67,20 @@ class Db implements ISqlBuilder
/**
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @return void
*/
public static function commit(): void
{
$event = \Kiri::getDi()->get(EventDispatch::class);
$event->dispatch(new Commit());
Context::remove('transactions::status');
fire(new Commit());
}
/**
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public static function rollback(): void
{
$event = \Kiri::getDi()->get(EventDispatch::class);
$event->dispatch(new Rollback());
Context::remove('transactions::status');
fire(new Rollback());
}
@@ -173,10 +155,10 @@ class Db implements ISqlBuilder
/**
* @param Connection|null $connection
* @return mixed
* @return array|bool|null
* @throws Exception
*/
public function find(Connection $connection = NULL): mixed
public function find(Connection $connection = NULL): array|bool|null
{
$connection = static::getDefaultConnection($connection);