This commit is contained in:
as2252258@163.com
2021-02-23 02:14:47 +08:00
parent 779940b151
commit f0e090b0b1
2 changed files with 262 additions and 261 deletions
-1
View File
@@ -221,7 +221,6 @@ class Command extends Component
if (!($connect instanceof PDO)) { if (!($connect instanceof PDO)) {
return null; return null;
} }
var_dump($connect->getAttribute(PDO::ATTR_SERVER_INFO), $this->sql);
if (!($query = $connect->query($this->sql))) return null; if (!($query = $connect->query($this->sql))) return null;
if ($type === static::ROW_COUNT) { if ($type === static::ROW_COUNT) {
$result = $query->rowCount(); $result = $query->rowCount();
+262 -260
View File
@@ -29,308 +29,310 @@ use Snowflake\Snowflake;
*/ */
class Connection extends Component class Connection extends Component
{ {
const TRANSACTION_COMMIT = 'transaction::commit'; const TRANSACTION_COMMIT = 'transaction::commit';
const TRANSACTION_ROLLBACK = 'transaction::rollback'; const TRANSACTION_ROLLBACK = 'transaction::rollback';
public string $id = 'db'; public string $id = 'db';
public string $cds = ''; public string $cds = '';
public string $password = ''; public string $password = '';
public string $username = ''; public string $username = '';
public string $charset = 'utf-8'; public string $charset = 'utf-8';
public string $tablePrefix = ''; public string $tablePrefix = '';
public int $timeout = 1900; public int $timeout = 1900;
public int $maxNumber = 200; public int $maxNumber = 200;
/** /**
* @var bool * @var bool
* enable database cache * enable database cache
*/ */
public bool $enableCache = false; public bool $enableCache = false;
public string $cacheDriver = 'redis'; public string $cacheDriver = 'redis';
/** /**
* @var array * @var array
* *
* @example [ * @example [
* 'cds' => 'mysql:dbname=dbname;host=127.0.0.1', * 'cds' => 'mysql:dbname=dbname;host=127.0.0.1',
* 'username' => 'root', * 'username' => 'root',
* 'password' => 'root' * 'password' => 'root'
* ] * ]
*/ */
public array $slaveConfig = []; public array $slaveConfig = [];
private ?Schema $_schema = null; private ?Schema $_schema = null;
/** /**
* @throws Exception * @throws Exception
*/ */
public function init() public function init()
{ {
$event = Snowflake::app()->getEvent(); $event = Snowflake::app()->getEvent();
$event->on(Event::SYSTEM_RESOURCE_CLEAN, [$this, 'disconnect']); $event->on(Event::SYSTEM_RESOURCE_CLEAN, [$this, 'disconnect']);
$event->on(Event::SYSTEM_RESOURCE_RELEASES, [$this, 'clear_connection']); $event->on(Event::SYSTEM_RESOURCE_RELEASES, [$this, 'clear_connection']);
} }
/** /**
* @throws Exception * @throws Exception
*/ */
public function enablingTransactions() public function enablingTransactions()
{ {
if (!Db::transactionsActive()) { if (!Db::transactionsActive()) {
return; return;
} }
$this->beginTransaction(); $this->beginTransaction();
$event = Snowflake::app()->getEvent(); $event = Snowflake::app()->getEvent();
$event->on(Connection::TRANSACTION_COMMIT, [$this, 'commit'], false, true); $event->on(Connection::TRANSACTION_COMMIT, [$this, 'commit'], false, true);
$event->on(Connection::TRANSACTION_ROLLBACK, [$this, 'rollback'], false, true); $event->on(Connection::TRANSACTION_ROLLBACK, [$this, 'rollback'], false, true);
} }
/** /**
* @param null $sql * @param null $sql
* @return PDO * @return PDO
* @throws Exception * @throws Exception
*/ */
public function getConnect($sql = NULL): PDO public function getConnect($sql = NULL): PDO
{ {
$connections = $this->connections(); $connections = $this->connections();
$connections->initConnections('mysql', $this->cds, true, $this->maxNumber); $connections->initConnections('mysql', $this->cds, true, $this->maxNumber);
$connections->initConnections('mysql', $this->slaveConfig['cds'], false, $this->maxNumber); $connections->initConnections('mysql', $this->slaveConfig['cds'], false, $this->maxNumber);
$connections->setTimeout($this->timeout); $connections->setTimeout($this->timeout);
return $this->getPdo($sql); var_dump($this->cds, $this->slaveConfig['cds']);
}
return $this->getPdo($sql);
}
/** /**
* 初始化 Channel * 初始化 Channel
* @throws ComponentException * @throws ComponentException
*/ */
public function fill() public function fill()
{ {
$connections = $this->connections(); $connections = $this->connections();
$connections->initConnections('mysql', $this->cds, true, $this->maxNumber); $connections->initConnections('mysql', $this->cds, true, $this->maxNumber);
$connections->initConnections('mysql', $this->slaveConfig['cds'], false, $this->maxNumber); $connections->initConnections('mysql', $this->slaveConfig['cds'], false, $this->maxNumber);
} }
/** /**
* @param $sql * @param $sql
* @return PDO * @return PDO
* @throws Exception * @throws Exception
*/ */
private function getPdo($sql): PDO private function getPdo($sql): PDO
{ {
if ($this->isWrite($sql)) { if ($this->isWrite($sql)) {
$connect = $this->masterInstance(); $connect = $this->masterInstance();
} else { } else {
$connect = $this->slaveInstance(); $connect = $this->slaveInstance();
} }
return $connect; return $connect;
} }
/** /**
* @return mixed * @return mixed
* @throws ReflectionException * @throws ReflectionException
* @throws NotFindClassException * @throws NotFindClassException
*/ */
public function getSchema(): mixed public function getSchema(): mixed
{ {
if ($this->_schema === null) { if ($this->_schema === null) {
$this->_schema = Snowflake::createObject([ $this->_schema = Snowflake::createObject([
'class' => Schema::class, 'class' => Schema::class,
'db' => $this 'db' => $this
]); ]);
} }
return $this->_schema; return $this->_schema;
} }
/** /**
* @param $sql * @param $sql
* @return bool * @return bool
*/ */
#[Pure] public function isWrite($sql): bool #[Pure] public function isWrite($sql): bool
{ {
if (empty($sql)) return false; if (empty($sql)) return false;
$prefix = strtolower(mb_substr($sql, 0, 6)); $prefix = strtolower(mb_substr($sql, 0, 6));
return in_array($prefix, ['insert', 'update', 'delete']); return in_array($prefix, ['insert', 'update', 'delete']);
} }
/** /**
* @return mixed * @return mixed
* @throws ComponentException * @throws ComponentException
*/ */
public function getCacheDriver(): mixed public function getCacheDriver(): mixed
{ {
if (!$this->enableCache) { if (!$this->enableCache) {
return null; return null;
} }
return Snowflake::app()->get($this->cacheDriver); return Snowflake::app()->get($this->cacheDriver);
} }
/** /**
* @return PDO * @return PDO
* @throws Exception * @throws Exception
*/ */
public function masterInstance(): PDO public function masterInstance(): PDO
{ {
return $this->connections()->getConnection([ return $this->connections()->getConnection([
'cds' => $this->cds, 'cds' => $this->cds,
'username' => $this->username, 'username' => $this->username,
'password' => $this->password 'password' => $this->password
], true); ], true);
} }
/** /**
* @return PDO * @return PDO
* @throws Exception * @throws Exception
*/ */
public function slaveInstance(): PDO public function slaveInstance(): PDO
{ {
if (empty($this->slaveConfig) || $this->slaveConfig['cds'] == $this->cds) { if (empty($this->slaveConfig) || $this->slaveConfig['cds'] == $this->cds) {
return $this->masterInstance(); return $this->masterInstance();
} }
return $this->connections()->getConnection($this->slaveConfig, false); return $this->connections()->getConnection($this->slaveConfig, false);
} }
/** /**
* @return \Snowflake\Pool\Connection * @return \Snowflake\Pool\Connection
* @throws ComponentException * @throws ComponentException
*/ */
private function connections(): \Snowflake\Pool\Connection private function connections(): \Snowflake\Pool\Connection
{ {
return Snowflake::app()->getConnections(); return Snowflake::app()->getConnections();
} }
/** /**
* @return $this * @return $this
* @throws Exception * @throws Exception
*/ */
public function beginTransaction(): static public function beginTransaction(): static
{ {
$this->connections()->beginTransaction($this->cds); $this->connections()->beginTransaction($this->cds);
return $this; return $this;
} }
/** /**
* @return $this|bool * @return $this|bool
* @throws Exception * @throws Exception
*/ */
public function inTransaction(): bool|static public function inTransaction(): bool|static
{ {
return $this->connections()->inTransaction($this->cds); return $this->connections()->inTransaction($this->cds);
} }
/** /**
* @throws Exception * @throws Exception
* 事务回滚 * 事务回滚
*/ */
public function rollback() public function rollback()
{ {
$this->connections()->rollback($this->cds); $this->connections()->rollback($this->cds);
} }
/** /**
* @throws Exception * @throws Exception
* 事务提交 * 事务提交
*/ */
public function commit() public function commit()
{ {
$this->connections()->commit($this->cds); $this->connections()->commit($this->cds);
} }
/** /**
* @param $sql * @param $sql
* @return PDO * @return PDO
* @throws Exception * @throws Exception
*/ */
public function refresh($sql): PDO public function refresh($sql): PDO
{ {
if ($this->isWrite($sql)) { if ($this->isWrite($sql)) {
$instance = $this->masterInstance(); $instance = $this->masterInstance();
} else { } else {
$instance = $this->slaveInstance(); $instance = $this->slaveInstance();
} }
return $instance; return $instance;
} }
/** /**
* @param $sql * @param $sql
* @param array $attributes * @param array $attributes
* @return Command * @return Command
* @throws * @throws
*/ */
public function createCommand($sql = null, $attributes = []): Command public function createCommand($sql = null, $attributes = []): Command
{ {
$command = new Command(['db' => $this, 'sql' => $sql]); $command = new Command(['db' => $this, 'sql' => $sql]);
return $command->bindValues($attributes); return $command->bindValues($attributes);
} }
/** /**
* @return Select * @return Select
* @throws Exception * @throws Exception
*/ */
public function getBuild(): Select public function getBuild(): Select
{ {
return $this->getSchema()->getQueryBuilder(); return $this->getSchema()->getQueryBuilder();
} }
/** /**
* *
* 回收链接 * 回收链接
* @throws * @throws
*/ */
public function release() public function release()
{ {
$connections = $this->connections(); $connections = $this->connections();
$connections->release($this->cds, true); $connections->release($this->cds, true);
$connections->release($this->slaveConfig['cds'], false); $connections->release($this->slaveConfig['cds'], false);
} }
/** /**
* 临时回收 * 临时回收
* @throws ComponentException * @throws ComponentException
*/ */
public function recovery() public function recovery()
{ {
$connections = $this->connections(); $connections = $this->connections();
$connections->release($this->cds, true); $connections->release($this->cds, true);
$connections->release($this->slaveConfig['cds'], false); $connections->release($this->slaveConfig['cds'], false);
} }
/** /**
* *
* 回收链接 * 回收链接
* @throws * @throws
*/ */
public function clear_connection() public function clear_connection()
{ {
$connections = $this->connections(); $connections = $this->connections();
$connections->release($this->cds, true); $connections->release($this->cds, true);
$connections->release($this->slaveConfig['cds'], false); $connections->release($this->slaveConfig['cds'], false);
} }
/** /**
* @throws Exception * @throws Exception
*/ */
public function disconnect() public function disconnect()
{ {
$connections = $this->connections(); $connections = $this->connections();
$connections->disconnect($this->cds); $connections->disconnect($this->cds);
$connections->disconnect($this->slaveConfig['cds']); $connections->disconnect($this->slaveConfig['cds']);
} }
} }