@@ -105,6 +105,7 @@ public class BigQueryStatement extends BigQueryNoOpsStatement {
105105 protected int currentJobIdIndex = -1 ;
106106 protected List <String > batchQueries = new ArrayList <>();
107107 protected BigQueryConnection connection ;
108+ protected BigQueryParameterHandler parameterHandler = null ;
108109 protected String connectionId ;
109110 protected int maxFieldSize = 0 ;
110111 protected int maxRows = 0 ;
@@ -242,9 +243,10 @@ public ResultSet executeQuery(String sql) throws SQLException {
242243 private ResultSet executeQueryImpl (String sql ) throws SQLException {
243244 logQueryExecutionStart (sql );
244245 try {
245- QueryJobConfiguration jobConfiguration =
246- setDestinationDatasetAndTableInJobConfig (getJobConfig (sql ).build ());
247- runQuery (sql , jobConfiguration );
246+ QueryJobConfiguration .Builder jobConfiguration = getJobConfig (sql );
247+ jobConfiguration = applyParametersIfPresent (jobConfiguration );
248+ jobConfiguration = setDestinationDatasetAndTableInJobConfig (jobConfiguration );
249+ runQuery (sql , jobConfiguration .build ());
248250 } catch (InterruptedException ex ) {
249251 throw new BigQueryJdbcException ("Interrupted during executeQuery" , ex );
250252 }
@@ -266,6 +268,7 @@ private long executeLargeUpdateImpl(String sql) throws SQLException {
266268 logQueryExecutionStart (sql );
267269 try {
268270 QueryJobConfiguration .Builder jobConfiguration = getJobConfig (sql );
271+ jobConfiguration = applyParametersIfPresent (jobConfiguration );
269272 runQuery (sql , jobConfiguration .build ());
270273 } catch (InterruptedException ex ) {
271274 throw new BigQueryJdbcRuntimeException ("Interrupted during executeLargeUpdate" , ex );
@@ -301,12 +304,13 @@ public boolean execute(String sql) throws SQLException {
301304 private boolean executeImpl (String sql ) throws SQLException {
302305 logQueryExecutionStart (sql );
303306 try {
304- QueryJobConfiguration jobConfiguration = getJobConfig (sql ).build ();
305- // If Large Results are enabled, ensure query type is SELECT
306- if (isLargeResultsEnabled () && getQueryType (jobConfiguration , null ) == SqlType .SELECT ) {
307+ QueryJobConfiguration .Builder jobConfiguration = getJobConfig (sql );
308+ jobConfiguration = applyParametersIfPresent (jobConfiguration );
309+ if (isLargeResultsEnabled ()
310+ && getQueryType (jobConfiguration .build (), null ) == SqlType .SELECT ) {
307311 jobConfiguration = setDestinationDatasetAndTableInJobConfig (jobConfiguration );
308312 }
309- runQuery (sql , jobConfiguration );
313+ runQuery (sql , jobConfiguration . build () );
310314 } catch (InterruptedException ex ) {
311315 throw new BigQueryJdbcRuntimeException ("Interrupted during execute" , ex );
312316 }
@@ -623,35 +627,43 @@ void runQuery(String query, QueryJobConfiguration jobConfiguration)
623627 }
624628 }
625629
626- private boolean isLargeResultsEnabled () {
630+ protected QueryJobConfiguration .Builder applyParametersIfPresent (
631+ QueryJobConfiguration .Builder jobConfigurationBuilder ) throws SQLException {
632+ if (this .parameterHandler != null && this .parameterHandler .getParametersArraySize () > 0 ) {
633+ jobConfigurationBuilder .setParameterMode ("POSITIONAL" );
634+ jobConfigurationBuilder = this .parameterHandler .configureParameters (jobConfigurationBuilder );
635+ }
636+ return jobConfigurationBuilder ;
637+ }
638+
639+ boolean isLargeResultsEnabled () {
627640 String destinationTable = this .querySettings .getDestinationTable ();
628641 String destinationDataset = this .querySettings .getDestinationDataset ();
629642 return destinationDataset != null || destinationTable != null ;
630643 }
631644
632- private QueryJobConfiguration setDestinationDatasetAndTableInJobConfig (
633- QueryJobConfiguration jobConfiguration ) {
645+ QueryJobConfiguration . Builder setDestinationDatasetAndTableInJobConfig (
646+ QueryJobConfiguration . Builder jobConfigurationBuilder ) {
634647 String destinationTable = this .querySettings .getDestinationTable ();
635648 String destinationDataset = this .querySettings .getDestinationDataset ();
636649 if (destinationDataset != null || destinationTable != null ) {
637650 if (destinationDataset != null ) {
638651 checkIfDatasetExistElseCreate (destinationDataset );
639652 }
640- if (jobConfiguration . useLegacySql () && destinationDataset == null ) {
653+ if (getUseLegacySql () && destinationDataset == null ) {
641654 checkIfDatasetExistElseCreate (DEFAULT_DATASET_NAME );
642655 destinationDataset = DEFAULT_DATASET_NAME ;
643656 }
644657 if (destinationTable == null ) {
645658 destinationTable = getDefaultDestinationTable ();
646659 }
647- return jobConfiguration . toBuilder ()
660+ return jobConfigurationBuilder
648661 .setAllowLargeResults (this .querySettings .getAllowLargeResults ())
649662 .setDestinationTable (TableId .of (destinationDataset , destinationTable ))
650663 .setCreateDisposition (JobInfo .CreateDisposition .CREATE_IF_NEEDED )
651- .setWriteDisposition (JobInfo .WriteDisposition .WRITE_TRUNCATE )
652- .build ();
664+ .setWriteDisposition (JobInfo .WriteDisposition .WRITE_TRUNCATE );
653665 }
654- return jobConfiguration ;
666+ return jobConfigurationBuilder ;
655667 }
656668
657669 Job getNextJob () {
@@ -1407,14 +1419,16 @@ QueryJobConfiguration.Builder getJobConfig(String query) {
14071419 if (this .querySettings .getQueryProperties () != null ) {
14081420 queryConfigBuilder .setConnectionProperties (this .querySettings .getQueryProperties ());
14091421 }
1410- boolean useLegacy =
1411- QueryDialectType .BIG_QUERY .equals (
1412- QueryDialectType .valueOf (this .querySettings .getQueryDialect ()));
1413- queryConfigBuilder .setUseLegacySql (useLegacy );
1422+ queryConfigBuilder .setUseLegacySql (getUseLegacySql ());
14141423
14151424 return queryConfigBuilder ;
14161425 }
14171426
1427+ private boolean getUseLegacySql () {
1428+ return QueryDialectType .BIG_QUERY .equals (
1429+ QueryDialectType .valueOf (this .querySettings .getQueryDialect ()));
1430+ }
1431+
14181432 private void checkIfDatasetExistElseCreate (String datasetName ) {
14191433 Dataset dataset = bigQuery .getDataset (DatasetId .of (datasetName ));
14201434 if (dataset == null ) {
0 commit comments