gcp_bigquery_client/model/
table_data_insert_all_request.rs

1use crate::error::BQError;
2use crate::model::table_data_insert_all_request_rows::TableDataInsertAllRequestRows;
3use serde::{Deserialize, Serialize};
4
5#[cfg(feature = "gzip")]
6use flate2::{write::GzEncoder, Compression};
7#[cfg(feature = "gzip")]
8use std::io::Write;
9
10#[derive(Debug, Default, Clone, Serialize, Deserialize)]
11#[serde(rename_all = "camelCase")]
12pub struct TableDataInsertAllRequest {
13    /// [Optional] Accept rows that contain values that do not match the schema. The unknown values are ignored. Default is false, which treats unknown values as errors.
14    ignore_unknown_values: bool,
15    /// The resource type of the response.
16    #[serde(skip_serializing_if = "Option::is_none")]
17    kind: Option<String>,
18    /// The rows to insert.
19    rows: Vec<TableDataInsertAllRequestRows>,
20    /// [Optional] Insert all valid rows of a request, even if invalid rows exist. The default value is false, which causes the entire request to fail if any invalid rows exist.
21    skip_invalid_rows: bool,
22    /// If specified, treats the destination table as a base template, and inserts the rows into an instance table named \"{destination}{templateSuffix}\". BigQuery will manage creation of the instance table, using the schema of the base template table. See https://cloud.google.com/bigquery/streaming-data-into-bigquery#template-tables for considerations when working with templates tables.
23    #[serde(skip_serializing_if = "Option::is_none")]
24    template_suffix: Option<String>,
25}
26
27impl TableDataInsertAllRequest {
28    pub fn new() -> Self {
29        TableDataInsertAllRequest {
30            ignore_unknown_values: false,
31            kind: None,
32            rows: vec![],
33            skip_invalid_rows: false,
34            template_suffix: None,
35        }
36    }
37
38    pub fn ignore_unknown_values(&mut self) -> &mut Self {
39        self.ignore_unknown_values = true;
40        self
41    }
42
43    pub fn kind(&mut self, kind: impl Into<String>) -> &mut Self {
44        self.kind = Some(kind.into());
45        self
46    }
47
48    pub fn add_row<T: Serialize>(&mut self, insert_id: Option<String>, object: T) -> Result<(), BQError> {
49        let json = serde_json::to_value(object)?;
50        self.rows.push(TableDataInsertAllRequestRows { insert_id, json });
51        Ok(())
52    }
53
54    pub fn add_rows(&mut self, objects: Vec<TableDataInsertAllRequestRows>) -> Result<(), BQError> {
55        self.rows.extend(objects);
56        Ok(())
57    }
58
59    pub fn skip_invalid_rows(&mut self) -> &mut Self {
60        self.skip_invalid_rows = true;
61        self
62    }
63
64    pub fn template_suffix(&mut self, suffix: impl Into<String>) -> &mut Self {
65        self.template_suffix = Some(suffix.into());
66        self
67    }
68
69    pub fn is_empty(&self) -> bool {
70        self.rows.is_empty()
71    }
72
73    pub fn len(&self) -> usize {
74        self.rows.len()
75    }
76
77    pub fn clear(&mut self) {
78        self.rows.clear()
79    }
80}
81
82/// A gzipped version of `TableDataInsertAllRequest`.
83#[cfg(feature = "gzip")]
84pub struct TableDataInsertAllRequestGzipped {
85    pub(crate) data: Vec<u8>,
86}
87
88#[cfg(feature = "gzip")]
89impl TryFrom<TableDataInsertAllRequest> for TableDataInsertAllRequestGzipped {
90    type Error = BQError;
91
92    fn try_from(request: TableDataInsertAllRequest) -> Result<Self, Self::Error> {
93        let mut encoder = GzEncoder::new(Vec::new(), Compression::default());
94        encoder.write_all(serde_json::to_string(&request)?.as_bytes())?;
95        let gzipped_data = encoder.finish()?;
96        Ok(Self { data: gzipped_data })
97    }
98}
99
100#[cfg(feature = "gzip")]
101impl TableDataInsertAllRequestGzipped {
102    pub fn new(data: Vec<u8>) -> Self {
103        Self { data }
104    }
105
106    pub fn len(&self) -> usize {
107        self.data.len()
108    }
109
110    pub fn is_empty(&self) -> bool {
111        self.data.is_empty()
112    }
113}