1use crate::error::{Failure, Result};
35
36#[derive(Debug, Clone)]
40pub struct Stripped {
41 text: String,
42 conditional_comment: bool,
43 unterminated: Option<Unterminated>,
44}
45
46#[derive(Debug, Clone, Copy, PartialEq, Eq)]
48pub struct Unterminated {
49 pub kind: UnterminatedKind,
51 pub offset: usize,
53}
54
55#[derive(Debug, Clone, Copy, PartialEq, Eq)]
57pub enum UnterminatedKind {
58 BlockComment,
60 TextLiteral,
62 QuotedIdentifier,
64}
65
66impl UnterminatedKind {
67 #[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 #[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 #[must_use]
91 pub fn text(&self) -> &str {
92 &self.text
93 }
94
95 #[must_use]
97 pub const fn has_conditional_comment(&self) -> bool {
98 self.conditional_comment
99 }
100
101 #[must_use]
103 pub const fn unterminated(&self) -> Option<Unterminated> {
104 self.unterminated
105 }
106
107 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 .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#[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#[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#[derive(Debug, Clone, Copy, PartialEq, Eq)]
228enum Mode {
229 BlankEverything,
231 ExpandConditional,
233}
234
235struct Scanner<'a> {
237 src: &'a str,
238 bytes: &'a [u8],
239 mode: Mode,
240 pos: usize,
242 kept_from: usize,
244 out: String,
245 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 fn at(&self, index: usize) -> u8 {
271 self.bytes.get(index).copied().unwrap_or(0)
272 }
273
274 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 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 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 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 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 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 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 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 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 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
425const fn starts_line_comment(next: u8) -> bool {
430 next == 0 || next == b' ' || next == b'\t' || next == b'\n' || next == b'\r'
431}
432
433fn 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 #[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 #[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 #[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 #[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 #[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 #[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}