fv-connect 0.1.0

FusionVault Conduit: read relational, file and REST sources into rows with mandatory access control pushed into the read, write results back with atomic promotion, and drive incremental base-ingest scans, changelogs and label normalisation checked against the published conformance vectors.
Documentation
//! SQL identifier quoting — the ONE home for it (née).
//!
//! This logic previously existed four times with two dialects: `lib.rs quote_pg_table` and
//! `write.rs qident` (identical pg double-quoting), `write.rs my_ident` and an inline copy in
//! `mysql.rs` (identical backtick quoting). Semantics preserved exactly; only the homes merged.
//! Why it matters at all: an unquoted kebab-case table (`curated.delivery-site`) parses `-` as
//! subtraction and the read fails.

/// Double-quote a (possibly schema-qualified) Postgres identifier: each dotted part quoted,
/// embedded `"` escaped by doubling — `curated.delivery-site` → `"curated"."delivery-site"`.
pub(crate) fn pg_ident(ident: &str) -> String {
    ident
        .split('.')
        .map(|p| format!("\"{}\"", p.replace('"', "\"\"")))
        .collect::<Vec<_>>()
        .join(".")
}

/// The UNQUALIFIED last segment, pg-quoted — for contexts that forbid qualification
/// (`ALTER TABLE .. RENAME TO <bare>`, column lists in DDL).
pub(crate) fn pg_bare(ident: &str) -> String {
    format!(
        "\"{}\"",
        ident.split('.').next_back().unwrap_or(ident).replace('"', "\"\"")
    )
}

/// Backtick-quote a (possibly schema-qualified) MySQL identifier: each dotted part quoted,
/// embedded backtick escaped by doubling — `db.t` → `` `db`.`t` ``.
pub(crate) fn mysql_ident(ident: &str) -> String {
    ident
        .split('.')
        .map(|p| format!("`{}`", p.replace('`', "``")))
        .collect::<Vec<_>>()
        .join(".")
}

/// Backtick-quote a MySQL COLUMN name as-is (no dot-splitting — a dot in a column name stays
/// inside one pair of backticks). Preserves the exact semantics of write.rs's two inline sites.
pub(crate) fn mysql_col(name: &str) -> String {
    format!("`{}`", name.replace('`', "``"))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn pg_quotes_schema_qualified_and_hyphenated_tables() {
        // The regression case: kebab-case must not parse as subtraction.
        assert_eq!(pg_ident("curated.delivery-site"), r#""curated"."delivery-site""#);
        assert_eq!(pg_ident("shipment"), r#""shipment""#);
        assert_eq!(pg_ident("curated.shipment"), r#""curated"."shipment""#);
        // embedded double-quote is escaped by doubling
        assert_eq!(pg_ident(r#"a"b"#), r#""a""b""#);
    }

    #[test]
    fn pg_bare_takes_last_segment_only() {
        assert_eq!(pg_bare("curated.sites-in-region"), r#""sites-in-region""#);
        assert_eq!(pg_bare("plain"), r#""plain""#);
        assert_eq!(pg_bare(r#"s.a"b"#), r#""a""b""#);
    }

    #[test]
    fn mysql_backticks_schema_qualified_tables() {
        assert_eq!(mysql_ident("billing.invoice"), "`billing`.`invoice`");
        assert_eq!(mysql_ident("invoice"), "`invoice`");
        // embedded backtick is escaped by doubling
        assert_eq!(mysql_ident("we`ird"), "`we``ird`");
    }

    #[test]
    fn mysql_col_never_splits_on_dots() {
        // Column quoting is whole-name: a dotted column stays one identifier.
        assert_eq!(mysql_col("amount.total"), "`amount.total`");
        assert_eq!(mysql_col("riskBand"), "`riskBand`");
        assert_eq!(mysql_col("we`ird"), "`we``ird`");
    }
}