#[cfg(feature = "sqlite")]
use smol_bytes::Utf8BytesMut;
#[cfg(any(feature = "sqlite", feature = "postgres", feature = "mysql"))]
use crate::FieldName;
#[test]
fn the_like_pattern_escapes_the_metacharacters_and_wraps_the_affix() {
use super::like_pattern;
use crate::comparison::TextMatch;
assert_eq!(like_pattern("cam", TextMatch::Contains), "%cam%");
assert_eq!(like_pattern("cam", TextMatch::Prefix), "cam%");
assert_eq!(like_pattern("cam", TextMatch::Suffix), "%cam");
assert_eq!(like_pattern("50%", TextMatch::Contains), "%50\\%%");
assert_eq!(like_pattern("a_b", TextMatch::Prefix), "a\\_b%");
assert_eq!(like_pattern("c:\\x", TextMatch::Suffix), "%c:\\\\x");
}
#[cfg(feature = "sqlite")]
#[test]
fn sqlite_brackets_with_double_quotes_and_doubles_one_inside() {
use super::quote_ident;
assert_eq!(quote_ident::<sqlx::Sqlite>(FieldName::new("n")), "\"n\"");
assert_eq!(
quote_ident::<sqlx::Sqlite>(FieldName::new("a\"b")),
"\"a\"\"b\""
);
assert_eq!(
quote_ident::<sqlx::Sqlite>(FieldName::new("a`b")),
"\"a`b\""
);
assert_eq!(
quote_ident::<sqlx::Sqlite>(FieldName::new("n\" = 1 OR \"x")),
"\"n\"\" = 1 OR \"\"x\""
);
assert_eq!(
quote_ident::<sqlx::Sqlite>(FieldName::new("t.col")),
"\"t.col\""
);
}
#[cfg(feature = "sqlite")]
#[test]
fn a_column_path_runs_its_segments_together_inside_one_pair_of_brackets() {
use super::quote_column;
use crate::{At, RecordAddress};
const ROW: RecordAddress = RecordAddress::new(FieldName::new("t"), &[FieldName::new("id")]);
let at = At::new(&ROW);
let timebase = at.frame(FieldName::new("timebase"));
let num = timebase.at().frame(FieldName::new("num"));
let mut rendered = Utf8BytesMut::new();
quote_column::<sqlx::Sqlite>(timebase.at(), &mut rendered);
assert_eq!(rendered, "\"timebase\"");
let mut rendered = Utf8BytesMut::new();
quote_column::<sqlx::Sqlite>(num.at(), &mut rendered);
assert_eq!(rendered, "\"timebase_num\"");
let quoted = at.frame(FieldName::new("time\"base"));
let part = quoted.at().frame(FieldName::new("num"));
let mut rendered = Utf8BytesMut::new();
quote_column::<sqlx::Sqlite>(part.at(), &mut rendered);
assert_eq!(rendered, "\"time\"\"base_num\"");
}
#[cfg(feature = "sqlite")]
#[test]
fn the_top_level_names_no_column_so_nothing_is_written() {
use super::quote_column;
use crate::{At, RecordAddress};
const ROW: RecordAddress = RecordAddress::new(FieldName::new("t"), &[FieldName::new("id")]);
let mut rendered = Utf8BytesMut::from("WHERE ");
quote_column::<sqlx::Sqlite>(At::new(&ROW), &mut rendered);
assert_eq!(rendered, "WHERE ");
}
#[cfg(feature = "sqlite")]
#[test]
fn a_qualified_reference_brackets_each_half_and_joins_them_with_the_dot() {
use super::{quote_ident, quote_qualified};
assert_eq!(
quote_qualified::<sqlx::Sqlite>(FieldName::new("t"), FieldName::new("col")),
"\"t\".\"col\""
);
assert_eq!(
quote_qualified::<sqlx::Sqlite>(FieldName::new("a\"b"), FieldName::new("c\"d")),
"\"a\"\"b\".\"c\"\"d\""
);
assert_ne!(
quote_qualified::<sqlx::Sqlite>(FieldName::new("t"), FieldName::new("col")),
quote_ident::<sqlx::Sqlite>(FieldName::new("t.col"))
);
}
#[cfg(feature = "mysql")]
#[test]
fn a_qualified_reference_takes_the_dialects_own_bracket() {
use super::quote_qualified;
assert_eq!(
quote_qualified::<sqlx::MySql>(FieldName::new("t"), FieldName::new("col")),
"`t`.`col`"
);
}
#[cfg(feature = "postgres")]
#[test]
fn postgres_brackets_with_double_quotes_and_doubles_one_inside() {
use super::quote_ident;
assert_eq!(quote_ident::<sqlx::Postgres>(FieldName::new("n")), "\"n\"");
assert_eq!(
quote_ident::<sqlx::Postgres>(FieldName::new("a\"b")),
"\"a\"\"b\""
);
assert_eq!(
quote_ident::<sqlx::Postgres>(FieldName::new("a`b")),
"\"a`b\""
);
}
#[cfg(feature = "mysql")]
#[test]
fn mysql_brackets_with_backticks_and_doubles_one_inside() {
use super::quote_ident;
assert_eq!(quote_ident::<sqlx::MySql>(FieldName::new("n")), "`n`");
assert_eq!(quote_ident::<sqlx::MySql>(FieldName::new("a`b")), "`a``b`");
assert_eq!(quote_ident::<sqlx::MySql>(FieldName::new("a\"b")), "`a\"b`");
}