ch-database 0.1.0

Database and object storage primitives for SQLx, SpacetimeDB, and S3
Documentation
/// Splits a migration script into individual statements and trims whitespace.
///
/// Unlike a plain `split(';')`, this parser ignores semicolons inside:
/// - single-quoted strings
/// - double-quoted strings
/// - backtick identifiers
/// - line comments (`-- ...`, `# ...`)
/// - block comments (`/* ... */`)
pub fn iter_migration_statements(sql: &str) -> impl Iterator<Item = &str> {
    #[derive(Clone, Copy, PartialEq, Eq)]
    enum Mode {
        Normal,
        SingleQuote,
        DoubleQuote,
        Backtick,
        LineComment,
        BlockComment,
    }

    let bytes = sql.as_bytes();
    let mut mode = Mode::Normal;
    let mut statement_start = 0usize;
    let mut i = 0usize;
    let mut statements: Vec<&str> = Vec::new();

    while i < bytes.len() {
        let b = bytes[i];
        let next = bytes.get(i + 1).copied();

        match mode {
            Mode::Normal => match b {
                b'\'' => mode = Mode::SingleQuote,
                b'"' => mode = Mode::DoubleQuote,
                b'`' => mode = Mode::Backtick,
                b'-' if next == Some(b'-') => {
                    mode = Mode::LineComment;
                    i += 1;
                }
                b'#' => mode = Mode::LineComment,
                b'/' if next == Some(b'*') => {
                    mode = Mode::BlockComment;
                    i += 1;
                }
                b';' => {
                    let statement = sql[statement_start..i]
                        .trim()
                        .trim_start_matches('\u{feff}')
                        .trim();
                    if !statement.is_empty() {
                        statements.push(statement);
                    }
                    statement_start = i + 1;
                }
                _ => {}
            },
            Mode::SingleQuote => {
                if b == b'\\' {
                    // Skip escaped character in string.
                    i += 1;
                } else if b == b'\'' {
                    mode = Mode::Normal;
                }
            }
            Mode::DoubleQuote => {
                if b == b'\\' {
                    i += 1;
                } else if b == b'"' {
                    mode = Mode::Normal;
                }
            }
            Mode::Backtick => {
                if b == b'`' {
                    mode = Mode::Normal;
                }
            }
            Mode::LineComment => {
                if b == b'\n' || b == b'\r' {
                    mode = Mode::Normal;
                }
            }
            Mode::BlockComment => {
                if b == b'*' && next == Some(b'/') {
                    mode = Mode::Normal;
                    i += 1;
                }
            }
        }

        i += 1;
    }

    let tail = sql[statement_start..]
        .trim()
        .trim_start_matches('\u{feff}')
        .trim();
    if !tail.is_empty() {
        statements.push(tail);
    }

    statements.into_iter()
}

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

    #[test]
    fn strips_utf8_bom_from_first_statement() {
        let sql = "\u{feff}CREATE TABLE first (id INT); CREATE TABLE second (id INT);";
        let statements: Vec<_> = iter_migration_statements(sql).collect();
        assert_eq!(
            statements,
            vec![
                "CREATE TABLE first (id INT)",
                "CREATE TABLE second (id INT)"
            ]
        );
    }

    #[test]
    fn ignores_semicolons_in_line_comments() {
        let sql = "-- comment with ; inside\nCREATE TABLE t1 (id INT);";
        let statements: Vec<_> = iter_migration_statements(sql).collect();
        assert_eq!(
            statements,
            vec!["-- comment with ; inside\nCREATE TABLE t1 (id INT)"]
        );
    }

    #[test]
    fn ignores_semicolons_in_block_comments() {
        let sql = "/* comment ; inside */\nCREATE TABLE t1 (id INT);";
        let statements: Vec<_> = iter_migration_statements(sql).collect();
        assert_eq!(
            statements,
            vec!["/* comment ; inside */\nCREATE TABLE t1 (id INT)"]
        );
    }

    #[test]
    fn ignores_semicolons_in_strings() {
        let sql = "INSERT INTO t1 (txt) VALUES ('a;b;c'); INSERT INTO t1 (txt) VALUES (\"x;y\");";
        let statements: Vec<_> = iter_migration_statements(sql).collect();
        assert_eq!(
            statements,
            vec![
                "INSERT INTO t1 (txt) VALUES ('a;b;c')",
                "INSERT INTO t1 (txt) VALUES (\"x;y\")",
            ]
        );
    }
}