This commit is contained in:
as2252258@163.com
2021-05-02 15:06:38 +08:00
parent bba045d69a
commit 145da638c9
+27 -52
View File
@@ -126,9 +126,6 @@ class Command extends Component
} else {
$result = $this->search($type);
}
if ($this->prepare) {
$this->prepare->closeCursor();
}
return $result;
} catch (\Throwable $exception) {
return $this->addError($this->sql . '. error: ' . $exception->getMessage(), 'mysql');
@@ -143,19 +140,23 @@ class Command extends Component
*/
private function search($type): mixed
{
$connect = $this->db->getConnect($this->sql);
if (!($this->prepare = $connect?->query($this->sql))) {
return $this->addError($connect->errorInfo()[2] ?? '数据库异常, 请稍后再试.');
if (!(($connect = $this->db->getConnect($this->sql)) instanceof PDO)) {
return $this->addError('get client error.', 'mysql');
}
if (!(($prepare = $connect->prepare($this->sql, [PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL])) instanceof PDOStatement)) {
$error = $prepare->errorInfo()[2] ?? static::DB_ERROR_MESSAGE;
return $this->addError($this->sql . ':' . $error, 'mysql');
}
if ($type === static::FETCH_COLUMN) {
$data = $this->prepare->fetchAll(PDO::FETCH_ASSOC);
$data = $prepare->fetchAll(PDO::FETCH_ASSOC);
} else if ($type === static::ROW_COUNT) {
$data = $this->prepare->rowCount();
$data = $prepare->rowCount();
} else if ($type === static::FETCH_ALL) {
$data = $this->prepare->fetchAll(PDO::FETCH_ASSOC);
$data = $prepare->fetchAll(PDO::FETCH_ASSOC);
} else {
$data = $this->prepare->fetch(PDO::FETCH_ASSOC);
$data = $prepare->fetch(PDO::FETCH_ASSOC);
}
$prepare->closeCursor();
return $data;
}
@@ -168,57 +169,31 @@ class Command extends Component
*/
private function insert_or_change($isInsert, $hasAutoIncrement): bool|string|int
{
if (($result = $this->initPDOStatement()) === false) {
return $result;
}
if ($isInsert === false) {
return true;
}
if ($result == 0 && $hasAutoIncrement->isAutoIncrement()) {
return $this->addError(static::DB_ERROR_MESSAGE, 'mysql');
}
return $result == 0 ? true : $result;
}
/**
* 重新构建
* @throws
*/
private function initPDOStatement(): bool|int
{
if (empty($this->sql)) {
return $this->addError('no sql.', 'mysql');
}
if (!(($connect = $this->db->getConnect($this->sql)) instanceof PDO)) {
return $this->addError('get client error.', 'mysql');
}
if (!(($this->prepare = $connect->prepare($this->sql)) instanceof PDOStatement)) {
$error = $this->prepare->errorInfo()[2] ?? static::DB_ERROR_MESSAGE;
if (!(($prepare = $connect->prepare($this->sql, [PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL])) instanceof PDOStatement)) {
$error = $prepare->errorInfo()[2] ?? static::DB_ERROR_MESSAGE;
return $this->addError($this->sql . ':' . $error, 'mysql');
}
$result = $this->checkResponse($this->prepare, $connect);
return $result;
}
/**
* @param $prepare
* @param $connect
* @return bool|int
* @throws \Exception
*/
private function checkResponse($prepare, $connect)
{
$result = $prepare->execute($this->params);
if ($result === false) {
return $this->addError($connect->errorInfo()[2], 'mysql');
if ($prepare->execute() === false) {
$response = $this->addError(static::DB_ERROR_MESSAGE, 'mysql');
} else {
$result = $connect->lastInsertId();
if ($isInsert === false) {
$response = true;
} else if ($result == 0 && $hasAutoIncrement->isAutoIncrement()) {
$response = $this->addError(static::DB_ERROR_MESSAGE, 'mysql');
} else {
$response = $result == 0 ? true : $result;
}
}
return (int)$connect->lastInsertId();
$prepare->closeCursor();
return $response;
}
/**
* @param $modelName
* @return $this