Files
kiri-core/kiri-engine/Pool/Connection.php
T
2022-02-23 16:32:08 +08:00

262 lines
5.5 KiB
PHP

<?php
declare(strict_types=1);
namespace Kiri\Pool;
use Closure;
use Database\Mysql\PDO;
use Exception;
use Kiri;
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
{
use Alias;
/**
* @param $cds
* @return bool
*
* db is in transaction
* @throws Exception
*/
public function inTransaction($cds): bool
{
$name = $this->name('Mysql:' . $cds, true);
$connection = Context::getContext($name);
if ($connection instanceof PDO) {
return $connection->inTransaction();
}
return false;
}
/**
* @param $coroutineName
* @throws Exception
*/
public function beginTransaction($coroutineName)
{
$coroutineName = $this->name('Mysql:' . $coroutineName, true);
$connection = Context::getContext($coroutineName);
if ($connection instanceof PDO) {
$connection->beginTransaction();
}
}
/**
* @param $coroutineName
* @throws Exception
*/
public function commit($coroutineName)
{
$coroutineName = $this->name('Mysql:' . $coroutineName, true);
$connection = Context::getContext($coroutineName);
if ($connection instanceof PDO) {
$connection->commit();
}
}
/**
* @param $coroutineName
* @throws Exception
*/
public function rollback($coroutineName)
{
$coroutineName = $this->name('Mysql:' . $coroutineName, true);
$connection = Context::getContext($coroutineName);
if ($connection instanceof PDO) {
$connection->rollBack();
}
}
/**
* @param mixed $config
* @param bool $isMaster
* @return PDO|null
* @throws Exception
*/
public function get(mixed $config, bool $isMaster = false): ?PDO
{
$coroutineName = $this->name('Mysql:' . $config['cds'], $isMaster);
if ($isMaster == false) {
return $this->_get($config, $coroutineName);
}
if (!(($pdo = Context::getContext($coroutineName)) instanceof PDO)) {
$pdo = Context::setContext($coroutineName, $this->_get($config, $coroutineName));
}
return $pdo;
}
/**
* @param mixed $config
* @param string $coroutineName
* @return PDO
* @throws Kiri\Exception\ConfigException
* @throws Exception
*/
private function _get(mixed $config, string $coroutineName): PDO
{
$minx = Config::get('databases.pool.min', 1);
/** @var PDO $connections */
$connections = $this->getPool()->get($coroutineName, $this->create($coroutineName, $config), $minx);
if (Context::hasContext('begin_' . $coroutineName)) {
$connections->beginTransaction();
}
return $connections;
}
/**
* @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]);
};
}
/**
* @param string $name
* @param PDO $PDO
* @return void
* @throws Kiri\Exception\ConfigException
* @throws Exception
*/
public function addItem(string $name, PDO $PDO)
{
$this->getPool()->push($name, $PDO);
}
/**
* @param $name
* @param $isMaster
* @param $max
* @throws Exception
*/
public function initConnections($name, $isMaster, $max)
{
$pool = $this->getPool();
$pool->initConnections($name, $isMaster, $max);
}
/**
* @param $coroutineName
* @param $isMaster
* @throws Kiri\Exception\ConfigException
* @throws Exception
*/
public function release($coroutineName, $isMaster)
{
$coroutineName = $this->name('Mysql:' . $coroutineName, $isMaster);
$client = Context::getContext($coroutineName);
if (!($client instanceof PDO) || $client->inTransaction()) {
return;
}
$this->getPool()->push($coroutineName, $client);
Context::remove($coroutineName);
}
/**
* @param $coroutineName
* @param PDO $PDO
* @param bool $isMaster
* @return void
* @throws Exception
* @throws Kiri\Exception\ConfigException
*/
public function recover($coroutineName, PDO $PDO, bool $isMaster = false)
{
$coroutineName = $this->name('Mysql:' . $coroutineName, $isMaster);
$this->getPool()->push($coroutineName, $PDO);
}
/**
* @param $coroutineName
* @return bool
*/
private function hasClient($coroutineName): bool
{
return Context::hasContext($coroutineName);
}
/**
* batch release
* @throws Exception
*/
public function connection_clear($name, $isMaster)
{
$this->getPool()->clean($this->name($name, $isMaster));
}
/**
* @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;
}
} catch (Error|Throwable $exception) {
$result = $this->logger->addError($exception, 'mysql');
} finally {
return $result;
}
}
/**
* @param $coroutineName
* @param bool $isMaster
* @throws Exception
*/
public function disconnect($coroutineName, bool $isMaster = false)
{
Context::remove($coroutineName);
$coroutineName = $this->name('Mysql:' . $coroutineName, $isMaster);
$this->getPool()->clean($coroutineName);
}
/**
* @return Pool
* @throws Exception
*/
public function getPool(): Pool
{
return Kiri::getDi()->get(Pool::class);
}
}