gcp_bigquery_client/model/
dataset.rs1use crate::error::BQError;
2use crate::model::dataset_reference::DatasetReference;
3use crate::model::table::Table;
4use crate::Client;
5use std::collections::HashMap;
6
7#[derive(Debug, Default, Clone, Serialize, Deserialize)]
8#[serde(rename_all = "camelCase")]
9pub struct Dataset {
10 pub dataset_reference: DatasetReference,
12 #[serde(skip_serializing_if = "Option::is_none")]
14 pub friendly_name: Option<String>,
15 #[serde(skip_serializing_if = "Option::is_none")]
17 pub id: Option<String>,
18 #[serde(skip_serializing_if = "Option::is_none")]
20 pub kind: Option<String>,
21 #[serde(skip_serializing_if = "Option::is_none")]
23 pub labels: Option<std::collections::HashMap<String, String>>,
24 #[serde(skip_serializing_if = "Option::is_none")]
26 pub location: Option<String>,
27}
28
29impl Dataset {
30 pub fn new(project_id: &str, dataset_id: &str) -> Self {
31 Self {
32 dataset_reference: DatasetReference {
33 dataset_id: dataset_id.into(),
34 project_id: project_id.into(),
35 },
36 friendly_name: None,
37 id: None,
38 kind: None,
39 labels: None,
40 location: None,
41 }
42 }
43
44 pub fn project_id(&self) -> &String {
46 &self.dataset_reference.project_id
47 }
48
49 pub fn dataset_id(&self) -> &String {
51 &self.dataset_reference.dataset_id
52 }
53
54 pub fn location(mut self, location: &str) -> Self {
58 self.location = Some(location.into());
59 self
60 }
61
62 pub fn friendly_name(mut self, friendly_name: &str) -> Self {
66 self.friendly_name = Some(friendly_name.into());
67 self
68 }
69
70 pub fn label(mut self, key: &str, value: &str) -> Self {
75 if let Some(labels) = self.labels.as_mut() {
76 labels.insert(key.into(), value.into());
77 } else {
78 let mut labels = HashMap::default();
79 labels.insert(key.into(), value.into());
80 self.labels = Some(labels);
81 }
82 self
83 }
84
85 pub async fn create_table(&self, client: &Client, table: Table) -> Result<Table, BQError> {
90 client.table().create(table).await
91 }
92
93 pub async fn delete(self, client: &Client, delete_contents: bool) -> Result<(), BQError> {
98 client
99 .dataset()
100 .delete(self.project_id(), self.dataset_id(), delete_contents)
101 .await
102 }
103}