use crate::conn::handshake::ServerFacts;
use crate::output::{Envelope, Row};
use serde_json::{json, Value};
const SINCE: [(&str, u16, u16, &str); 4] = [
(
"system_versioning",
10,
3,
"tables can keep a version history",
),
(
"sequences",
10,
3,
"number generators exist as objects of their own",
),
(
"roles",
10,
0,
"rights can be granted to a role rather than a user",
),
(
"checked_documents",
10,
4,
"structured documents are checked rather than stored as plain text",
),
];
#[must_use]
pub fn run(facts: &ServerFacts) -> Envelope {
Envelope::new(
"server capabilities",
json!({
"variant": variant(facts),
"version": facts.version,
"version_parts": [
facts.version_parts.0,
facts.version_parts.1,
facts.version_parts.2,
],
"meets_minimum_version": facts.meets_minimum_version,
"rows": features(facts),
}),
)
}
#[must_use]
pub fn variant(facts: &ServerFacts) -> &'static str {
if facts.is_mariadb {
"mariadb"
} else {
"other"
}
}
#[must_use]
pub fn features(facts: &ServerFacts) -> Vec<Row> {
SINCE
.iter()
.map(|(name, major, minor, about)| {
let present = facts.is_mariadb && at_least(facts.version_parts, *major, *minor);
let mut row = Row::new();
row.insert("feature".to_owned(), json!(name));
row.insert("present".to_owned(), json!(present));
row.insert("about".to_owned(), json!(about));
row.insert("since".to_owned(), json!(format!("{major}.{minor}")));
row.insert(
"absent_because".to_owned(),
reason(facts.is_mariadb, present, *major, *minor),
);
row
})
.collect()
}
fn reason(is_mariadb: bool, present: bool, major: u16, minor: u16) -> Value {
if present {
return Value::Null;
}
if is_mariadb {
json!(format!(
"this feature needs release {major}.{minor} or newer"
))
} else {
json!("this feature belongs to MariaDB and this destination is another server")
}
}
const fn at_least(parts: (u16, u16, u16), major: u16, minor: u16) -> bool {
parts.0 > major || (parts.0 == major && parts.1 >= minor)
}
#[cfg(test)]
mod tests {
use super::*;
fn facts(version: &str, comment: &str) -> ServerFacts {
ServerFacts::from_probe(
version.to_owned(),
comment.to_owned(),
false,
"STRICT_TRANS_TABLES".to_owned(),
false,
None,
42,
)
}
fn present(rows: &[Row], feature: &str) -> bool {
rows.iter()
.find(|row| row["feature"] == json!(feature))
.is_some_and(|row| row["present"] == json!(true))
}
#[test]
fn every_optional_feature_appears_present_or_absent() {
let rows = features(&facts("10.11.8-MariaDB", "MariaDB Server"));
assert_eq!(rows.len(), SINCE.len());
assert!(rows.iter().all(|row| row.contains_key("present")));
}
#[test]
fn a_feature_the_release_is_too_old_for_says_why() {
let rows = features(&facts("10.3.39-MariaDB", "MariaDB Server"));
assert!(present(&rows, "system_versioning"));
assert!(!present(&rows, "checked_documents"));
let documents = rows
.iter()
.find(|row| row["feature"] == json!("checked_documents"))
.expect("checked_documents is published");
assert!(documents["absent_because"]
.as_str()
.is_some_and(|text| text.contains("10.4")));
}
#[test]
fn another_server_claims_no_mariadb_feature() {
let answer = run(&facts("8.0.36", "MySQL Community Server"));
assert_eq!(answer.data["variant"], json!("other"));
let rows = features(&facts("8.0.36", "MySQL Community Server"));
assert!(rows.iter().all(|row| row["present"] == json!(false)));
}
#[test]
fn a_present_feature_carries_no_reason() {
let rows = features(&facts("11.7.1-MariaDB", "MariaDB Server"));
let documents = rows
.iter()
.find(|row| row["feature"] == json!("checked_documents"))
.expect("checked_documents is published");
assert_eq!(documents["absent_because"], Value::Null);
}
}