@@ -107,6 +107,7 @@ public class BigQueryStatement extends BigQueryNoOpsStatement {
107107 protected int currentJobIdIndex = -1 ;
108108 protected List <String > batchQueries = new ArrayList <>();
109109 protected BigQueryConnection connection ;
110+ protected BigQueryParameterHandler parameterHandler = null ;
110111 protected String connectionId ;
111112 protected int maxFieldSize = 0 ;
112113 protected int maxRows = 0 ;
@@ -244,9 +245,10 @@ public ResultSet executeQuery(String sql) throws SQLException {
244245 private ResultSet executeQueryImpl (String sql ) throws SQLException {
245246 logQueryExecutionStart (sql );
246247 try {
247- QueryJobConfiguration jobConfiguration =
248- setDestinationDatasetAndTableInJobConfig (getJobConfig (sql ).build ());
249- runQuery (sql , jobConfiguration );
248+ QueryJobConfiguration .Builder jobConfiguration = getJobConfig (sql );
249+ jobConfiguration = applyParametersIfPresent (jobConfiguration );
250+ jobConfiguration = setDestinationDatasetAndTableInJobConfig (jobConfiguration );
251+ runQuery (sql , jobConfiguration .build ());
250252 } catch (InterruptedException ex ) {
251253 throw new BigQueryJdbcException ("Interrupted during executeQuery" , ex );
252254 }
@@ -268,6 +270,7 @@ private long executeLargeUpdateImpl(String sql) throws SQLException {
268270 logQueryExecutionStart (sql );
269271 try {
270272 QueryJobConfiguration .Builder jobConfiguration = getJobConfig (sql );
273+ jobConfiguration = applyParametersIfPresent (jobConfiguration );
271274 runQuery (sql , jobConfiguration .build ());
272275 } catch (InterruptedException ex ) {
273276 throw new BigQueryJdbcRuntimeException ("Interrupted during executeLargeUpdate" , ex );
@@ -303,12 +306,13 @@ public boolean execute(String sql) throws SQLException {
303306 private boolean executeImpl (String sql ) throws SQLException {
304307 logQueryExecutionStart (sql );
305308 try {
306- QueryJobConfiguration jobConfiguration = getJobConfig (sql ).build ();
307- // If Large Results are enabled, ensure query type is SELECT
308- if (isLargeResultsEnabled () && getQueryType (jobConfiguration , null ) == SqlType .SELECT ) {
309+ QueryJobConfiguration .Builder jobConfiguration = getJobConfig (sql );
310+ jobConfiguration = applyParametersIfPresent (jobConfiguration );
311+ if (isLargeResultsEnabled ()
312+ && getQueryType (jobConfiguration .build (), null ) == SqlType .SELECT ) {
309313 jobConfiguration = setDestinationDatasetAndTableInJobConfig (jobConfiguration );
310314 }
311- runQuery (sql , jobConfiguration );
315+ runQuery (sql , jobConfiguration . build () );
312316 } catch (InterruptedException ex ) {
313317 throw new BigQueryJdbcRuntimeException ("Interrupted during execute" , ex );
314318 }
@@ -625,35 +629,43 @@ void runQuery(String query, QueryJobConfiguration jobConfiguration)
625629 }
626630 }
627631
628- private boolean isLargeResultsEnabled () {
632+ protected QueryJobConfiguration .Builder applyParametersIfPresent (
633+ QueryJobConfiguration .Builder jobConfigurationBuilder ) throws SQLException {
634+ if (this .parameterHandler != null && this .parameterHandler .getParametersArraySize () > 0 ) {
635+ jobConfigurationBuilder .setParameterMode ("POSITIONAL" );
636+ jobConfigurationBuilder = this .parameterHandler .configureParameters (jobConfigurationBuilder );
637+ }
638+ return jobConfigurationBuilder ;
639+ }
640+
641+ boolean isLargeResultsEnabled () {
629642 String destinationTable = this .querySettings .getDestinationTable ();
630643 String destinationDataset = this .querySettings .getDestinationDataset ();
631644 return destinationDataset != null || destinationTable != null ;
632645 }
633646
634- private QueryJobConfiguration setDestinationDatasetAndTableInJobConfig (
635- QueryJobConfiguration jobConfiguration ) {
647+ QueryJobConfiguration . Builder setDestinationDatasetAndTableInJobConfig (
648+ QueryJobConfiguration . Builder jobConfigurationBuilder ) {
636649 String destinationTable = this .querySettings .getDestinationTable ();
637650 String destinationDataset = this .querySettings .getDestinationDataset ();
638651 if (destinationDataset != null || destinationTable != null ) {
639652 if (destinationDataset != null ) {
640653 checkIfDatasetExistElseCreate (destinationDataset );
641654 }
642- if (jobConfiguration . useLegacySql () && destinationDataset == null ) {
655+ if (getUseLegacySql () && destinationDataset == null ) {
643656 checkIfDatasetExistElseCreate (DEFAULT_DATASET_NAME );
644657 destinationDataset = DEFAULT_DATASET_NAME ;
645658 }
646659 if (destinationTable == null ) {
647660 destinationTable = getDefaultDestinationTable ();
648661 }
649- return jobConfiguration . toBuilder ()
662+ return jobConfigurationBuilder
650663 .setAllowLargeResults (this .querySettings .getAllowLargeResults ())
651664 .setDestinationTable (TableId .of (destinationDataset , destinationTable ))
652665 .setCreateDisposition (JobInfo .CreateDisposition .CREATE_IF_NEEDED )
653- .setWriteDisposition (JobInfo .WriteDisposition .WRITE_TRUNCATE )
654- .build ();
666+ .setWriteDisposition (JobInfo .WriteDisposition .WRITE_TRUNCATE );
655667 }
656- return jobConfiguration ;
668+ return jobConfigurationBuilder ;
657669 }
658670
659671 Job getNextJob () {
@@ -1349,14 +1361,16 @@ QueryJobConfiguration.Builder getJobConfig(String query) {
13491361 if (this .querySettings .getQueryProperties () != null ) {
13501362 queryConfigBuilder .setConnectionProperties (this .querySettings .getQueryProperties ());
13511363 }
1352- boolean useLegacy =
1353- QueryDialectType .BIG_QUERY .equals (
1354- QueryDialectType .valueOf (this .querySettings .getQueryDialect ()));
1355- queryConfigBuilder .setUseLegacySql (useLegacy );
1364+ queryConfigBuilder .setUseLegacySql (getUseLegacySql ());
13561365
13571366 return queryConfigBuilder ;
13581367 }
13591368
1369+ private boolean getUseLegacySql () {
1370+ return QueryDialectType .BIG_QUERY .equals (
1371+ QueryDialectType .valueOf (this .querySettings .getQueryDialect ()));
1372+ }
1373+
13601374 private void checkIfDatasetExistElseCreate (String datasetName ) {
13611375 Dataset dataset = bigQuery .getDataset (DatasetId .of (datasetName ));
13621376 if (dataset == null ) {
0 commit comments