Skip to content

Commit

Permalink
add a simple commandline main() function to try out ParameterizedQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
fluentfuture committed Dec 3, 2023
1 parent 02b42b7 commit e5ccee4
Showing 1 changed file with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collector;

import com.google.cloud.bigquery.BigQuery.JobOption;
import com.google.cloud.bigquery.BigQueryOptions;
import com.google.cloud.bigquery.JobException;
import com.google.cloud.bigquery.QueryJobConfiguration;
Expand Down Expand Up @@ -140,7 +142,7 @@ public static StringFormat.To<ParameterizedQuery> template(@CompileTimeConstant
* <p>To use alternative options, pass {@link #jobConfiguration} to the {link BigQueryOptions} of
* your choice.
*/
public TableResult run() throws JobException, InterruptedException {
public TableResult run(JobOption... options) throws JobException, InterruptedException {
return BigQueryOptions.getDefaultInstance().getService().query(jobConfiguration());
}

Expand Down Expand Up @@ -259,4 +261,26 @@ public boolean equals(Object obj) {
public String toString() {
return query;
}

/**
* A simple command-line tool for you to try it out. Pass command line args like:
*
* <pre>"select name from {tbl} where id = {id}" students 123</pre>
* @param args
*/
public static void main(String[] args) {
if (args.length < 1) {
System.out.println("'[parameterized query]' [args...]");
return;
}
try {
@SuppressWarnings("CompileTimeConstant")
TableResult result = of(args[0], Arrays.asList(args).subList(1, args.length).toArray(new Object[0]))
.run();
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
}

0 comments on commit e5ccee4

Please sign in to comment.