Skip to main content

easy_sql_macros/
lib.rs

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