Skip to main content

ignis_db/builders/
macros.rs

1//! Convenience macros for database object builders
2//!
3//! This module provides macros that make it even easier to create database objects
4//! using the builder pattern, with a more concise syntax.
5
6/// Macro to create a function with a schema-qualified name
7///
8/// # Examples
9///
10/// ```rust
11/// use ignis_db::builders::*;
12///
13/// let func = build_function!("auth.get_user_by_id")
14///     .parameter("user_id", "uuid")
15///     .returns("jsonb")
16///     .language(ProcedureLanguage::PlPgSql)
17///     .body("SELECT to_jsonb(u.*) FROM auth.users u WHERE u.id = user_id");
18/// ```
19#[macro_export]
20macro_rules! build_function {
21    ($name:expr) => {
22        $crate::builders::function().name($name)
23    };
24    ($name:expr, $($args:tt)*) => {
25        $crate::builders::function().name($name) $(.$args)*
26    };
27}
28
29/// Macro to create a procedure with a schema-qualified name
30///
31/// # Examples
32///
33/// ```rust
34/// use ignis_db::builders::*;
35///
36/// let proc = build_procedure!("auth.create_user")
37///     .parameter("email", "text")
38///     .parameter("name", "text")
39///     .language(ProcedureLanguage::PlPgSql)
40///     .body("INSERT INTO auth.users (email, name) VALUES (email, name)");
41/// ```
42#[macro_export]
43macro_rules! build_procedure {
44    ($name:expr) => {
45        $crate::builders::procedure().name($name)
46    };
47    ($name:expr, $($args:tt)*) => {
48        $crate::builders::procedure().name($name) $(.$args)*
49    };
50}
51
52/// Macro to create a view with a schema-qualified name
53///
54/// # Examples
55///
56/// ```rust
57/// use ignis_db::builders::*;
58///
59/// let view = build_view!("auth.active_users")
60///     .query("SELECT id, email, name FROM auth.users WHERE status = 'active'")
61///     .columns(vec!["id", "email", "name"]);
62/// ```
63#[macro_export]
64macro_rules! build_view {
65    ($name:expr) => {
66        $crate::builders::view().name($name)
67    };
68    ($name:expr, $($args:tt)*) => {
69        $crate::builders::view().name($name) $(.$args)*
70    };
71}
72
73/// Macro to create a materialized view with a schema-qualified name
74///
75/// # Examples
76///
77/// ```rust
78/// use ignis_db::builders::*;
79///
80/// let mview = build_materialized_view!("auth.user_stats")
81///     .query("SELECT status, COUNT(*) as count FROM auth.users GROUP BY status")
82///     .with_data();
83/// ```
84#[macro_export]
85macro_rules! build_materialized_view {
86    ($name:expr) => {
87        $crate::builders::materialized_view().name($name)
88    };
89    ($name:expr, $($args:tt)*) => {
90        $crate::builders::materialized_view().name($name) $(.$args)*
91    };
92}
93
94/// Macro to create a table with a schema-qualified name
95///
96/// # Examples
97///
98/// ```rust
99/// use ignis_db::builders::*;
100///
101/// let table = build_table!("auth.users")
102///     .column(column("id", ColumnType::Uuid).primary_key())
103///     .column(column("email", ColumnType::Text).not_null().unique());
104/// ```
105#[macro_export]
106macro_rules! build_table {
107    ($name:expr) => {
108        $crate::builders::table($name)
109    };
110    ($name:expr, $($args:tt)*) => {
111        $crate::builders::table($name) $(.$args)*
112    };
113}
114
115/// Macro to create a schema
116///
117/// # Examples
118///
119/// ```rust
120/// use ignis_db::builders::*;
121///
122/// let schema = build_schema!("auth")
123///     .comment("Authentication schema");
124/// ```
125#[macro_export]
126macro_rules! build_schema {
127    ($name:expr) => {
128        $crate::builders::schema($name)
129    };
130    ($name:expr, $($args:tt)*) => {
131        $crate::builders::schema($name) $(.$args)*
132    };
133}
134
135/// Macro to create a column
136///
137/// # Examples
138///
139/// ```rust
140/// use ignis_db::builders::*;
141///
142/// let col = build_column!("id", ColumnType::Uuid)
143///     .primary_key()
144///     .not_null();
145/// ```
146#[macro_export]
147macro_rules! build_column {
148    ($name:expr, $type:expr) => {
149        $crate::builders::column($name, $type)
150    };
151    ($name:expr, $type:expr, $($args:tt)*) => {
152        $crate::builders::column($name, $type) $(.$args)*
153    };
154}
155
156/// Macro to create an index
157///
158/// # Examples
159///
160/// ```rust
161/// use ignis_db::builders::*;
162///
163/// let idx = build_index!("idx_user_email")
164///     .on_table("auth.users")
165///     .column("email")
166///     .unique();
167/// ```
168#[macro_export]
169macro_rules! build_index {
170    ($name:expr) => {
171        $crate::builders::IndexBuilder::new($name)
172    };
173    ($name:expr, $($args:tt)*) => {
174        $crate::builders::IndexBuilder::new($name) $(.$args)*
175    };
176}
177
178/// Macro to create a constraint
179///
180/// # Examples
181///
182/// ```rust
183/// use ignis_db::builders::*;
184///
185/// let constraint = build_constraint!("pk_users")
186///     .primary_key()
187///     .column("id");
188/// ```
189#[macro_export]
190macro_rules! build_constraint {
191    ($name:expr) => {
192        $crate::builders::ConstraintBuilder::new($name)
193    };
194    ($name:expr, $($args:tt)*) => {
195        $crate::builders::ConstraintBuilder::new($name) $(.$args)*
196    };
197}
198
199/// Macro to create a policy
200///
201/// # Examples
202///
203/// ```rust
204/// use ignis_db::builders::*;
205///
206/// let policy = build_policy!("users_select_policy")
207///     .on_table("auth.users")
208///     .for_select()
209///     .to_role("authenticated")
210///     .using("tenant_id = current_setting('app.tenant_id')::uuid");
211/// ```
212#[macro_export]
213macro_rules! build_policy {
214    ($name:expr) => {
215        $crate::builders::policy().name($name)
216    };
217    ($name:expr, $($args:tt)*) => {
218        $crate::builders::policy().name($name) $(.$args)*
219    };
220}
221
222/// Macro to create an extension
223///
224/// # Examples
225///
226/// ```rust
227/// use ignis_db::builders::*;
228///
229/// let ext = build_extension!("pgcrypto")
230///     .if_not_exists();
231/// ```
232#[macro_export]
233macro_rules! build_extension {
234    ($name:expr) => {
235        $crate::builders::extension().name($name)
236    };
237    ($name:expr, $($args:tt)*) => {
238        $crate::builders::extension().name($name) $(.$args)*
239    };
240}
241
242/// Macro to create a role
243///
244/// # Examples
245///
246/// ```rust
247/// use ignis_db::builders::*;
248///
249/// let role = build_role!("authenticated")
250///     .login()
251///     .password("secure_password");
252/// ```
253#[macro_export]
254macro_rules! build_role {
255    ($name:expr) => {
256        $crate::builders::role().name($name)
257    };
258    ($name:expr, $($args:tt)*) => {
259        $crate::builders::role().name($name) $(.$args)*
260    };
261}
262
263/// Macro to create a GUC setting
264///
265/// # Examples
266///
267/// ```rust
268/// use ignis_db::builders::*;
269///
270/// let guc = build_guc!("app.tenant_id")
271///     .value("uuid")
272///     .description("Current tenant ID");
273/// ```
274#[macro_export]
275macro_rules! build_guc {
276    ($name:expr) => {
277        $crate::builders::guc().name($name)
278    };
279    ($name:expr, $($args:tt)*) => {
280        $crate::builders::guc().name($name) $(.$args)*
281    };
282}
283
284/// Macro to create an information schema query
285///
286/// # Examples
287///
288/// ```rust
289/// use ignis_db::builders::*;
290///
291/// let query = info_schema!("tables")
292///     .where_equals("table_schema", "auth")
293///     .order_by("table_name");
294/// ```
295#[macro_export]
296macro_rules! info_schema {
297    ($table:expr) => {
298        $crate::builders::information_schema().table($crate::builders::InformationSchemaTable::$table)
299    };
300    ($table:expr, $($args:tt)*) => {
301        $crate::builders::information_schema().table($crate::builders::InformationSchemaTable::$table) $(.$args)*
302    };
303}
304
305#[cfg(test)]
306mod tests {
307    use super::*;
308    use crate::schema::{ColumnType, ProcedureLanguage};
309
310    #[test]
311    fn test_function_macro() {
312        let func = build_function!("auth.get_user")
313            .parameter("id", "uuid")
314            .returns("jsonb")
315            .language(ProcedureLanguage::PlPgSql);
316
317        assert_eq!(func.get_name(), Some("get_user"));
318        assert_eq!(func.get_schema(), Some("auth"));
319    }
320
321    #[test]
322    fn test_view_macro() {
323        let view = build_view!("auth.active_users")
324            .query("SELECT * FROM users WHERE active = true")
325            .columns(vec!["id", "email"]);
326
327        assert_eq!(view.get_name(), Some("active_users"));
328        assert_eq!(view.get_schema(), Some("auth"));
329    }
330
331    #[test]
332    fn test_table_macro() {
333        // Test that the macro compiles without errors
334        let _table = build_table!("auth.users");
335
336        // Note: This is a basic compilation test
337        // More comprehensive testing would require the actual TableBuilder API
338    }
339}