diff --git a/mug-guava/src/main/java/com/google/mu/safesql/SafeQuery.java b/mug-guava/src/main/java/com/google/mu/safesql/SafeQuery.java index 8da6a9211d..e9ee7ec8e6 100644 --- a/mug-guava/src/main/java/com/google/mu/safesql/SafeQuery.java +++ b/mug-guava/src/main/java/com/google/mu/safesql/SafeQuery.java @@ -45,7 +45,6 @@ public final class SafeQuery { firstNonNull( System.getProperty("com.google.mu.safesql.SafeQuery.trusted_sql_type"), "com.google.storage.googlesql.safesql.TrustedSqlString"); - private static final StringFormat.To PARENTHESIZED = template("({q})"); private final String query; @@ -58,15 +57,6 @@ public static SafeQuery of(@CompileTimeConstant String query) { return new SafeQuery(checkNotNull(query)); } - /** - * Returns a collector that can join {@link SafeQuery} objects using {@code delim} as the - * delimiter. - */ - public static Collector joining(@CompileTimeConstant String delim) { - return collectingAndThen( - mapping(SafeQuery::toString, Collectors.joining(checkNotNull(delim))), SafeQuery::new); - } - /** * Creates a template with the placeholders to be filled by subsequent {@link * StringFormat.To#with} calls. For example: @@ -133,7 +123,7 @@ public static StringFormat.To template(@CompileTimeConstant String fo */ public static Collector and() { return collectingAndThen( - mapping(PARENTHESIZED::with, joining(" AND ")), + mapping(SafeQuery::parenthesized, joining(" AND ")), query -> query.toString().isEmpty() ? of("TRUE") : query); } @@ -144,10 +134,19 @@ public static StringFormat.To template(@CompileTimeConstant String fo */ public static Collector or() { return collectingAndThen( - mapping(PARENTHESIZED::with, joining(" OR ")), + mapping(SafeQuery::parenthesized, joining(" OR ")), query -> query.toString().isEmpty() ? of("FALSE") : query); } + /** + * Returns a collector that can join {@link SafeQuery} objects using {@code delim} as the + * delimiter. + */ + public static Collector joining(@CompileTimeConstant String delim) { + return collectingAndThen( + mapping(SafeQuery::toString, Collectors.joining(checkNotNull(delim))), SafeQuery::new); + } + /** Returns the encapsulated SQL query. */ @Override public String toString() { @@ -167,6 +166,10 @@ public int hashCode() { return query.hashCode(); } + private SafeQuery parenthesized() { + return new SafeQuery("(" + query + ")"); + } + /** * An SPI class for subclasses to provide additional translation from placeholder values to safe * query strings.