floz-orm 0.1.1

A lightweight, typesafe Rust ORM — unifying DAO and DSL from a single schema
Documentation
//! Placeholder conversion utilities.
//!
//! Postgres uses `$1, $2, ...` while SQLite uses `?1, ?2, ...`.
//! Generated code always emits `$N` style; these helpers convert for SQLite.

/// Replace Postgres-style `$1, $2, ...` placeholders with
/// SQLite-style `?1, ?2, ...` placeholders.
pub(crate) fn replace_pg_placeholders(sql: &str) -> String {
    let mut result = String::with_capacity(sql.len());
    let mut chars = sql.chars().peekable();

    while let Some(ch) = chars.next() {
        if ch == '$' {
            // Check if followed by digits
            let mut digits = String::new();
            while let Some(&d) = chars.peek() {
                if d.is_ascii_digit() {
                    digits.push(d);
                    chars.next();
                } else {
                    break;
                }
            }
            if digits.is_empty() {
                result.push(ch); // bare $, keep as-is
            } else {
                result.push('?');
                result.push_str(&digits);
            }
        } else {
            result.push(ch);
        }
    }

    result
}

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

    #[test]
    fn replace_basic() {
        assert_eq!(
            replace_pg_placeholders("SELECT * FROM users WHERE id = $1"),
            "SELECT * FROM users WHERE id = ?1"
        );
    }

    #[test]
    fn replace_multi() {
        assert_eq!(
            replace_pg_placeholders("INSERT INTO t (a, b, c) VALUES ($1, $2, $3)"),
            "INSERT INTO t (a, b, c) VALUES (?1, ?2, ?3)"
        );
    }

    #[test]
    fn replace_no_dollar() {
        let sql = "SELECT 1";
        assert_eq!(replace_pg_placeholders(sql), sql);
    }

    #[test]
    fn replace_bare_dollar() {
        assert_eq!(
            replace_pg_placeholders("SELECT $ FROM t"),
            "SELECT $ FROM t"
        );
    }
}