This commit is contained in:
2021-08-18 15:06:23 +08:00
parent 46001126e5
commit 4e5bfe4887
2 changed files with 10 additions and 8 deletions
+4 -6
View File
@@ -13,7 +13,6 @@ namespace Database;
use Exception; use Exception;
use Kiri\Abstracts\Component; use Kiri\Abstracts\Component;
use Kiri\Core\Json; use Kiri\Core\Json;
use PDO;
use PDOStatement; use PDOStatement;
/** /**
@@ -37,10 +36,10 @@ class Command extends Component
/** @var array */ /** @var array */
public array $params = []; public array $params = [];
/** @var string */ /** @var string */
public string $dbname = ''; public string $dbname = '';
/** @var PDOStatement|null */ /** @var PDOStatement|null */
private ?PDOStatement $prepare = null; private ?PDOStatement $prepare = null;
@@ -129,7 +128,6 @@ class Command extends Component
if (microtime(true) - $time >= 0.02) { if (microtime(true) - $time >= 0.02) {
$this->warning('Mysql:' . Json::encode([$this->sql, $this->params]) . (microtime(true) - $time)); $this->warning('Mysql:' . Json::encode([$this->sql, $this->params]) . (microtime(true) - $time));
} }
$this->prepare?->closeCursor();
} catch (\Throwable $exception) { } catch (\Throwable $exception) {
$result = $this->addError($this->sql . '. error: ' . $exception->getMessage(), 'mysql'); $result = $this->addError($this->sql . '. error: ' . $exception->getMessage(), 'mysql');
} finally { } finally {
@@ -169,8 +167,8 @@ class Command extends Component
private function insert_or_change($isInsert, $hasAutoIncrement): bool|int private function insert_or_change($isInsert, $hasAutoIncrement): bool|int
{ {
$pdo = $this->db->getConnect($this->sql); $pdo = $this->db->getConnect($this->sql);
$result = $pdo->execute($this->sql, $this->params); $result = $pdo->execute($this->sql, $isInsert, $this->params);
if (($hasAutoIncrement || !$isInsert) && $result == 0){ if ($hasAutoIncrement && $result == 0) {
return false; return false;
} }
return $result; return $result;
+6 -2
View File
@@ -222,11 +222,12 @@ class PDO implements StopHeartbeatCheck
/** /**
* @param string $sql * @param string $sql
* @param $isInsert
* @param array $params * @param array $params
* @return int * @return int
* @throws Exception * @throws Exception
*/ */
public function execute(string $sql, array $params = []): int public function execute(string $sql, $isInsert, array $params = []): int
{ {
$this->_last = time(); $this->_last = time();
if (!(($prepare = $this->_pdo()->prepare($sql)) instanceof PDOStatement)) { if (!(($prepare = $this->_pdo()->prepare($sql)) instanceof PDOStatement)) {
@@ -236,7 +237,10 @@ class PDO implements StopHeartbeatCheck
if ($prepare->execute($params) === false) { if ($prepare->execute($params) === false) {
throw new Exception($prepare->errorInfo()[2] ?? static::DB_ERROR_MESSAGE); throw new Exception($prepare->errorInfo()[2] ?? static::DB_ERROR_MESSAGE);
} }
return (int)$this->_pdo()->lastInsertId(); if (!$isInsert) {
return (int)$this->_pdo()->lastInsertId();
}
return 1;
} }