This commit is contained in:
as2252258@163.com
2021-05-02 14:28:16 +08:00
parent 7bef5a75a4
commit b2f8f90b7d
+20 -13
View File
@@ -34,7 +34,7 @@ class Command extends Component
public Connection $db; public Connection $db;
/** @var ?string */ /** @var ?string */
public ?string $sql = ''; private ?string $sql = '';
/** @var array */ /** @var array */
public array $params = []; public array $params = [];
@@ -126,6 +126,9 @@ 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');
@@ -141,19 +144,18 @@ class Command extends Component
private function search($type): mixed private function search($type): mixed
{ {
$connect = $this->db->getConnect($this->sql); $connect = $this->db->getConnect($this->sql);
if (!($query = $connect?->query($this->sql))) { if (!($this->prepare = $connect?->query($this->sql))) {
return $this->addError($connect->errorInfo()[2] ?? '数据库异常, 请稍后再试.'); return $this->addError($connect->errorInfo()[2] ?? '数据库异常, 请稍后再试.');
} }
if ($type === static::FETCH_COLUMN) { if ($type === static::FETCH_COLUMN) {
$data = $query->fetchAll(PDO::FETCH_ASSOC); $data = $this->prepare->fetchAll(PDO::FETCH_ASSOC);
} else if ($type === static::ROW_COUNT) { } else if ($type === static::ROW_COUNT) {
$data = $query->rowCount(); $data = $this->prepare->rowCount();
} else if ($type === static::FETCH_ALL) { } else if ($type === static::FETCH_ALL) {
$data = $query->fetchAll(PDO::FETCH_ASSOC); $data = $this->prepare->fetchAll(PDO::FETCH_ASSOC);
} else { } else {
$data = $query->fetch(PDO::FETCH_ASSOC); $data = $this->prepare->fetch(PDO::FETCH_ASSOC);
} }
$query->closeCursor();
return $data; return $data;
} }
@@ -191,17 +193,22 @@ class Command extends Component
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 (!(($prepare = $connect->prepare($this->sql)) instanceof PDOStatement)) { if (!(($this->prepare = $connect->prepare($this->sql)) instanceof PDOStatement)) {
$error = $prepare->errorInfo()[2] ?? static::DB_ERROR_MESSAGE; $error = $this->prepare->errorInfo()[2] ?? static::DB_ERROR_MESSAGE;
return $this->addError($this->sql . ':' . $error, 'mysql'); return $this->addError($this->sql . ':' . $error, 'mysql');
} }
$result = $this->checkResponse($prepare, $connect); $result = $this->checkResponse($this->prepare, $connect);
$prepare->closeCursor();
return $result; return $result;
} }
/**
* @param $prepare
* @param $connect
* @return bool|int
* @throws \Exception
*/
private function checkResponse($prepare, $connect) private function checkResponse($prepare, $connect)
{ {
$result = $prepare->execute($this->params); $result = $prepare->execute($this->params);
@@ -251,10 +258,10 @@ class Command extends Component
} }
/** /**
* @param array|null $data * @param array $data
* @return $this * @return $this
*/ */
public function bindValues(array $data = NULL): static public function bindValues(array $data = []): static
{ {
if (!is_array($this->params)) { if (!is_array($this->params)) {
$this->params = []; $this->params = [];