omni_orchestrator/schemas/v1/db/
utils.rs

1/// Split SQL into individual statements while handling edge cases
2pub fn split_sql_statements(sql: &str) -> Vec<String> {
3    let mut statements = Vec::new();
4    let mut current_statement = String::new();
5    let mut in_string = false;
6    let mut in_comment = false;
7    let mut delimiter = ';';
8
9    for line in sql.lines() {
10        let trimmed = line.trim();
11
12        // Skip empty lines
13        if trimmed.is_empty() {
14            continue;
15        }
16
17        // Handle DELIMITER changes (common in MySQL scripts)
18        if trimmed.to_uppercase().starts_with("DELIMITER") {
19            if let Some(new_delimiter) = trimmed.chars().nth(9) {
20                delimiter = new_delimiter;
21                continue;
22            }
23        }
24
25        // Handle comments
26        if trimmed.starts_with("--") || trimmed.starts_with("#") {
27            continue;
28        }
29
30        if trimmed.starts_with("/*") {
31            in_comment = true;
32            continue;
33        }
34
35        if trimmed.ends_with("*/") {
36            in_comment = false;
37            continue;
38        }
39
40        if in_comment {
41            continue;
42        }
43
44        // Add the line to current statement
45        current_statement.push_str(line);
46        current_statement.push('\n');
47
48        // Check for statement termination
49        let mut chars: Vec<char> = line.chars().collect();
50        while let Some(c) = chars.pop() {
51            if c == '"' || c == '\'' {
52                in_string = !in_string;
53            } else if c == delimiter && !in_string {
54                // We found a statement terminator
55                if !current_statement.trim().is_empty() {
56                    statements.push(current_statement.trim().to_string());
57                    current_statement.clear();
58                }
59                break;
60            }
61        }
62    }
63
64    // Add the last statement if it doesn't end with a delimiter
65    if !current_statement.trim().is_empty() {
66        statements.push(current_statement.trim().to_string());
67    }
68
69    statements
70}