Skip to content

Commit

Permalink
Fixed bug that sql listener cannot work when the value contains ?. (#…
Browse files Browse the repository at this point in the history
…78)


Co-authored-by: ζŽι“­ζ˜• <[email protected]>
  • Loading branch information
c-v-c-v and limingxinleo authored Aug 18, 2022
1 parent 3355a4c commit c07be0f
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions app/Listener/DbQueryExecutedListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Hyperf\Event\Contract\ListenerInterface;
use Hyperf\Logger\LoggerFactory;
use Hyperf\Utils\Arr;
use Hyperf\Utils\Str;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;

Expand Down Expand Up @@ -48,8 +47,15 @@ public function process(object $event): void
if ($event instanceof QueryExecuted) {
$sql = $event->sql;
if (! Arr::isAssoc($event->bindings)) {
foreach ($event->bindings as $key => $value) {
$sql = Str::replaceFirst('?', "'{$value}'", $sql);
$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);
}
}

Expand Down

0 comments on commit c07be0f

Please sign in to comment.