use std::collections::HashMap;
use serde::Deserialize;
#[allow(clippy::large_enum_variant)]
#[derive(Deserialize, Debug)]
#[serde(untagged)]
pub enum ExecResponse {
QueryAsync(QueryAsyncExecResponse),
Query(QueryExecResponse),
PutGet(PutGetExecResponse),
Error(ExecErrorResponse),
}
pub fn is_query_in_progress(code: Option<&String>) -> bool {
matches!(code.map(String::as_str), Some("333333" | "333334"))
}
pub fn is_session_expired(code: Option<&String>) -> bool {
matches!(code.map(String::as_str), Some("390112"))
}
pub fn is_query_not_executing(code: Option<&String>) -> bool {
matches!(code.map(String::as_str), Some("000605"))
}
pub fn is_sql_execution_cancelled(code: Option<&String>) -> bool {
matches!(code.map(String::as_str), Some("000604"))
}
#[allow(clippy::large_enum_variant, dead_code)]
#[derive(Deserialize, Debug)]
#[serde(untagged)]
pub enum AuthResponse {
Login(LoginResponse),
Auth(AuthenticatorResponse),
Renew(RenewSessionResponse),
Close(CloseSessionResponse),
Error(AuthErrorResponse),
}
#[derive(Deserialize, Debug)]
pub struct BaseRestResponse<D> {
pub code: Option<String>,
pub message: Option<String>,
pub success: bool,
pub data: D,
}
pub type PutGetExecResponse = BaseRestResponse<PutGetResponseData>;
pub type QueryExecResponse = BaseRestResponse<QueryExecResponseData>;
pub type QueryAsyncExecResponse = BaseRestResponse<QueryAsyncExecResponseData>;
pub type ExecErrorResponse = BaseRestResponse<ExecErrorResponseData>;
pub type CancelQueryResponse = BaseRestResponse<serde_json::Value>;
pub type MonitoringResponse = BaseRestResponse<MonitoringResponseData>;
pub type AuthErrorResponse = BaseRestResponse<AuthErrorResponseData>;
pub type AuthenticatorResponse = BaseRestResponse<AuthenticatorResponseData>;
pub type LoginResponse = BaseRestResponse<LoginResponseData>;
pub type RenewSessionResponse = BaseRestResponse<RenewSessionResponseData>;
pub type CloseSessionResponse = BaseRestResponse<Option<()>>;
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct ExecErrorResponseData {
pub age: i64,
pub error_code: String,
pub internal_error: Option<bool>,
pub line: Option<i64>,
pub pos: Option<i64>,
pub query_id: Option<String>,
pub sql_state: Option<String>,
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct MonitoringResponseData {
#[serde(default)]
pub queries: Vec<MonitoringQueryEntry>,
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
#[allow(dead_code)]
pub struct MonitoringQueryEntry {
pub id: String,
pub status: String,
pub error_code: Option<String>,
pub error_message: Option<String>,
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct QueryAsyncExecResponseData {
pub query_id: String,
pub get_result_url: String,
pub query_aborts_after_secs: Option<i64>,
pub progress_desc: Option<String>,
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
#[allow(dead_code)]
pub struct AuthErrorResponseData {
pub authn_method: Option<String>,
pub error_code: Option<String>,
}
#[derive(Deserialize, Debug)]
pub struct NameValueParameter {
pub name: String,
pub value: serde_json::Value,
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
#[allow(dead_code)]
pub struct LoginResponseData {
pub session_id: i64,
pub token: String,
pub master_token: String,
pub server_version: String,
#[serde(default)]
pub parameters: Vec<NameValueParameter>,
pub session_info: SessionInfo,
pub master_validity_in_seconds: i64,
pub validity_in_seconds: i64,
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
#[allow(dead_code)]
pub struct SessionInfo {
pub database_name: Option<String>,
pub schema_name: Option<String>,
pub warehouse_name: Option<String>,
pub role_name: String,
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
#[allow(dead_code)]
pub struct AuthenticatorResponseData {
pub token_url: Option<String>,
pub sso_url: String,
pub proof_key: String,
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
#[allow(dead_code)]
pub struct RenewSessionResponseData {
pub session_token: String,
pub validity_in_seconds_s_t: i64,
pub master_token: String,
pub validity_in_seconds_m_t: i64,
pub session_id: i64,
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct QueryExecResponseData {
pub parameters: Vec<NameValueParameter>,
pub rowtype: Vec<ExecResponseRowType>,
pub rowset: Option<serde_json::Value>,
pub rowset_base64: Option<String>,
pub total: i64,
pub returned: i64, pub query_id: String, pub database_provider: Option<String>,
pub final_database_name: Option<String>, pub final_schema_name: Option<String>,
pub final_warehouse_name: Option<String>, pub final_role_name: String, pub number_of_binds: Option<i32>, pub statement_type_id: i64,
pub version: i64,
#[serde(default)] pub chunks: Vec<ExecResponseChunk>,
pub qrmk: Option<String>,
#[serde(default)] pub chunk_headers: HashMap<String, String>,
pub get_result_url: Option<String>,
pub result_ids: Option<String>,
}
#[derive(Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct ExecResponseRowType {
#[serde(default)]
pub name: String,
pub byte_length: Option<i64>,
pub length: Option<i64>,
#[serde(rename = "type")]
pub type_: SnowflakeType,
pub scale: Option<i64>,
pub precision: Option<i64>,
pub nullable: bool,
#[serde(default)]
pub ext_type_name: Option<String>,
#[serde(default)]
pub vector_dimension: Option<i64>,
#[serde(default)]
pub fields: Option<Vec<ExecResponseRowType>>,
}
#[derive(Deserialize, Debug, Clone, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum SnowflakeType {
Fixed,
Real,
Text,
Date,
Variant,
TimestampLtz,
TimestampNtz,
TimestampTz,
Object,
Binary,
Time,
Boolean,
Array,
Vector,
Map,
Decfloat,
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct ExecResponseChunk {
pub url: String,
pub row_count: i32,
pub uncompressed_size: i64,
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct PutGetResponseData {
pub query_id: String,
pub command: CommandType,
pub local_location: Option<String>,
#[serde(rename = "src_locations", default)]
pub src_locations: Vec<String>,
pub parallel: usize, pub threshold: i64,
pub auto_compress: bool,
pub overwrite: bool,
pub source_compression: String,
pub stage_info: PutGetStageInfo,
pub encryption_material: EncryptionMaterialVariant,
#[serde(default)]
pub presigned_urls: Vec<String>,
#[serde(default)]
pub parameters: Vec<NameValueParameter>,
pub statement_type_id: Option<i64>,
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "UPPERCASE")]
pub enum CommandType {
Upload,
Download,
}
#[derive(Deserialize, Debug)]
#[serde(untagged)]
pub enum PutGetStageInfo {
Aws(AwsPutGetStageInfo),
Azure(AzurePutGetStageInfo),
Gcs(GcsPutGetStageInfo),
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct AwsPutGetStageInfo {
pub location_type: String,
pub location: String,
pub region: String,
pub creds: AwsCredentials,
pub end_point: Option<String>,
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub struct AwsCredentials {
pub aws_key_id: String,
pub aws_secret_key: String,
pub aws_token: String,
pub aws_id: String,
pub aws_key: String,
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct GcsPutGetStageInfo {
pub location_type: String,
pub location: String,
pub storage_account: String,
pub creds: GcsCredentials,
pub presigned_url: String,
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub struct GcsCredentials {
pub gcs_access_token: String,
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct AzurePutGetStageInfo {
pub location_type: String,
pub location: String,
pub storage_account: String,
pub creds: AzureCredentials,
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub struct AzureCredentials {
pub azure_sas_token: String,
}
#[derive(Deserialize, Debug)]
#[serde(untagged)]
pub enum EncryptionMaterialVariant {
Single(PutGetEncryptionMaterial),
Multiple(Vec<PutGetEncryptionMaterial>),
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct PutGetEncryptionMaterial {
pub query_stage_master_key: String,
pub query_id: String,
pub smk_id: i64,
}