modify
This commit is contained in:
+27
-52
@@ -126,9 +126,6 @@ class Command extends Component
|
|||||||
} else {
|
} else {
|
||||||
$result = $this->search($type);
|
$result = $this->search($type);
|
||||||
}
|
}
|
||||||
if ($this->prepare) {
|
|
||||||
$this->prepare->closeCursor();
|
|
||||||
}
|
|
||||||
return $result;
|
return $result;
|
||||||
} catch (\Throwable $exception) {
|
} catch (\Throwable $exception) {
|
||||||
return $this->addError($this->sql . '. error: ' . $exception->getMessage(), 'mysql');
|
return $this->addError($this->sql . '. error: ' . $exception->getMessage(), 'mysql');
|
||||||
@@ -143,19 +140,23 @@ class Command extends Component
|
|||||||
*/
|
*/
|
||||||
private function search($type): mixed
|
private function search($type): mixed
|
||||||
{
|
{
|
||||||
$connect = $this->db->getConnect($this->sql);
|
if (!(($connect = $this->db->getConnect($this->sql)) instanceof PDO)) {
|
||||||
if (!($this->prepare = $connect?->query($this->sql))) {
|
return $this->addError('get client error.', 'mysql');
|
||||||
return $this->addError($connect->errorInfo()[2] ?? '数据库异常, 请稍后再试.');
|
}
|
||||||
|
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) {
|
if ($type === static::FETCH_COLUMN) {
|
||||||
$data = $this->prepare->fetchAll(PDO::FETCH_ASSOC);
|
$data = $prepare->fetchAll(PDO::FETCH_ASSOC);
|
||||||
} else if ($type === static::ROW_COUNT) {
|
} else if ($type === static::ROW_COUNT) {
|
||||||
$data = $this->prepare->rowCount();
|
$data = $prepare->rowCount();
|
||||||
} else if ($type === static::FETCH_ALL) {
|
} else if ($type === static::FETCH_ALL) {
|
||||||
$data = $this->prepare->fetchAll(PDO::FETCH_ASSOC);
|
$data = $prepare->fetchAll(PDO::FETCH_ASSOC);
|
||||||
} else {
|
} else {
|
||||||
$data = $this->prepare->fetch(PDO::FETCH_ASSOC);
|
$data = $prepare->fetch(PDO::FETCH_ASSOC);
|
||||||
}
|
}
|
||||||
|
$prepare->closeCursor();
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -168,57 +169,31 @@ class Command extends Component
|
|||||||
*/
|
*/
|
||||||
private function insert_or_change($isInsert, $hasAutoIncrement): bool|string|int
|
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)) {
|
if (!(($connect = $this->db->getConnect($this->sql)) instanceof PDO)) {
|
||||||
return $this->addError('get client error.', 'mysql');
|
return $this->addError('get client error.', 'mysql');
|
||||||
}
|
}
|
||||||
if (!(($this->prepare = $connect->prepare($this->sql)) instanceof PDOStatement)) {
|
if (!(($prepare = $connect->prepare($this->sql, [PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL])) instanceof PDOStatement)) {
|
||||||
$error = $this->prepare->errorInfo()[2] ?? static::DB_ERROR_MESSAGE;
|
$error = $prepare->errorInfo()[2] ?? static::DB_ERROR_MESSAGE;
|
||||||
|
|
||||||
return $this->addError($this->sql . ':' . $error, 'mysql');
|
return $this->addError($this->sql . ':' . $error, 'mysql');
|
||||||
}
|
}
|
||||||
$result = $this->checkResponse($this->prepare, $connect);
|
if ($prepare->execute() === false) {
|
||||||
return $result;
|
$response = $this->addError(static::DB_ERROR_MESSAGE, 'mysql');
|
||||||
}
|
} else {
|
||||||
|
$result = $connect->lastInsertId();
|
||||||
|
if ($isInsert === false) {
|
||||||
/**
|
$response = true;
|
||||||
* @param $prepare
|
} else if ($result == 0 && $hasAutoIncrement->isAutoIncrement()) {
|
||||||
* @param $connect
|
$response = $this->addError(static::DB_ERROR_MESSAGE, 'mysql');
|
||||||
* @return bool|int
|
} else {
|
||||||
* @throws \Exception
|
$response = $result == 0 ? true : $result;
|
||||||
*/
|
}
|
||||||
private function checkResponse($prepare, $connect)
|
|
||||||
{
|
|
||||||
$result = $prepare->execute($this->params);
|
|
||||||
if ($result === false) {
|
|
||||||
return $this->addError($connect->errorInfo()[2], 'mysql');
|
|
||||||
}
|
}
|
||||||
return (int)$connect->lastInsertId();
|
$prepare->closeCursor();
|
||||||
|
return $response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $modelName
|
* @param $modelName
|
||||||
* @return $this
|
* @return $this
|
||||||
|
|||||||
Reference in New Issue
Block a user