Earlier in the project, I used the following class to make debugging easier.
However, now NativeQueryImpl has changed a lot and I don’t understand some details. Previously I used nativeQuery.getQueryParameters().getNamedParameters() to get Map<String,TypedValue>. However, now TypedValue has been completely removed. I was comfortable having a value and type pair. How to get something like this in version 6+?
public class DebugQueryBinder {
public static String bindQuery(NativeQueryImpl<?> nativeQuery) {
String queryString = nativeQuery.getQueryString();
Map<String, TypedValue> namedParameters = nativeQuery.getQueryParameters().getNamedParameters();
if (!namedParameters.isEmpty()) {
for (var namedParameter : namedParameters.entrySet()) {
String regex = "((:%s))".formatted(namedParameter.getKey().trim());
queryString = queryString.replaceAll(regex, getBindString(namedParameter.getValue()));
}
}
return queryString;
}
private static String getBindString(TypedValue typedValue) {
Type type = typedValue.getType();
String stringValue = typedValue.getValue().toString();
if (type instanceof DateType) return "DATE '%s'".formatted(stringValue);
if (type instanceof TimestampType) return "TIMESTAMP '%s'".formatted(stringValue);
return "'%s'".formatted(stringValue);
}
}