From 4e5bfe4887ee1adcea222e3373c4c6029bf11d60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Wed, 18 Aug 2021 15:06:23 +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 --- src/Command.php | 10 ++++------ src/Mysql/PDO.php | 8 ++++++-- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/Command.php b/src/Command.php index 5ed01bd..a9b3035 100644 --- a/src/Command.php +++ b/src/Command.php @@ -13,7 +13,6 @@ namespace Database; use Exception; use Kiri\Abstracts\Component; use Kiri\Core\Json; -use PDO; use PDOStatement; /** @@ -37,10 +36,10 @@ class Command extends Component /** @var array */ public array $params = []; - /** @var string */ + /** @var string */ public string $dbname = ''; - /** @var PDOStatement|null */ + /** @var PDOStatement|null */ private ?PDOStatement $prepare = null; @@ -129,7 +128,6 @@ class Command extends Component if (microtime(true) - $time >= 0.02) { $this->warning('Mysql:' . Json::encode([$this->sql, $this->params]) . (microtime(true) - $time)); } - $this->prepare?->closeCursor(); } catch (\Throwable $exception) { $result = $this->addError($this->sql . '. error: ' . $exception->getMessage(), 'mysql'); } finally { @@ -169,8 +167,8 @@ class Command extends Component private function insert_or_change($isInsert, $hasAutoIncrement): bool|int { $pdo = $this->db->getConnect($this->sql); - $result = $pdo->execute($this->sql, $this->params); - if (($hasAutoIncrement || !$isInsert) && $result == 0){ + $result = $pdo->execute($this->sql, $isInsert, $this->params); + if ($hasAutoIncrement && $result == 0) { return false; } return $result; diff --git a/src/Mysql/PDO.php b/src/Mysql/PDO.php index 24f34de..27f718f 100644 --- a/src/Mysql/PDO.php +++ b/src/Mysql/PDO.php @@ -222,11 +222,12 @@ class PDO implements StopHeartbeatCheck /** * @param string $sql + * @param $isInsert * @param array $params * @return int * @throws Exception */ - public function execute(string $sql, array $params = []): int + public function execute(string $sql, $isInsert, array $params = []): int { $this->_last = time(); if (!(($prepare = $this->_pdo()->prepare($sql)) instanceof PDOStatement)) { @@ -236,7 +237,10 @@ class PDO implements StopHeartbeatCheck if ($prepare->execute($params) === false) { throw new Exception($prepare->errorInfo()[2] ?? static::DB_ERROR_MESSAGE); } - return (int)$this->_pdo()->lastInsertId(); + if (!$isInsert) { + return (int)$this->_pdo()->lastInsertId(); + } + return 1; }