use crate::types::sql_hints::{OrderByClause, OrderDirection};
pub(super) fn map_mysql_error_code(code: u16) -> Option<String> {
let sqlstate = match code {
1062 | 1169 => "23505",
1048 => "23502",
1451 | 1452 => "23503",
1205 => "40001",
1213 => "40001",
_ => return None,
};
Some(sqlstate.to_string())
}
pub(super) fn build_mysql_relay_order_sql(
quoted_col: &str,
order_by: Option<&[OrderByClause]>,
forward: bool,
) -> String {
let mut parts: Vec<String> = Vec::new();
if let Some(clauses) = order_by {
for c in clauses {
let dir = match (c.direction, forward) {
(OrderDirection::Asc, true) | (OrderDirection::Desc, false) => "ASC",
(OrderDirection::Desc, true) | (OrderDirection::Asc, false) => "DESC",
};
let escaped = c.field.replace('\'', "''");
parts.push(format!("JSON_UNQUOTE(JSON_EXTRACT(data, '$.{escaped}')) {dir}"));
}
}
let cursor_dir = if forward { "ASC" } else { "DESC" };
parts.push(format!("{quoted_col} {cursor_dir}"));
format!(" ORDER BY {}", parts.join(", "))
}
pub(super) fn build_mysql_relay_where(cursor_sql: Option<&str>, user_sql: Option<&str>) -> String {
match (cursor_sql, user_sql) {
(None, None) => String::new(),
(Some(c), None) => format!(" WHERE {c}"),
(None, Some(u)) => format!(" WHERE ({u})"),
(Some(c), Some(u)) => format!(" WHERE {c} AND ({u})"),
}
}