use std::collections::HashMap;
use crate::condition::Condition;
pub fn generate_where_condition_str(condition: Option<Condition>) -> String {
if let Some(condition) = condition {
format!("WHERE {}", condition.build())
} else {
String::new()
}
}
pub fn generate_group_by_str(columns: &Option<Vec<String>>) -> String {
match columns {
Some(columns) => format!("GROUP BY {}", columns.join(", ")),
None => String::new(),
}
}
pub fn generate_order_by_str(order_by: &Option<HashMap<Vec<String>, String>>) -> String {
let order_by_str = if let Some(order_by) = order_by.as_ref() {
let order_by_str: Vec<String> = order_by
.iter()
.map(|(columns, order)| format!("{} {}", columns.join(", "), order))
.collect();
if !order_by_str.is_empty() {
format!("ORDER BY {}", order_by_str.join(", "))
} else {
String::new()
}
} else {
String::new()
};
order_by_str
}
pub fn generate_limit_str(limit: Option<usize>) -> String {
limit.map_or(String::new(), |count| format!("LIMIT {}", count))
}
pub fn generate_offset_str(offset: Option<usize>) -> String {
offset.map_or(String::new(), |offset| format!("OFFSET {}", offset))
}
pub fn generate_having_str(group_by: bool, having_condition: Option<&Condition>) -> String {
if group_by && having_condition.is_some() {
format!("HAVING {}", having_condition.unwrap().build())
} else {
String::new()
}
}
pub fn remove_quotes_and_backslashes(input: &str) -> String {
input.replace("\"", "").replace("\\", "")
}
#[cfg(test)]
mod tests {
use super::*;
use crate::condition::{Condition, Value};
#[test]
fn test_generate_where_condition_str() {
let condition = Condition::Eq("age".to_string(), Value::Literal("30".to_string()));
let _result = generate_where_condition_str(Some(condition)); let result = generate_where_condition_str(None);
assert_eq!(result, "");
}
#[test]
fn test_generate_group_by_str() {
let columns = Some(vec!["name".to_string(), "age".to_string()]);
let result = generate_group_by_str(&columns);
assert_eq!(result, "GROUP BY name, age");
let result = generate_group_by_str(&None);
assert_eq!(result, "");
}
#[test]
fn test_generate_limit_str() {
let result = generate_limit_str(Some(10));
assert_eq!(result, "LIMIT 10");
let result = generate_limit_str(None);
assert_eq!(result, "");
}
#[test]
fn test_generate_offset_str() {
let result = generate_offset_str(Some(5));
assert_eq!(result, "OFFSET 5");
let result = generate_offset_str(None);
assert_eq!(result, "");
}
#[test]
fn test_generate_having_str() {
let condition = Condition::Gt("COUNT(age)".to_string(), Value::Literal("5".to_string()));
let result = generate_having_str(true, Some(&condition));
assert_eq!(result, format!("HAVING {}", condition.build()));
let result = generate_having_str(false, Some(&condition));
assert_eq!(result, "");
let result = generate_having_str(true, None);
assert_eq!(result, "");
let result = generate_having_str(false, None);
assert_eq!(result, "");
}
#[test]
fn test_remove_quotes_and_backslashes() {
let input = r#""table_name\"""#;
let result = remove_quotes_and_backslashes(input);
assert_eq!(result, "table_name");
}
}