Skip to main content

easy_sql_macros/
lib.rs

1mod macros;
2mod macros_components;
3
4mod derive;
5mod derive_components;
6
7use ::{proc_macro2, quote::quote};
8use easy_macros::{always_context, anyhow_result, find_crate_list};
9use proc_macro::TokenStream;
10
11/// Prefix for aliases generated by #[sql(select = ...)] to avoid conflicts with table columns
12const CUSTOM_SELECT_ALIAS_PREFIX: &str = "__easy_sql_custom_select_";
13
14fn sql_crate() -> proc_macro2::TokenStream {
15    if let Some(found) = find_crate_list(&[("easy-sql", quote! {})]) {
16        found
17    } else {
18        quote! {self}
19    }
20}
21
22#[proc_macro]
23#[always_context]
24#[anyhow_result]
25pub fn query(item: TokenStream) -> anyhow::Result<TokenStream> {
26    macros::query(item)
27}
28
29#[always_context]
30/// Debug version of `query!` that prints the generated code and panics.
31/// Useful for inspecting macro expansion during development.
32#[proc_macro]
33#[anyhow_result]
34#[no_context]
35pub fn query_debug(item: TokenStream) -> anyhow::Result<TokenStream> {
36    let result = macros::query(item)?;
37    panic!("{}", result);
38}
39
40#[proc_macro]
41#[always_context]
42#[anyhow_result]
43pub fn query_lazy(item: TokenStream) -> anyhow::Result<TokenStream> {
44    macros::query_lazy(item)
45}
46
47#[always_context]
48/// Debug version of `query_lazy!` that prints the generated code and panics.
49/// Useful for inspecting macro expansion during development.
50#[proc_macro]
51#[anyhow_result]
52#[no_context]
53pub fn query_lazy_debug(item: TokenStream) -> anyhow::Result<TokenStream> {
54    let result = macros::query_lazy(item)?;
55    panic!("{}", result);
56}
57
58#[always_context]
59#[proc_macro]
60#[anyhow_result]
61pub fn table_join(item: TokenStream) -> anyhow::Result<TokenStream> {
62    macros::table_join(item)
63}
64
65#[always_context]
66#[proc_macro]
67#[anyhow_result]
68#[no_context]
69pub fn table_join_debug(item: TokenStream) -> anyhow::Result<TokenStream> {
70    let result = macros::table_join(item)?;
71    panic!("{}", result);
72}
73
74#[always_context]
75#[proc_macro_derive(DatabaseSetup, attributes(sql))]
76#[anyhow_result]
77pub fn database_setup(item: TokenStream) -> anyhow::Result<TokenStream> {
78    derive::database_setup(item)
79}
80
81#[always_context]
82#[proc_macro_derive(Output, attributes(sql))]
83#[anyhow_result]
84pub fn output(item: TokenStream) -> anyhow::Result<TokenStream> {
85    derive::output(item)
86}
87
88#[always_context]
89#[proc_macro_derive(OutputDebug, attributes(sql))]
90#[anyhow_result]
91#[no_context]
92pub fn output_debug(item: TokenStream) -> anyhow::Result<TokenStream> {
93    let output = derive::output(item)?;
94
95    panic!("{}", output);
96}
97
98#[always_context]
99#[proc_macro_derive(Insert, attributes(sql))]
100#[anyhow_result]
101pub fn insert(item: TokenStream) -> anyhow::Result<TokenStream> {
102    derive::insert(item)
103}
104
105#[always_context]
106#[proc_macro_derive(InsertDebug, attributes(sql))]
107#[anyhow_result]
108#[no_context]
109pub fn insert_debug(item: TokenStream) -> anyhow::Result<TokenStream> {
110    let output = derive::insert(item)?;
111
112    panic!("{}", output);
113}
114
115#[always_context]
116#[proc_macro_derive(Update, attributes(sql))]
117#[anyhow_result]
118pub fn sql_update(item: TokenStream) -> anyhow::Result<TokenStream> {
119    derive::update(item)
120}
121
122#[always_context]
123#[proc_macro_derive(UpdateDebug, attributes(sql))]
124#[anyhow_result]
125#[no_context]
126pub fn sql_update_debug(item: TokenStream) -> anyhow::Result<TokenStream> {
127    let output = derive::update(item)?;
128
129    panic!("{}", output);
130}
131
132#[always_context]
133#[proc_macro_derive(Table, attributes(sql))]
134#[anyhow_result]
135pub fn table(item: TokenStream) -> anyhow::Result<TokenStream> {
136    derive::table(item)
137}
138
139#[always_context]
140#[proc_macro_derive(TableDebug, attributes(sql))]
141#[anyhow_result]
142#[no_context]
143pub fn table_debug(item: TokenStream) -> anyhow::Result<TokenStream> {
144    let output = derive::table(item)?;
145
146    panic!("{}", output);
147}
148
149#[proc_macro]
150#[always_context]
151#[anyhow_result]
152pub fn custom_sql_function(input: TokenStream) -> anyhow::Result<TokenStream> {
153    Ok(macros::custom_sql_function_impl(input))
154}
155
156#[proc_macro]
157#[always_context]
158#[anyhow_result]
159pub fn impl_supports_fn_any(input: TokenStream) -> anyhow::Result<TokenStream> {
160    macros::impl_supports_fn_any(input)
161}
162
163#[proc_macro]
164#[always_context]
165#[anyhow_result]
166pub fn impl_supports_fn(input: TokenStream) -> anyhow::Result<TokenStream> {
167    macros::impl_supports_fn(input)
168}
169
170#[proc_macro]
171#[always_context]
172#[anyhow_result]
173pub fn define_supports_fn_trait(input: TokenStream) -> anyhow::Result<TokenStream> {
174    macros::define_supports_fn_trait(input)
175}
176
177#[proc_macro]
178#[always_context]
179#[anyhow_result]
180pub fn define_supports_operator_trait(input: TokenStream) -> anyhow::Result<TokenStream> {
181    macros::define_supports_operator_trait(input)
182}