use regex::Regex;
use std::sync::OnceLock;
pub fn sql_requires_query_annotation(sql: &str) -> bool {
!is_transaction_command(sql) && !has_leading_query_annotation(sql)
}
#[doc(hidden)]
pub fn has_leading_query_annotation(sql: &str) -> bool {
annotation_re().is_match(sql)
}
pub fn leading_query_annotation(sql: &str) -> Option<&str> {
let comment = annotation_re().find(sql)?.as_str().trim();
Some(comment[2..comment.len() - 2].trim())
}
fn is_transaction_command(sql: &str) -> bool {
transaction_re().is_match(sql)
}
#[doc(hidden)]
pub fn transaction_command_prefix_end(sql: &str) -> Option<usize> {
transaction_re().find(sql).map(|matched| matched.end())
}
fn annotation_re() -> &'static Regex {
static RE: OnceLock<Regex> = OnceLock::new();
RE.get_or_init(|| Regex::new(r"(?s)^\s*/\*\s*\S.*?\*/").expect("query annotation regex"))
}
fn transaction_re() -> &'static Regex {
static RE: OnceLock<Regex> = OnceLock::new();
RE.get_or_init(|| {
Regex::new(r"(?is)^\s*(?:/\*.*?\*/\s*)?(?:BEGIN|COMMIT|ROLLBACK)\b")
.expect("transaction command regex")
})
}
#[cfg(all(test, any(nm_shard_all, nm_shard_2)))]
mod tests;