88use GearboxSolutions \EloquentFileMaker \Database \Schema \FMBuilder ;
99use GearboxSolutions \EloquentFileMaker \Exceptions \FileMakerDataApiException ;
1010use Illuminate \Database \Connection ;
11+ use Illuminate \Database \Events \QueryExecuted ;
1112use Illuminate \Http \Client \PendingRequest ;
1213use Illuminate \Http \UploadedFile ;
1314use Illuminate \Support \Arr ;
1415use Illuminate \Support \Collection ;
1516use Illuminate \Support \Facades \Cache ;
1617use Illuminate \Support \Facades \Http ;
18+ use Illuminate \Support \Str ;
1719use Symfony \Component \HttpFoundation \File \File ;
1820
1921class 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