Files
kiri-core/Database/Connection.php
T

337 lines
6.3 KiB
PHP
Raw Normal View History

2020-08-31 12:38:32 +08:00
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/3/30 0030
* Time: 14:09
*/
2020-10-29 11:08:14 +08:00
declare(strict_types=1);
2020-08-31 12:38:32 +08:00
namespace Database;
2021-02-20 13:02:58 +08:00
use JetBrains\PhpStorm\Pure;
2020-12-17 14:09:14 +08:00
use ReflectionException;
2020-08-31 12:38:32 +08:00
use Snowflake\Abstracts\Component;
use Database\Mysql\Schema;
use Database\Orm\Select;
use Exception;
use PDO;
use Snowflake\Event;
use Snowflake\Exception\ComponentException;
2020-12-17 14:09:14 +08:00
use Snowflake\Exception\NotFindClassException;
2020-08-31 12:38:32 +08:00
use Snowflake\Snowflake;
/**
* Class Connection
* @package Database
*/
class Connection extends Component
{
const TRANSACTION_COMMIT = 'transaction::commit';
const TRANSACTION_ROLLBACK = 'transaction::rollback';
2020-10-29 11:08:14 +08:00
public string $id = 'db';
public string $cds = '';
public string $password = '';
public string $username = '';
public string $charset = 'utf-8';
public string $tablePrefix = '';
2020-08-31 12:38:32 +08:00
2020-10-29 11:08:14 +08:00
public int $timeout = 1900;
2020-08-31 12:38:32 +08:00
2020-10-29 11:08:14 +08:00
public int $maxNumber = 200;
2020-08-31 12:38:32 +08:00
/**
* @var bool
* enable database cache
*/
2020-10-29 11:08:14 +08:00
public bool $enableCache = false;
public string $cacheDriver = 'redis';
2020-08-31 12:38:32 +08:00
/**
* @var array
*
* @example [
2020-08-31 13:58:40 +08:00
* 'cds' => 'mysql:dbname=dbname;host=127.0.0.1',
* 'username' => 'root',
* 'password' => 'root'
2020-08-31 12:38:32 +08:00
* ]
*/
2020-10-29 11:08:14 +08:00
public array $slaveConfig = [];
2020-08-31 12:38:32 +08:00
2020-10-29 11:08:14 +08:00
private ?Schema $_schema = null;
2020-08-31 12:38:32 +08:00
/**
* @throws Exception
*/
public function init()
{
2021-02-20 13:02:58 +08:00
$event = Snowflake::app()->getEvent();
2021-02-20 15:21:42 +08:00
$event->on(Event::SYSTEM_RESOURCE_CLEAN, [$this, 'disconnect']);
$event->on(Event::SYSTEM_RESOURCE_RELEASES, [$this, 'clear_connection']);
2020-08-31 12:38:32 +08:00
}
/**
* @throws Exception
*/
public function enablingTransactions()
{
if (!Db::transactionsActive()) {
return;
}
$this->beginTransaction();
2021-02-20 13:02:58 +08:00
$event = Snowflake::app()->getEvent();
2020-08-31 12:38:32 +08:00
$event->on(Connection::TRANSACTION_COMMIT, [$this, 'commit'], false, true);
$event->on(Connection::TRANSACTION_ROLLBACK, [$this, 'rollback'], false, true);
}
/**
* @param null $sql
* @return PDO
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public function getConnect($sql = NULL): PDO
2020-08-31 12:38:32 +08:00
{
2021-02-20 13:02:58 +08:00
$connections = $this->connections();
2021-02-20 13:35:17 +08:00
$connections->initConnections('mysql', $this->cds, true, $this->maxNumber);
$connections->initConnections('mysql', $this->slaveConfig['cds'], false, $this->maxNumber);
2020-08-31 12:38:32 +08:00
$connections->setTimeout($this->timeout);
return $this->getPdo($sql);
}
2020-09-17 13:56:57 +08:00
/**
* 初始化 Channel
2021-02-20 13:02:58 +08:00
* @throws ComponentException
2020-09-17 13:56:57 +08:00
*/
public function fill()
{
2021-02-20 13:02:58 +08:00
$connections = $this->connections();
2021-02-20 13:35:17 +08:00
$connections->initConnections('mysql', $this->cds, true, $this->maxNumber);
$connections->initConnections('mysql', $this->slaveConfig['cds'], false, $this->maxNumber);
2020-09-17 13:56:57 +08:00
}
2020-08-31 12:38:32 +08:00
/**
* @param $sql
* @return PDO
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
private function getPdo($sql): PDO
2020-08-31 12:38:32 +08:00
{
if ($this->isWrite($sql)) {
$connect = $this->masterInstance();
} else {
$connect = $this->slaveInstance();
}
return $connect;
}
/**
2020-12-17 14:09:14 +08:00
* @return mixed
* @throws ReflectionException
* @throws NotFindClassException
2020-08-31 12:38:32 +08:00
*/
2020-12-17 14:09:14 +08:00
public function getSchema(): mixed
2020-08-31 12:38:32 +08:00
{
if ($this->_schema === null) {
$this->_schema = Snowflake::createObject([
'class' => Schema::class,
'db' => $this
]);
}
return $this->_schema;
}
/**
* @param $sql
* @return bool
*/
2021-02-20 13:02:58 +08:00
#[Pure] public function isWrite($sql): bool
2020-08-31 12:38:32 +08:00
{
if (empty($sql)) return false;
$prefix = strtolower(mb_substr($sql, 0, 6));
return in_array($prefix, ['insert', 'update', 'delete']);
}
/**
2020-12-17 14:09:14 +08:00
* @return mixed
2020-08-31 12:38:32 +08:00
* @throws ComponentException
*/
2020-12-17 14:09:14 +08:00
public function getCacheDriver(): mixed
2020-08-31 12:38:32 +08:00
{
if (!$this->enableCache) {
return null;
}
2020-09-03 11:39:20 +08:00
return Snowflake::app()->get($this->cacheDriver);
2020-08-31 12:38:32 +08:00
}
/**
* @return PDO
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public function masterInstance(): PDO
2020-08-31 12:38:32 +08:00
{
2021-02-20 13:02:58 +08:00
return $this->connections()->getConnection([
2020-08-31 12:38:32 +08:00
'cds' => $this->cds,
'username' => $this->username,
'password' => $this->password
2021-02-20 13:02:58 +08:00
], true);
2020-08-31 12:38:32 +08:00
}
/**
* @return PDO
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public function slaveInstance(): PDO
2020-08-31 12:38:32 +08:00
{
2021-02-20 13:02:58 +08:00
if (empty($this->slaveConfig) || $this->slaveConfig['cds'] == $this->cds) {
2020-08-31 12:38:32 +08:00
return $this->masterInstance();
}
2021-02-20 13:02:58 +08:00
return $this->connections()->getConnection($this->slaveConfig, false);
}
2020-08-31 12:38:32 +08:00
2021-02-20 13:02:58 +08:00
/**
* @return \Snowflake\Pool\Connection
* @throws ComponentException
*/
private function connections(): \Snowflake\Pool\Connection
{
return Snowflake::app()->getConnections();
2020-08-31 12:38:32 +08:00
}
/**
* @return $this
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public function beginTransaction(): static
2020-08-31 12:38:32 +08:00
{
2021-02-20 13:02:58 +08:00
$this->connections()->beginTransaction($this->cds);
2020-08-31 12:38:32 +08:00
return $this;
}
/**
* @return $this|bool
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public function inTransaction(): bool|static
2020-08-31 12:38:32 +08:00
{
2021-02-20 13:02:58 +08:00
return $this->connections()->inTransaction($this->cds);
2020-08-31 12:38:32 +08:00
}
/**
* @throws Exception
* 事务回滚
*/
public function rollback()
{
2021-02-20 13:02:58 +08:00
$this->connections()->rollback($this->cds);
2020-08-31 12:38:32 +08:00
}
/**
* @throws Exception
* 事务提交
*/
public function commit()
{
2021-02-20 13:02:58 +08:00
$this->connections()->commit($this->cds);
2020-08-31 12:38:32 +08:00
}
/**
* @param $sql
* @return PDO
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public function refresh($sql): PDO
2020-08-31 12:38:32 +08:00
{
if ($this->isWrite($sql)) {
$instance = $this->masterInstance();
} else {
$instance = $this->slaveInstance();
}
return $instance;
}
/**
* @param $sql
* @param array $attributes
* @return Command
* @throws
*/
2020-12-17 14:09:14 +08:00
public function createCommand($sql = null, $attributes = []): Command
2020-08-31 12:38:32 +08:00
{
2020-09-11 17:59:38 +08:00
$command = new Command(['db' => $this, 'sql' => $sql]);
2020-08-31 12:38:32 +08:00
return $command->bindValues($attributes);
}
/**
* @return Select
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public function getBuild(): Select
2020-08-31 12:38:32 +08:00
{
return $this->getSchema()->getQueryBuilder();
}
/**
*
* 回收链接
* @throws
*/
public function release()
{
2021-02-20 13:02:58 +08:00
$connections = $this->connections();
2020-08-31 12:38:32 +08:00
$connections->release($this->cds, true);
$connections->release($this->slaveConfig['cds'], false);
}
/**
* 临时回收
2021-02-20 13:02:58 +08:00
* @throws ComponentException
2020-08-31 12:38:32 +08:00
*/
public function recovery()
{
2021-02-20 13:02:58 +08:00
$connections = $this->connections();
2020-08-31 12:38:32 +08:00
$connections->release($this->cds, true);
$connections->release($this->slaveConfig['cds'], false);
}
/**
*
* 回收链接
* @throws
*/
public function clear_connection()
{
2021-02-20 13:02:58 +08:00
$connections = $this->connections();
2020-08-31 12:38:32 +08:00
$connections->release($this->cds, true);
$connections->release($this->slaveConfig['cds'], false);
}
/**
* @throws Exception
*/
public function disconnect()
{
2021-02-20 13:02:58 +08:00
$connections = $this->connections();
2020-08-31 12:38:32 +08:00
$connections->disconnect($this->cds);
$connections->disconnect($this->slaveConfig['cds']);
}
}