变更
This commit is contained in:
+51
-2
@@ -26,6 +26,8 @@ class Command extends Component
|
|||||||
const EXECUTE = 'execute';
|
const EXECUTE = 'execute';
|
||||||
const FETCH_COLUMN = 'fetchColumn';
|
const FETCH_COLUMN = 'fetchColumn';
|
||||||
|
|
||||||
|
const DB_ERROR_MESSAGE = 'The system is busy, please try again later.';
|
||||||
|
|
||||||
/** @var Connection */
|
/** @var Connection */
|
||||||
public Connection $db;
|
public Connection $db;
|
||||||
|
|
||||||
@@ -139,7 +141,17 @@ class Command extends Component
|
|||||||
{
|
{
|
||||||
$pdo = $this->db->getPdo();
|
$pdo = $this->db->getPdo();
|
||||||
try {
|
try {
|
||||||
$result = $pdo->execute($this->sql, $this->params);
|
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();
|
||||||
|
$prepare->closeCursor();
|
||||||
|
|
||||||
|
$result = $result == 0 ? true : $result;
|
||||||
} catch (\Throwable $exception) {
|
} catch (\Throwable $exception) {
|
||||||
$result = $this->logger->addError($this->sql . '. error: ' . $exception->getMessage(), 'mysql');
|
$result = $this->logger->addError($this->sql . '. error: ' . $exception->getMessage(), 'mysql');
|
||||||
} finally {
|
} finally {
|
||||||
@@ -149,6 +161,33 @@ class Command extends Component
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param \PDO $pdo
|
||||||
|
* @param string $sql
|
||||||
|
* @param array $params
|
||||||
|
* @return PDOStatement
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
private function queryPrev(\PDO $pdo): PDOStatement
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
if (($statement = $pdo->query($this->sql)) === false) {
|
||||||
|
throw new Exception($pdo->errorInfo()[1]);
|
||||||
|
}
|
||||||
|
foreach ($this->params as $key => $param) {
|
||||||
|
$statement->bindValue($key, $param);
|
||||||
|
}
|
||||||
|
return $statement;
|
||||||
|
} catch (\PDOException|\Throwable $throwable) {
|
||||||
|
if (str_contains($throwable->getMessage(), 'MySQL server has gone away')) {
|
||||||
|
unset($pdo);
|
||||||
|
return $this->queryPrev($this->db->getSlaveClient());
|
||||||
|
}
|
||||||
|
throw new Exception($throwable->getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $type
|
* @param string $type
|
||||||
* @return array|int|bool|null
|
* @return array|int|bool|null
|
||||||
@@ -158,7 +197,17 @@ class Command extends Component
|
|||||||
{
|
{
|
||||||
$pdo = $this->db->getSlaveClient();
|
$pdo = $this->db->getSlaveClient();
|
||||||
try {
|
try {
|
||||||
$data = $pdo->{$type}($this->sql, $this->params);
|
if ($type == self::FETCH_ALL) {
|
||||||
|
$data = $this->queryPrev($pdo)->fetchAll(\PDO::FETCH_ASSOC);
|
||||||
|
} else if ($type == self::FETCH) {
|
||||||
|
$data = $this->queryPrev($pdo)->fetch(\PDO::FETCH_ASSOC);
|
||||||
|
} else if ($type == self::FETCH_COLUMN) {
|
||||||
|
$data = $this->queryPrev($pdo)->fetchColumn(\PDO::FETCH_ASSOC);
|
||||||
|
} else if ($type == self::ROW_COUNT) {
|
||||||
|
$data = $this->queryPrev($pdo)->rowCount();
|
||||||
|
} else {
|
||||||
|
$data = null;
|
||||||
|
}
|
||||||
} catch (\Throwable $throwable) {
|
} catch (\Throwable $throwable) {
|
||||||
$data = $this->logger->addError($this->sql . '. error: ' . $throwable->getMessage(), 'mysql');
|
$data = $this->logger->addError($this->sql . '. error: ' . $throwable->getMessage(), 'mysql');
|
||||||
} finally {
|
} finally {
|
||||||
|
|||||||
+1
-1
@@ -14,7 +14,7 @@ namespace Database;
|
|||||||
use Database\Affair\BeginTransaction;
|
use Database\Affair\BeginTransaction;
|
||||||
use Database\Affair\Commit;
|
use Database\Affair\Commit;
|
||||||
use Database\Affair\Rollback;
|
use Database\Affair\Rollback;
|
||||||
use Database\Mysql\PDO;
|
use PDO;
|
||||||
use Database\Mysql\Schema;
|
use Database\Mysql\Schema;
|
||||||
use Exception;
|
use Exception;
|
||||||
use Kiri;
|
use Kiri;
|
||||||
|
|||||||
Reference in New Issue
Block a user