modify plugin name

This commit is contained in:
2022-06-08 16:13:16 +08:00
parent eb36a30b26
commit 62591edf44
2 changed files with 42 additions and 31 deletions
+3 -3
View File
@@ -137,13 +137,13 @@ class Command extends Component
*/
private function _execute(): bool|int
{
$pdo = $this->db->masterInstance();
$pdo = $this->db->getPdo();
try {
$result = $pdo->execute($this->sql, $this->params);
} catch (\Throwable $exception) {
$result = $this->logger->addError($this->sql . '. error: ' . $exception->getMessage(), 'mysql');
} finally {
$this->db->release($pdo, true);
$this->db->release($pdo);
return $result;
}
}
@@ -156,7 +156,7 @@ class Command extends Component
*/
private function search(string $type): array|int|bool|null
{
$pdo = $this->db->slaveInstance();
$pdo = $this->db->getSlaveClient();
try {
$data = $pdo->{$type}($this->sql, $this->params);
} catch (\Throwable $throwable) {
+39 -28
View File
@@ -20,7 +20,6 @@ use Exception;
use Kiri;
use Kiri\Abstracts\Component;
use Kiri\Abstracts\Config;
use Kiri\Context;
use Kiri\Events\EventProvider;
use Kiri\Exception\NotFindClassException;
use Kiri\Server\Events\OnWorkerExit;
@@ -56,6 +55,9 @@ class Connection extends Component
public bool $enableCache = false;
private ?PDO $_pdo = null;
/**
* @var string
*/
@@ -104,7 +106,7 @@ class Connection extends Component
*/
public function getConnect($isSearch): PDO
{
return !$isSearch ? $this->masterInstance() : $this->slaveInstance();
return !$isSearch ? $this->getMasterClient() : $this->getSlaveClient();
}
@@ -145,7 +147,7 @@ class Connection extends Component
* @return PDO
* @throws Exception
*/
public function masterInstance(): PDO
public function getMasterClient(): PDO
{
return $this->connections()->get([
'cds' => $this->cds,
@@ -163,10 +165,10 @@ class Connection extends Component
* @return PDO
* @throws Exception
*/
public function slaveInstance(): PDO
public function getSlaveClient(): PDO
{
if (empty($this->slaveConfig) || $this->slaveConfig['cds'] == $this->cds) {
return $this->masterInstance();
return $this->getMasterClient();
}
return $this->connections()->get($this->slaveConfig);
}
@@ -188,19 +190,34 @@ class Connection extends Component
*/
public function beginTransaction(): static
{
$pdo = $this->masterInstance();
$pdo = $this->getMasterClient();
$pdo->beginTransaction();
return $this;
}
/**
* @return PDO
* @throws Exception
*/
public function getPdo(): PDO
{
if (!$this->_pdo) {
$this->_pdo = $this->getMasterClient();
}
return $this->_pdo;
}
/**
* @return $this|bool
* @throws Exception
*/
public function inTransaction(): bool|static
{
$pdo = $this->masterInstance();
return $pdo->inTransaction();
if (!$this->_pdo) {
$this->_pdo = $this->getMasterClient();
}
return $this->_pdo->inTransaction();
}
/**
@@ -209,11 +226,11 @@ class Connection extends Component
*/
public function rollback()
{
$pdo = $this->masterInstance();
if ($pdo->inTransaction()) {
$pdo->rollback();
if ($this->_pdo->inTransaction()) {
$this->_pdo->rollback();
}
$this->release($pdo, $this->cds);
$this->release($this->_pdo);
$this->_pdo = null;
}
/**
@@ -222,11 +239,11 @@ class Connection extends Component
*/
public function commit()
{
$pdo = $this->masterInstance();
if ($pdo->inTransaction()) {
$pdo->commit();
if ($this->_pdo->inTransaction()) {
$this->_pdo->commit();
}
$this->release($pdo, $this->cds);
$this->release($this->_pdo);
$this->_pdo = null;
}
@@ -248,21 +265,15 @@ class Connection extends Component
* 回收链接
* @throws
*/
public function release(PDO $pdo, $isMaster)
public function release(PDO $pdo)
{
$connections = $this->connections();
if ($pdo->inTransaction()) {
return;
}
Context::remove($this->cds);
Context::remove($this->slaveConfig['cds']);
if (!$isMaster) {
if (!isset($this->slaveConfig['cds'])) {
$this->slaveConfig['cds'] = $this->cds;
if (!$pdo->inTransaction()) {
$cds = $this->cds;
if (isset($this->slaveConfig['cds'])) {
$cds = $this->slaveConfig['cds'];
}
$connections->addItem($this->slaveConfig['cds'], $pdo);
} else {
$connections->addItem($this->cds, $pdo);
$connections->addItem($cds, $pdo);
}
}