use crate::admin::{logging_errors, DatasetEdits, DatasetSettings, LabelEdit};
use crate::{
BigQueryDataset, BigQueryDatasetRef, BigQueryDatasetSummary, BigQueryDb, BigQueryResult,
BigQueryTableSummary,
};
use crate::{BigQueryLabels, BigQueryLocation};
use futures::stream::BoxStream;
use std::time::Duration;
#[derive(Clone, Debug)]
pub struct BigQueryDatasetBuilder<'a> {
db: &'a BigQueryDb,
dataset: BigQueryDatasetRef,
}
impl<'a> BigQueryDatasetBuilder<'a> {
pub(crate) fn new(db: &'a BigQueryDb, dataset: BigQueryDatasetRef) -> Self {
Self { db, dataset }
}
#[inline]
pub fn create(self) -> BigQueryDatasetCreateBuilder<'a> {
BigQueryDatasetCreateBuilder {
db: self.db,
dataset: self.dataset,
settings: DatasetSettings::default(),
}
}
pub async fn get(self) -> BigQueryResult<BigQueryDataset> {
self.db.get_dataset(&self.dataset).await
}
#[inline]
pub fn update(self) -> BigQueryDatasetUpdateBuilder<'a> {
BigQueryDatasetUpdateBuilder {
db: self.db,
dataset: self.dataset,
edits: DatasetEdits::default(),
}
}
pub async fn delete(self) -> BigQueryResult<()> {
self.db.delete_dataset(&self.dataset, false).await
}
pub async fn dangerously_delete_with_contents(self) -> BigQueryResult<()> {
self.db.delete_dataset(&self.dataset, true).await
}
#[inline]
pub fn tables(self) -> BigQueryTableListBuilder<'a> {
BigQueryTableListBuilder {
db: self.db,
dataset: self.dataset,
page_size: None,
}
}
}
#[derive(Clone, Debug)]
pub struct BigQueryDatasetCreateBuilder<'a> {
db: &'a BigQueryDb,
dataset: BigQueryDatasetRef,
settings: DatasetSettings,
}
impl BigQueryDatasetCreateBuilder<'_> {
#[inline]
pub fn location(mut self, location: BigQueryLocation) -> Self {
self.settings.location = Some(location);
self
}
#[inline]
pub fn description(mut self, description: impl Into<String>) -> Self {
self.settings.description = Some(description.into());
self
}
#[inline]
pub fn default_table_expiration(mut self, expiration: Duration) -> Self {
self.settings.default_table_expiration = Some(expiration);
self
}
#[inline]
pub fn labels(mut self, labels: impl Into<BigQueryLabels>) -> Self {
self.settings.labels = labels.into();
self
}
pub async fn execute(self) -> BigQueryResult<BigQueryDataset> {
self.db.create_dataset(&self.dataset, self.settings).await
}
}
#[derive(Clone, Debug)]
pub struct BigQueryDatasetUpdateBuilder<'a> {
db: &'a BigQueryDb,
dataset: BigQueryDatasetRef,
edits: DatasetEdits,
}
impl BigQueryDatasetUpdateBuilder<'_> {
#[inline]
pub fn description(mut self, description: impl Into<String>) -> Self {
self.edits.description = Some(Some(description.into()));
self
}
#[inline]
pub fn clear_description(mut self) -> Self {
self.edits.description = Some(None);
self
}
#[inline]
pub fn labels(mut self, labels: impl Into<BigQueryLabels>) -> Self {
self.edits.labels.push(LabelEdit::ReplaceAll(labels.into()));
self
}
#[inline]
pub fn label(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.edits
.labels
.push(LabelEdit::Set(key.into(), value.into()));
self
}
#[inline]
pub fn remove_label(mut self, key: impl Into<String>) -> Self {
self.edits.labels.push(LabelEdit::Remove(key.into()));
self
}
pub async fn execute(self) -> BigQueryResult<BigQueryDataset> {
self.db.update_dataset(&self.dataset, self.edits).await
}
}
#[derive(Clone, Debug)]
pub struct BigQueryDatasetListBuilder<'a> {
db: &'a BigQueryDb,
project: Option<String>,
page_size: Option<u32>,
}
impl<'a> BigQueryDatasetListBuilder<'a> {
pub(crate) fn new(db: &'a BigQueryDb) -> Self {
Self {
db,
project: None,
page_size: None,
}
}
#[inline]
pub fn project(mut self, project: impl Into<String>) -> Self {
self.project = Some(project.into());
self
}
#[inline]
pub fn page_size(mut self, page_size: u32) -> Self {
self.page_size = Some(page_size);
self
}
pub async fn stream_all<'b>(self) -> BigQueryResult<BoxStream<'b, BigQueryDatasetSummary>> {
Ok(logging_errors(
self.stream_all_with_errors().await?,
"datasets",
))
}
pub async fn stream_all_with_errors<'b>(
self,
) -> BigQueryResult<BoxStream<'b, BigQueryResult<BigQueryDatasetSummary>>> {
Ok(self.db.list_datasets(self.project, self.page_size))
}
}
#[derive(Clone, Debug)]
pub struct BigQueryTableListBuilder<'a> {
db: &'a BigQueryDb,
dataset: BigQueryDatasetRef,
page_size: Option<u32>,
}
impl BigQueryTableListBuilder<'_> {
#[inline]
pub fn page_size(mut self, page_size: u32) -> Self {
self.page_size = Some(page_size);
self
}
pub async fn stream_all<'b>(self) -> BigQueryResult<BoxStream<'b, BigQueryTableSummary>> {
Ok(logging_errors(
self.stream_all_with_errors().await?,
"tables",
))
}
pub async fn stream_all_with_errors<'b>(
self,
) -> BigQueryResult<BoxStream<'b, BigQueryResult<BigQueryTableSummary>>> {
Ok(self.db.list_tables(&self.dataset, self.page_size))
}
}