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

218 lines
4.3 KiB
PHP
Raw Normal View History

2020-08-31 01:27:08 +08:00
<?php
2020-10-29 18:17:25 +08:00
declare(strict_types=1);
2020-08-31 01:27:08 +08:00
2021-08-11 01:04:57 +08:00
namespace Kiri\Pool;
2020-08-31 01:27:08 +08:00
2021-08-09 11:12:01 +08:00
use Closure;
2021-08-16 18:24:32 +08:00
use Database\Mysql\PDO;
2021-04-23 18:37:18 +08:00
use Exception;
2021-08-11 01:04:57 +08:00
use Kiri\Abstracts\Component;
2021-10-28 18:46:14 +08:00
use Kiri\Abstracts\Config;
use Kiri\Context;
2021-08-11 01:04:57 +08:00
use Kiri\Kiri;
2021-03-08 18:45:32 +08:00
use Swoole\Error;
use Throwable;
2020-08-31 01:27:08 +08:00
/**
* Class Connection
2021-08-11 01:04:57 +08:00
* @package Kiri\Pool
2020-08-31 01:27:08 +08:00
*/
2021-07-05 15:42:44 +08:00
class Connection extends Component
2020-08-31 01:27:08 +08:00
{
2021-08-09 11:12:01 +08:00
use Alias;
/**
* @param $cds
* @return bool
*
* db is in transaction
* @throws Exception
*/
public function inTransaction($cds): bool
{
$name = $this->name('Mysql:' . $cds, true);
2021-08-16 18:31:41 +08:00
$connection = Context::getContext($name);
if ($connection instanceof PDO) {
2021-08-17 19:01:37 +08:00
return $connection->inTransaction();
2021-08-16 18:31:41 +08:00
}
return false;
2021-08-09 11:12:01 +08:00
}
/**
* @param $coroutineName
* @throws Exception
*/
public function beginTransaction($coroutineName)
{
$coroutineName = $this->name('Mysql:' . $coroutineName, true);
$connection = Context::getContext($coroutineName);
2021-08-16 18:11:18 +08:00
if ($connection instanceof PDO) {
2021-08-09 11:12:01 +08:00
$connection->beginTransaction();
}
}
/**
* @param $coroutineName
* @throws Exception
*/
public function commit($coroutineName)
{
$coroutineName = $this->name('Mysql:' . $coroutineName, true);
2021-08-16 18:11:18 +08:00
$connection = Context::getContext($coroutineName);
if ($connection instanceof PDO) {
$connection->commit();
2021-08-09 11:12:01 +08:00
}
}
/**
* @param $coroutineName
* @throws Exception
*/
public function rollback($coroutineName)
{
$coroutineName = $this->name('Mysql:' . $coroutineName, true);
2021-08-16 18:11:18 +08:00
$connection = Context::getContext($coroutineName);
if ($connection instanceof PDO) {
$connection->rollBack();
2021-08-09 11:12:01 +08:00
}
}
/**
* @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 (($pdo = Context::getContext($coroutineName)) instanceof PDO) {
return $pdo;
}
2021-10-28 18:46:14 +08:00
$minx = Config::get('databases.pool.min', 1);
2021-08-09 11:12:01 +08:00
/** @var PDO $connections */
2021-10-28 18:46:14 +08:00
$connections = $this->getPool()->get($coroutineName, $this->create($coroutineName, $config), $minx);
2021-08-09 11:12:01 +08:00
if (Context::hasContext('begin_' . $coroutineName)) {
$connections->beginTransaction();
}
return Context::setContext($coroutineName, $connections);
}
/**
* @param $coroutineName
* @param $config
* @return Closure
*/
public function create($coroutineName, $config): Closure
{
return static function () use ($coroutineName, $config) {
2021-12-06 16:14:48 +08:00
return Kiri::getDi()->create(PDO::class, [$config]);
2021-08-09 11:12:01 +08:00
};
}
/**
* @param $name
* @param $isMaster
* @param $max
* @throws Exception
*/
public function initConnections($name, $isMaster, $max)
{
$this->getPool()->initConnections($name, $isMaster, $max);
}
/**
* @param $coroutineName
* @param $isMaster
* @throws Exception
*/
public function release($coroutineName, $isMaster)
{
$coroutineName = $this->name('Mysql:' . $coroutineName, $isMaster);
/** @var PDO $client */
if (!($client = Context::getContext($coroutineName)) instanceof PDO) {
return;
}
if ($client->inTransaction()) {
return;
}
$this->getPool()->push($coroutineName, $client);
Context::remove($coroutineName);
}
/**
* @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->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
{
2021-08-11 01:04:57 +08:00
return Kiri::getDi()->get(Pool::class);
2021-08-09 11:12:01 +08:00
}
2021-03-01 16:01:32 +08:00
2020-08-31 01:27:08 +08:00
}