This commit is contained in:
2023-04-03 00:37:30 +08:00
parent 59cf5e7d64
commit 3c240efa7a
3 changed files with 25 additions and 89 deletions
+18 -81
View File
@@ -65,23 +65,12 @@ class Command extends Component
*/ */
public function all(): ?array public function all(): ?array
{ {
try { $pdo = $this->db->getSlaveClient();
[$pdo, $statement] = $this->search();
$statement->execute($this->params); $result = $pdo->fetchAll($this->sql, $this->params);
return $statement->fetchAll(PDO::FETCH_ASSOC);
} catch (\Throwable $throwable) {
if (!str_contains($throwable->getMessage(), 'MySQL server has gone away')) {
return $this->all();
} else {
return $this->printErrorMessage($throwable);
}
} finally {
if (isset($pdo)) {
$this->db->release($pdo); $this->db->release($pdo);
} return $result;
}
} }
/** /**
@@ -90,23 +79,12 @@ class Command extends Component
*/ */
public function one(): ?array public function one(): ?array
{ {
try { $pdo = $this->db->getSlaveClient();
[$pdo, $statement] = $this->search();
$statement->execute($this->params); $result = $pdo->fetch($this->sql, $this->params);
return $statement->fetch(PDO::FETCH_ASSOC);
} catch (\Throwable $throwable) {
if (!str_contains($throwable->getMessage(), 'MySQL server has gone away')) {
return $this->printErrorMessage($throwable);
} else {
return $this->one();
}
} finally {
if (isset($pdo)) {
$this->db->release($pdo); $this->db->release($pdo);
} return $result;
}
} }
/** /**
@@ -115,23 +93,12 @@ class Command extends Component
*/ */
public function fetchColumn(): mixed public function fetchColumn(): mixed
{ {
try { $pdo = $this->db->getSlaveClient();
[$pdo, $statement] = $this->search();
$statement->execute($this->params); $result = $pdo->fetchColumn($this->sql, $this->params);
return $statement->fetchColumn(PDO::FETCH_ASSOC);
} catch (\Throwable $throwable) {
if (!str_contains($throwable->getMessage(), 'MySQL server has gone away')) {
return $this->printErrorMessage($throwable);
} else {
return $this->fetchColumn();
}
} finally {
if (isset($pdo)) {
$this->db->release($pdo); $this->db->release($pdo);
} return $result;
}
} }
/** /**
@@ -140,23 +107,12 @@ class Command extends Component
*/ */
public function rowCount(): ?int public function rowCount(): ?int
{ {
try { $pdo = $this->db->getSlaveClient();
[$pdo, $statement] = $this->search();
$statement->execute($this->params); $result = $pdo->rowCount($this->sql, $this->params);
return $statement->rowCount();
} catch (\Throwable $throwable) {
if (!str_contains($throwable->getMessage(), 'MySQL server has gone away')) {
return $this->printErrorMessage($throwable);
} else {
return $this->rowCount();
}
} finally {
if (isset($pdo)) {
$this->db->release($pdo); $this->db->release($pdo);
} return $result;
}
} }
@@ -181,30 +137,15 @@ class Command extends Component
* @return bool|int * @return bool|int
* @throws Exception * @throws Exception
*/ */
private function _execute(bool $restore = false): bool|int private function _execute(): bool|int
{ {
try { $pdo = $this->db->getPdo();
$pdo = $this->db->getPdo($restore);
if (!(($prepare = $pdo->prepare($this->sql)) instanceof PDOStatement)) {
throw new Exception($prepare->errorInfo()[2] ?? static::DB_ERROR_MESSAGE);
}
if ($prepare->execute($this->params) === false) {
throw new Exception($prepare->errorInfo()[2] ?? static::DB_ERROR_MESSAGE);
}
$result = (int)$pdo->lastInsertId(); $result = $pdo->execute($this->sql, $this->params);
$prepare->closeCursor();
$this->db->release($pdo);
return $result == 0 ? true : $result; return $result == 0 ? true : $result;
} catch (\PDOException|\Throwable $throwable) {
if (str_contains($throwable->getMessage(), 'MySQL server has gone away')) {
return $this->_execute(true);
} else {
return $this->printErrorMessage($throwable);
}
} finally {
isset($pdo) && $this->db->release($pdo);
}
} }
/** /**
@@ -214,11 +155,7 @@ class Command extends Component
private function search(): bool|array private function search(): bool|array
{ {
$pdo = $this->db->getSlaveClient(); $pdo = $this->db->getSlaveClient();
return [$pdo, null];
if (($statement = $pdo->prepare($this->sql)) === false) {
throw new Exception($pdo->errorInfo()[1]);
}
return [$pdo, $statement];
} }
+2 -2
View File
@@ -183,8 +183,8 @@ class Connection extends Component
/** /**
* @param bool $restore * @param bool $restore
* @return PDO * @return Mysql\PDO
* @throws Exception * @throws ConfigException
*/ */
public function getPdo(bool $restore = false): Mysql\PDO public function getPdo(bool $restore = false): Mysql\PDO
{ {
-1
View File
@@ -13,7 +13,6 @@ use Kiri\Server\Abstracts\StatusEnum;
use Swoole\Timer; use Swoole\Timer;
/** /**
* @mixin \PDO
*/ */
class PDO implements StopHeartbeatCheck class PDO implements StopHeartbeatCheck
{ {