Skip to content

Commit 7508dd5

Browse files
committed
fix: different approach for the cfg_if
1 parent 3fd130f commit 7508dd5

File tree

1 file changed

+20
-26
lines changed
  • canyon_macros/src/query_operations

1 file changed

+20
-26
lines changed

canyon_macros/src/query_operations/read.rs

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ use crate::utils::macro_tokens::MacroTokens;
22
use proc_macro2::{Ident, TokenStream};
33
use quote::{ToTokens, quote};
44

5-
#[cfg(feature = "mssql")]
6-
const MSSQL_ENABLED: bool = true;
7-
85
/// Facade function that acts as the unique API for export to the real macro implementation
96
/// of all the generated macros for the READ operations
107
pub fn generate_read_operations_tokens(
@@ -169,22 +166,11 @@ mod __details {
169166
}
170167

171168
pub mod count_generators {
172-
use crate::query_operations::read::MSSQL_ENABLED;
173-
174169
use super::*;
175170
use proc_macro2::TokenStream;
176171

177172
pub fn create_count_macro(stmt: &str) -> TokenStream {
178-
let mssql_arm = if MSSQL_ENABLED {
179-
quote! {
180-
canyon_sql::connection::DatabaseType::SqlServer => {
181-
let count_i32: i32 = default_db_conn.query_one_for::<i32>(#stmt, &[]).await?;
182-
Ok(count_i32 as i64)
183-
}
184-
}
185-
} else {
186-
quote! {} // MSSQL disabled → no branch emitted
187-
};
173+
let mssql_arm = get_mssql_arm_tokens_if_enabled(stmt, false);
188174

189175
quote! {
190176
async fn count() -> Result<i64, Box<dyn std::error::Error + Send + Sync>> {
@@ -201,17 +187,7 @@ mod __details {
201187
}
202188

203189
pub fn create_count_with_macro(stmt: &str) -> TokenStream {
204-
let mssql_arm = if MSSQL_ENABLED {
205-
quote! {
206-
canyon_sql::connection::DatabaseType::SqlServer => {
207-
// SQL Server COUNT(*) returns i32, convert to i64
208-
let count_i32: i32 = input.query_one_for::<i32>(#stmt, &[]).await?;
209-
Ok(count_i32 as i64)
210-
}
211-
}
212-
} else {
213-
quote! {} // No MSSQL support compiled → emit nothing
214-
};
190+
let mssql_arm = get_mssql_arm_tokens_if_enabled(stmt, true);
215191

216192
quote! {
217193
async fn count_with<'a, I>(input: I)
@@ -230,6 +206,24 @@ mod __details {
230206
}
231207
}
232208
}
209+
210+
fn get_mssql_arm_tokens_if_enabled(stmt: &str, is_with_input: bool) -> TokenStream {
211+
let db_conn = if is_with_input {
212+
quote! {input}
213+
} else {
214+
quote! {default_db_conn}
215+
};
216+
if cfg!(feature = "mssql") {
217+
quote! {
218+
canyon_sql::connection::DatabaseType::SqlServer => {
219+
let count_i32: i32 = #db_conn.query_one_for::<i32>(#stmt, &[]).await?;
220+
Ok(count_i32 as i64)
221+
}
222+
}
223+
} else {
224+
quote! {} // nothing emitted
225+
}
226+
}
233227
}
234228

235229
pub mod find_by_pk_generators {

0 commit comments

Comments
 (0)