dune_api/matviews/
types.rs

1//! Types for the Materialized Views API
2
3use serde::{Deserialize, Serialize};
4
5/// Materialized view
6#[derive(Debug, Clone, Deserialize, Serialize)]
7pub struct Matview {
8    /// View ID (fully qualified name)
9    pub id: Option<String>,
10    /// Query ID
11    pub query_id: Option<i64>,
12    /// SQL ID
13    pub sql_id: Option<String>,
14    /// Whether the view is private
15    pub is_private: Option<bool>,
16    /// Table size in bytes
17    pub table_size_bytes: Option<i64>,
18}
19
20/// Request to create/update a materialized view
21#[derive(Debug, Clone, Serialize)]
22pub struct UpsertMatviewRequest {
23    /// View name
24    pub name: String,
25    /// Query ID to materialize
26    pub query_id: i64,
27    /// Cron expression for refresh schedule
28    #[serde(skip_serializing_if = "Option::is_none")]
29    pub cron_expression: Option<String>,
30    /// Expiration timestamp
31    #[serde(skip_serializing_if = "Option::is_none")]
32    pub expires_at: Option<String>,
33    /// Whether the view is private
34    #[serde(skip_serializing_if = "Option::is_none")]
35    pub is_private: Option<bool>,
36    /// Performance tier (medium, large)
37    #[serde(skip_serializing_if = "Option::is_none")]
38    pub performance: Option<String>,
39}
40
41impl UpsertMatviewRequest {
42    /// Create a new materialized view request
43    pub fn new(name: &str, query_id: i64) -> Self {
44        Self {
45            name: name.to_string(),
46            query_id,
47            cron_expression: None,
48            expires_at: None,
49            is_private: None,
50            performance: None,
51        }
52    }
53
54    /// Set the cron expression for refresh schedule
55    pub fn cron(mut self, cron_expression: &str) -> Self {
56        self.cron_expression = Some(cron_expression.to_string());
57        self
58    }
59
60    /// Set as private
61    pub fn private(mut self, is_private: bool) -> Self {
62        self.is_private = Some(is_private);
63        self
64    }
65
66    /// Set performance tier to large
67    pub fn large(mut self) -> Self {
68        self.performance = Some("large".to_string());
69        self
70    }
71}
72
73/// Response from creating/updating a materialized view
74#[derive(Debug, Clone, Deserialize, Serialize)]
75pub struct UpsertMatviewResponse {
76    /// View name
77    pub name: Option<String>,
78    /// Execution ID
79    pub execution_id: Option<String>,
80}
81
82/// Request to refresh a materialized view
83#[derive(Debug, Clone, Serialize, Default)]
84pub struct RefreshMatviewRequest {
85    /// Performance tier (medium, large)
86    #[serde(skip_serializing_if = "Option::is_none")]
87    pub performance: Option<String>,
88}
89
90/// Response from refreshing a materialized view
91#[derive(Debug, Clone, Deserialize, Serialize)]
92pub struct RefreshMatviewResponse {
93    /// Execution ID
94    pub execution_id: Option<String>,
95}
96
97/// Response from deleting a materialized view
98#[derive(Debug, Clone, Deserialize, Serialize)]
99pub struct DeleteMatviewResponse {
100    /// Success message
101    pub message: Option<String>,
102}
103
104/// Response from listing materialized views
105#[derive(Debug, Clone, Deserialize, Serialize)]
106pub struct ListMatviewsResponse {
107    /// List of materialized views
108    #[serde(default)]
109    pub materialized_views: Vec<Matview>,
110    /// Next page offset
111    pub next_offset: Option<i64>,
112}
113
114/// Options for listing materialized views
115#[derive(Debug, Clone, Default)]
116pub struct ListMatviewsOptions {
117    /// Maximum number of results
118    pub limit: Option<u32>,
119    /// Pagination offset
120    pub offset: Option<i64>,
121}
122
123impl ListMatviewsOptions {
124    pub fn to_query_string(&self) -> String {
125        let mut params = Vec::new();
126        if let Some(limit) = self.limit {
127            params.push(format!("limit={}", limit));
128        }
129        if let Some(offset) = self.offset {
130            params.push(format!("offset={}", offset));
131        }
132        if params.is_empty() {
133            String::new()
134        } else {
135            format!("?{}", params.join("&"))
136        }
137    }
138}