diff --git a/Command.php b/Command.php index aff1edb..c4725cc 100644 --- a/Command.php +++ b/Command.php @@ -10,6 +10,7 @@ declare(strict_types=1); namespace Database; +use Database\Mysql\PDO; use Exception; use Kiri\Abstracts\Component; use Kiri\Core\Json; @@ -21,11 +22,11 @@ use PDOStatement; */ class Command extends Component { - const ROW_COUNT = 'ROW_COUNT'; - const FETCH = 'FETCH'; - const FETCH_ALL = 'FETCH_ALL'; - const EXECUTE = 'EXECUTE'; - const FETCH_COLUMN = 'FETCH_COLUMN'; + const ROW_COUNT = 'count'; + const FETCH = 'fetch'; + const FETCH_ALL = 'fetchAll'; + const EXECUTE = 'execute'; + const FETCH_COLUMN = 'fetchColumn'; /** @var Connection */ public Connection $db; @@ -104,23 +105,49 @@ class Command extends Component return $this->execute(static::EXECUTE); } + /** * @param string $type * @return int|bool|array|string|null * @throws Exception */ private function execute(string $type): int|bool|array|string|null + { + $pdo = $this->db->getConnect($this->sql); + $time = microtime(true); + if ($type !== static::EXECUTE) { + $result = $this->search($type, $pdo); + } else { + $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) { + $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 { - $time = microtime(true); - if ($type === static::EXECUTE) { - $result = $this->db->getConnect($this->sql)->execute($this->sql,$this->params); - } else { - $result = $this->search($type); - } - if (microtime(true) - $time >= 0.02) { - $this->warning('Mysql:' . Json::encode([$this->sql, $this->params]) . (microtime(true) - $time)); - } + $result = $pdo->execute($this->sql, $this->params); } catch (\Throwable $exception) { $result = $this->addError($this->sql . '. error: ' . $exception->getMessage(), 'mysql'); } finally { @@ -132,22 +159,20 @@ class Command extends Component /** * @param string $type + * @param PDO $pdo * @return array|int|bool|null * @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); - if ($type === static::FETCH_COLUMN) { - $data = $pdo->fetchColumn($this->sql, $this->params); - } else if ($type === static::ROW_COUNT) { - $data = $pdo->count($this->sql, $this->params); - } else if ($type === static::FETCH_ALL) { - $data = $pdo->fetchAll($this->sql, $this->params); - } else { - $data = $pdo->fetch($this->sql, $this->params); + try { + $data = $pdo->{$type}($this->sql, $this->params); + } catch (\Throwable $throwable) { + $data = $this->addError($this->sql . '. error: ' . $throwable->getMessage(), 'mysql'); + } finally { + $this->db->releaseSlaveConnect($pdo); + return $data; } - return $data; } diff --git a/Connection.php b/Connection.php index 1ab0168..2aecff0 100644 --- a/Connection.php +++ b/Connection.php @@ -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; } $connections = $this->connections(); - $connections->release($this->cds, true, $config = $this->_config()); + $connections->release($this->cds, true); - $config['cds'] = $this->slaveConfig['cds']; - $config['username'] = $this->slaveConfig['username']; - $config['password'] = $this->slaveConfig['password']; + if (empty($this->slaveConfig) || !isset($this->slaveConfig['cds'])) { + $this->slaveConfig['cds'] = $this->cds; + } - $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->release($this->cds, true, $config = $this->_config()); + $connections->release($this->cds, true); - $config['cds'] = $this->slaveConfig['cds']; - $config['username'] = $this->slaveConfig['username']; - $config['password'] = $this->slaveConfig['password']; + if (empty($this->slaveConfig) || !isset($this->slaveConfig['cds'])) { + $this->slaveConfig['cds'] = $this->cds; + } - $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->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); } @@ -375,6 +398,11 @@ class Connection extends Component { $connections = $this->connections(); $connections->disconnect($this->cds, true); + + if (empty($this->slaveConfig) || !isset($this->slaveConfig['cds'])) { + $this->slaveConfig['cds'] = $this->cds; + } + $connections->disconnect($this->slaveConfig['cds'], false); }