变更
This commit is contained in:
+23
-86
@@ -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);
|
$this->db->release($pdo);
|
||||||
} catch (\Throwable $throwable) {
|
return $result;
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -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);
|
$this->db->release($pdo);
|
||||||
} catch (\Throwable $throwable) {
|
return $result;
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -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);
|
$this->db->release($pdo);
|
||||||
} catch (\Throwable $throwable) {
|
return $result;
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -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();
|
$this->db->release($pdo);
|
||||||
} catch (\Throwable $throwable) {
|
return $result;
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -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();
|
|
||||||
|
|
||||||
return $result == 0 ? true : $result;
|
$this->db->release($pdo);
|
||||||
} catch (\PDOException|\Throwable $throwable) {
|
|
||||||
if (str_contains($throwable->getMessage(), 'MySQL server has gone away')) {
|
return $result == 0 ? true : $result;
|
||||||
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
@@ -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
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -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
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user