Skip to content

Commit 6b45e18

Browse files
committed
debugString()
1 parent b71fa17 commit 6b45e18

File tree

2 files changed

+59
-52
lines changed

2 files changed

+59
-52
lines changed

mug-guava/src/main/java/com/google/mu/safesql/SafeSql.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import static com.google.common.collect.Streams.stream;
2525
import static com.google.mu.safesql.InternalCollectors.skippingEmpty;
2626
import static com.google.mu.safesql.SafeQuery.checkIdentifier;
27+
import static com.google.mu.util.Substring.all;
2728
import static com.google.mu.util.Substring.first;
2829
import static com.google.mu.util.Substring.firstOccurrence;
2930
import static com.google.mu.util.Substring.prefix;
@@ -1068,12 +1069,16 @@ public static Template<Integer> prepareToUpdate(
10681069
}
10691070

10701071
/**
1071-
* Returns the parameter values in the order they occur in the SQL. They are used by methods
1072-
* like {@link #query query()}, {@link #update update()} or {@link #prepareStatement} to
1073-
* populate the {@link PreparedStatement}.
1072+
* Returns a query string with the parameter values embedded for easier debugging (logging,
1073+
* testing, golden file etc.). DO NOT use it as the production SQL query because embedding the
1074+
* parameter values isn't safe from SQL injection.
1075+
*
1076+
* @since 8.8
10741077
*/
1075-
List<?> getParameters() {
1076-
return paramValues;
1078+
public String debugString() {
1079+
StringFormat placeholderWithValue = new StringFormat("? /* {...} */");
1080+
Iterator<?> args = paramValues.iterator();
1081+
return all("?").replaceAllFrom(sql, q -> placeholderWithValue.format(args.next()));
10771082
}
10781083

10791084
/**
@@ -1448,7 +1453,7 @@ private static final class Builder {
14481453

14491454
@CanIgnoreReturnValue Builder addSubQuery(SafeSql subQuery) {
14501455
safeAppend(subQuery.sql);
1451-
paramValues.addAll(subQuery.getParameters());
1456+
paramValues.addAll(subQuery.paramValues);
14521457
return this;
14531458
}
14541459

0 commit comments

Comments
 (0)