use of com.google.api.services.bigquery.model.QueryParameter in project google-cloud-java by GoogleCloudPlatform.
the class QueryRequest method namedParameterToPb.
static QueryParameter namedParameterToPb(Map.Entry<String, QueryParameterValue> entry) {
QueryParameter queryParameterPb = new QueryParameter();
queryParameterPb.setName(entry.getKey());
queryParameterPb.setParameterValue(entry.getValue().toValuePb());
queryParameterPb.setParameterType(entry.getValue().toTypePb());
return queryParameterPb;
}
use of com.google.api.services.bigquery.model.QueryParameter in project google-cloud-java by GoogleCloudPlatform.
the class QueryRequest method fromPb.
static QueryRequest fromPb(com.google.api.services.bigquery.model.QueryRequest queryRequestPb) {
Builder builder = newBuilder(queryRequestPb.getQuery());
if (queryRequestPb.getQueryParameters() != null && !queryRequestPb.getQueryParameters().isEmpty()) {
if (queryRequestPb.getQueryParameters().get(0).getName() == null) {
builder.setPositionalParameters(Lists.transform(queryRequestPb.getQueryParameters(), POSITIONAL_PARAMETER_FROM_PB_FUNCTION));
} else {
Map<String, QueryParameterValue> values = Maps.newHashMap();
for (QueryParameter queryParameterPb : queryRequestPb.getQueryParameters()) {
checkNotNull(queryParameterPb.getName());
QueryParameterValue value = QueryParameterValue.fromPb(queryParameterPb.getParameterValue(), queryParameterPb.getParameterType());
values.put(queryParameterPb.getName(), value);
}
builder.setNamedParameters(values);
}
}
if (queryRequestPb.getMaxResults() != null) {
builder.setPageSize(queryRequestPb.getMaxResults());
}
if (queryRequestPb.getDefaultDataset() != null) {
builder.setDefaultDataset(DatasetId.fromPb(queryRequestPb.getDefaultDataset()));
}
if (queryRequestPb.getTimeoutMs() != null) {
builder.setMaxWaitTime(queryRequestPb.getTimeoutMs());
}
if (queryRequestPb.getDryRun() != null) {
builder.setDryRun(queryRequestPb.getDryRun());
}
if (queryRequestPb.getUseQueryCache() != null) {
builder.setUseQueryCache(queryRequestPb.getUseQueryCache());
}
if (queryRequestPb.getUseLegacySql() != null) {
builder.setUseLegacySql(queryRequestPb.getUseLegacySql());
}
return builder.build();
}
use of com.google.api.services.bigquery.model.QueryParameter in project java-bigquery by googleapis.
the class ConnectionImpl method createQueryJob.
// Used by jobs.getQueryResults API endpoint
@VisibleForTesting
com.google.api.services.bigquery.model.Job createQueryJob(String sql, ConnectionSettings connectionSettings, List<Parameter> queryParameters, Map<String, String> labels) {
com.google.api.services.bigquery.model.JobConfiguration configurationPb = new com.google.api.services.bigquery.model.JobConfiguration();
JobConfigurationQuery queryConfigurationPb = new JobConfigurationQuery();
queryConfigurationPb.setQuery(sql);
if (queryParameters != null) {
if (queryParameters.get(0).getName() == null) {
// If query parameter name is unset, then assume mode is positional
queryConfigurationPb.setParameterMode("POSITIONAL");
// pass query parameters
List<QueryParameter> queryParametersPb = Lists.transform(queryParameters, POSITIONAL_PARAMETER_TO_PB_FUNCTION);
queryConfigurationPb.setQueryParameters(queryParametersPb);
} else {
queryConfigurationPb.setParameterMode("NAMED");
// pass query parameters
List<QueryParameter> queryParametersPb = Lists.transform(queryParameters, NAMED_PARAMETER_TO_PB_FUNCTION);
queryConfigurationPb.setQueryParameters(queryParametersPb);
}
}
if (connectionSettings.getDestinationTable() != null) {
queryConfigurationPb.setDestinationTable(connectionSettings.getDestinationTable().toPb());
}
if (connectionSettings.getTableDefinitions() != null) {
queryConfigurationPb.setTableDefinitions(Maps.transformValues(connectionSettings.getTableDefinitions(), ExternalTableDefinition.TO_EXTERNAL_DATA_FUNCTION));
}
if (connectionSettings.getUserDefinedFunctions() != null) {
queryConfigurationPb.setUserDefinedFunctionResources(connectionSettings.getUserDefinedFunctions().stream().map(UserDefinedFunction.TO_PB_FUNCTION).collect(Collectors.toList()));
}
if (connectionSettings.getCreateDisposition() != null) {
queryConfigurationPb.setCreateDisposition(connectionSettings.getCreateDisposition().toString());
}
if (connectionSettings.getWriteDisposition() != null) {
queryConfigurationPb.setWriteDisposition(connectionSettings.getWriteDisposition().toString());
}
if (connectionSettings.getDefaultDataset() != null) {
queryConfigurationPb.setDefaultDataset(connectionSettings.getDefaultDataset().toPb());
}
if (connectionSettings.getPriority() != null) {
queryConfigurationPb.setPriority(connectionSettings.getPriority().toString());
}
if (connectionSettings.getAllowLargeResults() != null) {
queryConfigurationPb.setAllowLargeResults(connectionSettings.getAllowLargeResults());
}
if (connectionSettings.getUseQueryCache() != null) {
queryConfigurationPb.setUseQueryCache(connectionSettings.getUseQueryCache());
}
if (connectionSettings.getFlattenResults() != null) {
queryConfigurationPb.setFlattenResults(connectionSettings.getFlattenResults());
}
if (connectionSettings.getMaximumBillingTier() != null) {
queryConfigurationPb.setMaximumBillingTier(connectionSettings.getMaximumBillingTier());
}
if (connectionSettings.getMaximumBytesBilled() != null) {
queryConfigurationPb.setMaximumBytesBilled(connectionSettings.getMaximumBytesBilled());
}
if (connectionSettings.getSchemaUpdateOptions() != null) {
ImmutableList.Builder<String> schemaUpdateOptionsBuilder = new ImmutableList.Builder<>();
for (JobInfo.SchemaUpdateOption schemaUpdateOption : connectionSettings.getSchemaUpdateOptions()) {
schemaUpdateOptionsBuilder.add(schemaUpdateOption.name());
}
queryConfigurationPb.setSchemaUpdateOptions(schemaUpdateOptionsBuilder.build());
}
if (connectionSettings.getDestinationEncryptionConfiguration() != null) {
queryConfigurationPb.setDestinationEncryptionConfiguration(connectionSettings.getDestinationEncryptionConfiguration().toPb());
}
if (connectionSettings.getTimePartitioning() != null) {
queryConfigurationPb.setTimePartitioning(connectionSettings.getTimePartitioning().toPb());
}
if (connectionSettings.getClustering() != null) {
queryConfigurationPb.setClustering(connectionSettings.getClustering().toPb());
}
if (connectionSettings.getRangePartitioning() != null) {
queryConfigurationPb.setRangePartitioning(connectionSettings.getRangePartitioning().toPb());
}
if (connectionSettings.getConnectionProperties() != null) {
queryConfigurationPb.setConnectionProperties(connectionSettings.getConnectionProperties().stream().map(ConnectionProperty.TO_PB_FUNCTION).collect(Collectors.toList()));
}
if (connectionSettings.getCreateSession() != null) {
queryConfigurationPb.setCreateSession(connectionSettings.getCreateSession());
}
if (connectionSettings.getJobTimeoutMs() != null) {
configurationPb.setJobTimeoutMs(connectionSettings.getJobTimeoutMs());
}
if (labels != null) {
configurationPb.setLabels(labels);
}
// The new Connection interface only supports StandardSQL dialect
queryConfigurationPb.setUseLegacySql(false);
configurationPb.setQuery(queryConfigurationPb);
com.google.api.services.bigquery.model.Job jobPb = JobInfo.of(QueryJobConfiguration.fromPb(configurationPb)).toPb();
com.google.api.services.bigquery.model.Job queryJob;
try {
queryJob = BigQueryRetryHelper.runWithRetries(() -> bigQueryRpc.createJobForQuery(jobPb), bigQueryOptions.getRetrySettings(), BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, bigQueryOptions.getClock(), retryConfig);
} catch (BigQueryRetryHelper.BigQueryRetryHelperException e) {
throw BigQueryException.translateAndThrow(e);
}
return queryJob;
}
use of com.google.api.services.bigquery.model.QueryParameter in project google-cloud-java by GoogleCloudPlatform.
the class QueryRequest method positionalParameterToPb.
static QueryParameter positionalParameterToPb(QueryParameterValue value) {
QueryParameter queryParameterPb = new QueryParameter();
queryParameterPb.setParameterValue(value.toValuePb());
queryParameterPb.setParameterType(value.toTypePb());
return queryParameterPb;
}
use of com.google.api.services.bigquery.model.QueryParameter in project java-bigquery by googleapis.
the class ConnectionImpl method createQueryRequest.
// Used for job.query API endpoint
@VisibleForTesting
QueryRequest createQueryRequest(ConnectionSettings connectionSettings, String sql, List<Parameter> queryParameters, Map<String, String> labels) {
QueryRequest content = new QueryRequest();
String requestId = UUID.randomUUID().toString();
if (connectionSettings.getConnectionProperties() != null) {
content.setConnectionProperties(connectionSettings.getConnectionProperties().stream().map(ConnectionProperty.TO_PB_FUNCTION).collect(Collectors.toList()));
}
if (connectionSettings.getDefaultDataset() != null) {
content.setDefaultDataset(connectionSettings.getDefaultDataset().toPb());
}
if (connectionSettings.getMaximumBytesBilled() != null) {
content.setMaximumBytesBilled(connectionSettings.getMaximumBytesBilled());
}
if (connectionSettings.getMaxResults() != null) {
content.setMaxResults(connectionSettings.getMaxResults());
}
if (queryParameters != null) {
// content.setQueryParameters(queryParameters);
if (queryParameters.get(0).getName() == null) {
// If query parameter name is unset, then assume mode is positional
content.setParameterMode("POSITIONAL");
// pass query parameters
List<QueryParameter> queryParametersPb = Lists.transform(queryParameters, POSITIONAL_PARAMETER_TO_PB_FUNCTION);
content.setQueryParameters(queryParametersPb);
} else {
content.setParameterMode("NAMED");
// pass query parameters
List<QueryParameter> queryParametersPb = Lists.transform(queryParameters, NAMED_PARAMETER_TO_PB_FUNCTION);
content.setQueryParameters(queryParametersPb);
}
}
if (connectionSettings.getCreateSession() != null) {
content.setCreateSession(connectionSettings.getCreateSession());
}
if (labels != null) {
content.setLabels(labels);
}
content.setQuery(sql);
content.setRequestId(requestId);
// The new Connection interface only supports StandardSQL dialect
content.setUseLegacySql(false);
return content;
}
Aggregations