use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct TableColumn {
pub name: String,
#[serde(rename = "type")]
pub column_type: String,
#[serde(default)]
pub nullable: bool,
}
impl TableColumn {
pub fn new(name: &str, column_type: &str) -> Self {
Self {
name: name.to_string(),
column_type: column_type.to_string(),
nullable: true,
}
}
pub fn not_null(mut self) -> Self {
self.nullable = false;
self
}
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct TableColumnInfo {
pub name: Option<String>,
#[serde(rename = "type")]
pub column_type: Option<String>,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct TableOwner {
pub handle: Option<String>,
#[serde(rename = "type")]
pub owner_type: Option<String>,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Table {
pub full_name: Option<String>,
#[serde(default)]
pub columns: Vec<TableColumnInfo>,
pub is_private: Option<bool>,
pub owner: Option<TableOwner>,
pub table_size_bytes: Option<String>,
pub created_at: Option<String>,
pub updated_at: Option<String>,
pub purged_at: Option<String>,
}
#[derive(Debug, Clone, Serialize)]
pub struct CreateTableRequest {
pub namespace: String,
pub table_name: String,
pub schema: Vec<TableColumn>,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub is_private: Option<bool>,
}
impl CreateTableRequest {
pub fn new(namespace: &str, table_name: &str, schema: Vec<TableColumn>) -> Self {
Self {
namespace: namespace.to_string(),
table_name: table_name.to_string(),
schema,
description: None,
is_private: None,
}
}
pub fn description(mut self, description: &str) -> Self {
self.description = Some(description.to_string());
self
}
pub fn private(mut self, is_private: bool) -> Self {
self.is_private = Some(is_private);
self
}
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct CreateTableResponse {
pub full_name: Option<String>,
pub namespace: Option<String>,
pub table_name: Option<String>,
pub already_existed: Option<bool>,
pub example_query: Option<String>,
pub message: Option<String>,
}
#[derive(Debug, Clone, Serialize)]
pub struct UploadCsvRequest {
pub table_name: String,
pub data: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub is_private: Option<bool>,
}
impl UploadCsvRequest {
pub fn new(table_name: &str, csv_data: &str) -> Self {
Self {
table_name: table_name.to_string(),
data: csv_data.to_string(),
description: None,
is_private: None,
}
}
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct UploadCsvResponse {
pub success: Option<bool>,
pub table_name: Option<String>,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct InsertResponse {
pub name: Option<String>,
pub rows_written: Option<i64>,
pub bytes_written: Option<i64>,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct ClearTableResponse {
pub message: Option<String>,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct DeleteTableResponse {
pub message: Option<String>,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct ListTablesResponse {
#[serde(default)]
pub tables: Vec<Table>,
pub next_offset: Option<i64>,
}
#[derive(Debug, Clone, Default)]
pub struct ListTablesOptions {
pub limit: Option<u32>,
pub offset: Option<i64>,
}
impl ListTablesOptions {
pub fn to_query_string(&self) -> String {
let mut params = Vec::new();
if let Some(limit) = self.limit {
params.push(format!("limit={}", limit));
}
if let Some(offset) = self.offset {
params.push(format!("offset={}", offset));
}
if params.is_empty() {
String::new()
} else {
format!("?{}", params.join("&"))
}
}
}