This commit is contained in:
2026-06-30 23:12:17 +08:00
parent 9fc910f10f
commit 28fa17ecfb
+42 -44
View File
@@ -29,7 +29,7 @@ class Command extends Component
public Connection $connection;
public ?string $sql = '';
public array $params = [];
/**
* @param array $params
* @throws
@@ -39,8 +39,8 @@ class Command extends Component
parent::__construct();
Container::configure($this, $params);
}
/**
* @return bool
* @throws
@@ -49,8 +49,8 @@ class Command extends Component
{
return (bool)$this->_prepare();
}
/**
* @return bool|array
* @throws
@@ -59,7 +59,7 @@ class Command extends Component
{
return $this->search('fetchAll');
}
/**
* @return array|bool|null
* @throws
@@ -68,7 +68,7 @@ class Command extends Component
{
return $this->search('fetch');
}
/**
* @return bool
* @throws Exception
@@ -81,7 +81,7 @@ class Command extends Component
}
return true;
}
/**
* @return mixed
* @throws
@@ -90,7 +90,7 @@ class Command extends Component
{
return $this->search('fetchColumn');
}
/**
* @return mixed
* @throws
@@ -98,11 +98,11 @@ class Command extends Component
public function rowCount(): int
{
$data = $this->search('fetch');
return !$data ? 0 : +current($data);
}
/**
* 执行查询类 SQL 操作,内置断连重试机制
* 当数据库连接断开时自动重试,断开的连接不再归还池中防止污染连接池
@@ -121,37 +121,37 @@ class Command extends Component
if (($prepare = $client->prepare($this->sql)) === false) {
throw new Exception('(' . $prepare->errorInfo()[0] . ')' . $client->errorInfo()[2]);
}
$prepare->execute($this->params);
$result = $method == 'rowCount' ? $prepare->rowCount() : $prepare->{$method}(PDO::FETCH_ASSOC);
$prepare->closeCursor();
$this->connection->println($startTime, microtime(true), $this->sql, $this->params);
$this->connection->release($client);
return $result;
} catch (Throwable $throwable) {
$this->getLogger()->json_log($throwable);
if ($this->isRefresh($throwable) && $retryCount < self::MAX_RETRIES) {
$retryCount++;
$this->connection->connections->abandon($this->connection->getName());
continue;
}
$this->connection->release($client);
$errorMsg = $throwable->getMessage() . PHP_EOL . ' Sql: ' . $this->sql . '.' . json_encode($this->params);
return $this->getLogger()->logCategory($errorMsg . PHP_EOL, 'mysql');
}
}
return $this->getLogger()->logCategory('Max retry exceeded for SQL: ' . $this->sql, 'mysql');
}
/**
* @return bool
* @throws
@@ -160,8 +160,8 @@ class Command extends Component
{
return (bool)$this->_prepare();
}
/**
* 执行写入类 SQL 操作,内置断连重试机制
* 当数据库连接断开时自动重试,断开的连接不再归还池中防止污染连接池
@@ -182,42 +182,40 @@ class Command extends Component
if ($prepare->execute($this->params) === false) {
throw new Exception('(' . $prepare->errorInfo()[0] . ')' . $prepare->errorInfo()[2]);
}
$prepare->closeCursor();
$result = $client->lastInsertId();
$this->connection->println($startTime, microtime(true), $this->sql, $this->params);
if (str_starts_with($this->sql, 'DELETE')) {
$this->connection->release($client);
return $prepare->rowCount();
}
var_dump($this->sql, $result);
$this->connection->release($client);
return $result == 0 ? $prepare->rowCount() : (int)$result;
} catch (Throwable $throwable) {
$this->getLogger()->json_log($throwable);
if ($this->isRefresh($throwable) && $retryCount < self::MAX_RETRIES) {
$retryCount++;
$this->connection->connections->abandon($this->connection->getName());
continue;
}
$this->connection->release($client);
$errorMsg = $throwable->getMessage() . PHP_EOL . ' Sql: ' . $this->sql . '.' . json_encode($this->params, JSON_UNESCAPED_UNICODE);
return $this->getLogger()->logCategory($errorMsg . PHP_EOL, 'mysql');
}
}
return $this->getLogger()->logCategory('Max retry exceeded for SQL: ' . $this->sql, 'mysql');
}
/**
* @param Throwable $throwable
* @return bool
@@ -225,7 +223,7 @@ class Command extends Component
protected function isRefresh(Throwable $throwable): bool
{
$message = $throwable->getMessage();
// MySQL 错误处理
if (str_contains($message, 'MySQL server has gone away')) {
return true;
@@ -236,7 +234,7 @@ class Command extends Component
if (str_contains($message, 'Lost connection to MySQL server during query')) {
return true;
}
// PostgreSQL 错误处理
if (str_contains($message, 'server closed the connection unexpectedly')) {
return true;
@@ -250,11 +248,11 @@ class Command extends Component
if (str_contains($message, 'connection to server was lost')) {
return true;
}
return false;
}
/**
* @return bool
* @throws
@@ -263,8 +261,8 @@ class Command extends Component
{
return $this->_prepare();
}
/**
* @return int|bool
*/
@@ -272,7 +270,7 @@ class Command extends Component
{
return $this->_prepare();
}
/**
* @param array $data
* @return $this
@@ -284,5 +282,5 @@ class Command extends Component
}
return $this;
}
}