From fe1711325b5d4ddcb9f044ac4356b5527f7f062d Mon Sep 17 00:00:00 2001 From: "as2252258@163.com" Date: Thu, 6 Apr 2023 22:43:43 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=98=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Command.php | 43 ++++++++++++++++++++++--------------------- Connection.php | 4 ++-- 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/Command.php b/Command.php index 5df8048..077a573 100644 --- a/Command.php +++ b/Command.php @@ -32,10 +32,7 @@ class Command extends Component const DB_ERROR_MESSAGE = 'The system is busy, please try again later.'; /** @var Connection */ - public Connection $db; - - - public PDO $pdo; + public Connection $connection; /** @var ?string */ public ?string $sql = ''; @@ -70,8 +67,9 @@ class Command extends Component public function all(): null|bool|array { try { - if (($prepare = $this->pdo->query($this->sql)) === false) { - throw new Exception($this->pdo->errorInfo()[1]); + $pdo = $this->connection->getConnection(); + if (($prepare = $pdo->query($this->sql)) === false) { + throw new Exception($pdo->errorInfo()[1]); } $prepare->execute($this->params); return $prepare->fetchAll(PDO::FETCH_ASSOC); @@ -81,7 +79,7 @@ class Command extends Component } return $this->error($throwable); } finally { - $this->db->release($this->pdo); + $this->connection->release($pdo ?? null); } } @@ -92,8 +90,9 @@ class Command extends Component public function one(): null|bool|array { try { - if (($prepare = $this->pdo->query($this->sql)) === false) { - throw new Exception($this->pdo->errorInfo()[1]); + $client = $this->connection->getConnection(); + if (($prepare = $client->query($this->sql)) === false) { + throw new Exception($client->errorInfo()[1]); } $prepare->execute($this->params); return $prepare->fetch(PDO::FETCH_ASSOC); @@ -103,7 +102,7 @@ class Command extends Component } return $this->error($throwable); } finally { - $this->db->release($this->pdo); + $this->connection->release($client ?? null); } } @@ -114,8 +113,9 @@ class Command extends Component public function fetchColumn(): null|bool|array { try { - if (($prepare = $this->pdo->query($this->sql)) === false) { - throw new Exception($this->pdo->errorInfo()[1]); + $client = $this->connection->getConnection(); + if (($prepare = $client->query($this->sql)) === false) { + throw new Exception($client->errorInfo()[1]); } $prepare->execute($this->params); return $prepare->fetchColumn(PDO::FETCH_ASSOC); @@ -125,7 +125,7 @@ class Command extends Component } return $this->error($throwable); } finally { - $this->db->release($this->pdo); + $this->connection->release($client ?? null); } } @@ -136,8 +136,9 @@ class Command extends Component public function rowCount(): int|bool { try { - if (($prepare = $this->pdo->query($this->sql)) === false) { - throw new Exception($this->pdo->errorInfo()[1]); + $client = $this->connection->getConnection(); + if (($prepare = $client->query($this->sql)) === false) { + throw new Exception($client->errorInfo()[1]); } $prepare->execute($this->params); return $prepare->rowCount(); @@ -147,7 +148,7 @@ class Command extends Component } return $this->error($throwable); } finally { - $this->db->release($this->pdo); + $this->connection->release($client ?? null); } } @@ -169,14 +170,14 @@ class Command extends Component private function _execute(): bool|int { try { - $prepare = $this->pdo->prepare($this->sql); - if ($prepare === false) { - throw new Exception($this->pdo->errorInfo()[1]); + $client = $this->connection->getConnection(); + if (($prepare = $client->prepare($this->sql)) === false) { + throw new Exception($client->errorInfo()[1]); } if ($prepare->execute($this->params) === false) { throw new Exception($prepare->errorInfo()[1]); } - $result = $this->pdo->lastInsertId(); + $result = $client->lastInsertId(); $prepare->closeCursor(); return $result == 0 ? true : $result; } catch (\Throwable $throwable) { @@ -185,7 +186,7 @@ class Command extends Component } return $this->error($throwable); } finally { - $this->db->release($this->pdo); + $this->connection->release($client ?? null); } } diff --git a/Connection.php b/Connection.php index ba4d59e..4520845 100644 --- a/Connection.php +++ b/Connection.php @@ -178,7 +178,7 @@ class Connection extends Component * @return PDO * @throws Exception */ - public function getMasterClient(): PDO + public function getConnection(): PDO { $client = $this->connections->get($this->cds); if ($client === false) { @@ -208,7 +208,7 @@ class Connection extends Component { $pdo = Context::get($this->cds); if ($pdo === null) { - $pdo = $this->getMasterClient(); + $pdo = $this->getConnection(); } $pdo->beginTransaction(); return $this;