-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c8d6ac5
commit f07afbc
Showing
2 changed files
with
78 additions
and
11 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
mug-guava/src/main/java/com/google/mu/safesql/GoogleSql.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.google.mu.safesql; | ||
|
||
import java.time.Instant; | ||
import java.time.LocalDate; | ||
import java.time.ZoneId; | ||
import java.time.ZonedDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
import com.google.errorprone.annotations.CompileTimeConstant; | ||
import com.google.mu.util.StringFormat; | ||
|
||
/** Facade class providing {@link SafeQuery} templates for GoogleSQL. */ | ||
public final class GoogleSql { | ||
private static final DateTimeFormatter LOCAL_DATE_TIME_FORMATTER = | ||
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss[.SSSSSS]"); | ||
private static final StringFormat DATE_TIME_EXPRESSION = | ||
new StringFormat("DATETIME('{time}', '{zone}')"); | ||
private static final StringFormat TIMESTAMP_EXPRESSION = | ||
new StringFormat("TIMESTAMP('{time}', '{zone}')"); | ||
private static final StringFormat DATE_EXPRESSION = | ||
new StringFormat("DATE({year}, {month}, {day})"); | ||
private static final ZoneId GOOGLE_ZONE_ID = ZoneId.of("America/Los_Angeles"); | ||
|
||
|
||
/** | ||
* 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. | ||
*/ | ||
public static StringFormat.To<SafeQuery> template(@CompileTimeConstant String formatString) { | ||
return SafeQuery.template(formatString, value -> { | ||
if (value instanceof Instant) { | ||
return timestampExpression((Instant) value); | ||
} | ||
if (value instanceof ZonedDateTime) { | ||
return dateTimeExpression((ZonedDateTime) value); | ||
} | ||
if (value instanceof LocalDate) { | ||
return dateExpression((LocalDate) value); | ||
} | ||
throw new IllegalArgumentException("Unsupported argument type: " + value.getClass().getName()); | ||
}); | ||
} | ||
|
||
|
||
private static String dateTimeExpression(ZonedDateTime dateTime) { | ||
return DATE_TIME_EXPRESSION.format( | ||
dateTime.toLocalDateTime().format(LOCAL_DATE_TIME_FORMATTER), dateTime.getZone()); | ||
} | ||
|
||
private static String dateExpression(LocalDate date) { | ||
return DATE_EXPRESSION.format(date.getYear(), date.getMonthValue(), date.getDayOfMonth()); | ||
} | ||
|
||
private static String timestampExpression(Instant instant) { | ||
return TIMESTAMP_EXPRESSION.format( | ||
instant.atZone(GOOGLE_ZONE_ID).toLocalDateTime().format(LOCAL_DATE_TIME_FORMATTER), GOOGLE_ZONE_ID); | ||
} | ||
|
||
private GoogleSql() {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters