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;
|
2022-03-03 18:30:59 +08:00
|
|
|
use Kiri\Events\EventProvider;
|
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
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
public string $id = 'db';
|
|
|
|
|
public string $cds = '';
|
|
|
|
|
public string $password = '';
|
|
|
|
|
public string $username = '';
|
|
|
|
|
public string $charset = 'utf-8';
|
2022-02-23 17:08:35 +08:00
|
|
|
|
2022-01-09 03:49:51 +08:00
|
|
|
public string $tablePrefix = '';
|
|
|
|
|
|
|
|
|
|
public string $database = '';
|
|
|
|
|
|
|
|
|
|
public int $connect_timeout = 30;
|
|
|
|
|
|
|
|
|
|
public int $read_timeout = 10;
|
|
|
|
|
|
|
|
|
|
public array $pool;
|
|
|
|
|
|
2022-06-17 11:59:19 +08:00
|
|
|
|
|
|
|
|
private PoolConnection $connection;
|
|
|
|
|
|
2022-01-09 03:49:51 +08:00
|
|
|
/**
|
|
|
|
|
* @var bool
|
|
|
|
|
* enable database cache
|
|
|
|
|
*/
|
|
|
|
|
public bool $enableCache = false;
|
|
|
|
|
|
|
|
|
|
|
2022-06-08 16:13:16 +08:00
|
|
|
private ?PDO $_pdo = null;
|
|
|
|
|
|
|
|
|
|
|
2022-01-09 03:49:51 +08:00
|
|
|
/**
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
public string $cacheDriver = 'redis';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
public array $slaveConfig = [];
|
|
|
|
|
public array $attributes = [];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private ?Schema $_schema = null;
|
|
|
|
|
|
|
|
|
|
|
2022-03-03 18:30:59 +08:00
|
|
|
/**
|
|
|
|
|
* @param EventProvider $eventProvider
|
2022-06-17 11:59:19 +08:00
|
|
|
* @param Kiri\Di\ContainerInterface $container
|
2022-03-03 18:30:59 +08:00
|
|
|
* @param array $config
|
2022-06-17 11:59:19 +08:00
|
|
|
* @throws ContainerExceptionInterface
|
|
|
|
|
* @throws NotFoundExceptionInterface
|
2022-03-03 18:30:59 +08:00
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2022-06-17 11:59:19 +08:00
|
|
|
public function __construct(public EventProvider $eventProvider, public ContainerInterface $container, array $config = [])
|
2022-03-03 18:30:59 +08:00
|
|
|
{
|
|
|
|
|
parent::__construct($config);
|
2022-06-17 11:59:19 +08:00
|
|
|
|
|
|
|
|
$this->connection = $this->container->get(PoolConnection::class);
|
2022-03-03 18:30:59 +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);
|
2022-02-18 15:28:29 +08:00
|
|
|
|
|
|
|
|
$this->connectPoolInstance();
|
2022-01-09 03:49:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2022-02-18 15:28:29 +08:00
|
|
|
public function connectPoolInstance()
|
2022-01-09 03:49:51 +08:00
|
|
|
{
|
|
|
|
|
$pool = Config::get('databases.pool.max', 10);
|
|
|
|
|
if (!empty($this->slaveConfig) && $this->cds != $this->slaveConfig['cds']) {
|
2022-06-17 11:59:19 +08:00
|
|
|
$this->connection->initConnections('Mysql:' . $this->slaveConfig['cds'], $pool);
|
2022-06-08 14:43:23 +08:00
|
|
|
} else {
|
2022-06-17 11:59:19 +08:00
|
|
|
$this->connection->initConnections('Mysql:' . $this->cds, $pool);
|
2022-01-09 03:49:51 +08:00
|
|
|
}
|
2022-01-14 16:23:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return PDO
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2022-06-08 16:13:16 +08:00
|
|
|
public function getMasterClient(): PDO
|
2022-01-09 03:49:51 +08:00
|
|
|
{
|
2022-06-17 11:59:19 +08:00
|
|
|
return $this->connection->get([
|
2022-01-09 03:49:51 +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
|
2022-09-20 18:23:19 +08:00
|
|
|
], true);
|
2022-01-09 03:49:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return PDO
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2022-06-08 16:13:16 +08:00
|
|
|
public function getSlaveClient(): PDO
|
2022-01-09 03:49:51 +08:00
|
|
|
{
|
2022-09-20 18:23:19 +08:00
|
|
|
return $this->connection->get([
|
|
|
|
|
'cds' => $this->slaveConfig['cds'] ?? $this->cds,
|
|
|
|
|
'username' => $this->slaveConfig['username'] ?? $this->username,
|
|
|
|
|
'password' => $this->slaveConfig['password'] ?? $this->password,
|
|
|
|
|
'attributes' => $this->slaveConfig['attributes'] ?? $this->attributes,
|
|
|
|
|
'connect_timeout' => $this->connect_timeout,
|
|
|
|
|
'read_timeout' => $this->read_timeout,
|
|
|
|
|
'dbname' => $this->slaveConfig['database'] ?? $this->database,
|
|
|
|
|
'pool' => $this->pool
|
|
|
|
|
], false);
|
2022-01-09 03:49:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return $this
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function beginTransaction(): static
|
|
|
|
|
{
|
2022-06-09 10:05:37 +08:00
|
|
|
$pdo = $this->getPdo();
|
2022-02-25 18:16:50 +08:00
|
|
|
$pdo->beginTransaction();
|
2022-01-09 03:49:51 +08:00
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-08 16:13:16 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return PDO
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function getPdo(): PDO
|
|
|
|
|
{
|
2022-09-20 18:15:50 +08:00
|
|
|
if (!Context::hasContext('master:client')) {
|
2022-09-20 18:33:03 +08:00
|
|
|
Context::setContext('master:client', $this->getMasterClient());
|
2022-06-08 16:13:16 +08:00
|
|
|
}
|
2022-09-20 18:15:50 +08:00
|
|
|
return Context::getContext('master:client');
|
2022-06-08 16:13:16 +08:00
|
|
|
}
|
|
|
|
|
|
2022-01-09 03:49:51 +08:00
|
|
|
/**
|
|
|
|
|
* @return $this|bool
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function inTransaction(): bool|static
|
|
|
|
|
{
|
2022-06-09 10:05:37 +08:00
|
|
|
return $this->getPdo()->inTransaction();
|
2022-01-09 03:49:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @throws Exception
|
|
|
|
|
* 事务回滚
|
|
|
|
|
*/
|
|
|
|
|
public function rollback()
|
|
|
|
|
{
|
2022-09-20 18:15:50 +08:00
|
|
|
$pdo = $this->getPdo();
|
|
|
|
|
if ($pdo->inTransaction()) {
|
|
|
|
|
$pdo->rollback();
|
2022-02-25 15:39:08 +08:00
|
|
|
}
|
2022-09-20 18:15:50 +08:00
|
|
|
$this->release($pdo, true);
|
|
|
|
|
Context::remove('master:client');
|
2022-01-09 03:49:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @throws Exception
|
|
|
|
|
* 事务提交
|
|
|
|
|
*/
|
|
|
|
|
public function commit()
|
|
|
|
|
{
|
2022-09-20 18:15:50 +08:00
|
|
|
$pdo = $this->getPdo();
|
|
|
|
|
if ($pdo->inTransaction()) {
|
|
|
|
|
$pdo->commit();
|
2022-02-25 15:39:08 +08:00
|
|
|
}
|
2022-09-20 18:15:50 +08:00
|
|
|
$this->release($pdo, true);
|
|
|
|
|
Context::remove('master:client');
|
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
|
|
|
|
|
{
|
|
|
|
|
$command = new Command(['db' => $this, 'sql' => $sql]);
|
|
|
|
|
return $command->bindValues($attributes);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* 回收链接
|
|
|
|
|
* @throws
|
|
|
|
|
*/
|
2022-09-20 18:33:03 +08:00
|
|
|
public function release(PDO $pdo, bool $isMaster)
|
2022-01-09 03:49:51 +08:00
|
|
|
{
|
2022-06-17 11:59:19 +08:00
|
|
|
$connections = $this->connection;
|
2022-09-20 18:33:03 +08:00
|
|
|
$cds = ($this->slaveConfig['cds'] ?? $this->cds) . ($isMaster ? 'master' : 'slave');
|
2023-02-06 14:01:06 +08:00
|
|
|
if ($isMaster && !$pdo->inTransaction()) {
|
2022-09-20 18:33:03 +08:00
|
|
|
$connections->addItem($cds, $pdo);
|
2022-02-18 14:45:58 +08:00
|
|
|
}
|
2022-01-09 03:49:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* 回收链接
|
|
|
|
|
* @throws
|
|
|
|
|
*/
|
|
|
|
|
public function clear_connection()
|
|
|
|
|
{
|
2022-09-20 18:35:20 +08:00
|
|
|
$cds = $this->slaveConfig['cds'] ?? $this->cds;
|
|
|
|
|
|
|
|
|
|
$this->connection->connection_clear($cds.'master');
|
|
|
|
|
$this->connection->connection_clear($cds.'slave');
|
2022-01-09 03:49:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function disconnect()
|
|
|
|
|
{
|
2022-09-20 18:35:20 +08:00
|
|
|
$cds = $this->slaveConfig['cds'] ?? $this->cds;
|
|
|
|
|
|
|
|
|
|
$this->connection->connection_clear($cds.'master');
|
|
|
|
|
$this->connection->connection_clear($cds.'slave');
|
2022-01-09 03:49:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|