gcp_bigquery_client/model/
table_data_insert_all_request.rsuse crate::error::BQError;
use crate::model::table_data_insert_all_request_rows::TableDataInsertAllRequestRows;
use serde::{Deserialize, Serialize};
#[cfg(feature = "gzip")]
use flate2::{write::GzEncoder, Compression};
#[cfg(feature = "gzip")]
use std::io::Write;
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TableDataInsertAllRequest {
ignore_unknown_values: bool,
#[serde(skip_serializing_if = "Option::is_none")]
kind: Option<String>,
rows: Vec<TableDataInsertAllRequestRows>,
skip_invalid_rows: bool,
#[serde(skip_serializing_if = "Option::is_none")]
template_suffix: Option<String>,
}
impl TableDataInsertAllRequest {
pub fn new() -> Self {
TableDataInsertAllRequest {
ignore_unknown_values: false,
kind: None,
rows: vec![],
skip_invalid_rows: false,
template_suffix: None,
}
}
pub fn ignore_unknown_values(&mut self) -> &mut Self {
self.ignore_unknown_values = true;
self
}
pub fn kind(&mut self, kind: impl Into<String>) -> &mut Self {
self.kind = Some(kind.into());
self
}
pub fn add_row<T: Serialize>(&mut self, insert_id: Option<String>, object: T) -> Result<(), BQError> {
let json = serde_json::to_value(object)?;
self.rows.push(TableDataInsertAllRequestRows { insert_id, json });
Ok(())
}
pub fn add_rows(&mut self, objects: Vec<TableDataInsertAllRequestRows>) -> Result<(), BQError> {
self.rows.extend(objects);
Ok(())
}
pub fn skip_invalid_rows(&mut self) -> &mut Self {
self.skip_invalid_rows = true;
self
}
pub fn template_suffix(&mut self, suffix: impl Into<String>) -> &mut Self {
self.template_suffix = Some(suffix.into());
self
}
pub fn is_empty(&self) -> bool {
self.rows.is_empty()
}
pub fn len(&self) -> usize {
self.rows.len()
}
pub fn clear(&mut self) {
self.rows.clear()
}
}
#[cfg(feature = "gzip")]
pub struct TableDataInsertAllRequestGzipped {
pub(crate) data: Vec<u8>,
}
#[cfg(feature = "gzip")]
impl TryFrom<TableDataInsertAllRequest> for TableDataInsertAllRequestGzipped {
type Error = BQError;
fn try_from(request: TableDataInsertAllRequest) -> Result<Self, Self::Error> {
let mut encoder = GzEncoder::new(Vec::new(), Compression::default());
encoder.write_all(serde_json::to_string(&request)?.as_bytes())?;
let gzipped_data = encoder.finish()?;
Ok(Self { data: gzipped_data })
}
}
#[cfg(feature = "gzip")]
impl TableDataInsertAllRequestGzipped {
pub fn new(data: Vec<u8>) -> Self {
Self { data }
}
pub fn len(&self) -> usize {
self.data.len()
}
pub fn is_empty(&self) -> bool {
self.data.is_empty()
}
}