use crate::types::map::{family_of, Family};
use mysql_async::Value as Wire;
#[must_use]
pub fn literal(value: &Wire, column: &mysql_async::Column) -> String {
match value {
Wire::NULL => "NULL".to_owned(),
Wire::Int(inner) => inner.to_string(),
Wire::UInt(inner) => inner.to_string(),
Wire::Float(inner) => inner.to_string(),
Wire::Double(inner) => inner.to_string(),
Wire::Bytes(inner) => {
if holds_raw_bytes(column) {
bytes_literal(inner)
} else {
quoted(&value.as_sql(true))
}
}
Wire::Date(..) | Wire::Time(..) => quoted(&value.as_sql(true)),
}
}
fn holds_raw_bytes(column: &mysql_async::Column) -> bool {
matches!(
family_of(column),
Family::Binary | Family::Geometry | Family::Bits
)
}
fn bytes_literal(bytes: &[u8]) -> String {
if bytes.is_empty() {
return "''".to_owned();
}
let mut literal = String::with_capacity(2 + bytes.len() * 2);
literal.push_str("0x");
literal.push_str(&hex::encode(bytes));
literal
}
fn quoted(rendered: &str) -> String {
if rendered.starts_with('\'') {
return rendered.to_owned();
}
format!("'{rendered}'")
}
#[cfg(test)]
mod tests {
use super::*;
fn column_of(kind: mysql_async::consts::ColumnType) -> mysql_async::Column {
mysql_async::Column::new(kind)
}
#[test]
fn absence_of_a_value_is_told_apart_from_empty_content() {
let text = column_of(mysql_async::consts::ColumnType::MYSQL_TYPE_VAR_STRING);
assert_eq!(literal(&Wire::NULL, &text), "NULL");
assert_eq!(literal(&Wire::Bytes(Vec::new()), &text), "''");
}
#[test]
fn byte_content_travels_in_a_form_no_character_set_can_change() {
let raw = column_of(mysql_async::consts::ColumnType::MYSQL_TYPE_GEOMETRY);
let rendered = literal(&Wire::Bytes(b"O'Brien\\".to_vec()), &raw);
assert!(rendered.starts_with("0x"));
assert!(
!rendered.contains('\''),
"nothing to escape and nothing to break"
);
assert_eq!(rendered, format!("0x{}", hex::encode(b"O'Brien\\")));
}
#[test]
fn every_declaration_that_holds_bytes_leaves_in_the_restorable_form() {
use mysql_async::consts::ColumnType;
let content = b"\x00\x01\xff'\\".to_vec();
for kind in [
ColumnType::MYSQL_TYPE_VECTOR,
ColumnType::MYSQL_TYPE_GEOMETRY,
ColumnType::MYSQL_TYPE_BIT,
] {
let rendered = literal(&Wire::Bytes(content.clone()), &column_of(kind));
assert_eq!(
rendered,
format!("0x{}", hex::encode(&content)),
"a {kind:?} column left a copy in a form the server reads back as zero"
);
}
}
#[test]
fn a_number_that_arrived_as_digits_is_not_written_as_bytes() {
let amount = column_of(mysql_async::consts::ColumnType::MYSQL_TYPE_NEWDECIMAL);
let rendered = literal(&Wire::Bytes(b"120.50".to_vec()), &amount);
assert!(
!rendered.starts_with("0x"),
"a number must never travel as bytes"
);
assert!(
rendered.contains("120.50"),
"the amount itself has to be in there"
);
let whole = column_of(mysql_async::consts::ColumnType::MYSQL_TYPE_LONG);
let one = literal(&Wire::Bytes(b"1".to_vec()), &whole);
assert!(
!one.starts_with("0x"),
"`0x31` is the number 49, not the digit 1"
);
}
#[test]
fn a_backslash_is_written_untouched_and_the_copy_declares_the_mode_that_reads_it() {
let text = column_of(mysql_async::consts::ColumnType::MYSQL_TYPE_VAR_STRING);
let rendered = literal(&Wire::Bytes(br"C:\novo".to_vec()), &text);
assert_eq!(
rendered, r"'C:\novo'",
"the backslash is written as it stood, not doubled and not escaped"
);
let trailing = literal(&Wire::Bytes(br"ends with\".to_vec()), &text);
assert_eq!(
trailing, r"'ends with\'",
"a trailing backslash sits right against the closing quote"
);
let opening = crate::datamove::export::header();
assert!(
opening.contains("NO_BACKSLASH_ESCAPES"),
"the copy renders backslashes raw and must ask for the mode that \
reads them raw; the header says: {opening}"
);
}
#[test]
fn a_character_is_written_quoted_and_survives_its_own_accents() {
let text = column_of(mysql_async::consts::ColumnType::MYSQL_TYPE_VAR_STRING);
let rendered = literal(&Wire::Bytes("acentuação".as_bytes().to_vec()), &text);
assert!(!rendered.starts_with("0x"));
assert!(rendered.contains("acentuação"));
}
#[test]
fn numbers_are_written_without_quotes() {
let whole = column_of(mysql_async::consts::ColumnType::MYSQL_TYPE_LONG);
assert_eq!(literal(&Wire::Int(-7), &whole), "-7");
assert_eq!(
literal(&Wire::UInt(18_446_744_073_709_551_615), &whole),
"18446744073709551615"
);
}
#[test]
fn a_moment_in_time_is_written_quoted_exactly_once() {
let moment = column_of(mysql_async::consts::ColumnType::MYSQL_TYPE_DATETIME);
let rendered = literal(&Wire::Date(2026, 8, 7, 12, 30, 0, 0), &moment);
assert!(rendered.starts_with('\''));
assert!(rendered.ends_with('\''));
assert!(!rendered.starts_with("''"));
}
}