Skip to content

Commit

Permalink
SafeQuery::parenthesized
Browse files Browse the repository at this point in the history
  • Loading branch information
fluentfuture committed Jan 14, 2024
1 parent 4bba97b commit 0c53b0a
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions mug-guava/src/main/java/com/google/mu/safesql/SafeQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<SafeQuery> PARENTHESIZED = template("({q})");

private final String query;

Expand All @@ -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<SafeQuery, ?, SafeQuery> 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:
Expand Down Expand Up @@ -133,7 +123,7 @@ public static StringFormat.To<SafeQuery> template(@CompileTimeConstant String fo
*/
public static Collector<SafeQuery, ?, SafeQuery> and() {
return collectingAndThen(
mapping(PARENTHESIZED::with, joining(" AND ")),
mapping(SafeQuery::parenthesized, joining(" AND ")),
query -> query.toString().isEmpty() ? of("TRUE") : query);
}

Expand All @@ -144,10 +134,19 @@ public static StringFormat.To<SafeQuery> template(@CompileTimeConstant String fo
*/
public static Collector<SafeQuery, ?, SafeQuery> 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<SafeQuery, ?, SafeQuery> joining(@CompileTimeConstant String delim) {
return collectingAndThen(
mapping(SafeQuery::toString, Collectors.joining(checkNotNull(delim))), SafeQuery::new);
}

/** Returns the encapsulated SQL query. */
@Override
public String toString() {
Expand All @@ -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.
Expand Down

0 comments on commit 0c53b0a

Please sign in to comment.