Files
kiri-databases/Connection.php
T

310 lines
6.2 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\PDO;
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-02-25 15:39:08 +08:00
use Kiri\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-01-10 11:39:56 +08:00
use Kiri\Server\Events\OnWorkerExit;
use Kiri\Server\Events\OnWorkerStop;
2022-02-10 11:46:02 +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;
/**
* @var bool
* enable database cache
*/
public bool $enableCache = false;
/**
* @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
* @param array $config
* @throws Exception
*/
public function __construct(public EventProvider $eventProvider, array $config = [])
{
parent::__construct($config);
}
2022-01-09 03:49:51 +08:00
/**
2022-02-10 11:46:02 +08:00
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
2022-02-18 15:28:29 +08:00
* @throws Exception
2022-01-09 03:49:51 +08:00
*/
public function init()
{
2022-03-03 18:30:59 +08:00
$this->eventProvider->on(OnWorkerExit::class, [$this, 'clear_connection'], 0);
$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
}
/**
2022-02-24 15:24:28 +08:00
* @param $isSearch
2022-01-09 03:49:51 +08:00
* @return PDO
* @throws Exception
*/
2022-02-24 15:24:28 +08:00
public function getConnect($isSearch): PDO
2022-01-09 03:49:51 +08:00
{
2022-02-24 15:24:28 +08:00
return !$isSearch ? $this->masterInstance() : $this->slaveInstance();
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
{
$connections = $this->connections();
$pool = Config::get('databases.pool.max', 10);
2022-02-24 11:00:00 +08:00
$connections->initConnections('Mysql:' . $this->cds, $pool);
2022-01-09 03:49:51 +08:00
if (!empty($this->slaveConfig) && $this->cds != $this->slaveConfig['cds']) {
2022-02-24 11:00:00 +08:00
$connections->initConnections('Mysql:' . $this->slaveConfig['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
*/
public function masterInstance(): PDO
{
return $this->connections()->get([
'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-02-24 15:24:28 +08:00
]);
2022-01-09 03:49:51 +08:00
}
/**
* @return PDO
* @throws Exception
*/
public function slaveInstance(): PDO
{
2022-02-25 15:39:08 +08:00
if (empty($this->slaveConfig) || $this->slaveConfig['cds'] == $this->cds) {
2022-01-09 03:49:51 +08:00
return $this->masterInstance();
}
2022-02-24 11:00:00 +08:00
return $this->connections()->get($this->slaveConfig);
2022-01-09 03:49:51 +08:00
}
/**
* @return \Kiri\Pool\Connection
* @throws Exception
*/
private function connections(): \Kiri\Pool\Connection
{
return Kiri::getDi()->get(\Kiri\Pool\Connection::class);
}
/**
* @return $this
* @throws Exception
*/
public function beginTransaction(): static
{
2022-02-25 18:16:50 +08:00
$pdo = $this->masterInstance();
$pdo->beginTransaction();
2022-01-09 03:49:51 +08:00
return $this;
}
/**
* @return $this|bool
* @throws Exception
*/
public function inTransaction(): bool|static
{
2022-02-25 18:16:50 +08:00
$pdo = $this->masterInstance();
return $pdo->inTransaction();
2022-01-09 03:49:51 +08:00
}
/**
* @throws Exception
* 事务回滚
*/
public function rollback()
{
2022-02-25 18:16:50 +08:00
$pdo = $this->masterInstance();
if ($pdo->inTransaction()) {
$pdo->rollback();
2022-02-25 15:39:08 +08:00
}
2022-02-25 18:16:50 +08:00
$this->release($pdo, $this->cds);
2022-01-09 03:49:51 +08:00
}
/**
* @throws Exception
* 事务提交
*/
public function commit()
{
2022-02-25 18:16:50 +08:00
$pdo = $this->masterInstance();
if ($pdo->inTransaction()) {
$pdo->commit();
2022-02-25 15:39:08 +08:00
}
2022-02-25 18:16:50 +08:00
$this->release($pdo, $this->cds);
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-02-25 15:39:08 +08:00
public function release(PDO $pdo, $isMaster)
2022-01-09 03:49:51 +08:00
{
$connections = $this->connections();
2022-02-25 15:39:08 +08:00
if ($pdo->inTransaction()) {
return;
}
2022-02-25 18:47:45 +08:00
Context::remove($this->cds);
Context::remove($this->slaveConfig['cds']);
2022-02-24 15:24:28 +08:00
if (!$isMaster) {
2022-02-25 18:47:45 +08:00
if (!isset($this->slaveConfig['cds'])) {
2022-02-24 15:24:28 +08:00
$this->slaveConfig['cds'] = $this->cds;
}
$connections->addItem($this->slaveConfig['cds'], $pdo);
} else {
$connections->addItem($this->cds, $pdo);
2022-02-18 14:45:58 +08:00
}
2022-01-09 03:49:51 +08:00
}
/**
*
* 回收链接
* @throws
*/
public function clear_connection()
{
$connections = $this->connections();
2022-02-24 11:00:00 +08:00
$connections->connection_clear($this->cds);
2022-02-18 14:45:58 +08:00
2022-02-25 18:47:45 +08:00
if (!isset($this->slaveConfig['cds'])) {
2022-02-18 14:45:58 +08:00
$this->slaveConfig['cds'] = $this->cds;
}
2022-02-24 11:00:00 +08:00
$connections->connection_clear($this->slaveConfig['cds']);
2022-01-09 03:49:51 +08:00
}
/**
* @throws Exception
*/
public function disconnect()
{
$connections = $this->connections();
2022-02-24 11:00:00 +08:00
$connections->disconnect($this->cds);
2022-02-18 14:45:58 +08:00
2022-02-25 18:47:45 +08:00
if (!isset($this->slaveConfig['cds'])) {
2022-02-18 14:45:58 +08:00
$this->slaveConfig['cds'] = $this->cds;
}
2022-02-24 11:00:00 +08:00
$connections->disconnect($this->slaveConfig['cds']);
2022-01-09 03:49:51 +08:00
}
}