Skip to main content

mariadb_cli/sql/
strip.rs

1//! Blanking of comments, text literals and quoted identifiers.
2//!
3//! Every textual comparison in this product runs over the output of this
4//! module and never over the raw statement. Two measured defects in the
5//! reference implementations come from comparing raw text: a legitimate query
6//! carrying a reserved word inside a literal was refused, and a destructive
7//! statement hidden behind a comment was admitted.
8//!
9//! # What is blanked
10//!
11//! Blanked regions are replaced byte for byte with spaces, so every offset in
12//! the output still names the same position in the input and a refusal message
13//! can point at the right place.
14//!
15//! Quoted identifiers are blanked too. They are names rather than literals, but
16//! a name may legally contain a separator or a reserved word, and blanking is
17//! what stops `` `a;b` `` from being read as two statements.
18//!
19//! # What is not blanked
20//!
21//! A conditional comment (`/*! ... */` or `/*M! ... */`) is executable on this
22//! server, so its body is code and stays. Only the introducer and the closing
23//! delimiter are blanked. Callers that must know a statement carried one ask
24//! [`Stripped::has_conditional_comment`].
25//!
26//! # Why the same scan also feeds the parser
27//!
28//! [`expand_conditional_comments`] applies that one rule and nothing else, so
29//! the parser is handed the statement the server will run. Leaving that to the
30//! parser crate was measured to be wrong: it expands the introducer that starts
31//! with an exclamation mark and drops the server-specific one as whitespace,
32//! which took a file-reading call out of the tree the refusals walk.
33
34use crate::error::{Failure, Result};
35
36/// A statement with its comments, text literals and quoted identifiers blanked.
37///
38/// The text has exactly the same byte length as the input.
39#[derive(Debug, Clone)]
40pub struct Stripped {
41    text: String,
42    conditional_comment: bool,
43    unterminated: Option<Unterminated>,
44}
45
46/// A quoted or commented region that never closed.
47#[derive(Debug, Clone, Copy, PartialEq, Eq)]
48pub struct Unterminated {
49    /// What kind of region was left open.
50    pub kind: UnterminatedKind,
51    /// The byte offset where the open region started.
52    pub offset: usize,
53}
54
55/// The kinds of region that can be left open.
56#[derive(Debug, Clone, Copy, PartialEq, Eq)]
57pub enum UnterminatedKind {
58    /// A `/* ... */` comment, conditional or not.
59    BlockComment,
60    /// A `'...'` or `"..."` literal.
61    TextLiteral,
62    /// A `` `...` `` identifier.
63    QuotedIdentifier,
64}
65
66impl UnterminatedKind {
67    /// The stable name used in a refusal message.
68    #[must_use]
69    pub const fn as_str(self) -> &'static str {
70        match self {
71            Self::BlockComment => "block comment",
72            Self::TextLiteral => "text literal",
73            Self::QuotedIdentifier => "quoted identifier",
74        }
75    }
76
77    /// The characters the region opens with.
78    #[must_use]
79    pub const fn opening_delimiter(self) -> &'static str {
80        match self {
81            Self::BlockComment => "/*",
82            Self::TextLiteral => "'",
83            Self::QuotedIdentifier => "`",
84        }
85    }
86}
87
88impl Stripped {
89    /// The blanked text, byte-for-byte aligned with the input.
90    #[must_use]
91    pub fn text(&self) -> &str {
92        &self.text
93    }
94
95    /// Whether the input carried a conditional comment, whose body is code.
96    #[must_use]
97    pub const fn has_conditional_comment(&self) -> bool {
98        self.conditional_comment
99    }
100
101    /// The region left open, when the input ended inside one.
102    #[must_use]
103    pub const fn unterminated(&self) -> Option<Unterminated> {
104        self.unterminated
105    }
106
107    /// Refuses a statement that ends inside a quote or a comment.
108    ///
109    /// An open region means the rest of the text was read as data rather than
110    /// as code, so no later decision about it can be trusted.
111    ///
112    /// # Errors
113    ///
114    /// Returns a validation failure when a region was left open.
115    pub fn refuse_if_unterminated(&self) -> Result<()> {
116        match self.unterminated {
117            None => Ok(()),
118            Some(open) => Err(Failure::validation(
119                "sql.unterminated_region",
120                format!(
121                    "the statement ends inside an unterminated {} that opens at byte {}",
122                    open.kind.as_str(),
123                    open.offset
124                ),
125            )
126            // The delimiter and the offset, rather than the name of the kind,
127            // because the catalogue substitutes this into prose a reader asked
128            // for in their own language and a delimiter reads the same in all.
129            .with_detail(
130                "detail",
131                format!("{} @ {}", open.kind.opening_delimiter(), open.offset),
132            )
133            .with_hint("close the quote or the comment before sending the statement")),
134        }
135    }
136}
137
138/// Blanks comments, text literals and quoted identifiers.
139///
140/// Never fails: an unterminated region is blanked to the end of the input and
141/// reported through [`Stripped::unterminated`], so a caller decides what to do.
142///
143/// # Examples
144///
145/// Blanked rather than removed, so every offset in the result still points at
146/// the same character of the input. That is what lets a later stage report a
147/// position the caller can find in the text they wrote:
148///
149/// ```
150/// use mariadb_cli::sql::strip::strip;
151///
152/// let stripped = strip("SELECT 1 -- drop everything");
153/// assert_eq!(stripped.text().len(), "SELECT 1 -- drop everything".len());
154/// assert!(!stripped.text().contains("drop"));
155/// ```
156///
157/// A keyword hidden inside a text literal is blanked too, which is the point:
158/// classification runs over the result, so the literal cannot pose as code.
159///
160/// ```
161/// use mariadb_cli::sql::strip::strip;
162///
163/// let stripped = strip("SELECT 'DELETE FROM pedidos'");
164/// assert!(stripped.text().starts_with("SELECT "));
165/// assert!(!stripped.text().contains("DELETE"));
166/// ```
167///
168/// A region that never closes is reported rather than passed off as clean:
169///
170/// ```
171/// use mariadb_cli::sql::strip::strip;
172///
173/// assert!(strip("SELECT 'aberta").unterminated().is_some());
174/// assert!(strip("SELECT 'fechada'").unterminated().is_none());
175/// ```
176#[must_use]
177pub fn strip(sql: &str) -> Stripped {
178    let mut scanner = Scanner::new(sql, Mode::BlankEverything);
179    scanner.run();
180    scanner.finish()
181}
182
183/// The statement as the server will run it, with conditional comments expanded.
184///
185/// Only the introducer and the closing delimiter of a conditional comment are
186/// blanked. Literals, names and plain comments are handed back byte for byte,
187/// so the result parses like the input and every offset still names the same
188/// position, which is what lets a parser complaint point at the caller's text.
189///
190/// # Why this is not left to the parser
191///
192/// The parser crate expands `/*! ... */` and drops `/*M! ... */` as whitespace.
193/// This server runs both. A file-reading call written inside the second one was
194/// therefore absent from the tree while present in the statement the server
195/// executed, and every refusal that walks the tree was blind to it. Deciding
196/// here what the server runs is what removes that difference of opinion.
197///
198/// # Examples
199///
200/// The body of both introducers survives, and the delimiters do not:
201///
202/// ```
203/// use mariadb_cli::sql::strip::expand_conditional_comments;
204///
205/// let expanded = expand_conditional_comments("SELECT 1 /*M!100000 ,2 */");
206/// assert!(expanded.contains(",2"));
207/// assert!(!expanded.contains("/*"));
208/// assert_eq!(expanded.len(), "SELECT 1 /*M!100000 ,2 */".len());
209/// ```
210///
211/// A literal is data, so it is handed back exactly as it arrived:
212///
213/// ```
214/// use mariadb_cli::sql::strip::expand_conditional_comments;
215///
216/// let text = "SELECT '/*M!100000 hidden */' AS note";
217/// assert_eq!(expand_conditional_comments(text), text);
218/// ```
219#[must_use]
220pub fn expand_conditional_comments(sql: &str) -> String {
221    let mut scanner = Scanner::new(sql, Mode::ExpandConditional);
222    scanner.run();
223    scanner.finish().text
224}
225
226/// What the scan does with a region that is not a conditional delimiter.
227#[derive(Debug, Clone, Copy, PartialEq, Eq)]
228enum Mode {
229    /// Replace it with spaces, for textual comparison.
230    BlankEverything,
231    /// Hand it back unchanged, for parsing.
232    ExpandConditional,
233}
234
235/// One left-to-right pass over the input.
236struct Scanner<'a> {
237    src: &'a str,
238    bytes: &'a [u8],
239    mode: Mode,
240    /// The read position.
241    pos: usize,
242    /// The start of the run of bytes kept verbatim and not yet emitted.
243    kept_from: usize,
244    out: String,
245    /// How many conditional comments are open around the read position.
246    conditional_depth: usize,
247    conditional_comment: bool,
248    unterminated: Option<Unterminated>,
249}
250
251impl<'a> Scanner<'a> {
252    fn new(src: &'a str, mode: Mode) -> Self {
253        Self {
254            src,
255            bytes: src.as_bytes(),
256            mode,
257            pos: 0,
258            kept_from: 0,
259            out: String::with_capacity(src.len()),
260            conditional_depth: 0,
261            conditional_comment: false,
262            unterminated: None,
263        }
264    }
265
266    /// The byte at `index`, or zero past the end.
267    ///
268    /// Zero never appears in the delimiters this scanner matches, so it is a
269    /// safe stand-in for the end of the input.
270    fn at(&self, index: usize) -> u8 {
271        self.bytes.get(index).copied().unwrap_or(0)
272    }
273
274    /// Emits every byte kept verbatim up to `upto`.
275    fn flush_kept(&mut self, upto: usize) {
276        if let Some(kept) = self.src.get(self.kept_from..upto) {
277            self.out.push_str(kept);
278        }
279        self.kept_from = upto;
280    }
281
282    /// Replaces `from..to` with as many spaces as it has bytes.
283    ///
284    /// Region ends always land on a character boundary, because every
285    /// delimiter this scanner matches is ASCII and a continuation byte of a
286    /// multi-byte character never equals an ASCII byte.
287    fn blank(&mut self, from: usize, to: usize) {
288        self.flush_kept(from);
289        for _ in from..to {
290            self.out.push(' ');
291        }
292        self.kept_from = to;
293        self.pos = to;
294    }
295
296    /// Passes over a region that is data or commentary rather than a delimiter.
297    ///
298    /// Which of the two scans is running decides what that means. The textual
299    /// one replaces the region with spaces so a comparison cannot read data as
300    /// code; the parsing one hands it back untouched, because a literal with
301    /// its quotes removed is no longer a statement anybody can read.
302    ///
303    /// Both leave the byte length alone, so an offset means the same thing on
304    /// either side.
305    fn erase(&mut self, from: usize, to: usize) {
306        match self.mode {
307            Mode::BlankEverything => self.blank(from, to),
308            Mode::ExpandConditional => self.pos = to,
309        }
310    }
311
312    /// Records the first open region seen.
313    fn mark_unterminated(&mut self, kind: UnterminatedKind, offset: usize) {
314        if self.unterminated.is_none() {
315            self.unterminated = Some(Unterminated { kind, offset });
316        }
317    }
318
319    fn run(&mut self) {
320        while self.pos < self.bytes.len() {
321            let start = self.pos;
322            match self.at(start) {
323                // A closing delimiter of a conditional comment is punctuation,
324                // never code, so it is blanked while its body is kept.
325                b'*' if self.conditional_depth > 0 && self.at(start + 1) == b'/' => {
326                    self.conditional_depth -= 1;
327                    self.blank(start, start + 2);
328                }
329                b'-' if self.at(start + 1) == b'-' && starts_line_comment(self.at(start + 2)) => {
330                    self.blank_to_end_of_line(start);
331                }
332                b'#' => self.blank_to_end_of_line(start),
333                b'/' if self.at(start + 1) == b'*' => self.take_block_comment(start),
334                b'\'' | b'"' => self.take_quoted(start, UnterminatedKind::TextLiteral),
335                b'`' => self.take_quoted(start, UnterminatedKind::QuotedIdentifier),
336                _ => self.pos = start + 1,
337            }
338        }
339        if self.conditional_depth > 0 {
340            self.mark_unterminated(UnterminatedKind::BlockComment, self.bytes.len());
341        }
342    }
343
344    /// Passes over a `-- ` or `#` comment, keeping the line break that ends it.
345    fn blank_to_end_of_line(&mut self, start: usize) {
346        let end = self.src[start..]
347            .find('\n')
348            .map_or(self.bytes.len(), |offset| start + offset);
349        self.erase(start, end);
350    }
351
352    /// Handles `/*`, which opens either an executable directive or a comment.
353    fn take_block_comment(&mut self, start: usize) {
354        if let Some(body_start) = conditional_body_start(self.bytes, start) {
355            self.conditional_comment = true;
356            self.conditional_depth += 1;
357            self.blank(start, body_start);
358            return;
359        }
360        self.blank_block_comment(start);
361    }
362
363    /// Blanks a plain `/* ... */` comment, honouring nesting.
364    fn blank_block_comment(&mut self, start: usize) {
365        let mut depth = 1_usize;
366        let mut cursor = start + 2;
367        while cursor < self.bytes.len() {
368            if self.at(cursor) == b'/' && self.at(cursor + 1) == b'*' {
369                depth += 1;
370                cursor += 2;
371            } else if self.at(cursor) == b'*' && self.at(cursor + 1) == b'/' {
372                depth -= 1;
373                cursor += 2;
374                if depth == 0 {
375                    self.erase(start, cursor);
376                    return;
377                }
378            } else {
379                cursor += 1;
380            }
381        }
382        self.mark_unterminated(UnterminatedKind::BlockComment, start);
383        self.erase(start, self.bytes.len());
384    }
385
386    /// Blanks a region delimited by the byte at `start`.
387    ///
388    /// A doubled delimiter escapes itself everywhere. A backslash escapes the
389    /// next byte inside a text literal but not inside a quoted identifier,
390    /// which is how this server reads them.
391    fn take_quoted(&mut self, start: usize, kind: UnterminatedKind) {
392        let delimiter = self.at(start);
393        let backslash_escapes = kind == UnterminatedKind::TextLiteral;
394        let mut cursor = start + 1;
395        while cursor < self.bytes.len() {
396            let byte = self.at(cursor);
397            // A backslash and a doubled delimiter both consume two bytes: the
398            // marker and the byte it protects.
399            let escaped = (backslash_escapes && byte == b'\\')
400                || (byte == delimiter && self.at(cursor + 1) == delimiter);
401            if escaped {
402                cursor += 2;
403            } else if byte == delimiter {
404                self.erase(start, cursor + 1);
405                return;
406            } else {
407                cursor += 1;
408            }
409        }
410        self.mark_unterminated(kind, start);
411        self.erase(start, self.bytes.len());
412    }
413
414    fn finish(mut self) -> Stripped {
415        let end = self.bytes.len();
416        self.flush_kept(end);
417        Stripped {
418            text: self.out,
419            conditional_comment: self.conditional_comment,
420            unterminated: self.unterminated,
421        }
422    }
423}
424
425/// Whether the byte after `--` turns it into a comment.
426///
427/// This server needs a space, a control character or the end of the input
428/// after the two dashes, so `a--1` stays an expression.
429const fn starts_line_comment(next: u8) -> bool {
430    next == 0 || next == b' ' || next == b'\t' || next == b'\n' || next == b'\r'
431}
432
433/// Where the executable body of a conditional comment starts.
434///
435/// Returns `None` when the comment opening at `start` is a plain comment. The
436/// introducer is `/*!` or `/*M!`, optionally followed by the version digits
437/// that decide whether this server runs the body.
438fn conditional_body_start(bytes: &[u8], start: usize) -> Option<usize> {
439    let read = |index: usize| bytes.get(index).copied().unwrap_or(0);
440    let mut cursor = match read(start + 2) {
441        b'!' => start + 3,
442        b'M' | b'm' if read(start + 3) == b'!' => start + 4,
443        _ => return None,
444    };
445    while read(cursor).is_ascii_digit() {
446        cursor += 1;
447    }
448    Some(cursor)
449}
450
451#[cfg(test)]
452mod tests {
453    use super::*;
454
455    fn text_of(sql: &str) -> String {
456        strip(sql).text().to_string()
457    }
458
459    /// Every offset in the output still names the same input position, which
460    /// is what lets a refusal point at the right byte.
461    #[test]
462    fn the_blanked_text_keeps_the_byte_length_of_the_input() {
463        for sql in [
464            "SELECT 1",
465            "SELECT 'olá' -- acento\nFROM t",
466            "SELECT /* ácido */ `coluna` FROM t",
467            "SELECT '\u{1f600}' FROM t",
468            "SELECT # fim",
469        ] {
470            assert_eq!(text_of(sql).len(), sql.len(), "input {sql:?}");
471        }
472    }
473
474    #[test]
475    fn a_reserved_word_inside_a_literal_disappears_from_the_text() {
476        let stripped = text_of("SELECT 'DROP TABLE users' FROM t");
477        assert!(!stripped.contains("DROP"), "got {stripped:?}");
478        assert!(stripped.contains("SELECT"));
479        assert!(stripped.contains("FROM t"));
480    }
481
482    #[test]
483    fn a_doubled_quote_does_not_end_the_literal() {
484        let stripped = text_of("SELECT 'it''s DROP' , 1");
485        assert!(!stripped.contains("DROP"), "got {stripped:?}");
486        assert!(stripped.contains(", 1"));
487    }
488
489    #[test]
490    fn a_backslash_escaped_quote_does_not_end_the_literal() {
491        let stripped = text_of("SELECT 'a\\' DROP' , 1");
492        assert!(!stripped.contains("DROP"), "got {stripped:?}");
493        assert!(stripped.contains(", 1"));
494    }
495
496    #[test]
497    fn a_double_quoted_literal_is_blanked_like_a_single_quoted_one() {
498        let stripped = text_of("SELECT \"DROP TABLE\" , 1");
499        assert!(!stripped.contains("DROP"), "got {stripped:?}");
500    }
501
502    /// A backslash is an ordinary byte inside a quoted identifier, so the
503    /// closing backtick still closes it.
504    #[test]
505    fn a_backslash_does_not_escape_inside_a_quoted_identifier() {
506        let stripped = text_of("SELECT `a\\` , 1");
507        assert!(stripped.contains(", 1"), "got {stripped:?}");
508        assert!(strip("SELECT `a\\` , 1").unterminated().is_none());
509    }
510
511    #[test]
512    fn a_separator_inside_a_quoted_identifier_disappears_from_the_text() {
513        let stripped = text_of("SELECT * FROM `a;b`");
514        assert!(!stripped.contains(';'), "got {stripped:?}");
515    }
516
517    #[test]
518    fn a_line_comment_needs_a_space_after_the_two_dashes() {
519        assert!(text_of("SELECT 1-- DROP").contains("SELECT 1"));
520        assert!(!text_of("SELECT 1-- DROP").contains("DROP"));
521        assert!(
522            text_of("SELECT 1--2").contains("--2"),
523            "without a space the dashes are an operator"
524        );
525    }
526
527    #[test]
528    fn a_hash_comment_runs_to_the_end_of_the_line() {
529        let stripped = text_of("SELECT 1 # DROP TABLE t\nFROM u");
530        assert!(!stripped.contains("DROP"), "got {stripped:?}");
531        assert!(stripped.contains("FROM u"));
532    }
533
534    #[test]
535    fn a_block_comment_is_blanked_and_may_nest() {
536        let stripped = text_of("SELECT /* a /* b */ DROP */ 1");
537        assert!(!stripped.contains("DROP"), "got {stripped:?}");
538        assert!(stripped.contains("SELECT"));
539        assert!(stripped.contains('1'));
540    }
541
542    #[test]
543    fn a_comment_placed_mid_statement_does_not_hide_the_rest() {
544        let stripped = text_of("DELETE /* looks harmless */ FROM users");
545        assert!(stripped.contains("DELETE"));
546        assert!(stripped.contains("FROM users"));
547    }
548
549    #[test]
550    fn a_conditional_comment_is_kept_as_code() {
551        let stripped = text_of("SELECT /*!40001 SQL_NO_CACHE */ 1");
552        assert!(
553            stripped.contains("SQL_NO_CACHE"),
554            "the body is executable, got {stripped:?}"
555        );
556        assert!(!stripped.contains("/*"), "the introducer is punctuation");
557        assert!(!stripped.contains("*/"), "the terminator is punctuation");
558        assert!(strip("SELECT /*!40001 SQL_NO_CACHE */ 1").has_conditional_comment());
559    }
560
561    #[test]
562    fn a_server_specific_conditional_comment_is_kept_as_code() {
563        let stripped = text_of("SELECT /*M!100301 DROP */ 1");
564        assert!(stripped.contains("DROP"), "got {stripped:?}");
565        assert!(strip("SELECT /*M!100301 DROP */ 1").has_conditional_comment());
566    }
567
568    #[test]
569    fn a_literal_inside_a_conditional_comment_is_still_blanked() {
570        let stripped = text_of("SELECT /*! 'DROP' */ 1");
571        assert!(!stripped.contains("DROP"), "got {stripped:?}");
572    }
573
574    #[test]
575    fn a_conditional_comment_inside_a_plain_comment_stays_commented_out() {
576        let stripped = text_of("SELECT /* /*!40001 DROP */ */ 1");
577        assert!(!stripped.contains("DROP"), "got {stripped:?}");
578        assert!(!strip("SELECT /* /*!40001 DROP */ */ 1").has_conditional_comment());
579    }
580
581    #[test]
582    fn a_comment_marker_inside_a_literal_does_not_open_a_comment() {
583        let stripped = text_of("SELECT '/*' , 1 , '*/'");
584        assert!(stripped.contains(", 1 ,"), "got {stripped:?}");
585    }
586
587    #[test]
588    fn a_quote_inside_a_comment_does_not_open_a_literal() {
589        let stripped = text_of("SELECT /* it's here */ 1 , 2");
590        assert!(stripped.contains("1 , 2"), "got {stripped:?}");
591    }
592
593    #[test]
594    fn an_unterminated_literal_is_reported_and_refused() {
595        let stripped = strip("SELECT 'never closed");
596        let open = stripped.unterminated().expect("open region");
597        assert_eq!(open.kind, UnterminatedKind::TextLiteral);
598        assert_eq!(open.offset, 7);
599        assert!(stripped.refuse_if_unterminated().is_err());
600    }
601
602    #[test]
603    fn an_unterminated_block_comment_is_reported_and_refused() {
604        let stripped = strip("SELECT 1 /* never closed");
605        let open = stripped.unterminated().expect("open region");
606        assert_eq!(open.kind, UnterminatedKind::BlockComment);
607        assert!(stripped.refuse_if_unterminated().is_err());
608    }
609
610    #[test]
611    fn an_unterminated_quoted_identifier_is_reported() {
612        let stripped = strip("SELECT * FROM `never closed");
613        let open = stripped.unterminated().expect("open region");
614        assert_eq!(open.kind, UnterminatedKind::QuotedIdentifier);
615    }
616
617    #[test]
618    fn a_closed_statement_reports_no_open_region() {
619        assert!(strip("SELECT 'a' , `b` /* c */ -- d\n")
620            .refuse_if_unterminated()
621            .is_ok());
622    }
623
624    #[test]
625    fn an_empty_input_produces_an_empty_text() {
626        assert_eq!(text_of(""), "");
627    }
628
629    /// The parsing scan keeps every offset, so a parser complaint still points
630    /// at the byte the caller wrote.
631    #[test]
632    fn expanding_a_conditional_comment_keeps_the_byte_length() {
633        for sql in [
634            "SELECT 1",
635            "SELECT /*!40001 SQL_NO_CACHE */ 1",
636            "SELECT /*M!100301 SQL_NO_CACHE */ 1",
637            "SELECT 'olá' /*M! ,2 */ FROM t",
638            "SELECT /* plain */ 1 -- tail\n",
639        ] {
640            assert_eq!(
641                expand_conditional_comments(sql).len(),
642                sql.len(),
643                "input {sql:?}"
644            );
645        }
646    }
647
648    /// Both introducers this server honours are expanded, and the version
649    /// digits go with the introducer they belong to.
650    ///
651    /// The parser crate expands only the first of the two, which is what let a
652    /// file-reading call written inside the second one reach the server while
653    /// staying out of the tree every refusal walks.
654    #[test]
655    fn both_conditional_introducers_leave_their_body_as_code() {
656        for sql in [
657            "SELECT 1 /*!100000 ,2 */",
658            "SELECT 1 /*M!100000 ,2 */",
659            "SELECT 1 /*m!100000 ,2 */",
660            "SELECT 1 /*M! ,2 */",
661        ] {
662            let expanded = expand_conditional_comments(sql);
663            assert!(expanded.contains(",2"), "input {sql:?} gave {expanded:?}");
664            assert!(!expanded.contains("/*"), "input {sql:?}");
665            assert!(!expanded.contains("*/"), "input {sql:?}");
666            assert!(!expanded.contains('!'), "input {sql:?}");
667            assert!(
668                !expanded.contains("100000"),
669                "the version gate is punctuation, not code: {expanded:?}"
670            );
671        }
672    }
673
674    /// Data reaches the parser exactly as it arrived, because a literal with
675    /// its quotes replaced by spaces is no longer a statement anybody can read.
676    #[test]
677    fn the_parsing_scan_hands_back_data_and_commentary_untouched() {
678        for sql in [
679            "SELECT '/*M!100000 hidden */' AS note",
680            "SELECT `/*M!100000`",
681            "SELECT 1 /* /*M!100000 hidden */",
682            "SELECT 1 -- /*M!100000 hidden\n",
683            "SELECT 1 # /*M!100000 hidden",
684        ] {
685            assert_eq!(expand_conditional_comments(sql), sql, "input {sql:?}");
686        }
687    }
688
689    /// The two scans answer different questions and must not be swapped.
690    ///
691    /// The textual one exists so a comparison cannot read data as code, and the
692    /// parsing one exists so the tree is the statement the server will run.
693    #[test]
694    fn the_two_scans_agree_on_the_conditional_body_and_on_nothing_else() {
695        let sql = "SELECT 'x' /*M!100000 ,LOAD_FILE(1) */";
696        let blanked = text_of(sql);
697        let expanded = expand_conditional_comments(sql);
698
699        assert!(blanked.contains("LOAD_FILE"), "got {blanked:?}");
700        assert!(expanded.contains("LOAD_FILE"), "got {expanded:?}");
701        assert!(
702            !blanked.contains('x'),
703            "the literal is data to a comparison"
704        );
705        assert!(expanded.contains("'x'"), "the literal is code to a parser");
706    }
707}