14
14
15
15
namespace RodrigoPedra \QueryLogger ;
16
16
17
- use Illuminate \Database \ConnectionInterface ;
18
17
use Illuminate \Database \Events \QueryExecuted ;
19
18
use Psr \Log \LoggerInterface ;
20
19
21
20
class QueryLogger
22
21
{
23
- protected ConnectionInterface $ connection ;
24
22
protected LoggerInterface $ logger ;
25
- protected $ pdo = null ;
26
23
27
- public function __construct (ConnectionInterface $ connection , LoggerInterface $ logger )
24
+ public function __construct (LoggerInterface $ logger )
28
25
{
29
- $ this ->connection = $ connection ;
30
26
$ this ->logger = $ logger ;
31
-
32
- if (\method_exists ($ connection , 'getPdo ' )) {
33
- $ this ->pdo = $ connection ->getPdo ();
34
- }
35
27
}
36
28
37
29
public function handle (QueryExecuted $ event )
38
30
{
39
- $ bindings = $ this ->connection ->prepareBindings ($ event ->bindings );
40
- $ bindings = \array_map ([$ this , 'prepareValue ' ], $ bindings );
31
+ $ pdo = \method_exists ($ event ->connection , 'getPdo ' )
32
+ ? $ event ->connection ->getPdo ()
33
+ : null ;
34
+
35
+ $ bindings = $ event ->connection ->prepareBindings ($ event ->bindings );
36
+ $ bindings = \array_map (fn ($ value ) => $ this ->prepareValue ($ pdo , $ value ), $ bindings );
41
37
42
38
$ query = $ this ->prepareQuery ($ event ->sql , $ bindings );
43
39
44
- $ this ->logger ->info ($ query , ['bindings ' => $ event ->bindings , 'time ' => $ event ->time ]);
40
+ $ this ->logger ->info ($ query , [
41
+ 'bindings ' => $ event ->bindings ,
42
+ 'time ' => $ event ->time ,
43
+ 'connection ' => $ event ->connectionName ,
44
+ ]);
45
45
}
46
46
47
47
protected function prepareQuery (string $ query , array $ bindings ): string
@@ -57,7 +57,7 @@ protected function prepareQuery(string $query, array $bindings): string
57
57
return $ query ;
58
58
}
59
59
60
- protected function prepareValue ($ value ): string
60
+ protected function prepareValue ($ pdo , $ value ): string
61
61
{
62
62
if (\is_null ($ value )) {
63
63
return 'NULL ' ;
@@ -72,7 +72,7 @@ protected function prepareValue($value): string
72
72
}
73
73
74
74
if (\is_string ($ value ) && ! \mb_check_encoding ($ value , 'UTF-8 ' )) {
75
- return $ this ->quote ('[BINARY DATA] ' );
75
+ return $ this ->quote ($ pdo , '[BINARY DATA] ' );
76
76
}
77
77
78
78
if (\is_object ($ value ) && \method_exists ($ value , '__toString ' )) {
@@ -88,13 +88,13 @@ protected function prepareValue($value): string
88
88
}
89
89
90
90
// objects not implementing __toString() or toString() will fail here
91
- return $ this ->quote (\strval ($ value ));
91
+ return $ this ->quote ($ pdo , \strval ($ value ));
92
92
}
93
93
94
- protected function quote (string $ value ): string
94
+ protected function quote ($ pdo , string $ value ): string
95
95
{
96
- if ($ this -> pdo ) {
97
- return $ this -> pdo ->quote ($ value );
96
+ if ($ pdo ) {
97
+ return $ pdo ->quote ($ value );
98
98
}
99
99
100
100
$ search = ["\\" , "\x00" , "\n" , "\r" , "' " , '" ' , "\x1a" ];
0 commit comments