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 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;
+6 -2
View File
@@ -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;
}