Files
kiri-databases/Connection.php
T

314 lines
6.6 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;
2022-01-09 03:49:51 +08:00
use Kiri\Abstracts\Component;
use Kiri\Abstracts\Config;
2022-12-12 17:31:12 +08:00
use Kiri\Di\ContainerInterface;
use Kiri\Di\Context;
2023-04-06 22:39:31 +08:00
use Kiri\Annotation\Inject;
use Kiri\Pool\Pool;
2022-03-03 18:30:59 +08:00
use Kiri\Events\EventProvider;
2023-04-02 23:31:33 +08:00
use Kiri\Exception\ConfigException;
2022-01-09 03:49:51 +08:00
use Kiri\Exception\NotFindClassException;
2022-12-12 17:31:12 +08:00
use Kiri\Pool\Connection as PoolConnection;
2022-01-10 11:39:56 +08:00
use Kiri\Server\Events\OnWorkerExit;
2022-12-12 17:31:12 +08:00
use PDO;
2022-06-17 11:59:19 +08:00
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
2022-01-14 15:52:38 +08:00
use ReflectionException;
2022-01-09 03:49:51 +08:00
/**
* Class Connection
* @package Database
*/
class Connection extends Component
{
2023-04-06 22:39:31 +08:00
2022-01-09 03:49:51 +08:00
public string $id = 'db';
public string $cds = '';
public string $password = '';
public string $username = '';
public string $charset = 'utf-8';
2023-04-06 22:39:31 +08:00
2022-01-09 03:49:51 +08:00
public string $tablePrefix = '';
2023-04-06 22:39:31 +08:00
2022-01-09 03:49:51 +08:00
public string $database = '';
2023-04-06 22:39:31 +08:00
2022-01-09 03:49:51 +08:00
public int $connect_timeout = 30;
2023-04-06 22:39:31 +08:00
2022-01-09 03:49:51 +08:00
public int $read_timeout = 10;
2023-04-06 22:39:31 +08:00
2023-04-03 13:45:59 +08:00
public array $pool = ['max' => 10, 'min' => 1];
2023-04-06 22:39:31 +08:00
2022-01-09 03:49:51 +08:00
/**
* @var bool
* enable database cache
*/
public bool $enableCache = false;
2023-04-06 22:39:31 +08:00
2022-06-08 16:13:16 +08:00
private ?PDO $_pdo = null;
2023-04-06 22:39:31 +08:00
2022-01-09 03:49:51 +08:00
/**
* @var string
*/
public string $cacheDriver = 'redis';
2023-04-06 22:39:31 +08:00
2022-01-09 03:49:51 +08:00
/**
* @var array
*/
public array $slaveConfig = [];
public array $attributes = [];
2023-04-06 22:39:31 +08:00
2022-01-09 03:49:51 +08:00
private ?Schema $_schema = null;
2023-04-06 22:39:31 +08:00
2022-03-03 18:30:59 +08:00
/**
* @param EventProvider $eventProvider
2023-04-06 22:39:31 +08:00
* @param Pool $connections
* @param ContainerInterface $container
2022-03-03 18:30:59 +08:00
* @param array $config
* @throws Exception
*/
2023-04-06 22:39:31 +08:00
public function __construct(public EventProvider $eventProvider, public Pool $connections, public ContainerInterface $container, array $config = [])
2022-03-03 18:30:59 +08:00
{
parent::__construct($config);
}
2023-04-06 22:39:31 +08:00
2022-01-09 03:49:51 +08:00
/**
2022-02-10 11:46:02 +08:00
* @return void
2022-02-18 15:28:29 +08:00
* @throws Exception
2022-01-09 03:49:51 +08:00
*/
2022-06-17 11:59:19 +08:00
public function init(): void
2022-01-09 03:49:51 +08:00
{
2022-06-22 19:16:07 +08:00
$this->eventProvider->on(OnWorkerExit::class, [$this, 'clear_connection'], 9999);
2022-03-03 18:30:59 +08:00
$this->eventProvider->on(BeginTransaction::class, [$this, 'beginTransaction'], 0);
$this->eventProvider->on(Rollback::class, [$this, 'rollback'], 0);
$this->eventProvider->on(Commit::class, [$this, 'commit'], 0);
2023-04-06 22:47:34 +08:00
$this->initConnections();
2022-01-09 03:49:51 +08:00
}
2023-04-06 22:39:31 +08:00
2022-01-09 03:49:51 +08:00
/**
2023-04-06 22:39:31 +08:00
* @return void
2022-01-09 03:49:51 +08:00
*/
2023-04-06 22:39:31 +08:00
public function initConnections(): void
2022-01-09 03:49:51 +08:00
{
2023-04-06 22:39:31 +08:00
$this->connections->initConnections($this->cds, $this->pool['max'] ?? 1, $this->gender([
2023-04-03 13:45:59 +08:00
'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
2023-04-06 22:39:31 +08:00
]));
2022-01-14 16:23:15 +08:00
}
2023-04-06 22:39:31 +08:00
/**
* @param array $config
* @return \Closure
*/
public function gender(array $config): \Closure
{
return static function () use ($config) {
$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 => false,
PDO::ATTR_TIMEOUT => $config['connect_timeout'],
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES ' . ($config['charset'] ?? 'utf8mb4')
];
if (!Context::inCoroutine()) {
$options[PDO::ATTR_PERSISTENT] = true;
}
$link = new PDO('mysql:dbname=' . $config['dbname'] . ';host=' . $config['cds'],
$config['username'], $config['password'], $options);
foreach ($config['attributes'] as $key => $attribute) {
$link->setAttribute($key, $attribute);
}
return $link;
};
}
2022-01-09 03:49:51 +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;
}
2023-04-06 22:39:31 +08:00
2022-01-09 03:49:51 +08:00
/**
2023-04-05 10:50:17 +08:00
* @return PDO
2023-04-05 10:26:28 +08:00
* @throws Exception
2022-01-09 03:49:51 +08:00
*/
2023-04-06 22:43:43 +08:00
public function getConnection(): PDO
2022-01-09 03:49:51 +08:00
{
2023-04-06 22:39:31 +08:00
$client = $this->connections->get($this->cds);
if ($client === false) {
throw new Exception('waite db client timeout.');
2023-04-05 10:26:28 +08:00
}
2023-04-06 22:39:31 +08:00
return $client;
2022-01-09 03:49:51 +08:00
}
2023-04-06 22:39:31 +08:00
2022-01-09 03:49:51 +08:00
/**
2023-04-05 10:50:17 +08:00
* @return PDO
2022-01-09 03:49:51 +08:00
* @throws Exception
*/
2023-04-05 10:50:17 +08:00
public function getSlaveClient(): PDO
2022-01-09 03:49:51 +08:00
{
2023-04-06 22:39:31 +08:00
$client = $this->connections->get($this->cds);
2023-04-05 10:26:28 +08:00
if ($client === false) {
throw new Exception('waite db client timeout.');
}
return $client;
2022-01-09 03:49:51 +08:00
}
2023-04-06 22:39:31 +08:00
2022-01-09 03:49:51 +08:00
/**
* @return $this
* @throws Exception
*/
public function beginTransaction(): static
{
2023-04-06 22:54:50 +08:00
$pdo = $this->getTransactionClient();
$pdo->beginTransaction();
return $this;
}
/**
* @return PDO
* @throws Exception
*/
public function getTransactionClient(): PDO
{
if (!Db::inTransactionsActive()) {
return $this->getConnection();
}
2023-04-06 22:39:31 +08:00
$pdo = Context::get($this->cds);
if ($pdo === null) {
2023-04-06 22:54:50 +08:00
$pdo = Context::set($this->cds, $this->getConnection());
2023-04-06 22:39:31 +08:00
}
2023-04-06 22:54:50 +08:00
return $pdo;
2022-01-09 03:49:51 +08:00
}
2023-04-06 22:39:31 +08:00
2022-01-09 03:49:51 +08:00
/**
* @return $this|bool
* @throws Exception
*/
public function inTransaction(): bool|static
{
2023-04-06 22:54:50 +08:00
$pdo = $this->getTransactionClient();
return $pdo->inTransaction();
2022-01-09 03:49:51 +08:00
}
2023-04-06 22:39:31 +08:00
2022-01-09 03:49:51 +08:00
/**
* @throws Exception
* 事务回滚
*/
public function rollback()
{
2023-04-06 22:54:50 +08:00
$pdo = $this->getTransactionClient();
2022-09-20 18:15:50 +08:00
if ($pdo->inTransaction()) {
$pdo->rollback();
2022-02-25 15:39:08 +08:00
}
2023-04-06 22:54:50 +08:00
$this->connections->push($this->cds, $pdo);
Context::remove($this->cds);
2022-01-09 03:49:51 +08:00
}
2023-04-06 22:39:31 +08:00
2022-01-09 03:49:51 +08:00
/**
* @throws Exception
* 事务提交
*/
public function commit()
{
2023-04-06 22:54:50 +08:00
$pdo = $this->getTransactionClient();
2022-09-20 18:15:50 +08:00
if ($pdo->inTransaction()) {
$pdo->commit();
2022-02-25 15:39:08 +08:00
}
2023-04-06 22:54:50 +08:00
$this->connections->push($this->cds, $pdo);
Context::remove($this->cds);
2022-01-09 03:49:51 +08:00
}
2023-04-06 22:39:31 +08:00
2022-01-09 03:49:51 +08:00
/**
* @param null $sql
* @param array $attributes
* @return Command
* @throws Exception
*/
public function createCommand($sql = null, array $attributes = []): Command
{
2023-04-06 22:45:34 +08:00
$command = new Command(['connection' => $this, 'sql' => $sql]);
2022-01-09 03:49:51 +08:00
return $command->bindValues($attributes);
}
2023-04-06 22:39:31 +08:00
2022-01-09 03:49:51 +08:00
/**
*
* 回收链接
* @throws
*/
2023-04-05 10:50:17 +08:00
public function release(PDO $PDO)
2022-01-09 03:49:51 +08:00
{
2023-04-06 22:54:50 +08:00
$this->connections->push($this->cds, $PDO);
2023-02-13 17:00:32 +08:00
}
2023-04-06 22:39:31 +08:00
2022-01-09 03:49:51 +08:00
/**
*
* 回收链接
* @throws
*/
public function clear_connection()
{
2023-04-06 22:39:31 +08:00
$this->connections->clean($this->cds);
2022-01-09 03:49:51 +08:00
}
2023-04-06 22:39:31 +08:00
2022-01-09 03:49:51 +08:00
/**
* @throws Exception
*/
public function disconnect()
{
2023-04-06 22:39:31 +08:00
$this->connections->clean($this->cds);
2022-01-09 03:49:51 +08:00
}
2023-04-06 22:39:31 +08:00
2022-01-09 03:49:51 +08:00
}