diff --git a/mug-bigquery/src/main/java/com/google/mu/bigquery/ParameterizedQuery.java b/mug-bigquery/src/main/java/com/google/mu/bigquery/ParameterizedQuery.java index 31fe8e9f04..3586d88909 100644 --- a/mug-bigquery/src/main/java/com/google/mu/bigquery/ParameterizedQuery.java +++ b/mug-bigquery/src/main/java/com/google/mu/bigquery/ParameterizedQuery.java @@ -27,11 +27,54 @@ import com.google.mu.util.stream.BiStream; /** - * Facade class to create BigQuery parameterized queries using a template string and parameters. + * Facade class to create + * BigQuery parameterized queries using a template string and parameters. * - *
The string template syntax is defined by {@link StringFormat} and protected by the same + *
The string template syntax is defined by {@link StringFormat} and protected by the same set of * compile-time checks. * + *
For simple use cases, a one-liner is enough to construct a parameterized query. For example: + * + *
{@code + * ParameterizedQuery query = ParameterizedQuery.of( + * "SELECT name FROM Students WHERE id = {id} and status = {status}", + * studentId, Status.ENROLLED); + * TableResult result = query.run(); + * }+ * + *
If you need to reuse the same query for different parameters, or to get a long query + * "out of the way", you can define the query template as a class constant: + * + *
{@code + * private static final StringFormat.To+ * + * Compared to building the {@link QueryJobConfiguration} object manually, you get the following benefits: + *GET_STUDENT = ParameterizedQuery.template( + * "SELECT name FROM Students WHERE id = {id} and status = {status}"); + * + * // 200 lines later + * TableResult enrolled = GET_STUDENT.with(studentId, Status.ENROLLED).run(); + * TableResult graduated = GET_STUDENT.with(alumniId, Status.GRADUATED).run(); + * }
In addition to parameterizing by values, you can also parameterize by columns, table names or + * sub-queries. The following example allows you to use the same query on different datasets: + * + *
{@code + * private static final StringFormat.To+ * + * Non-value string parameters must be wrapped inside {@code ParameterizedQuery} to ensure safety. + * * @since 7.1 */ @Immutable @@ -95,6 +138,7 @@ public static ParameterizedQuery of(@CompileTimeConstant String query, Object... *GET_TABLES = ParameterizedQuery.template( + * "SELECT table_name FROM `{dataset}.INFORMATION_SCHEMA.TABLES`"); + * + * TableResult marketingTables = GET_TABLES.with(ParameterizedQuery.of("marketing")).run(); + * TableResult humanResourceTables = GET_TABLES.with(ParameterizedQuery.of("human-resource")).run(); + * }