eee
This commit is contained in:
+229
-228
@@ -22,238 +22,239 @@ use Throwable;
|
|||||||
*/
|
*/
|
||||||
class Command extends Component
|
class Command extends Component
|
||||||
{
|
{
|
||||||
|
|
||||||
public Connection $connection;
|
public Connection $connection;
|
||||||
public ?string $sql = '';
|
public ?string $sql = '';
|
||||||
public array $params = [];
|
public array $params = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $params
|
* @param array $params
|
||||||
* @throws
|
* @throws
|
||||||
*/
|
*/
|
||||||
public function __construct(array $params = [])
|
public function __construct(array $params = [])
|
||||||
{
|
{
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
Container::configure($this, $params);
|
Container::configure($this, $params);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return bool
|
* @return bool
|
||||||
* @throws
|
* @throws
|
||||||
*/
|
*/
|
||||||
public function incrOrDecr(): bool
|
public function incrOrDecr(): bool
|
||||||
{
|
{
|
||||||
return (bool)$this->_prepare();
|
return (bool)$this->_prepare();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return bool|array
|
* @return bool|array
|
||||||
* @throws
|
* @throws
|
||||||
*/
|
*/
|
||||||
public function all(): bool|array
|
public function all(): bool|array
|
||||||
{
|
{
|
||||||
return $this->search('fetchAll');
|
return $this->search('fetchAll');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array|bool|null
|
* @return array|bool|null
|
||||||
* @throws
|
* @throws
|
||||||
*/
|
*/
|
||||||
public function one(): array|null|bool
|
public function one(): array|null|bool
|
||||||
{
|
{
|
||||||
return $this->search('fetch');
|
return $this->search('fetch');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return bool
|
* @return bool
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function exists(): bool
|
public function exists(): bool
|
||||||
{
|
{
|
||||||
$data = $this->search('fetch');
|
$data = $this->search('fetch');
|
||||||
if (!$data) {
|
if (!$data) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws
|
* @throws
|
||||||
*/
|
*/
|
||||||
public function fetchColumn(): mixed
|
public function fetchColumn(): mixed
|
||||||
{
|
{
|
||||||
return $this->search('fetchColumn');
|
return $this->search('fetchColumn');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws
|
* @throws
|
||||||
*/
|
*/
|
||||||
public function rowCount(): int
|
public function rowCount(): int
|
||||||
{
|
{
|
||||||
$data = $this->search('fetch');
|
$data = $this->search('fetch');
|
||||||
|
|
||||||
return !$data ? 0 : +current($data);
|
return !$data ? 0 : +current($data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $method
|
* @param string $method
|
||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws
|
* @throws
|
||||||
*/
|
*/
|
||||||
protected function search(string $method): mixed
|
protected function search(string $method): mixed
|
||||||
{
|
{
|
||||||
$client = $this->connection->getConnection();
|
$client = $this->connection->getConnection();
|
||||||
try {
|
try {
|
||||||
$startTime = microtime(true);
|
$startTime = microtime(true);
|
||||||
if (($prepare = $client->prepare($this->sql)) === false) {
|
if (($prepare = $client->prepare($this->sql)) === false) {
|
||||||
throw new Exception('(' . $prepare->errorInfo()[0] . ')' . $client->errorInfo()[2]);
|
throw new Exception('(' . $prepare->errorInfo()[0] . ')' . $client->errorInfo()[2]);
|
||||||
}
|
}
|
||||||
|
|
||||||
$prepare->execute($this->params);
|
$prepare->execute($this->params);
|
||||||
|
|
||||||
$result = $method == 'rowCount' ? $prepare->rowCount() : $prepare->{$method}(PDO::FETCH_ASSOC);
|
$result = $method == 'rowCount' ? $prepare->rowCount() : $prepare->{$method}(PDO::FETCH_ASSOC);
|
||||||
$prepare->closeCursor();
|
$prepare->closeCursor();
|
||||||
|
|
||||||
$this->connection->println($startTime, microtime(true), $this->sql, $this->params);
|
$this->connection->println($startTime, microtime(true), $this->sql, $this->params);
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
} catch (Throwable $throwable) {
|
} catch (Throwable $throwable) {
|
||||||
$this->getLogger()->json_log($throwable);
|
$this->getLogger()->json_log($throwable);
|
||||||
|
|
||||||
if ($this->isRefresh($throwable)) {
|
if ($this->isRefresh($throwable)) {
|
||||||
return $this->search($method);
|
return $this->search($method);
|
||||||
}
|
}
|
||||||
|
|
||||||
$errorMsg = $throwable->getMessage() . PHP_EOL . ' Sql: ' . $this->sql . '.' . json_encode($this->params);
|
|
||||||
|
|
||||||
return $this->getLogger()->logCategory($errorMsg . PHP_EOL, 'mysql');
|
|
||||||
} finally {
|
|
||||||
$this->connection->release($client);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return bool
|
|
||||||
* @throws
|
|
||||||
*/
|
|
||||||
public function flush(): bool
|
|
||||||
{
|
|
||||||
return (bool)$this->_prepare();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return int|bool
|
|
||||||
* @throws
|
|
||||||
*/
|
|
||||||
private function _prepare(): int|bool
|
|
||||||
{
|
|
||||||
$client = $this->connection->getConnection();
|
|
||||||
try {
|
|
||||||
$startTime = microtime(true);
|
|
||||||
if (($prepare = $client->prepare($this->sql)) === false) {
|
|
||||||
throw new Exception('(' . $prepare->errorInfo()[0] . ')' . $prepare->errorInfo()[2]);
|
|
||||||
}
|
|
||||||
if ($prepare->execute($this->params) === false) {
|
|
||||||
throw new Exception('(' . $prepare->errorInfo()[0] . ')' . $prepare->errorInfo()[2]);
|
|
||||||
}
|
|
||||||
|
|
||||||
var_dump($client->inTransaction(), $prepare->errorInfo(), $client->lastInsertId());
|
$errorMsg = $throwable->getMessage() . PHP_EOL . ' Sql: ' . $this->sql . '.' . json_encode($this->params);
|
||||||
|
|
||||||
$prepare->closeCursor();
|
return $this->getLogger()->logCategory($errorMsg . PHP_EOL, 'mysql');
|
||||||
|
} finally {
|
||||||
$result = $client->lastInsertId();
|
$this->connection->release($client);
|
||||||
|
}
|
||||||
$this->connection->println($startTime, microtime(true), $this->sql, $this->params);
|
}
|
||||||
|
|
||||||
return $result == 0 ? $prepare->rowCount() : (int)$result;
|
|
||||||
} catch (Throwable $throwable) {
|
/**
|
||||||
$this->getLogger()->json_log($throwable);
|
* @return bool
|
||||||
|
* @throws
|
||||||
if ($this->isRefresh($throwable)) {
|
*/
|
||||||
|
public function flush(): bool
|
||||||
|
{
|
||||||
|
return (bool)$this->_prepare();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return int|bool
|
||||||
|
* @throws
|
||||||
|
*/
|
||||||
|
private function _prepare(): int|bool
|
||||||
|
{
|
||||||
|
$client = $this->connection->getConnection();
|
||||||
|
try {
|
||||||
|
$startTime = microtime(true);
|
||||||
|
if (($prepare = $client->prepare($this->sql)) === false) {
|
||||||
|
throw new Exception('(' . $prepare->errorInfo()[0] . ')' . $prepare->errorInfo()[2]);
|
||||||
|
}
|
||||||
|
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')) {
|
||||||
|
return $prepare->rowCount();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $result == 0 ? $prepare->rowCount() : (int)$result;
|
||||||
|
} catch (Throwable $throwable) {
|
||||||
|
$this->getLogger()->json_log($throwable);
|
||||||
|
|
||||||
|
if ($this->isRefresh($throwable)) {
|
||||||
return $this->_prepare();
|
return $this->_prepare();
|
||||||
}
|
}
|
||||||
|
|
||||||
$errorMsg = $throwable->getMessage() . PHP_EOL . ' Sql: ' . $this->sql . '.' . json_encode($this->params, JSON_UNESCAPED_UNICODE);
|
$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($errorMsg . PHP_EOL, 'mysql');
|
||||||
} finally {
|
} finally {
|
||||||
$this->connection->release($client);
|
$this->connection->release($client);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Throwable $throwable
|
* @param Throwable $throwable
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
protected function isRefresh(Throwable $throwable): bool
|
protected function isRefresh(Throwable $throwable): bool
|
||||||
{
|
{
|
||||||
$message = $throwable->getMessage();
|
$message = $throwable->getMessage();
|
||||||
|
|
||||||
// MySQL 错误处理
|
// MySQL 错误处理
|
||||||
if (str_contains($message, 'MySQL server has gone away')) {
|
if (str_contains($message, 'MySQL server has gone away')) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (str_contains($message, 'Send of 14 bytes failed with errno=32 Broken pipe')) {
|
if (str_contains($message, 'Send of 14 bytes failed with errno=32 Broken pipe')) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (str_contains($message, 'Lost connection to MySQL server during query')) {
|
if (str_contains($message, 'Lost connection to MySQL server during query')) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// PostgreSQL 错误处理
|
// PostgreSQL 错误处理
|
||||||
if (str_contains($message, 'server closed the connection unexpectedly')) {
|
if (str_contains($message, 'server closed the connection unexpectedly')) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (str_contains($message, 'Connection refused')) {
|
if (str_contains($message, 'Connection refused')) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (str_contains($message, 'Broken pipe')) {
|
if (str_contains($message, 'Broken pipe')) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (str_contains($message, 'connection to server was lost')) {
|
if (str_contains($message, 'connection to server was lost')) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return bool
|
* @return bool
|
||||||
* @throws
|
* @throws
|
||||||
*/
|
*/
|
||||||
public function delete(): bool
|
public function delete(): bool
|
||||||
{
|
{
|
||||||
return $this->_prepare();
|
return $this->_prepare();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return int|bool
|
* @return int|bool
|
||||||
*/
|
*/
|
||||||
public function exec(): int|bool
|
public function exec(): int|bool
|
||||||
{
|
{
|
||||||
return $this->_prepare();
|
return $this->_prepare();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $data
|
* @param array $data
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
public function bindValues(array $data = []): static
|
public function bindValues(array $data = []): static
|
||||||
{
|
{
|
||||||
if (count($data) > 0) {
|
if (count($data) > 0) {
|
||||||
$this->params = array_merge($this->params, $data);
|
$this->params = array_merge($this->params, $data);
|
||||||
}
|
}
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user