use crate::errors::BigQueryError;
use crate::query::{infer_param, struct_params, typed_param, ParamLabel};
use crate::{
BigQueryDatasetRef, BigQueryDryRunResult, BigQueryJobCreation, BigQueryJobStats,
BigQueryParamType, BigQueryQueryOutcome, BigQueryQueryParams, BigQueryQuerySupport,
BigQueryReadOptions, BigQueryResult,
};
use crate::{BigQueryLabels, BigQueryLocation, BigQueryRequestId};
use arrow_array::RecordBatch;
use futures::stream::BoxStream;
use gcloud_sdk::google::cloud::bigquery::v2::QueryParameter;
use serde::de::DeserializeOwned;
use serde::Serialize;
use std::marker::PhantomData;
use std::time::Duration;
#[derive(Clone, Debug)]
pub struct BigQueryQueryBuilder<'a, D>
where
D: BigQueryQuerySupport,
{
db: &'a D,
params: BigQueryQueryParams,
failure: Option<BigQueryError>,
}
impl<'a, D> BigQueryQueryBuilder<'a, D>
where
D: BigQueryQuerySupport,
{
pub(crate) fn new(db: &'a D, params: BigQueryQueryParams) -> Self {
Self {
db,
params,
failure: None,
}
}
fn push(mut self, encoded: Result<QueryParameter, BigQueryError>) -> Self {
match encoded {
Ok(parameter) if self.failure.is_none() => self.params.query_parameters.push(parameter),
Ok(_) => {}
Err(failure) => {
self.failure.get_or_insert(failure);
}
}
self
}
fn next_position(&self) -> ParamLabel<'static> {
ParamLabel::Positional(self.params.query_parameters.len())
}
pub fn param<N: Into<String>, V: Serialize>(self, name: N, value: V) -> Self {
let name = name.into();
let encoded = infer_param(ParamLabel::Named(&name), &value);
self.push(encoded)
}
pub fn param_as<N: Into<String>, V: Serialize>(
self,
name: N,
ty: impl Into<BigQueryParamType>,
value: V,
) -> Self {
let name = name.into();
let encoded = typed_param(ParamLabel::Named(&name), &ty.into(), &value);
self.push(encoded)
}
pub fn params<P: Serialize + ?Sized>(self, params: &P) -> Self {
match struct_params(params) {
Ok(encoded) => encoded.into_iter().fold(self, |b, p| b.push(Ok(p))),
Err(failure) => self.push(Err(failure)),
}
}
pub fn positional_param<V: Serialize>(self, value: V) -> Self {
let encoded = infer_param(self.next_position(), &value);
self.push(encoded)
}
pub fn positional_param_as<V: Serialize>(
self,
ty: impl Into<BigQueryParamType>,
value: V,
) -> Self {
let encoded = typed_param(self.next_position(), &ty.into(), &value);
self.push(encoded)
}
pub fn location(self, location: BigQueryLocation) -> Self {
Self {
params: self.params.with_location(location),
..self
}
}
pub fn default_dataset(self, dataset: impl Into<BigQueryDatasetRef>) -> Self {
Self {
params: self.params.with_default_dataset(dataset.into()),
..self
}
}
pub fn label(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.params.labels.insert(key.into(), value.into());
self
}
pub fn labels(mut self, labels: impl Into<BigQueryLabels>) -> Self {
for (key, value) in labels.into() {
self.params.labels.insert(key, value);
}
self
}
pub fn maximum_bytes_billed(self, bytes: i64) -> Self {
Self {
params: self.params.with_maximum_bytes_billed(bytes),
..self
}
}
pub fn use_query_cache(self, use_query_cache: bool) -> Self {
Self {
params: self.params.with_use_query_cache(use_query_cache),
..self
}
}
pub fn timeout(self, timeout: Duration) -> Self {
Self {
params: self.params.with_timeout(timeout),
..self
}
}
pub fn job_timeout(self, job_timeout: Duration) -> Self {
Self {
params: self.params.with_job_timeout(job_timeout),
..self
}
}
pub fn request_id(self, request_id: BigQueryRequestId) -> Self {
Self {
params: self.params.with_request_id(request_id),
..self
}
}
pub fn inline_rows_limit(self, rows: u32) -> Self {
Self {
params: self.params.with_inline_rows_limit(rows),
..self
}
}
pub fn job_creation_required(self) -> Self {
Self {
params: self.params.with_job_creation(BigQueryJobCreation::Required),
..self
}
}
pub fn read_options(self, options: BigQueryReadOptions) -> Self {
Self {
params: self.params.with_read_options(options),
..self
}
}
fn checked(self) -> BigQueryResult<(&'a D, BigQueryQueryParams)> {
match self.failure {
Some(failure) => Err(failure),
None => Ok((self.db, self.params)),
}
}
pub fn obj<T>(self) -> BigQueryQueryObjBuilder<'a, D, T>
where
T: DeserializeOwned + Send + 'static,
{
BigQueryQueryObjBuilder {
query: self,
_target: PhantomData,
}
}
pub async fn execute(self) -> BigQueryResult<BigQueryQueryOutcome> {
let (db, params) = self.checked()?;
db.execute_query(params).await
}
pub async fn dry_run(self) -> BigQueryResult<BigQueryDryRunResult> {
let (db, params) = self.checked()?;
db.dry_run_query(params).await
}
pub async fn record_batches<'b>(
self,
) -> BigQueryResult<BoxStream<'b, BigQueryResult<RecordBatch>>> {
let (db, params) = self.checked()?;
db.query_record_batches(params).await
}
}
#[derive(Clone, Debug)]
pub struct BigQueryQueryObjBuilder<'a, D, T>
where
D: BigQueryQuerySupport,
{
query: BigQueryQueryBuilder<'a, D>,
_target: PhantomData<fn() -> T>,
}
impl<'a, D, T> BigQueryQueryObjBuilder<'a, D, T>
where
D: BigQueryQuerySupport,
T: DeserializeOwned + Send + 'static,
{
fn checked(self) -> BigQueryResult<(&'a D, BigQueryQueryParams)> {
self.query.checked()
}
pub async fn query(self) -> BigQueryResult<Vec<T>> {
let (db, params) = self.checked()?;
db.query_obj(params).await
}
pub async fn query_with_stats(self) -> BigQueryResult<(Vec<T>, BigQueryJobStats)> {
let (db, params) = self.checked()?;
db.query_obj_with_stats(params).await
}
pub async fn stream_query_with_stats<'b>(
self,
) -> BigQueryResult<(BoxStream<'b, BigQueryResult<T>>, BigQueryJobStats)> {
let (db, params) = self.checked()?;
db.stream_query_obj_with_stats(params).await
}
pub async fn stream_query<'b>(self) -> BigQueryResult<BoxStream<'b, T>> {
let (db, params) = self.checked()?;
db.stream_query_obj(params).await
}
pub async fn stream_query_with_errors<'b>(
self,
) -> BigQueryResult<BoxStream<'b, BigQueryResult<T>>> {
let (db, params) = self.checked()?;
db.stream_query_obj_with_errors(params).await
}
}