Skip to content

Commit

Permalink
修复sql语句存在异常的时候,rawQuery() 和 query()方法不执行onQuery回调
Browse files Browse the repository at this point in the history
  • Loading branch information
kiss291323003 committed Feb 27, 2024
1 parent f6cf91a commit c97b436
Showing 1 changed file with 33 additions and 24 deletions.
57 changes: 33 additions & 24 deletions src/FastDb.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,22 +274,26 @@ function query(QueryBuilder|callable $queryBuilder,float $timeout = null):QueryR
{
$client = $this->getClient();
$t = microtime(true);
if(is_callable($queryBuilder)){
$call = $queryBuilder;
$queryBuilder = new QueryBuilder();
call_user_func($call,$queryBuilder);
$ret = $client->query($queryBuilder,$timeout);
}else{
$ret = $client->query($queryBuilder,$timeout);
}

$return = new QueryResult($t);
$return->setResult($ret);
$return->setConnection($client);
$return->setQueryBuilder(clone $queryBuilder);
$this->logStack($return);
if(is_callable($this->onQuery)){
call_user_func($this->onQuery,$return);
try{
if(is_callable($queryBuilder)){
$call = $queryBuilder;
$queryBuilder = new QueryBuilder();
call_user_func($call,$queryBuilder);
$ret = $client->query($queryBuilder,$timeout);
}else{
$ret = $client->query($queryBuilder,$timeout);
}
$return->setResult($ret);
}catch (\Throwable $throwable){
throw $throwable;
} finally {
$return->setConnection($client);
$return->setQueryBuilder(clone $queryBuilder);
$this->logStack($return);
if(is_callable($this->onQuery)){
call_user_func($this->onQuery,$return);
}
}
return $return;
}
Expand All @@ -303,14 +307,19 @@ function rawQuery(string $sql):QueryResult
{
$client = $this->getClient();
$t = microtime(true);
$ret = $client->rawQuery($sql);
$return = new QueryResult($t);
$return->setResult($ret);
$return->setConnection($client);
$return->setRawSql($sql);
$this->logStack($return);
if(is_callable($this->onQuery)){
call_user_func($this->onQuery,$return);
try {
$ret = $client->rawQuery($sql);
$return->setResult($ret);
}catch (\Throwable $throwable){
throw $throwable;
} finally {
$return->setConnection($client);
$return->setRawSql($sql);
$this->logStack($return);
if(is_callable($this->onQuery)){
call_user_func($this->onQuery,$return);
}
}
return $return;
}
Expand Down Expand Up @@ -366,8 +375,8 @@ private function getClient(bool $autoRecycle = true):Connection
$this->currentConnection[$cid][$name] = $obj;

Coroutine::defer(function ()use($cid,$name){
unset($this->currentConnection[$cid][$name]);
unset( $this->queryStack[$cid]);
unset($this->currentConnection[$cid][$name]);
unset( $this->queryStack[$cid]);
});
return $this->currentConnection[$cid][$name];
}
Expand Down

0 comments on commit c97b436

Please sign in to comment.