From 618c905f7a0a345dd41645f75e6ede135a0dd4b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Sun, 6 Sep 2020 04:22:52 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Database/Command.php | 38 ++++++++++++++++---------------------- 1 file changed, 16 insertions(+), 22 deletions(-) diff --git a/Database/Command.php b/Database/Command.php index 432e25e8..77ce3a1d 100644 --- a/Database/Command.php +++ b/Database/Command.php @@ -41,18 +41,6 @@ class Command extends Component /** @var PDOStatement */ private $prepare; - /** @var PDO $connection */ - private $connection; - - /** - * @return PDO - * @throws Exception - */ - private function getConnection() - { - return $this->connection = $this->db->getConnect($this->sql); - } - /** * 重新构建 * @throws @@ -60,15 +48,16 @@ class Command extends Component private function initPDOStatement() { if (empty($this->sql)) { - return; + return null; } - if (!(($connect = $this->getConnection()) instanceof PDO)) { - throw new Exception('数据库繁忙, 请稍后再试!'); + if (!(($connect = $this->db->getConnect($this->sql)) instanceof PDO)) { + return null; } $this->prepare = $connect->prepare($this->sql); if (!($this->prepare instanceof PDOStatement)) { - throw new Exception($this->sql . ':' . $this->getError()); + return $this->addError($this->sql . ':' . $this->getError()); } + return $this->prepare; } /** @@ -242,15 +231,18 @@ class Command extends Component */ private function search($type) { - $connect = $this->getConnection()->query($this->sql); + if (!(($connect = $this->db->getConnect($this->sql)) instanceof PDO)) { + return null; + } + if (!($query = $connect->query($this->sql))) return null; if ($type === static::ROW_COUNT) { - $result = $connect->rowCount(); + $result = $query->rowCount(); } else if ($type === static::FETCH_COLUMN) { - $result = $connect->fetchColumn(); + $result = $query->fetchColumn(); } else if ($type === static::FETCH_ALL) { - $result = $connect->fetchAll(PDO::FETCH_ASSOC); + $result = $query->fetchAll(PDO::FETCH_ASSOC); } else { - $result = $connect->fetch(PDO::FETCH_ASSOC); + $result = $query->fetch(PDO::FETCH_ASSOC); } return $result; } @@ -263,7 +255,9 @@ class Command extends Component */ private function insert_or_change($isInsert, $hasAutoIncrement) { - $this->initPDOStatement(); + if (!$this->initPDOStatement()) { + return false; + } $this->bind($this->prepare); if (($result = $this->prepare->execute()) === false) { return $this->addErrorLog();