Skip to content

Commit

Permalink
Merge pull request #49 from xingyutangyuan/master
Browse files Browse the repository at this point in the history
Fix GoogleSqlTest
  • Loading branch information
fluentfuture authored Nov 29, 2023
2 parents f5f8c61 + cf68931 commit da92ae4
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 12 deletions.
8 changes: 5 additions & 3 deletions mug-guava/src/main/java/com/google/mu/safesql/GoogleSql.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ public final class GoogleSql {


/**
* Similar to {@link SafeQuery#template}, except {@link Instant} are translated to `TIMESTAMP()` GoogleSql function,
* {@link ZonedDateTime} are translated to `DTETIME()` GoogleSql function, and {@link LocalDate} are translated
* to `DATE()` GoogleSql function.
* Much like {@link SafeQuery#template}, but with additional GoogleSQL translation rules.
*
* <p>Specifically, {@link Instant} are translated to `TIMESTAMP()` GoogleSql function,
* {@link ZonedDateTime} are translated to `DATETIME()` GoogleSql function,
* and {@link LocalDate} are translated to `DATE()` GoogleSql function.
*/
public static StringFormat.To<SafeQuery> template(@CompileTimeConstant String formatString) {
return SafeQuery.template(formatString, value -> {
Expand Down
16 changes: 9 additions & 7 deletions mug-guava/src/main/java/com/google/mu/safesql/SafeQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ static StringFormat.To<SafeQuery> template(
placeholders
.collect(
new StringBuilder(),
(b, p, v) -> b.append(it.next()).append(fillInPlaceholder(p, v, defaultConverter)))
(b, p, v) ->
b.append(it.next()).append(fillInPlaceholder(p, v, defaultConverter)))
.append(it.next())
.toString());
});
Expand All @@ -158,7 +159,7 @@ public int hashCode() {
}

private static String fillInPlaceholder(
Substring.Match placeholder, Object value, Function<Object, String> byDefault) {
Substring.Match placeholder, Object value, Function<Object, String> defaultConverter) {
validatePlaceholder(placeholder);
if (value instanceof Iterable) {
Iterable<?> iterable = (Iterable<?>) value;
Expand All @@ -173,7 +174,8 @@ private static String fillInPlaceholder(
return String.join(
"\", \"", Iterables.transform(iterable, v -> quotedBy('"', placeholder, v)));
}
return String.join(", ", Iterables.transform(iterable, v -> unquoted(placeholder, v)));
return String.join(
", ", Iterables.transform(iterable, v -> unquoted(placeholder, v, defaultConverter)));
}
if (placeholder.isImmediatelyBetween("'", "'")) {
return quotedBy('\'', placeholder, value);
Expand All @@ -184,11 +186,11 @@ private static String fillInPlaceholder(
if (placeholder.isImmediatelyBetween("`", "`")) {
return backquoted(placeholder, value);
}
String result = unquoted(placeholder, value);
return result == null ? byDefault.apply(value) : result;
return unquoted(placeholder, value, defaultConverter);
}

private static String unquoted(Substring.Match placeholder, Object value) {
private static String unquoted(
Substring.Match placeholder, Object value, Function<Object, String> byDefault) {
if (value == null) {
return "NULL";
}
Expand All @@ -210,7 +212,7 @@ private static String unquoted(Substring.Match placeholder, Object value) {
+ "subqueries must be wrapped in another SafeQuery object;\n"
+ "and string literals must be quoted like '%s'",
TRUSTED_SQL_TYPE_NAME, placeholder);
return null;
return byDefault.apply(value);
}

private static String quotedBy(char quoteChar, Substring.Match placeholder, Object value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,23 @@
import java.time.ZoneId;
import java.time.ZonedDateTime;

import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class GoogleSqlTest {
@BeforeClass // Consistently set the system property across the test suite
public static void setUpTrustedType() {
System.setProperty(
"com.google.mu.safesql.SafeQuery.trusted_sql_type",
SafeQueryTest.TrustedSql.class.getName());
}

@Test
public void farPastTimestampPlaceholder() {
ZonedDateTime time = ZonedDateTime.of(1900, 1, 1, 0, 0, 0, 0, googleZoneId());
ZonedDateTime time = ZonedDateTime.of(1900, 1, 1, 0, 0, 0, 0, ZoneId.of("America/Los_Angeles"));
assertThat(
template("SELECT * FROM tbl WHERE creation_time = {creation_time}")
.with(/* creation_time */ time.toInstant()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ public void trustedSqlStringShouldNotBeDoubleQuoted() {
.contains("TrustedSql should not be quoted: \"{value}\"");
}

private static final class TrustedSql {
static final class TrustedSql {
private final String sql;

TrustedSql(String sql) {
Expand Down

0 comments on commit da92ae4

Please sign in to comment.