Skip to content

Commit 4f19788

Browse files
committed
Reapply "fix: MSSQL count operation with our 'query_one_for::<T>'"
This reverts commit b503bc7.
1 parent b503bc7 commit 4f19788

1 file changed

Lines changed: 28 additions & 2 deletions

File tree

  • canyon_macros/src/query_operations

canyon_macros/src/query_operations/read.rs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,20 @@ mod __details {
173173
quote! {
174174
async fn count() -> Result<i64, Box<(dyn std::error::Error + Send + Sync)>> {
175175
let default_db_conn = canyon_sql::core::Canyon::instance()?.get_default_connection()?;
176-
default_db_conn.query_one_for(#stmt, &[]).await
176+
// Handle different database types for COUNT(*) operations
177+
let db_type = default_db_conn.get_database_type()?;
178+
match db_type {
179+
#[cfg(feature = "mssql")]
180+
canyon_sql::connection::DatabaseType::SqlServer => {
181+
// SQL Server COUNT(*) returns i32, convert to i64
182+
let count_i32: i32 = default_db_conn.query_one_for::<i32>(#stmt, &[]).await?;
183+
Ok(count_i32 as i64)
184+
}
185+
_ => {
186+
// PostgreSQL and MySQL COUNT(*) return i64
187+
default_db_conn.query_one_for::<i64>(#stmt, &[]).await
188+
}
189+
}
177190
}
178191
}
179192
}
@@ -183,7 +196,20 @@ mod __details {
183196
async fn count_with<'a, I>(input: I) -> Result<i64, Box<(dyn std::error::Error + Send + Sync + 'a)>>
184197
where I: canyon_sql::connection::DbConnection + Send + 'a
185198
{
186-
Ok(input.query_one_for::<i64>(#stmt, &[]).await? as i64)
199+
// Handle different database types for COUNT(*) operations
200+
let db_type = input.get_database_type()?;
201+
match db_type {
202+
#[cfg(feature = "mssql")]
203+
canyon_sql::connection::DatabaseType::SqlServer => {
204+
// SQL Server COUNT(*) returns i32, convert to i64
205+
let count_i32: i32 = input.query_one_for::<i32>(#stmt, &[]).await?;
206+
Ok(count_i32 as i64)
207+
}
208+
_ => {
209+
// PostgreSQL and MySQL COUNT(*) return i64
210+
input.query_one_for::<i64>(#stmt, &[]).await
211+
}
212+
}
187213
}
188214
}
189215
}

0 commit comments

Comments
 (0)