modify plugin name

This commit is contained in:
2022-02-18 14:45:58 +08:00
parent 0163758d8e
commit 42bea9d93a
2 changed files with 88 additions and 35 deletions
+45 -20
View File
@@ -10,6 +10,7 @@ declare(strict_types=1);
namespace Database; namespace Database;
use Database\Mysql\PDO;
use Exception; use Exception;
use Kiri\Abstracts\Component; use Kiri\Abstracts\Component;
use Kiri\Core\Json; use Kiri\Core\Json;
@@ -21,11 +22,11 @@ use PDOStatement;
*/ */
class Command extends Component class Command extends Component
{ {
const ROW_COUNT = 'ROW_COUNT'; const ROW_COUNT = 'count';
const FETCH = 'FETCH'; const FETCH = 'fetch';
const FETCH_ALL = 'FETCH_ALL'; const FETCH_ALL = 'fetchAll';
const EXECUTE = 'EXECUTE'; const EXECUTE = 'execute';
const FETCH_COLUMN = 'FETCH_COLUMN'; const FETCH_COLUMN = 'fetchColumn';
/** @var Connection */ /** @var Connection */
public Connection $db; public Connection $db;
@@ -104,6 +105,7 @@ class Command extends Component
return $this->execute(static::EXECUTE); return $this->execute(static::EXECUTE);
} }
/** /**
* @param string $type * @param string $type
* @return int|bool|array|string|null * @return int|bool|array|string|null
@@ -111,16 +113,41 @@ class Command extends Component
*/ */
private function execute(string $type): int|bool|array|string|null private function execute(string $type): int|bool|array|string|null
{ {
try { $pdo = $this->db->getConnect($this->sql);
$time = microtime(true); $time = microtime(true);
if ($type === static::EXECUTE) { if ($type !== static::EXECUTE) {
$result = $this->db->getConnect($this->sql)->execute($this->sql,$this->params); $result = $this->search($type, $pdo);
} else { } else {
$result = $this->search($type); $result = $this->_execute($pdo);
} }
return $this->_timeout_log($time, $result);
}
/**
* @param int $time
* @param mixed $result
* @return mixed
* @throws Exception
*/
private function _timeout_log(int $time, mixed $result): mixed
{
if (microtime(true) - $time >= 0.02) { if (microtime(true) - $time >= 0.02) {
$this->warning('Mysql:' . Json::encode([$this->sql, $this->params]) . (microtime(true) - $time)); $this->warning('Mysql:' . Json::encode([$this->sql, $this->params]) . (microtime(true) - $time));
} }
return $result;
}
/**
* @param PDO $pdo
* @return bool|int
* @throws Exception
*/
private function _execute(PDO $pdo): bool|int
{
try {
$result = $pdo->execute($this->sql, $this->params);
} catch (\Throwable $exception) { } catch (\Throwable $exception) {
$result = $this->addError($this->sql . '. error: ' . $exception->getMessage(), 'mysql'); $result = $this->addError($this->sql . '. error: ' . $exception->getMessage(), 'mysql');
} finally { } finally {
@@ -132,23 +159,21 @@ class Command extends Component
/** /**
* @param string $type * @param string $type
* @param PDO $pdo
* @return array|int|bool|null * @return array|int|bool|null
* @throws Exception * @throws Exception
*/ */
private function search(string $type): array|int|bool|null private function search(string $type, PDO $pdo): array|int|bool|null
{ {
$pdo = $this->db->getConnect($this->sql); try {
if ($type === static::FETCH_COLUMN) { $data = $pdo->{$type}($this->sql, $this->params);
$data = $pdo->fetchColumn($this->sql, $this->params); } catch (\Throwable $throwable) {
} else if ($type === static::ROW_COUNT) { $data = $this->addError($this->sql . '. error: ' . $throwable->getMessage(), 'mysql');
$data = $pdo->count($this->sql, $this->params); } finally {
} else if ($type === static::FETCH_ALL) { $this->db->releaseSlaveConnect($pdo);
$data = $pdo->fetchAll($this->sql, $this->params);
} else {
$data = $pdo->fetch($this->sql, $this->params);
}
return $data; return $data;
} }
}
/** /**
+38 -10
View File
@@ -316,6 +316,24 @@ class Connection extends Component
} }
/**
* @param PDO $PDO
* @return void
* @throws Kiri\Exception\ConfigException
* @throws Exception
*/
public function releaseSlaveConnect(PDO $PDO)
{
$connections = $this->connections();
if (empty($this->slaveConfig)) {
$this->slaveConfig = ['cds' => $this->cds, 'username' => $this->username, 'password' => $this->password];
}
$connections->recover($this->slaveConfig['cds'], $PDO, false);
}
/** /**
* *
* 回收链接 * 回收链接
@@ -328,13 +346,13 @@ class Connection extends Component
return; return;
} }
$connections = $this->connections(); $connections = $this->connections();
$connections->release($this->cds, true, $config = $this->_config()); $connections->release($this->cds, true);
$config['cds'] = $this->slaveConfig['cds']; if (empty($this->slaveConfig) || !isset($this->slaveConfig['cds'])) {
$config['username'] = $this->slaveConfig['username']; $this->slaveConfig['cds'] = $this->cds;
$config['password'] = $this->slaveConfig['password']; }
$connections->release($this->slaveConfig['cds'], false, $config); $connections->release($this->slaveConfig['cds'], false);
} }
@@ -345,13 +363,13 @@ class Connection extends Component
{ {
$connections = $this->connections(); $connections = $this->connections();
$connections->release($this->cds, true, $config = $this->_config()); $connections->release($this->cds, true);
$config['cds'] = $this->slaveConfig['cds']; if (empty($this->slaveConfig) || !isset($this->slaveConfig['cds'])) {
$config['username'] = $this->slaveConfig['username']; $this->slaveConfig['cds'] = $this->cds;
$config['password'] = $this->slaveConfig['password']; }
$connections->release($this->slaveConfig['cds'], false, $config); $connections->release($this->slaveConfig['cds'], false);
} }
/** /**
@@ -364,6 +382,11 @@ class Connection extends Component
$connections = $this->connections(); $connections = $this->connections();
$connections->connection_clear($this->cds, true); $connections->connection_clear($this->cds, true);
if (empty($this->slaveConfig) || !isset($this->slaveConfig['cds'])) {
$this->slaveConfig['cds'] = $this->cds;
}
$connections->connection_clear($this->slaveConfig['cds'], false); $connections->connection_clear($this->slaveConfig['cds'], false);
} }
@@ -375,6 +398,11 @@ class Connection extends Component
{ {
$connections = $this->connections(); $connections = $this->connections();
$connections->disconnect($this->cds, true); $connections->disconnect($this->cds, true);
if (empty($this->slaveConfig) || !isset($this->slaveConfig['cds'])) {
$this->slaveConfig['cds'] = $this->cds;
}
$connections->disconnect($this->slaveConfig['cds'], false); $connections->disconnect($this->slaveConfig['cds'], false);
} }