Skip to content

Commit c25e0f6

Browse files
Fire eloquent's QueryExecuted event when making requests to the DB
1 parent 2c4bf59 commit c25e0f6

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

src/Services/FileMakerConnection.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88
use GearboxSolutions\EloquentFileMaker\Database\Schema\FMBuilder;
99
use GearboxSolutions\EloquentFileMaker\Exceptions\FileMakerDataApiException;
1010
use Illuminate\Database\Connection;
11+
use Illuminate\Database\Events\QueryExecuted;
1112
use Illuminate\Http\Client\PendingRequest;
1213
use Illuminate\Http\UploadedFile;
1314
use Illuminate\Support\Arr;
1415
use Illuminate\Support\Collection;
1516
use Illuminate\Support\Facades\Cache;
1617
use Illuminate\Support\Facades\Http;
18+
use Illuminate\Support\Str;
1719
use Symfony\Component\HttpFoundation\File\File;
1820

1921
class FileMakerConnection extends Connection
@@ -690,6 +692,8 @@ protected function prepareRequestForSending($request)
690692
*/
691693
protected function makeRequest($method, $url, $params = [], ?PendingRequest $request = null)
692694
{
695+
$start = microtime(true);
696+
693697
$this->login();
694698

695699
$request = $this->prepareRequestForSending($request);
@@ -713,12 +717,65 @@ protected function makeRequest($method, $url, $params = [], ?PendingRequest $req
713717
}
714718
}
715719

720+
$this->logFMQuery($method, $url, $params, $start);
721+
716722
// Return the JSON response
717723
$json = $response->json();
718724

719725
return $json;
720726
}
721727

728+
protected function logFMQuery($method, $url, $params, $start)
729+
{
730+
$commandType = $this->getSqlCommandType($method, $url);
731+
732+
// Clockwork specifically looks for the commandType as the first word in the "sql" string
733+
$sql = <<<DOC
734+
{$commandType}
735+
Method: {$method}
736+
URL: {$url}
737+
DOC;
738+
739+
if (count($params) > 0) {
740+
$sql .= "\nData: " . json_encode($params, JSON_PRETTY_PRINT);
741+
}
742+
743+
$this->event(new QueryExecuted($sql, Arr::get($params, 'query', []), $this->getElapsedTime($start), $this));
744+
}
745+
746+
protected function getSqlCommandType($method, $url)
747+
{
748+
if ($method === 'delete') {
749+
return 'delete';
750+
}
751+
752+
if ($method === 'patch') {
753+
return 'update';
754+
}
755+
756+
if ($method === 'get') {
757+
if (Str::contains($url, 'script')) {
758+
return 'exec';
759+
}
760+
761+
return 'select';
762+
}
763+
764+
if ($method === 'post') {
765+
if (Str::contains($url, 'containers')) {
766+
return 'update';
767+
}
768+
769+
if (Str::contains($url, '_find')) {
770+
return 'select';
771+
}
772+
773+
return 'insert';
774+
}
775+
776+
return 'other';
777+
}
778+
722779
public function setRetries($retries)
723780
{
724781
$this->retries = $retries;

0 commit comments

Comments
 (0)