Skip to content

Commit

Permalink
Merge pull request #138 from socialblue/add-capabililty-to-show-faile…
Browse files Browse the repository at this point in the history
…d-queries

feat: add capability to show failed queries
  • Loading branch information
mbroersen committed Oct 31, 2023
2 parents b4fbf8f + f84f9c5 commit 8505cd3
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 10 deletions.
41 changes: 41 additions & 0 deletions src/DataListener/QueryListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
namespace Socialblue\LaravelQueryAdviser\DataListener;

use Illuminate\Database\Events\QueryExecuted;
use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\Cache;
use Socialblue\LaravelQueryAdviser\DataListener\Services\BindingsMapper;
use Socialblue\LaravelQueryAdviser\DataListener\Services\SessionFormatter;
use Socialblue\LaravelQueryAdviser\DataListener\Services\TraceMapper;
use Socialblue\LaravelQueryAdviser\Helper\QueryBuilderHelper;

class QueryListener
{
Expand Down Expand Up @@ -34,6 +38,43 @@ public static function listen(QueryExecuted $query)
);
}

public static function onQueryException(QueryException $queryException): void
{
if (config('laravel-query-adviser.enable_query_logging') === false) {
return;
}

$sessionKey = self::getSessionKey();

if (empty($sessionKey)) {
return;
}

$url = url()->current();
if (str_contains($url, '/query-adviser')) {
return;
}

$time = time();
$data = self::getFromCache($time, $sessionKey);

$key = count($data[$time]);
$data[$time][$key] = [
'time' => $time,
'timeKey' => $key,
'errorInfo' => $queryException->errorInfo,
'backtrace' => $queryException->getTrace(),
'sql' => QueryBuilderHelper::combineQueryAndBindings($queryException->getSql(), $queryException->getBindings()),
'rawSql' => $queryException->getSql(),
'bindings' => (new BindingsMapper())->toCache($queryException->getBindings()),
'queryTime' => 0,
'url' => empty($url) ? '/' : $url,
'referer' => empty($referer) ? '/' : $referer,
];
self::putToCache($data, $sessionKey);
}


/**
* @param $sessionKey
*/
Expand Down
34 changes: 24 additions & 10 deletions src/LaravelQueryAdviserServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Socialblue\LaravelQueryAdviser;

use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;
Expand All @@ -15,9 +16,6 @@ class LaravelQueryAdviserServiceProvider extends ServiceProvider
*/
public function boot()
{
/*
* Optional methods to load your package assets
*/
$this->loadViewsFrom(__DIR__ . '/../resources/views', 'QueryAdviser');

Route::group([
Expand Down Expand Up @@ -46,12 +44,10 @@ public function boot()
/**
* Register the application services.
*/
public function register()
public function register(): void
{
// Automatically apply the package configuration
$this->mergeConfigFrom(__DIR__ . '/../config/config.php', 'laravel-query-adviser');

// Register the main class to use with the facade
$this->app->singleton('laravel-query-adviser', function () {
return new LaravelQueryAdviser();
});
Expand All @@ -60,12 +56,15 @@ public function register()
/**
* Add helper functions to app
*/
protected function bootLaravelQueryAdviser()
protected function bootLaravelQueryAdviser(): void
{
DB::listen(static function ($query) {
QueryListener::listen($query);
});
$this->setupQueryLog();
$this->setExceptionHandler();
$this->setupMacro();
}

private function setupMacro(): void
{
\Illuminate\Database\Eloquent\Builder::macro(config('laravel-query-adviser.macros.dd'), function () {
dd(QueryBuilderHelper::infoByBuilder($this));
});
Expand All @@ -82,4 +81,19 @@ protected function bootLaravelQueryAdviser()
dump(QueryBuilderHelper::infoByBuilder($this));
});
}

private function setExceptionHandler(): void
{
set_exception_handler(function (QueryException $exception) {
QueryListener::onQueryException($exception);
throw $exception;
});
}

private function setupQueryLog(): void
{
DB::listen(static function ($query) {
QueryListener::listen($query);
});
}
}

0 comments on commit 8505cd3

Please sign in to comment.