Files
wh-api/app/Listener/DbQueryExecutedListener.php
ykxiao 0b2299c427
Some checks failed
Build Docker / build (push) Has been cancelled
协程版仓库后端项目
2025-07-08 14:59:47 +08:00

81 lines
2.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace App\Listener;
use Hyperf\Collection\Arr;
use Hyperf\Database\Events\QueryExecuted;
use Hyperf\Event\Annotation\Listener;
use Hyperf\Event\Contract\ListenerInterface;
use Hyperf\Logger\LoggerFactory;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Log\LoggerInterface;
#[Listener]
class DbQueryExecutedListener implements ListenerInterface
{
/**
* @var LoggerInterface
*/
private LoggerInterface $logger;
/**
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function __construct(ContainerInterface $container)
{
$this->logger = $container->get(LoggerFactory::class)->get('sql', 'sql');
}
public function listen(): array
{
return [
QueryExecuted::class,
];
}
/**
* @param object $event
*/
public function process(object $event): void
{
/**
* 处理QueryExecuted事件记录查询的SQL和执行时间。
*
* @param QueryExecuted $event 该事件对象包含执行的SQL语句、绑定参数和执行时间
*/
if ($event instanceof QueryExecuted) {
$sql = $event->sql; // 获取执行的SQL语句
// 检查绑定参数是否为关联数组,如果不是,将 "?" 替换为实际的参数值
if (!Arr::isAssoc($event->bindings)) {
$position = 0; // 初始化绑定参数的搜索位置
foreach ($event->bindings as $value) { // 遍历绑定参数
$position = strpos($sql, '?', $position); // 查找下一个 "?" 位置
if ($position === false) { // 如果找不到 "?",则退出循环
break;
}
$value = "'$value'"; // 将参数值包裹在单引号中
$sql = substr_replace($sql, $value, $position, 1); // 替换 "?" 为实际参数值
$position += strlen($value); // 更新搜索位置
}
}
// 使用logger记录SQL语句和执行时间
$this->logger->info(sprintf('[%s] %s', $event->time, $sql));
}
}
}