Skip to main content

qail_core/ast/cmd/
constructors.rs

1//! Static constructor methods for Qail.
2//!
3//! Methods like get(), set(), add(), del(), make(), etc.
4
5use crate::ast::{Action, Qail};
6
7impl Qail {
8    /// SELECT — query rows.
9    pub fn get(table: impl Into<String>) -> Self {
10        Self {
11            action: Action::Get,
12            table: table.into(),
13            ..Default::default()
14        }
15    }
16
17    /// UPDATE — modify rows.
18    pub fn set(table: impl Into<String>) -> Self {
19        Self {
20            action: Action::Set,
21            table: table.into(),
22            ..Default::default()
23        }
24    }
25
26    /// DELETE — remove rows.
27    pub fn del(table: impl Into<String>) -> Self {
28        Self {
29            action: Action::Del,
30            table: table.into(),
31            ..Default::default()
32        }
33    }
34
35    /// INSERT — add rows.
36    pub fn add(table: impl Into<String>) -> Self {
37        Self {
38            action: Action::Add,
39            table: table.into(),
40            ..Default::default()
41        }
42    }
43
44    /// UPSERT — insert or update.
45    pub fn put(table: impl Into<String>) -> Self {
46        Self {
47            action: Action::Put,
48            table: table.into(),
49            ..Default::default()
50        }
51    }
52
53    /// MERGE — conditionally insert, update, delete, or do nothing.
54    pub fn merge_into(table: impl Into<String>) -> Self {
55        Self {
56            action: Action::Merge,
57            table: table.into(),
58            ..Default::default()
59        }
60    }
61
62    /// COPY … TO — export data.
63    pub fn export(table: impl Into<String>) -> Self {
64        Self {
65            action: Action::Export,
66            table: table.into(),
67            ..Default::default()
68        }
69    }
70
71    /// CREATE TABLE.
72    pub fn make(table: impl Into<String>) -> Self {
73        Self {
74            action: Action::Make,
75            table: table.into(),
76            ..Default::default()
77        }
78    }
79
80    /// TRUNCATE — empty a table.
81    pub fn truncate(table: impl Into<String>) -> Self {
82        Self {
83            action: Action::Truncate,
84            table: table.into(),
85            ..Default::default()
86        }
87    }
88
89    /// EXPLAIN — show query plan.
90    pub fn explain(table: impl Into<String>) -> Self {
91        Self {
92            action: Action::Explain,
93            table: table.into(),
94            ..Default::default()
95        }
96    }
97
98    /// EXPLAIN ANALYZE — show query plan with execution stats.
99    pub fn explain_analyze(table: impl Into<String>) -> Self {
100        Self {
101            action: Action::ExplainAnalyze,
102            table: table.into(),
103            ..Default::default()
104        }
105    }
106
107    /// LOCK TABLE.
108    pub fn lock(table: impl Into<String>) -> Self {
109        Self {
110            action: Action::Lock,
111            table: table.into(),
112            ..Default::default()
113        }
114    }
115
116    /// CREATE MATERIALIZED VIEW.
117    pub fn create_materialized_view(name: impl Into<String>, query: Qail) -> Self {
118        Self {
119            action: Action::CreateMaterializedView,
120            table: name.into(),
121            source_query: Some(Box::new(query)),
122            ..Default::default()
123        }
124    }
125
126    /// REFRESH MATERIALIZED VIEW.
127    pub fn refresh_materialized_view(name: impl Into<String>) -> Self {
128        Self {
129            action: Action::RefreshMaterializedView,
130            table: name.into(),
131            ..Default::default()
132        }
133    }
134
135    /// DROP MATERIALIZED VIEW.
136    pub fn drop_materialized_view(name: impl Into<String>) -> Self {
137        Self {
138            action: Action::DropMaterializedView,
139            table: name.into(),
140            ..Default::default()
141        }
142    }
143
144    // PostgreSQL Pub/Sub (LISTEN/NOTIFY)
145
146    /// Create a LISTEN command to subscribe to a channel.
147    ///
148    /// # Example
149    /// ```ignore
150    /// let cmd = Qail::listen("orders");
151    /// // Generates: LISTEN orders
152    /// ```
153    pub fn listen(channel: impl Into<String>) -> Self {
154        Self {
155            action: Action::Listen,
156            channel: Some(channel.into()),
157            ..Default::default()
158        }
159    }
160
161    /// Create an UNLISTEN command to unsubscribe from a channel.
162    ///
163    /// # Example
164    /// ```ignore
165    /// let cmd = Qail::unlisten("orders");
166    /// // Generates: UNLISTEN orders
167    /// ```
168    pub fn unlisten(channel: impl Into<String>) -> Self {
169        Self {
170            action: Action::Unlisten,
171            channel: Some(channel.into()),
172            ..Default::default()
173        }
174    }
175
176    /// Create a NOTIFY command to send a message to a channel.
177    ///
178    /// # Example
179    /// ```ignore
180    /// let cmd = Qail::notify("orders", "new_order:123");
181    /// // Generates: NOTIFY orders, 'new_order:123'
182    /// ```
183    pub fn notify(channel: impl Into<String>, payload: impl Into<String>) -> Self {
184        Self {
185            action: Action::Notify,
186            channel: Some(channel.into()),
187            payload: Some(payload.into()),
188            ..Default::default()
189        }
190    }
191
192    // PostgreSQL Procedural Commands
193
194    /// Create a CALL command to invoke a stored procedure.
195    ///
196    /// # Example
197    /// ```ignore
198    /// let cmd = Qail::call("refresh_materialized_views()");
199    /// // Generates: CALL refresh_materialized_views()
200    /// ```
201    pub fn call(procedure: impl Into<String>) -> Self {
202        Self {
203            action: Action::Call,
204            table: procedure.into(),
205            ..Default::default()
206        }
207    }
208
209    /// Create a DO command to execute an anonymous code block.
210    ///
211    /// # Example
212    /// ```ignore
213    /// let cmd = Qail::do_block("BEGIN RAISE NOTICE 'hello'; END;", "plpgsql");
214    /// // Generates: DO $$ BEGIN RAISE NOTICE 'hello'; END; $$ LANGUAGE plpgsql
215    /// ```
216    pub fn do_block(body: impl Into<String>, language: impl Into<String>) -> Self {
217        Self {
218            action: Action::Do,
219            payload: Some(body.into()),
220            table: language.into(),
221            ..Default::default()
222        }
223    }
224
225    // PostgreSQL Session Commands
226
227    /// Create a SET command for session variables.
228    ///
229    /// # Example
230    /// ```ignore
231    /// let cmd = Qail::session_set("statement_timeout", "5000");
232    /// // Generates: SET statement_timeout = '5000'
233    /// ```
234    pub fn session_set(key: impl Into<String>, value: impl Into<String>) -> Self {
235        Self {
236            action: Action::SessionSet,
237            table: key.into(),
238            payload: Some(value.into()),
239            ..Default::default()
240        }
241    }
242
243    /// Create a SHOW command to inspect a session variable.
244    ///
245    /// # Example
246    /// ```ignore
247    /// let cmd = Qail::session_show("statement_timeout");
248    /// // Generates: SHOW statement_timeout
249    /// ```
250    pub fn session_show(key: impl Into<String>) -> Self {
251        Self {
252            action: Action::SessionShow,
253            table: key.into(),
254            ..Default::default()
255        }
256    }
257
258    /// Create a RESET command to restore a session variable to default.
259    ///
260    /// # Example
261    /// ```ignore
262    /// let cmd = Qail::session_reset("statement_timeout");
263    /// // Generates: RESET statement_timeout
264    /// ```
265    pub fn session_reset(key: impl Into<String>) -> Self {
266        Self {
267            action: Action::SessionReset,
268            table: key.into(),
269            ..Default::default()
270        }
271    }
272
273    /// Create a CREATE DATABASE command.
274    pub fn create_database(name: impl Into<String>) -> Self {
275        Self {
276            action: Action::CreateDatabase,
277            table: name.into(),
278            ..Default::default()
279        }
280    }
281
282    /// Create a DROP DATABASE command.
283    pub fn drop_database(name: impl Into<String>) -> Self {
284        Self {
285            action: Action::DropDatabase,
286            table: name.into(),
287            ..Default::default()
288        }
289    }
290}