use crate::schema::BigQueryTableDeclarationDraft;
use crate::BigQueryInstant;
use crate::BigQueryLabels;
use crate::{
BigQueryDatasetBuilder, BigQueryDatasetListBuilder, BigQueryDatasetRef, BigQueryDb,
BigQueryPartitionUnit, BigQueryPartitioning, BigQueryRecreatePolicy, BigQueryResult,
BigQuerySchemaColumns, BigQuerySchemaColumnsBuilder, BigQueryTable, BigQueryTableDeclaration,
BigQueryTablePlan, BigQueryTableRef, BigQueryTableSyncReport,
};
use std::time::Duration;
#[derive(Clone, Debug)]
pub struct BigQuerySchemaBuilder<'a> {
db: &'a BigQueryDb,
}
impl<'a> BigQuerySchemaBuilder<'a> {
pub(crate) fn new(db: &'a BigQueryDb) -> Self {
Self { db }
}
#[inline]
pub fn table(self, table: impl Into<BigQueryTableRef>) -> BigQueryTableSchemaBuilder<'a> {
BigQueryTableSchemaBuilder {
db: self.db,
draft: BigQueryTableDeclarationDraft::new(table.into()),
}
}
#[inline]
pub fn dataset(self, dataset: impl Into<BigQueryDatasetRef>) -> BigQueryDatasetBuilder<'a> {
BigQueryDatasetBuilder::new(self.db, dataset.into())
}
#[inline]
pub fn datasets(self) -> BigQueryDatasetListBuilder<'a> {
BigQueryDatasetListBuilder::new(self.db)
}
}
#[derive(Clone, Debug)]
pub struct BigQueryTableSchemaBuilder<'a> {
db: &'a BigQueryDb,
draft: BigQueryTableDeclarationDraft,
}
impl<'a> BigQueryTableSchemaBuilder<'a> {
#[inline]
pub fn columns<F, R>(mut self, columns: F) -> Self
where
F: FnOnce(BigQuerySchemaColumnsBuilder) -> R,
R: Into<BigQuerySchemaColumns>,
{
self.draft.columns = columns(BigQuerySchemaColumnsBuilder).into();
self
}
#[inline]
pub fn primary_key<I>(mut self, columns: I) -> Self
where
I: IntoIterator,
I::Item: Into<String>,
{
self.draft.primary_key = Some(columns.into_iter().map(Into::into).collect());
self
}
#[inline]
pub fn partition_by(mut self, partitioning: BigQueryPartitioning) -> Self {
self.draft.partitioning = Some(partitioning);
self
}
#[inline]
pub fn partition_by_hour(self, column: impl Into<String>) -> Self {
self.partition_on(BigQueryPartitionUnit::Hour, column)
}
#[inline]
pub fn partition_by_day(self, column: impl Into<String>) -> Self {
self.partition_on(BigQueryPartitionUnit::Day, column)
}
#[inline]
pub fn partition_by_month(self, column: impl Into<String>) -> Self {
self.partition_on(BigQueryPartitionUnit::Month, column)
}
#[inline]
pub fn partition_by_year(self, column: impl Into<String>) -> Self {
self.partition_on(BigQueryPartitionUnit::Year, column)
}
fn partition_on(self, unit: BigQueryPartitionUnit, column: impl Into<String>) -> Self {
self.partition_by(BigQueryPartitioning::Time {
unit,
column: Some(column.into()),
})
}
#[inline]
pub fn partition_expiration(mut self, expiration: Duration) -> Self {
self.draft.partition_expiration = Some(expiration);
self
}
#[inline]
pub fn cluster_by<I>(mut self, columns: I) -> Self
where
I: IntoIterator,
I::Item: Into<String>,
{
self.draft.clustering = Some(columns.into_iter().map(Into::into).collect());
self
}
#[inline]
pub fn description(mut self, description: impl Into<String>) -> Self {
self.draft.description = Some(description.into());
self
}
#[inline]
pub fn labels(mut self, labels: impl Into<BigQueryLabels>) -> Self {
self.draft.labels = labels.into();
self
}
#[inline]
pub fn expiration(mut self, at: BigQueryInstant) -> Self {
self.draft.expiration = Some(at);
self
}
#[inline]
pub fn allow_widening(mut self) -> Self {
self.draft.allow_widening = true;
self
}
#[inline]
pub fn prune_undeclared(mut self) -> Self {
self.draft.prune = true;
self
}
#[inline]
pub fn recreate_if_empty(mut self) -> Self {
self.draft.recreate = Some(BigQueryRecreatePolicy::IfEmpty);
self
}
#[inline]
pub fn dangerously_recreate_with_data_loss(mut self) -> Self {
self.draft.recreate = Some(BigQueryRecreatePolicy::DangerouslyWithDataLoss);
self
}
#[inline]
pub fn snapshot_first(mut self) -> Self {
self.draft.snapshot_first = true;
self
}
pub async fn plan(self) -> BigQueryResult<BigQueryTablePlan> {
let declaration = BigQueryTableDeclaration::try_from(self.draft)?;
self.db.plan_table_schema(declaration).await
}
pub async fn sync(self) -> BigQueryResult<BigQueryTableSyncReport> {
let declaration = BigQueryTableDeclaration::try_from(self.draft)?;
self.db.sync_table_schema(declaration).await
}
pub async fn get(self) -> BigQueryResult<BigQueryTable> {
self.db.get_table(&self.draft.table).await
}
pub async fn delete(self) -> BigQueryResult<()> {
self.db.delete_table(&self.draft.table).await
}
}