use crate::error::{Failure, Result};
#[derive(Debug, Clone)]
pub struct Stripped {
text: String,
conditional_comment: bool,
unterminated: Option<Unterminated>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Unterminated {
pub kind: UnterminatedKind,
pub offset: usize,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum UnterminatedKind {
BlockComment,
TextLiteral,
QuotedIdentifier,
}
impl UnterminatedKind {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::BlockComment => "block comment",
Self::TextLiteral => "text literal",
Self::QuotedIdentifier => "quoted identifier",
}
}
#[must_use]
pub const fn opening_delimiter(self) -> &'static str {
match self {
Self::BlockComment => "/*",
Self::TextLiteral => "'",
Self::QuotedIdentifier => "`",
}
}
}
impl Stripped {
#[must_use]
pub fn text(&self) -> &str {
&self.text
}
#[must_use]
pub const fn has_conditional_comment(&self) -> bool {
self.conditional_comment
}
#[must_use]
pub const fn unterminated(&self) -> Option<Unterminated> {
self.unterminated
}
pub fn refuse_if_unterminated(&self) -> Result<()> {
match self.unterminated {
None => Ok(()),
Some(open) => Err(Failure::validation(
"sql.unterminated_region",
format!(
"the statement ends inside an unterminated {} that opens at byte {}",
open.kind.as_str(),
open.offset
),
)
.with_detail(
"detail",
format!("{} @ {}", open.kind.opening_delimiter(), open.offset),
)
.with_hint("close the quote or the comment before sending the statement")),
}
}
}
#[must_use]
pub fn strip(sql: &str) -> Stripped {
let mut scanner = Scanner::new(sql, Mode::BlankEverything);
scanner.run();
scanner.finish()
}
#[must_use]
pub fn expand_conditional_comments(sql: &str) -> String {
let mut scanner = Scanner::new(sql, Mode::ExpandConditional);
scanner.run();
scanner.finish().text
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Mode {
BlankEverything,
ExpandConditional,
}
struct Scanner<'a> {
src: &'a str,
bytes: &'a [u8],
mode: Mode,
pos: usize,
kept_from: usize,
out: String,
conditional_depth: usize,
conditional_comment: bool,
unterminated: Option<Unterminated>,
}
impl<'a> Scanner<'a> {
fn new(src: &'a str, mode: Mode) -> Self {
Self {
src,
bytes: src.as_bytes(),
mode,
pos: 0,
kept_from: 0,
out: String::with_capacity(src.len()),
conditional_depth: 0,
conditional_comment: false,
unterminated: None,
}
}
fn at(&self, index: usize) -> u8 {
self.bytes.get(index).copied().unwrap_or(0)
}
fn flush_kept(&mut self, upto: usize) {
if let Some(kept) = self.src.get(self.kept_from..upto) {
self.out.push_str(kept);
}
self.kept_from = upto;
}
fn blank(&mut self, from: usize, to: usize) {
self.flush_kept(from);
for _ in from..to {
self.out.push(' ');
}
self.kept_from = to;
self.pos = to;
}
fn erase(&mut self, from: usize, to: usize) {
match self.mode {
Mode::BlankEverything => self.blank(from, to),
Mode::ExpandConditional => self.pos = to,
}
}
fn mark_unterminated(&mut self, kind: UnterminatedKind, offset: usize) {
if self.unterminated.is_none() {
self.unterminated = Some(Unterminated { kind, offset });
}
}
fn run(&mut self) {
while self.pos < self.bytes.len() {
let start = self.pos;
match self.at(start) {
b'*' if self.conditional_depth > 0 && self.at(start + 1) == b'/' => {
self.conditional_depth -= 1;
self.blank(start, start + 2);
}
b'-' if self.at(start + 1) == b'-' && starts_line_comment(self.at(start + 2)) => {
self.blank_to_end_of_line(start);
}
b'#' => self.blank_to_end_of_line(start),
b'/' if self.at(start + 1) == b'*' => self.take_block_comment(start),
b'\'' | b'"' => self.take_quoted(start, UnterminatedKind::TextLiteral),
b'`' => self.take_quoted(start, UnterminatedKind::QuotedIdentifier),
_ => self.pos = start + 1,
}
}
if self.conditional_depth > 0 {
self.mark_unterminated(UnterminatedKind::BlockComment, self.bytes.len());
}
}
fn blank_to_end_of_line(&mut self, start: usize) {
let end = self.src[start..]
.find('\n')
.map_or(self.bytes.len(), |offset| start + offset);
self.erase(start, end);
}
fn take_block_comment(&mut self, start: usize) {
if let Some(body_start) = conditional_body_start(self.bytes, start) {
self.conditional_comment = true;
self.conditional_depth += 1;
self.blank(start, body_start);
return;
}
self.blank_block_comment(start);
}
fn blank_block_comment(&mut self, start: usize) {
let mut depth = 1_usize;
let mut cursor = start + 2;
while cursor < self.bytes.len() {
if self.at(cursor) == b'/' && self.at(cursor + 1) == b'*' {
depth += 1;
cursor += 2;
} else if self.at(cursor) == b'*' && self.at(cursor + 1) == b'/' {
depth -= 1;
cursor += 2;
if depth == 0 {
self.erase(start, cursor);
return;
}
} else {
cursor += 1;
}
}
self.mark_unterminated(UnterminatedKind::BlockComment, start);
self.erase(start, self.bytes.len());
}
fn take_quoted(&mut self, start: usize, kind: UnterminatedKind) {
let delimiter = self.at(start);
let backslash_escapes = kind == UnterminatedKind::TextLiteral;
let mut cursor = start + 1;
while cursor < self.bytes.len() {
let byte = self.at(cursor);
let escaped = (backslash_escapes && byte == b'\\')
|| (byte == delimiter && self.at(cursor + 1) == delimiter);
if escaped {
cursor += 2;
} else if byte == delimiter {
self.erase(start, cursor + 1);
return;
} else {
cursor += 1;
}
}
self.mark_unterminated(kind, start);
self.erase(start, self.bytes.len());
}
fn finish(mut self) -> Stripped {
let end = self.bytes.len();
self.flush_kept(end);
Stripped {
text: self.out,
conditional_comment: self.conditional_comment,
unterminated: self.unterminated,
}
}
}
const fn starts_line_comment(next: u8) -> bool {
next == 0 || next == b' ' || next == b'\t' || next == b'\n' || next == b'\r'
}
fn conditional_body_start(bytes: &[u8], start: usize) -> Option<usize> {
let read = |index: usize| bytes.get(index).copied().unwrap_or(0);
let mut cursor = match read(start + 2) {
b'!' => start + 3,
b'M' | b'm' if read(start + 3) == b'!' => start + 4,
_ => return None,
};
while read(cursor).is_ascii_digit() {
cursor += 1;
}
Some(cursor)
}
#[cfg(test)]
mod tests {
use super::*;
fn text_of(sql: &str) -> String {
strip(sql).text().to_string()
}
#[test]
fn the_blanked_text_keeps_the_byte_length_of_the_input() {
for sql in [
"SELECT 1",
"SELECT 'olá' -- acento\nFROM t",
"SELECT /* ácido */ `coluna` FROM t",
"SELECT '\u{1f600}' FROM t",
"SELECT # fim",
] {
assert_eq!(text_of(sql).len(), sql.len(), "input {sql:?}");
}
}
#[test]
fn a_reserved_word_inside_a_literal_disappears_from_the_text() {
let stripped = text_of("SELECT 'DROP TABLE users' FROM t");
assert!(!stripped.contains("DROP"), "got {stripped:?}");
assert!(stripped.contains("SELECT"));
assert!(stripped.contains("FROM t"));
}
#[test]
fn a_doubled_quote_does_not_end_the_literal() {
let stripped = text_of("SELECT 'it''s DROP' , 1");
assert!(!stripped.contains("DROP"), "got {stripped:?}");
assert!(stripped.contains(", 1"));
}
#[test]
fn a_backslash_escaped_quote_does_not_end_the_literal() {
let stripped = text_of("SELECT 'a\\' DROP' , 1");
assert!(!stripped.contains("DROP"), "got {stripped:?}");
assert!(stripped.contains(", 1"));
}
#[test]
fn a_double_quoted_literal_is_blanked_like_a_single_quoted_one() {
let stripped = text_of("SELECT \"DROP TABLE\" , 1");
assert!(!stripped.contains("DROP"), "got {stripped:?}");
}
#[test]
fn a_backslash_does_not_escape_inside_a_quoted_identifier() {
let stripped = text_of("SELECT `a\\` , 1");
assert!(stripped.contains(", 1"), "got {stripped:?}");
assert!(strip("SELECT `a\\` , 1").unterminated().is_none());
}
#[test]
fn a_separator_inside_a_quoted_identifier_disappears_from_the_text() {
let stripped = text_of("SELECT * FROM `a;b`");
assert!(!stripped.contains(';'), "got {stripped:?}");
}
#[test]
fn a_line_comment_needs_a_space_after_the_two_dashes() {
assert!(text_of("SELECT 1-- DROP").contains("SELECT 1"));
assert!(!text_of("SELECT 1-- DROP").contains("DROP"));
assert!(
text_of("SELECT 1--2").contains("--2"),
"without a space the dashes are an operator"
);
}
#[test]
fn a_hash_comment_runs_to_the_end_of_the_line() {
let stripped = text_of("SELECT 1 # DROP TABLE t\nFROM u");
assert!(!stripped.contains("DROP"), "got {stripped:?}");
assert!(stripped.contains("FROM u"));
}
#[test]
fn a_block_comment_is_blanked_and_may_nest() {
let stripped = text_of("SELECT /* a /* b */ DROP */ 1");
assert!(!stripped.contains("DROP"), "got {stripped:?}");
assert!(stripped.contains("SELECT"));
assert!(stripped.contains('1'));
}
#[test]
fn a_comment_placed_mid_statement_does_not_hide_the_rest() {
let stripped = text_of("DELETE /* looks harmless */ FROM users");
assert!(stripped.contains("DELETE"));
assert!(stripped.contains("FROM users"));
}
#[test]
fn a_conditional_comment_is_kept_as_code() {
let stripped = text_of("SELECT /*!40001 SQL_NO_CACHE */ 1");
assert!(
stripped.contains("SQL_NO_CACHE"),
"the body is executable, got {stripped:?}"
);
assert!(!stripped.contains("/*"), "the introducer is punctuation");
assert!(!stripped.contains("*/"), "the terminator is punctuation");
assert!(strip("SELECT /*!40001 SQL_NO_CACHE */ 1").has_conditional_comment());
}
#[test]
fn a_server_specific_conditional_comment_is_kept_as_code() {
let stripped = text_of("SELECT /*M!100301 DROP */ 1");
assert!(stripped.contains("DROP"), "got {stripped:?}");
assert!(strip("SELECT /*M!100301 DROP */ 1").has_conditional_comment());
}
#[test]
fn a_literal_inside_a_conditional_comment_is_still_blanked() {
let stripped = text_of("SELECT /*! 'DROP' */ 1");
assert!(!stripped.contains("DROP"), "got {stripped:?}");
}
#[test]
fn a_conditional_comment_inside_a_plain_comment_stays_commented_out() {
let stripped = text_of("SELECT /* /*!40001 DROP */ */ 1");
assert!(!stripped.contains("DROP"), "got {stripped:?}");
assert!(!strip("SELECT /* /*!40001 DROP */ */ 1").has_conditional_comment());
}
#[test]
fn a_comment_marker_inside_a_literal_does_not_open_a_comment() {
let stripped = text_of("SELECT '/*' , 1 , '*/'");
assert!(stripped.contains(", 1 ,"), "got {stripped:?}");
}
#[test]
fn a_quote_inside_a_comment_does_not_open_a_literal() {
let stripped = text_of("SELECT /* it's here */ 1 , 2");
assert!(stripped.contains("1 , 2"), "got {stripped:?}");
}
#[test]
fn an_unterminated_literal_is_reported_and_refused() {
let stripped = strip("SELECT 'never closed");
let open = stripped.unterminated().expect("open region");
assert_eq!(open.kind, UnterminatedKind::TextLiteral);
assert_eq!(open.offset, 7);
assert!(stripped.refuse_if_unterminated().is_err());
}
#[test]
fn an_unterminated_block_comment_is_reported_and_refused() {
let stripped = strip("SELECT 1 /* never closed");
let open = stripped.unterminated().expect("open region");
assert_eq!(open.kind, UnterminatedKind::BlockComment);
assert!(stripped.refuse_if_unterminated().is_err());
}
#[test]
fn an_unterminated_quoted_identifier_is_reported() {
let stripped = strip("SELECT * FROM `never closed");
let open = stripped.unterminated().expect("open region");
assert_eq!(open.kind, UnterminatedKind::QuotedIdentifier);
}
#[test]
fn a_closed_statement_reports_no_open_region() {
assert!(strip("SELECT 'a' , `b` /* c */ -- d\n")
.refuse_if_unterminated()
.is_ok());
}
#[test]
fn an_empty_input_produces_an_empty_text() {
assert_eq!(text_of(""), "");
}
#[test]
fn expanding_a_conditional_comment_keeps_the_byte_length() {
for sql in [
"SELECT 1",
"SELECT /*!40001 SQL_NO_CACHE */ 1",
"SELECT /*M!100301 SQL_NO_CACHE */ 1",
"SELECT 'olá' /*M! ,2 */ FROM t",
"SELECT /* plain */ 1 -- tail\n",
] {
assert_eq!(
expand_conditional_comments(sql).len(),
sql.len(),
"input {sql:?}"
);
}
}
#[test]
fn both_conditional_introducers_leave_their_body_as_code() {
for sql in [
"SELECT 1 /*!100000 ,2 */",
"SELECT 1 /*M!100000 ,2 */",
"SELECT 1 /*m!100000 ,2 */",
"SELECT 1 /*M! ,2 */",
] {
let expanded = expand_conditional_comments(sql);
assert!(expanded.contains(",2"), "input {sql:?} gave {expanded:?}");
assert!(!expanded.contains("/*"), "input {sql:?}");
assert!(!expanded.contains("*/"), "input {sql:?}");
assert!(!expanded.contains('!'), "input {sql:?}");
assert!(
!expanded.contains("100000"),
"the version gate is punctuation, not code: {expanded:?}"
);
}
}
#[test]
fn the_parsing_scan_hands_back_data_and_commentary_untouched() {
for sql in [
"SELECT '/*M!100000 hidden */' AS note",
"SELECT `/*M!100000`",
"SELECT 1 /* /*M!100000 hidden */",
"SELECT 1 -- /*M!100000 hidden\n",
"SELECT 1 # /*M!100000 hidden",
] {
assert_eq!(expand_conditional_comments(sql), sql, "input {sql:?}");
}
}
#[test]
fn the_two_scans_agree_on_the_conditional_body_and_on_nothing_else() {
let sql = "SELECT 'x' /*M!100000 ,LOAD_FILE(1) */";
let blanked = text_of(sql);
let expanded = expand_conditional_comments(sql);
assert!(blanked.contains("LOAD_FILE"), "got {blanked:?}");
assert!(expanded.contains("LOAD_FILE"), "got {expanded:?}");
assert!(
!blanked.contains('x'),
"the literal is data to a comparison"
);
assert!(expanded.contains("'x'"), "the literal is code to a parser");
}
}