Files
kiri-core/Database/Connection.php
T

338 lines
6.1 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-08-03 18:18:09 +08:00
use Annotation\Inject;
use Database\Affair\BeginTransaction;
use Database\Affair\Commit;
use Database\Affair\Rollback;
2020-08-31 12:38:32 +08:00
use Database\Mysql\Schema;
use Exception;
2021-04-27 15:26:40 +08:00
use JetBrains\PhpStorm\Pure;
2020-08-31 12:38:32 +08:00
use PDO;
2021-04-27 15:26:40 +08:00
use ReflectionException;
2021-08-03 18:18:09 +08:00
use Server\Events\OnWorkerExit;
use Server\Events\OnWorkerStop;
2021-04-27 15:26:40 +08:00
use Snowflake\Abstracts\Component;
2021-07-05 10:37:29 +08:00
use Snowflake\Abstracts\Config;
2020-08-31 12:38:32 +08:00
use Snowflake\Event;
2021-08-03 18:18:09 +08:00
use Snowflake\Events\EventProvider;
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
{
2021-07-05 15:49:37 +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 $timeout = 1900;
/**
* @var bool
* enable database cache
*/
public bool $enableCache = false;
2021-08-03 18:18:09 +08:00
/**
* @var string
*/
2021-07-05 15:49:37 +08:00
public string $cacheDriver = 'redis';
/**
* @var array
*/
public array $slaveConfig = [];
2021-08-03 18:18:09 +08:00
/**
* @var Schema
*/
#[Inject(Schema::class)]
public Schema $_schema;
2021-07-05 15:49:37 +08:00
/**
2021-08-03 18:18:09 +08:00
* @var EventProvider
2021-07-05 15:49:37 +08:00
*/
2021-08-03 18:18:09 +08:00
#[Inject(EventProvider::class)]
public EventProvider $eventProvider;
2021-07-05 15:49:37 +08:00
/**
2021-08-03 18:18:09 +08:00
* execute by __construct
2021-07-05 15:49:37 +08:00
*/
2021-08-03 18:18:09 +08:00
public function init()
2021-07-05 15:49:37 +08:00
{
2021-08-03 18:18:09 +08:00
$this->eventProvider->on(OnWorkerStop::class, [$this, 'clear_connection'], 0);
$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);
2021-08-04 00:36:21 +08:00
$this->_schema->db = $this;
2021-07-05 15:49:37 +08:00
}
2021-08-03 18:18:09 +08:00
2021-07-05 15:49:37 +08:00
/**
* @param null $sql
* @return PDO
* @throws Exception
*/
public function getConnect($sql = NULL): PDO
{
return $this->getPdo($sql);
}
/**
* @throws Exception
*/
public function fill()
{
$connections = $this->connections();
$pool = Config::get('databases.pool.max', 10);
2021-07-05 15:50:01 +08:00
$connections->initConnections('Mysql:' . $this->cds, true, $pool);
2021-07-05 15:49:37 +08:00
if (!empty($this->slaveConfig) && $this->cds != $this->slaveConfig['cds']) {
2021-07-05 15:50:01 +08:00
$connections->initConnections('Mysql:' . $this->slaveConfig['cds'], false, $pool);
2021-07-05 15:49:37 +08:00
}
}
/**
* @param $sql
* @return PDO
* @throws Exception
*/
private function getPdo($sql): PDO
{
if ($this->isWrite($sql)) {
return $this->masterInstance();
} else {
return $this->slaveInstance();
}
}
/**
* @return mixed
* @throws ReflectionException
* @throws NotFindClassException
*/
public function getSchema(): Schema
{
if ($this->_schema === null) {
$this->_schema = Snowflake::createObject([
'class' => Schema::class,
'db' => $this
]);
}
return $this->_schema;
}
/**
* @param $sql
* @return bool
*/
#[Pure] public function isWrite($sql): bool
{
if (empty($sql)) return false;
if (str_starts_with(strtolower($sql), 'select')) {
return false;
}
return true;
}
/**
* @return mixed
* @throws Exception
*/
public function getCacheDriver(): mixed
{
if (!$this->enableCache) {
return null;
}
return Snowflake::app()->get($this->cacheDriver);
}
/**
* @return PDO
* @throws Exception
*/
public function masterInstance(): PDO
{
return $this->connections()->get([
'cds' => $this->cds,
'username' => $this->username,
'password' => $this->password,
'database' => $this->database
], true);
}
/**
* @return PDO
* @throws Exception
*/
public function slaveInstance(): PDO
{
if (empty($this->slaveConfig) || Db::transactionsActive()) {
return $this->masterInstance();
}
2021-07-05 16:31:35 +08:00
if ($this->slaveConfig['cds'] == $this->cds) {
return $this->masterInstance();
}
2021-07-05 15:49:37 +08:00
return $this->connections()->get($this->slaveConfig, false);
}
/**
* @return \Snowflake\Pool\Connection
* @throws Exception
*/
private function connections(): \Snowflake\Pool\Connection
{
return Snowflake::app()->getMysqlFromPool();
}
/**
* @return $this
* @throws Exception
*/
public function beginTransaction(): static
{
$this->connections()->beginTransaction($this->cds);
return $this;
}
/**
* @return $this|bool
* @throws Exception
*/
public function inTransaction(): bool|static
{
return $this->connections()->inTransaction($this->cds);
}
/**
* @throws Exception
* 事务回滚
*/
public function rollback()
{
$this->connections()->rollback($this->cds);
2021-08-03 18:18:09 +08:00
$this->release();
2021-07-05 15:49:37 +08:00
}
/**
* @throws Exception
* 事务提交
*/
public function commit()
{
$this->connections()->commit($this->cds);
2021-08-03 18:18:09 +08:00
$this->release();
2021-07-05 15:49:37 +08:00
}
/**
* @param $sql
* @return PDO
* @throws Exception
*/
public function refresh($sql): PDO
{
if ($this->isWrite($sql)) {
$instance = $this->masterInstance();
} else {
$instance = $this->slaveInstance();
}
return $instance;
}
2021-05-02 17:51:36 +08:00
2021-07-05 10:37:29 +08:00
/**
* @param null $sql
* @param array $attributes
* @return Command
* @throws Exception
*/
2021-08-03 11:44:58 +08:00
public function createCommand($sql = null, array $attributes = []): Command
2021-07-05 15:49:37 +08:00
{
2021-07-06 17:57:03 +08:00
$command = new Command(['db' => $this, 'sql' => $sql]);
2021-07-05 15:49:37 +08:00
return $command->bindValues($attributes);
}
2021-05-02 17:51:36 +08:00
2021-07-05 15:49:37 +08:00
/**
*
* 回收链接
* @throws
*/
public function release()
{
2021-08-03 18:18:09 +08:00
if (!Snowflake::isWorker() && !Snowflake::isProcess()) {
$this->clear_connection();
return;
}
2021-07-05 15:49:37 +08:00
$connections = $this->connections();
$connections->release($this->cds, true);
$connections->release($this->slaveConfig['cds'], false);
}
/**
* @throws Exception
*/
public function recovery()
{
$connections = $this->connections();
$connections->release($this->cds, true);
$connections->release($this->slaveConfig['cds'], false);
}
/**
*
* 回收链接
* @throws
*/
public function clear_connection()
{
$connections = $this->connections();
2021-08-03 18:18:09 +08:00
$connections->disconnect($this->cds, true);
$connections->disconnect($this->slaveConfig['cds'], false);
2021-07-05 15:49:37 +08:00
}
/**
* @throws Exception
*/
public function disconnect()
{
$connections = $this->connections();
$connections->disconnect($this->cds, true);
$connections->disconnect($this->slaveConfig['cds'], false);
}
2020-08-31 12:38:32 +08:00
}