Skip to main content

no_mistakes_postgres_sql/statements/
fallback.rs

1/// Mask comments, quoted strings, and dollar-quoted bodies in one scan so
2/// comment markers inside literals and quotes inside comments cannot nest.
3pub fn mask_quoted_sql(sql: &str) -> String {
4    mask_comments(sql)
5}
6
7/// Same one-pass mask as [`mask_quoted_sql`].
8pub fn mask_comments(sql: &str) -> String {
9    mask_regions(sql)
10}
11
12fn mask_regions(sql: &str) -> String {
13    let chars: Vec<char> = sql.chars().collect();
14    let mut out = String::with_capacity(sql.len());
15    let mut index = 0usize;
16    while index < chars.len() {
17        if chars[index] == '-' && chars.get(index + 1) == Some(&'-') {
18            while index < chars.len() && chars[index] != '\n' {
19                out.push(' ');
20                index += 1;
21            }
22            continue;
23        }
24        if chars[index] == '/' && chars.get(index + 1) == Some(&'*') {
25            index = skip_block_comment(&chars, index, &mut out);
26            continue;
27        }
28        if matches!(chars[index], 'e' | 'E') && chars.get(index + 1) == Some(&'\'') {
29            out.push(' ');
30            index = skip_quote(&chars, index + 1, '\'', &mut out, true);
31            continue;
32        }
33        if chars[index] == '\'' {
34            index = skip_quote(&chars, index, '\'', &mut out, false);
35            continue;
36        }
37        if chars[index] == '"' {
38            index = skip_quote(&chars, index, '"', &mut out, false);
39            continue;
40        }
41        if chars[index] == '$' {
42            if let Some(end) = skip_dollar(&chars, index, &mut out) {
43                index = end;
44                continue;
45            }
46        }
47        out.push(chars[index]);
48        index += 1;
49    }
50    out
51}
52
53fn skip_block_comment(chars: &[char], mut index: usize, out: &mut String) -> usize {
54    out.push(' ');
55    out.push(' ');
56    index += 2;
57    let mut depth = 1i32;
58    while index < chars.len() && depth > 0 {
59        if chars[index] == '/' && chars.get(index + 1) == Some(&'*') {
60            out.push(' ');
61            out.push(' ');
62            index += 2;
63            depth += 1;
64            continue;
65        }
66        if chars[index] == '*' && chars.get(index + 1) == Some(&'/') {
67            out.push(' ');
68            out.push(' ');
69            index += 2;
70            depth -= 1;
71            continue;
72        }
73        out.push(if chars[index] == '\n' { '\n' } else { ' ' });
74        index += 1;
75    }
76    index
77}
78
79fn skip_quote(chars: &[char], start: usize, quote: char, out: &mut String, escaped: bool) -> usize {
80    out.push(' ');
81    let mut index = start + 1;
82    while index < chars.len() {
83        out.push(' ');
84        if escaped && chars[index] == '\\' {
85            if index + 1 < chars.len() {
86                out.push(' ');
87                index += 2;
88                continue;
89            }
90            return chars.len();
91        }
92        if chars[index] == quote {
93            if index + 1 < chars.len() && chars[index + 1] == quote {
94                out.push(' ');
95                index += 2;
96                continue;
97            }
98            return index + 1;
99        }
100        index += 1;
101    }
102    index
103}
104
105fn skip_dollar(chars: &[char], start: usize, out: &mut String) -> Option<usize> {
106    let mut tag = String::from("$");
107    let mut index = start + 1;
108    while index < chars.len() && (chars[index].is_ascii_alphanumeric() || chars[index] == '_') {
109        tag.push(chars[index]);
110        index += 1;
111    }
112    if index >= chars.len() || chars[index] != '$' {
113        return None;
114    }
115    tag.push('$');
116    index += 1;
117    let tag_chars: Vec<char> = tag.chars().collect();
118    while index + tag_chars.len() <= chars.len() {
119        if chars[index..index + tag_chars.len()] == tag_chars[..] {
120            for _ in 0..tag_chars.len() {
121                out.push(' ');
122            }
123            return Some(index + tag_chars.len());
124        }
125        out.push(' ');
126        index += 1;
127    }
128    Some(chars.len())
129}
130
131pub fn insert_keyword_count(masked: &str) -> usize {
132    let lower = masked.to_ascii_lowercase();
133    let mut count = 0usize;
134    let mut search = lower.as_str();
135    while let Some(at) = search.find("insert") {
136        let after = &search[at + 6..];
137        let trimmed = after.trim_start();
138        if trimmed.starts_with("into") {
139            count += 1;
140        }
141        search = &search[at + 6..];
142    }
143    count
144}