Files
kiri-core/kiri-engine/Pool/Connection.php
T

218 lines
4.0 KiB
PHP
Raw Normal View History

2022-01-09 03:50:38 +08:00
<?php
declare(strict_types=1);
namespace Kiri\Pool;
use Closure;
2022-02-25 18:37:49 +08:00
use Database\Db;
2022-01-09 03:50:38 +08:00
use Database\Mysql\PDO;
use Exception;
2022-01-14 15:52:38 +08:00
use Kiri;
2022-01-09 03:50:38 +08:00
use Kiri\Abstracts\Component;
use Kiri\Abstracts\Config;
use Kiri\Context;
use Swoole\Error;
use Throwable;
/**
* Class Connection
* @package Kiri\Pool
*/
class Connection extends Component
{
2022-02-24 15:24:29 +08:00
/**
2022-03-03 17:25:47 +08:00
* @param Pool $pool
* @param array $config
* @throws Exception
2022-02-24 15:24:29 +08:00
*/
2022-03-03 17:25:47 +08:00
public function __construct(public Pool $pool, array $config = [])
2022-02-24 15:24:29 +08:00
{
2022-03-03 17:25:47 +08:00
parent::__construct($config);
2022-02-24 15:24:29 +08:00
}
2022-01-09 03:50:38 +08:00
/**
2022-02-24 13:51:44 +08:00
* @param $name
2022-01-09 03:50:38 +08:00
* @return bool
*/
2022-02-24 11:00:01 +08:00
public function inTransaction($name): bool
2022-01-09 03:50:38 +08:00
{
$connection = Context::getContext($name);
if ($connection instanceof PDO) {
return $connection->inTransaction();
}
return false;
}
2022-02-24 13:51:44 +08:00
2022-01-09 03:50:38 +08:00
/**
* @param $coroutineName
* @throws Exception
*/
public function beginTransaction($coroutineName)
{
2022-02-25 18:14:22 +08:00
$connection = $this->get($coroutineName);
2022-01-09 03:50:38 +08:00
if ($connection instanceof PDO) {
$connection->beginTransaction();
}
}
/**
* @param $coroutineName
* @throws Exception
*/
public function commit($coroutineName)
{
$connection = Context::getContext($coroutineName);
if ($connection instanceof PDO) {
$connection->commit();
}
}
/**
* @param $coroutineName
* @throws Exception
*/
public function rollback($coroutineName)
{
$connection = Context::getContext($coroutineName);
if ($connection instanceof PDO) {
$connection->rollBack();
}
}
/**
* @param mixed $config
* @return PDO|null
* @throws Exception
*/
2022-02-24 11:00:01 +08:00
public function get(mixed $config): ?PDO
2022-01-09 03:50:38 +08:00
{
2022-02-24 11:00:01 +08:00
$coroutineName = $config['cds'];
2022-01-09 03:50:38 +08:00
$minx = Config::get('databases.pool.min', 1);
2022-06-08 14:08:32 +08:00
return $this->pool->get($coroutineName, static function () use ($coroutineName, $config) {
$connect = Kiri::getDi()->create(PDO::class, [$config]);
if (!Db::inTransactionsActive()) {
return $connect;
}
if (!$connect->inTransaction()) {
$connect->beginTransaction();
}
return $connect;
2022-02-25 18:30:59 +08:00
}, $minx);
2022-01-09 03:50:38 +08:00
}
/**
* @param $coroutineName
* @param $config
* @return Closure
*/
public function create($coroutineName, $config): Closure
{
return static function () use ($coroutineName, $config) {
return Kiri::getDi()->create(PDO::class, [$config]);
};
}
2022-01-14 16:05:12 +08:00
/**
* @param string $name
* @param PDO $PDO
* @return void
* @throws Kiri\Exception\ConfigException
* @throws Exception
*/
public function addItem(string $name, PDO $PDO)
{
2022-02-24 15:24:29 +08:00
$this->pool->push($name, $PDO);
2022-01-14 16:05:12 +08:00
}
2022-01-09 03:50:38 +08:00
/**
* @param $name
* @param $max
* @throws Exception
*/
2022-02-24 11:00:01 +08:00
public function initConnections($name, $max)
2022-01-09 03:50:38 +08:00
{
2022-02-24 15:24:29 +08:00
$this->pool->initConnections($name, $max);
2022-01-09 03:50:38 +08:00
}
/**
* @param $coroutineName
2022-01-14 16:50:01 +08:00
* @throws Kiri\Exception\ConfigException
2022-01-09 03:50:38 +08:00
* @throws Exception
*/
2022-02-24 11:00:01 +08:00
public function release($coroutineName)
2022-01-09 03:50:38 +08:00
{
2022-02-18 14:45:59 +08:00
$client = Context::getContext($coroutineName);
if (!($client instanceof PDO) || $client->inTransaction()) {
2022-01-09 03:50:38 +08:00
return;
}
2022-02-18 14:45:59 +08:00
2022-02-24 15:24:29 +08:00
$this->pool->push($coroutineName, $client);
2022-01-09 03:50:38 +08:00
Context::remove($coroutineName);
}
/**
* @param $coroutineName
* @return bool
*/
private function hasClient($coroutineName): bool
{
return Context::hasContext($coroutineName);
}
/**
* batch release
* @throws Exception
*/
2022-02-24 11:00:01 +08:00
public function connection_clear($name)
2022-01-09 03:50:38 +08:00
{
2022-02-24 15:24:29 +08:00
$this->pool->clean($name);
2022-01-09 03:50:38 +08:00
}
/**
* @param string $name
* @param mixed $client
* @return bool
* @throws Exception
*/
public function checkCanUse(string $name, mixed $client): bool
{
try {
if (empty($client) || !($client instanceof PDO)) {
$result = false;
} else {
$result = true;
}
2022-01-14 15:52:38 +08:00
} catch (Error|Throwable $exception) {
2022-02-23 16:32:08 +08:00
$result = $this->logger->addError($exception, 'mysql');
2022-01-09 03:50:38 +08:00
} finally {
return $result;
}
}
/**
* @param $coroutineName
* @throws Exception
*/
2022-02-24 11:00:01 +08:00
public function disconnect($coroutineName)
2022-01-09 03:50:38 +08:00
{
Context::remove($coroutineName);
2022-02-24 15:24:29 +08:00
$this->pool->clean($coroutineName);
2022-01-09 03:50:38 +08:00
}
}