Skip to content

Commit bd4e501

Browse files
committed
return rawSql if exist or do it manually
1 parent a0f555e commit bd4e501

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

src/DBTransactionRetryHelper.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,13 @@ protected static function buildLogContext(QueryException $e, int $attempt, int $
101101
$sql = method_exists($e, 'getSql') ? $e->getSql() : null;
102102
$bindings = method_exists($e, 'getBindings') ? $e->getBindings() : [];
103103

104-
// Try to read connection name
105-
$connectionName = null;
106-
try {
107-
$connection = DB::connection();
108-
$connectionName = $connection?->getName();
109-
} catch (Throwable) {
110-
// ignore
104+
$connectionName = $e->getConnectionName();
105+
$conn = DB::connection($connectionName);
106+
107+
// if laravel version <= 11.x then getRawSql() is not available and we will do it manually
108+
$rawSql = method_exists($e, 'getRawSql') ? $e->getRawSql() : null;
109+
if (is_null($rawSql) && !is_null($sql) && !empty($bindings)) {
110+
$rawSql = $conn->getQueryGrammar()->substituteBindingsIntoRawSql($sql, $bindings);
111111
}
112112

113113
$requestData = [
@@ -136,11 +136,8 @@ protected static function buildLogContext(QueryException $e, int $attempt, int $
136136
'attempt' => $attempt,
137137
'maxRetries' => $maxRetries,
138138
'trxLabel' => $trxLabel,
139-
'Exception' => get_class($e),
140-
'message' => $e->getMessage(),
141-
'sql' => $sql,
142-
'bindings' => stringifyBindings($bindings),
143139
'errorInfo' => $e->errorInfo,
140+
'rawSql' => $rawSql,
144141
'connection' => $connectionName,
145142
'trace' => getDebugBacktraceArray(),
146143
]);

0 commit comments

Comments
 (0)