From 2bd723775e643cb62e8caf8b22a906b6a85b69bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Wed, 29 Sep 2021 17:54:48 +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/Model.php | 30 ++++++++++++++++++------------ src/SqlBuilder.php | 6 +++++- src/Traits/QueryTrait.php | 14 ++++++++++++++ 3 files changed, 37 insertions(+), 13 deletions(-) diff --git a/src/Model.php b/src/Model.php index e8b61af..0e6469b 100644 --- a/src/Model.php +++ b/src/Model.php @@ -105,23 +105,24 @@ class Model extends Base\Model * @throws NotFindClassException * @throws Exception */ - public static function findOrCreate(array $condition, array $attributes = []): bool|static + public static function findOrCreate(array $condition, array $attributes): bool|static { $logger = Kiri::app()->getLogger(); - - /** @var static $select */ - $select = static::query()->where($condition)->first(); - if (!empty($select)) { - return $select; - } if (empty($attributes)) { return $logger->addError(FIND_OR_CREATE_MESSAGE, 'mysql'); } - $select = duplicate(static::class); - $select->attributes = $attributes; - if (!$select->save()) { - return $logger->addError($select->getLastError(), 'mysql'); + Db::beginTransaction(); + /** @var static $select */ + $select = static::query()->where($condition)->first(); + if (empty($select)) { + $select = duplicate(static::class); + $select->attributes = $attributes; + if (!$select->save()) { + Db::rollback(); + return $logger->addError($select->getLastError(), 'mysql'); + } } + Db::commit(); return $select; } @@ -138,6 +139,8 @@ class Model extends Base\Model if (empty($attributes)) { return $logger->addError(FIND_OR_CREATE_MESSAGE, 'mysql'); } + + Db::beginTransaction(); /** @var static $select */ $select = static::query()->where($condition)->first(); if (empty($select)) { @@ -145,7 +148,10 @@ class Model extends Base\Model } $select->attributes = $attributes; if (!$select->save()) { - return $logger->addError($select->getLastError(), 'mysql'); + Db::rollback(); + $select = $logger->addError($select->getLastError(), 'mysql'); + } else { + Db::commit(); } return $select; } diff --git a/src/SqlBuilder.php b/src/SqlBuilder.php index cd8349e..115890b 100644 --- a/src/SqlBuilder.php +++ b/src/SqlBuilder.php @@ -276,7 +276,11 @@ class SqlBuilder extends Component if ($hasOrder === true && !empty($this->query->order)) { $select .= $this->builderOrder($this->query->order); } - return $select . $this->builderLimit($this->query); + $sql = $select . $this->builderLimit($this->query); + if ($this->query->lock) { + $sql .= ' FOR UPDATE'; + } + return $sql; } diff --git a/src/Traits/QueryTrait.php b/src/Traits/QueryTrait.php index be33a54..9c1bc1e 100644 --- a/src/Traits/QueryTrait.php +++ b/src/Traits/QueryTrait.php @@ -38,6 +38,9 @@ trait QueryTrait public string $alias = 't1'; public array $filter = []; + + public bool $lock = false; + public bool $ifNotWhere = false; @@ -83,6 +86,17 @@ trait QueryTrait } + /** + * @param bool $lock + * @return $this + */ + public function lock(bool $lock): static + { + $this->lock = $lock; + return $this; + } + + /** * @param string $whereRaw * @return QueryTrait