Skip to content

Commit

Permalink
支持selectForUpdate
Browse files Browse the repository at this point in the history
  • Loading branch information
kiss291323003 committed Sep 12, 2024
1 parent 4d234df commit ca0fc4a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
26 changes: 22 additions & 4 deletions src/AbstractInterface/AbstractEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ function setData(array $data,bool $mergeCompare = false):static
return $this;
}

function all():ListResult
function all( bool $selectForUpdate = false ):ListResult
{
$query = $this->queryLimit()->__getQueryBuilder();

Expand All @@ -108,6 +108,10 @@ function all():ListResult
$returnAsArray = $this->queryLimit()->getFields()['returnAsArray'];
}

if($selectForUpdate){
$query->selectForUpdate();
}

$query->get($this->tableName(),null,$fields);
$ret = FastDb::getInstance()->query($query);
$total = null;
Expand Down Expand Up @@ -622,7 +626,11 @@ public function find(array|string|int $queryLimit = null): ?static
return null;
}

public static function findRecord(callable|array|string|int $queryLimit, string $tableName = null): ?static
public static function findRecord(
callable|array|string|int $queryLimit,
string $tableName = null,
bool $selectForUpdate = false
): ?static
{
$entity = new static();
if (empty($tableName)) {
Expand All @@ -647,7 +655,9 @@ public static function findRecord(callable|array|string|int $queryLimit, string
}
$query->where($pk, $queryLimit);
}

if($selectForUpdate){
$query->selectForUpdate();
}
$query->get($tableName, 1);
$ret = FastDb::getInstance()->query($query)->getResult();
if (!empty($ret[0])) {
Expand All @@ -657,7 +667,12 @@ public static function findRecord(callable|array|string|int $queryLimit, string
return null;
}

public static function findAll(array|callable|string $queryLimit = null, string $tableName = null, bool $returnAsArray = false):mixed
public static function findAll(
array|callable|string $queryLimit = null,
string $tableName = null,
bool $returnAsArray = false,
bool $selectForUpdate = false
):mixed
{
$entity = new static();
if (empty($tableName)) {
Expand Down Expand Up @@ -692,6 +707,9 @@ public static function findAll(array|callable|string $queryLimit = null, string
}
}
}
if($selectForUpdate){
$query->selectForUpdate();
}

$query->get($tableName);
$result = FastDb::getInstance()->query($query)->getResult();
Expand Down
6 changes: 6 additions & 0 deletions src/Beans/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ function func(callable $func):Query
return $this;
}

function selectForUpdate():Query
{
$this->queryBuilder->selectForUpdate();
return $this;
}

function returnEntity():AbstractEntity
{
return $this->entity;
Expand Down

0 comments on commit ca0fc4a

Please sign in to comment.