Skip to content

Commit

Permalink
Move and(), or() to SafeQuery. They are ANSI SQL
Browse files Browse the repository at this point in the history
  • Loading branch information
fluentfuture committed Jan 13, 2024
1 parent 160701e commit 4bba97b
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 85 deletions.
27 changes: 0 additions & 27 deletions mug-guava/src/main/java/com/google/mu/safesql/GoogleSql.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
package com.google.mu.safesql;

import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.mapping;

import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.stream.Collector;

import com.google.errorprone.annotations.CompileTimeConstant;
import com.google.mu.util.StringFormat;
Expand All @@ -29,7 +25,6 @@ public final class GoogleSql {
private static final StringFormat DATE_EXPRESSION =
new StringFormat("DATE({year}, {month}, {day})");
private static final ZoneId GOOGLE_ZONE_ID = ZoneId.of("America/Los_Angeles");
private static final StringFormat.To<SafeQuery> PARENTHESIZED = template("({q})");


/**
Expand All @@ -56,28 +51,6 @@ public static StringFormat.To<SafeQuery> template(@CompileTimeConstant String fo
}.translate(formatString);
}

/**
* A collector that joins boolean query snippets using {@code AND} operator.
*
* @since 7.2
*/
public static Collector<SafeQuery, ?, SafeQuery> and() {
return collectingAndThen(
mapping(PARENTHESIZED::with, SafeQuery.joining(" AND ")),
query -> query.toString().isEmpty() ? SafeQuery.of("TRUE") : query);
}

/**
* A collector that joins boolean query snippets using {@code OR} operator.
*
* @since 7.2
*/
public static Collector<SafeQuery, ?, SafeQuery> or() {
return collectingAndThen(
mapping(PARENTHESIZED::with, SafeQuery.joining(" OR ")),
query -> query.toString().isEmpty() ? SafeQuery.of("FALSE") : query);
}


private static String dateTimeExpression(ZonedDateTime dateTime) {
return DATE_TIME_EXPRESSION.format(
Expand Down
23 changes: 23 additions & 0 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,6 +45,7 @@ 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 Down Expand Up @@ -125,6 +126,28 @@ public static StringFormat.To<SafeQuery> template(@CompileTimeConstant String fo
return new Translator().translate(formatString);
}

/**
* A collector that joins boolean query snippets using {@code AND} operator.
*
* @since 7.2
*/
public static Collector<SafeQuery, ?, SafeQuery> and() {
return collectingAndThen(
mapping(PARENTHESIZED::with, joining(" AND ")),
query -> query.toString().isEmpty() ? of("TRUE") : query);
}

/**
* A collector that joins boolean query snippets using {@code OR} operator.
*
* @since 7.2
*/
public static Collector<SafeQuery, ?, SafeQuery> or() {
return collectingAndThen(
mapping(PARENTHESIZED::with, joining(" OR ")),
query -> query.toString().isEmpty() ? of("FALSE") : query);
}

/** Returns the encapsulated SQL query. */
@Override
public String toString() {
Expand Down
58 changes: 0 additions & 58 deletions mug-guava/src/test/java/com/google/mu/safesql/GoogleSqlTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,64 +92,6 @@ public void datePlaceholder() {
.isEqualTo(SafeQuery.of("SELECT * FROM tbl WHERE creation_date = DATE(2023, 10, 1)"));
}

@Test
public void andCollector_empty() {
ImmutableList<SafeQuery> queries = ImmutableList.of();
assertThat(queries.stream().collect(GoogleSql.and())).isEqualTo(SafeQuery.of("TRUE"));
}

@Test
public void andCollector_singleCondition() {
ImmutableList<SafeQuery> queries = ImmutableList.of(SafeQuery.of("a = 1"));
assertThat(queries.stream().collect(GoogleSql.and())).isEqualTo(SafeQuery.of("(a = 1)"));
}

@Test
public void andCollector_twoConditions() {
ImmutableList<SafeQuery> queries =
ImmutableList.of(SafeQuery.of("a = 1"), SafeQuery.of("b = 2 OR c = 3"));
assertThat(queries.stream().collect(GoogleSql.and()))
.isEqualTo(SafeQuery.of("(a = 1) AND (b = 2 OR c = 3)"));
}

@Test
public void andCollector_threeConditions() {
ImmutableList<SafeQuery> queries =
ImmutableList.of(
SafeQuery.of("a = 1"), SafeQuery.of("b = 2 OR c = 3"), SafeQuery.of("d = 4"));
assertThat(queries.stream().collect(GoogleSql.and()))
.isEqualTo(SafeQuery.of("(a = 1) AND (b = 2 OR c = 3) AND (d = 4)"));
}

@Test
public void orCollector_empty() {
ImmutableList<SafeQuery> queries = ImmutableList.of();
assertThat(queries.stream().collect(GoogleSql.or())).isEqualTo(SafeQuery.of("FALSE"));
}

@Test
public void orCollector_singleCondition() {
ImmutableList<SafeQuery> queries = ImmutableList.of(SafeQuery.of("a = 1"));
assertThat(queries.stream().collect(GoogleSql.or())).isEqualTo(SafeQuery.of("(a = 1)"));
}

@Test
public void orCollector_twoConditions() {
ImmutableList<SafeQuery> queries =
ImmutableList.of(SafeQuery.of("a = 1"), SafeQuery.of("b = 2 AND c = 3"));
assertThat(queries.stream().collect(GoogleSql.or()))
.isEqualTo(SafeQuery.of("(a = 1) OR (b = 2 AND c = 3)"));
}

@Test
public void orCollector_threeConditions() {
ImmutableList<SafeQuery> queries =
ImmutableList.of(
SafeQuery.of("a = 1"), SafeQuery.of("b = 2 AND c = 3"), SafeQuery.of("d = 4"));
assertThat(queries.stream().collect(GoogleSql.or()))
.isEqualTo(SafeQuery.of("(a = 1) OR (b = 2 AND c = 3) OR (d = 4)"));
}

@Test
public void listOfTimestamp() {
ZonedDateTime time = ZonedDateTime.of(1900, 1, 1, 0, 0, 0, 0, ZoneId.of("America/Los_Angeles"));
Expand Down
59 changes: 59 additions & 0 deletions mug-guava/src/test/java/com/google/mu/safesql/SafeQueryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.junit.runner.RunWith;

import com.google.common.base.Ascii;
import com.google.common.collect.ImmutableList;
import com.google.common.testing.EqualsTester;
import com.google.common.testing.NullPointerTester;
import com.google.mu.util.StringFormat;
Expand Down Expand Up @@ -453,6 +454,64 @@ public void joiningByStringConstant() {
.isEqualTo(SafeQuery.of("a AND b"));
}

@Test
public void andCollector_empty() {
ImmutableList<SafeQuery> queries = ImmutableList.of();
assertThat(queries.stream().collect(SafeQuery.and())).isEqualTo(SafeQuery.of("TRUE"));
}

@Test
public void andCollector_singleCondition() {
ImmutableList<SafeQuery> queries = ImmutableList.of(SafeQuery.of("a = 1"));
assertThat(queries.stream().collect(SafeQuery.and())).isEqualTo(SafeQuery.of("(a = 1)"));
}

@Test
public void andCollector_twoConditions() {
ImmutableList<SafeQuery> queries =
ImmutableList.of(SafeQuery.of("a = 1"), SafeQuery.of("b = 2 OR c = 3"));
assertThat(queries.stream().collect(SafeQuery.and()))
.isEqualTo(SafeQuery.of("(a = 1) AND (b = 2 OR c = 3)"));
}

@Test
public void andCollector_threeConditions() {
ImmutableList<SafeQuery> queries =
ImmutableList.of(
SafeQuery.of("a = 1"), SafeQuery.of("b = 2 OR c = 3"), SafeQuery.of("d = 4"));
assertThat(queries.stream().collect(SafeQuery.and()))
.isEqualTo(SafeQuery.of("(a = 1) AND (b = 2 OR c = 3) AND (d = 4)"));
}

@Test
public void orCollector_empty() {
ImmutableList<SafeQuery> queries = ImmutableList.of();
assertThat(queries.stream().collect(SafeQuery.or())).isEqualTo(SafeQuery.of("FALSE"));
}

@Test
public void orCollector_singleCondition() {
ImmutableList<SafeQuery> queries = ImmutableList.of(SafeQuery.of("a = 1"));
assertThat(queries.stream().collect(SafeQuery.or())).isEqualTo(SafeQuery.of("(a = 1)"));
}

@Test
public void orCollector_twoConditions() {
ImmutableList<SafeQuery> queries =
ImmutableList.of(SafeQuery.of("a = 1"), SafeQuery.of("b = 2 AND c = 3"));
assertThat(queries.stream().collect(SafeQuery.or()))
.isEqualTo(SafeQuery.of("(a = 1) OR (b = 2 AND c = 3)"));
}

@Test
public void orCollector_threeConditions() {
ImmutableList<SafeQuery> queries =
ImmutableList.of(
SafeQuery.of("a = 1"), SafeQuery.of("b = 2 AND c = 3"), SafeQuery.of("d = 4"));
assertThat(queries.stream().collect(SafeQuery.or()))
.isEqualTo(SafeQuery.of("(a = 1) OR (b = 2 AND c = 3) OR (d = 4)"));
}

@Test
public void testEquals() {
new EqualsTester()
Expand Down

0 comments on commit 4bba97b

Please sign in to comment.