use crate::adapter::DatabaseAdapter;
use crate::adapter::MysqlAdapter;
use crate::adapter::mysql::query_builder::SqlQueryBuilder;
use crate::error::{QuickDbError, QuickDbResult};
use crate::pool::DatabaseConnection;
use crate::types::*;
use rat_logger::debug;
pub(crate) async fn delete(
adapter: &MysqlAdapter,
connection: &DatabaseConnection,
table: &str,
conditions: &[QueryConditionWithConfig],
alias: &str,
) -> QuickDbResult<u64> {
if let DatabaseConnection::MySQL(pool) = connection {
let (sql, params) = SqlQueryBuilder::new()
.delete()
.where_conditions(conditions)
.build(table, alias)?;
adapter.execute_update(pool, &sql, ¶ms, table).await
} else {
Err(QuickDbError::ConnectionError {
message: "连接类型不匹配,期望MySQL连接".to_string(),
})
}
}
pub(crate) async fn delete_by_id(
adapter: &MysqlAdapter,
connection: &DatabaseConnection,
table: &str,
id: &DataValue,
alias: &str,
) -> QuickDbResult<bool> {
if let DatabaseConnection::MySQL(pool) = connection {
let condition = QueryConditionWithConfig {
field: "id".to_string(),
operator: QueryOperator::Eq,
value: id.clone(),
case_insensitive: false,
};
let (sql, params) = SqlQueryBuilder::new()
.delete()
.where_condition(condition)
.build(table, alias)?;
let affected_rows = adapter.execute_update(pool, &sql, ¶ms, table).await?;
Ok(affected_rows > 0)
} else {
Err(QuickDbError::ConnectionError {
message: "连接类型不匹配,期望MySQL连接".to_string(),
})
}
}
pub(crate) async fn count(
adapter: &MysqlAdapter,
connection: &DatabaseConnection,
table: &str,
conditions: &[QueryConditionWithConfig],
alias: &str,
) -> QuickDbResult<u64> {
if let DatabaseConnection::MySQL(pool) = connection {
let (sql, params) = SqlQueryBuilder::new()
.select(&["COUNT(*) as count"])
.where_conditions(conditions)
.build(table, alias)?;
let results = adapter.execute_query(pool, &sql, ¶ms, table).await?;
if let Some(result) = results.first() {
if let DataValue::Object(map) = result {
if let Some(DataValue::Int(count)) = map.get("count") {
return Ok(*count as u64);
}
}
}
Ok(0)
} else {
Err(QuickDbError::ConnectionError {
message: "连接类型不匹配,期望MySQL连接".to_string(),
})
}
}
pub(crate) async fn count_with_groups(
adapter: &MysqlAdapter,
connection: &DatabaseConnection,
table: &str,
condition_groups: &[QueryConditionGroupWithConfig],
alias: &str,
) -> QuickDbResult<u64> {
if let DatabaseConnection::MySQL(pool) = connection {
let (sql, params) = SqlQueryBuilder::new()
.select(&["COUNT(*) as count"])
.where_condition_groups(condition_groups)
.build(table, alias)?;
debug!("执行MySQL条件组合计数: {}", sql);
let results = adapter.execute_query(pool, &sql, ¶ms, table).await?;
if let Some(result) = results.first() {
if let DataValue::Object(map) = result {
if let Some(DataValue::Int(count)) = map.get("count") {
return Ok(*count as u64);
}
}
}
Ok(0)
} else {
Err(QuickDbError::ConnectionError {
message: "连接类型不匹配,期望MySQL连接".to_string(),
})
}
}