This commit is contained in:
2023-04-06 22:43:43 +08:00
parent 3d8c01a42c
commit fe1711325b
2 changed files with 24 additions and 23 deletions
+22 -21
View File
@@ -32,10 +32,7 @@ class Command extends Component
const DB_ERROR_MESSAGE = 'The system is busy, please try again later.'; const DB_ERROR_MESSAGE = 'The system is busy, please try again later.';
/** @var Connection */ /** @var Connection */
public Connection $db; public Connection $connection;
public PDO $pdo;
/** @var ?string */ /** @var ?string */
public ?string $sql = ''; public ?string $sql = '';
@@ -70,8 +67,9 @@ class Command extends Component
public function all(): null|bool|array public function all(): null|bool|array
{ {
try { try {
if (($prepare = $this->pdo->query($this->sql)) === false) { $pdo = $this->connection->getConnection();
throw new Exception($this->pdo->errorInfo()[1]); if (($prepare = $pdo->query($this->sql)) === false) {
throw new Exception($pdo->errorInfo()[1]);
} }
$prepare->execute($this->params); $prepare->execute($this->params);
return $prepare->fetchAll(PDO::FETCH_ASSOC); return $prepare->fetchAll(PDO::FETCH_ASSOC);
@@ -81,7 +79,7 @@ class Command extends Component
} }
return $this->error($throwable); return $this->error($throwable);
} finally { } 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 public function one(): null|bool|array
{ {
try { try {
if (($prepare = $this->pdo->query($this->sql)) === false) { $client = $this->connection->getConnection();
throw new Exception($this->pdo->errorInfo()[1]); if (($prepare = $client->query($this->sql)) === false) {
throw new Exception($client->errorInfo()[1]);
} }
$prepare->execute($this->params); $prepare->execute($this->params);
return $prepare->fetch(PDO::FETCH_ASSOC); return $prepare->fetch(PDO::FETCH_ASSOC);
@@ -103,7 +102,7 @@ class Command extends Component
} }
return $this->error($throwable); return $this->error($throwable);
} finally { } 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 public function fetchColumn(): null|bool|array
{ {
try { try {
if (($prepare = $this->pdo->query($this->sql)) === false) { $client = $this->connection->getConnection();
throw new Exception($this->pdo->errorInfo()[1]); if (($prepare = $client->query($this->sql)) === false) {
throw new Exception($client->errorInfo()[1]);
} }
$prepare->execute($this->params); $prepare->execute($this->params);
return $prepare->fetchColumn(PDO::FETCH_ASSOC); return $prepare->fetchColumn(PDO::FETCH_ASSOC);
@@ -125,7 +125,7 @@ class Command extends Component
} }
return $this->error($throwable); return $this->error($throwable);
} finally { } finally {
$this->db->release($this->pdo); $this->connection->release($client ?? null);
} }
} }
@@ -136,8 +136,9 @@ class Command extends Component
public function rowCount(): int|bool public function rowCount(): int|bool
{ {
try { try {
if (($prepare = $this->pdo->query($this->sql)) === false) { $client = $this->connection->getConnection();
throw new Exception($this->pdo->errorInfo()[1]); if (($prepare = $client->query($this->sql)) === false) {
throw new Exception($client->errorInfo()[1]);
} }
$prepare->execute($this->params); $prepare->execute($this->params);
return $prepare->rowCount(); return $prepare->rowCount();
@@ -147,7 +148,7 @@ class Command extends Component
} }
return $this->error($throwable); return $this->error($throwable);
} finally { } finally {
$this->db->release($this->pdo); $this->connection->release($client ?? null);
} }
} }
@@ -169,14 +170,14 @@ class Command extends Component
private function _execute(): bool|int private function _execute(): bool|int
{ {
try { try {
$prepare = $this->pdo->prepare($this->sql); $client = $this->connection->getConnection();
if ($prepare === false) { if (($prepare = $client->prepare($this->sql)) === false) {
throw new Exception($this->pdo->errorInfo()[1]); throw new Exception($client->errorInfo()[1]);
} }
if ($prepare->execute($this->params) === false) { if ($prepare->execute($this->params) === false) {
throw new Exception($prepare->errorInfo()[1]); throw new Exception($prepare->errorInfo()[1]);
} }
$result = $this->pdo->lastInsertId(); $result = $client->lastInsertId();
$prepare->closeCursor(); $prepare->closeCursor();
return $result == 0 ? true : $result; return $result == 0 ? true : $result;
} catch (\Throwable $throwable) { } catch (\Throwable $throwable) {
@@ -185,7 +186,7 @@ class Command extends Component
} }
return $this->error($throwable); return $this->error($throwable);
} finally { } finally {
$this->db->release($this->pdo); $this->connection->release($client ?? null);
} }
} }
+2 -2
View File
@@ -178,7 +178,7 @@ class Connection extends Component
* @return PDO * @return PDO
* @throws Exception * @throws Exception
*/ */
public function getMasterClient(): PDO public function getConnection(): PDO
{ {
$client = $this->connections->get($this->cds); $client = $this->connections->get($this->cds);
if ($client === false) { if ($client === false) {
@@ -208,7 +208,7 @@ class Connection extends Component
{ {
$pdo = Context::get($this->cds); $pdo = Context::get($this->cds);
if ($pdo === null) { if ($pdo === null) {
$pdo = $this->getMasterClient(); $pdo = $this->getConnection();
} }
$pdo->beginTransaction(); $pdo->beginTransaction();
return $this; return $this;