Files
kiri-databases/Connection.php
T

365 lines
8.1 KiB
PHP
Raw Normal View History

2022-01-09 03:49:51 +08:00
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/3/30 0030
* Time: 14:09
*/
declare(strict_types=1);
namespace Database;
use Database\Affair\BeginTransaction;
use Database\Affair\Commit;
use Database\Affair\Rollback;
use Database\Mysql\Schema;
use Exception;
2022-01-14 15:52:38 +08:00
use Kiri;
2023-08-16 12:00:10 +08:00
use Kiri\Waite;
2022-01-09 03:49:51 +08:00
use Kiri\Abstracts\Component;
2022-12-12 17:31:12 +08:00
use Kiri\Di\Context;
2023-04-06 22:39:31 +08:00
use Kiri\Pool\Pool;
2022-03-03 18:30:59 +08:00
use Kiri\Events\EventProvider;
2022-01-09 03:49:51 +08:00
use Kiri\Exception\NotFindClassException;
2023-08-16 12:15:59 +08:00
use Monolog\Logger;
2022-12-12 17:31:12 +08:00
use PDO;
2023-08-16 12:00:10 +08:00
use Psr\Log\LoggerInterface;
2022-01-14 15:52:38 +08:00
use ReflectionException;
2023-08-16 01:01:47 +08:00
use Kiri\Server\Events\OnAfterRequest;
2023-08-16 12:15:59 +08:00
use Kiri\Di\Inject\Container;
2022-01-09 03:49:51 +08:00
/**
* Class Connection
* @package Database
*/
class Connection extends Component
{
2023-05-26 09:20:29 +08:00
public string $id = 'db';
public string $cds = '';
public string $password = '';
public string $username = '';
public string $charset = 'utf-8';
public string $tablePrefix = '';
public string $database = '';
public int $connect_timeout = 30;
public int $read_timeout = 10;
public array $pool = ['max' => 10, 'min' => 1];
private int $storey = 0;
/**
* @var bool
* enable database cache
*/
public bool $enableCache = false;
private ?PDO $_pdo = null;
/**
* @var string
*/
public string $cacheDriver = 'redis';
/**
* @var array
*/
public array $slaveConfig = [];
public array $attributes = [];
private ?Schema $_schema = null;
2023-08-16 12:15:59 +08:00
/**
* @var Logger
*/
#[Container(LoggerInterface::class)]
protected Logger $logger;
2023-08-16 00:39:55 +08:00
/**
* @param Pool $connections
*/
2023-08-16 12:15:59 +08:00
public function __construct(public Pool $connections)
2023-08-16 00:39:55 +08:00
{
parent::__construct();
}
2023-05-26 09:20:29 +08:00
/**
* @return void
* @throws Exception
*/
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);
2023-08-16 01:01:47 +08:00
$eventProvider->on(OnAfterRequest::class, [$this, 'clear']);
2023-05-26 09:20:29 +08:00
}
/**
* @return mixed
* @throws ReflectionException
* @throws NotFindClassException
* @throws Exception
*/
public function getSchema(): Schema
{
if ($this->_schema === null) {
$this->_schema = Kiri::createObject([
'class' => Schema::class,
'db' => $this
]);
}
return $this->_schema;
}
/**
* @return PDO
2023-08-16 00:39:55 +08:00
* @throws Exception
2023-05-26 09:20:29 +08:00
*/
public function getConnection(): PDO
{
2023-08-16 00:55:05 +08:00
if (!$this->inTransaction()) {
2023-08-16 12:00:10 +08:00
return $this->checkClientHealth();
2023-08-16 00:55:05 +08:00
} else {
return $this->getTransactionClient();
}
2023-05-26 09:20:29 +08:00
}
2023-08-16 12:00:10 +08:00
/**
* @return PDO
* @throws Exception
*/
protected function checkClientHealth(): PDO
{
/** @var PDO $client */
$client = $this->pool()->get($this->cds);
2023-08-16 12:19:47 +08:00
if ($this->canUse($client)) {
return $client;
2023-08-16 12:00:10 +08:00
}
Waite::sleep(10);
return $this->checkClientHealth();
}
2023-05-26 09:20:29 +08:00
/**
* @return $this
* @throws Exception
*/
public function beginTransaction(): static
{
if ($this->storey == 0) {
/** @var PDO $pdo */
$pdo = Context::get($this->cds);
2023-08-16 00:55:05 +08:00
if ($pdo instanceof PDO && !$pdo->inTransaction()) {
2023-05-26 09:20:29 +08:00
$pdo->beginTransaction();
}
}
$this->storey++;
return $this;
}
2023-08-16 12:19:47 +08:00
/**
* @param PDO|null $client
* @return bool
*/
protected function canUse(?PDO $client): bool
{
if (is_null($client)) {
return false;
}
try {
$steam = $client->query('select 1');
if ($steam !== false) {
return true;
}
return false;
} catch (\Throwable $exception) {
$this->logger->error($exception);
return false;
}
}
2023-05-26 09:20:29 +08:00
/**
* @return PDO
* @throws Exception
*/
public function getTransactionClient(): PDO
{
$pdo = Context::get($this->cds);
if ($pdo === null) {
$pdo = $this->getConnection();
if ($this->storey > 0 && !$pdo->inTransaction()) {
$pdo->beginTransaction();
}
Context::set($this->cds, $pdo);
}
return $pdo;
}
/**
* @return bool
* @throws Exception
*/
public function inTransaction(): bool
{
return $this->storey > 0;
}
/**
* @throws Exception
* 事务回滚
*/
public function rollback(): void
{
$this->storey--;
if ($this->storey == 0) {
2023-08-16 00:57:33 +08:00
$this->clear();
2023-05-26 09:20:29 +08:00
}
}
/**
* @throws Exception
* 事务提交
*/
public function commit(): void
{
$this->storey--;
if ($this->storey == 0) {
$pdo = Context::get($this->cds);
if ($pdo === null) {
return;
}
if ($pdo->inTransaction()) {
$pdo->commit();
}
$this->pool()->push($this->cds, $pdo);
Context::remove($this->cds);
}
}
2023-08-16 00:57:33 +08:00
/**
* @return void
* @throws Exception
*/
public function clear(): void
{
/** @var PDO $pdo */
$pdo = Context::get($this->cds);
if ($pdo === null) {
return;
}
if ($pdo->inTransaction()) {
$pdo->rollback();
}
$this->pool()->push($this->cds, $pdo);
Context::remove($this->cds);
}
2023-05-26 09:20:29 +08:00
/**
* @param null $sql
* @param array $attributes
* @return Command
* @throws Exception
*/
public function createCommand($sql = null, array $attributes = []): Command
{
$command = new Command(['connection' => $this, 'sql' => $sql]);
return $command->bindValues($attributes);
}
/**
* 回收链接
* @throws
*/
2023-07-20 15:01:24 +08:00
public function release(?PDO $PDO): void
2023-05-26 09:20:29 +08:00
{
2023-08-16 00:55:05 +08:00
if ($PDO === null || $PDO->inTransaction()) {
return;
}
2023-05-26 09:20:29 +08:00
$this->pool()->push($this->cds, $PDO);
}
/**
*
* 回收链接
* @throws
*/
public function clear_connection(): void
{
2023-08-16 00:55:05 +08:00
$this->pool()->flush($this->cds, 0);
2023-05-26 09:20:29 +08:00
}
/**
* @throws Exception
*/
public function disconnect(): void
{
2023-08-16 00:39:55 +08:00
$this->pool()->close($this->cds);
2023-05-26 09:20:29 +08:00
}
2023-08-14 14:14:40 +08:00
/**
* @return PDO
*/
public function newConnect(): PDO
{
$options = [
PDO::ATTR_CASE => PDO::CASE_NATURAL,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL,
PDO::ATTR_STRINGIFY_FETCHES => false,
PDO::ATTR_EMULATE_PREPARES => true,
PDO::ATTR_TIMEOUT => $this->connect_timeout,
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES ' . $this->charset
];
if (!Context::inCoroutine()) {
$options[PDO::ATTR_PERSISTENT] = true;
} else {
$options[PDO::ATTR_PERSISTENT] = false;
}
$link = new PDO('mysql:dbname=' . $this->database . ';host=' . $this->cds,
$this->username, $this->password, $options);
foreach ($this->attributes as $key => $attribute) {
$link->setAttribute($key, $attribute);
}
return $link;
}
2023-05-26 09:20:29 +08:00
/**
* @return Pool
*/
private function pool(): Pool
{
if (!$this->connections->hasChannel($this->cds)) {
2023-08-14 14:14:40 +08:00
$this->connections->created($this->cds, $this->pool['max'] ?? 1, [$this, 'newConnect']);
2023-05-26 09:20:29 +08:00
}
return $this->connections;
}
2022-01-09 03:49:51 +08:00
}