1use sqlite3_parser::ast::{As, Expr};
4
5pub use crate::token_scan::scan;
6
7#[derive(Clone, Debug, Eq, PartialEq)]
9pub enum Token {
10 Word(String),
12 Quoted(String),
14 String(String),
16 Symbol(char),
18}
19
20impl Token {
21 pub fn word(&self, word: &str) -> bool {
23 matches!(self, Self::Word(value) if value.eq_ignore_ascii_case(word))
24 }
25}
26
27pub fn tokens(sql: &str) -> rusqlite::Result<Vec<Token>> {
29 let scanned = crate::token_scan::scan(sql)?;
30 Ok(normalized(scanned.iter().map(|(_, range)| &sql[range.clone()])))
31}
32
33fn normalized<'a>(scanned: impl Iterator<Item = &'a str>) -> Vec<Token> {
37 let mut tokens = Vec::new();
38 for mut text in scanned {
39 if text.starts_with(['x', 'X']) && text.as_bytes().get(1) == Some(&b'\'') {
40 tokens.push(Token::Word("x".into()));
41 text = &text[1..];
42 }
43 match text.as_bytes()[0] {
44 quote @ (b'\'' | b'"' | b'`' | b'[') => {
45 let value = name(text);
46 tokens.push(if quote == b'\'' {
47 Token::String(value)
48 } else {
49 Token::Quoted(value)
50 });
51 }
52 _ => {
53 let mut word = String::new();
54 for ch in text.chars().chain(std::iter::once(' ')) {
55 if ch.is_alphanumeric() || ch == '_' || ch == '$' {
56 word.push(ch.to_ascii_lowercase());
57 } else {
58 if !word.is_empty() {
59 tokens.push(Token::Word(std::mem::take(&mut word)));
60 }
61 if !ch.is_whitespace() {
62 tokens.push(Token::Symbol(ch));
63 }
64 }
65 }
66 }
67 }
68 }
69 tokens
70}
71
72pub fn identifier(name: &str) -> String {
74 format!("\"{}\"", name.replace('"', "\"\""))
75}
76
77pub fn name(raw: &str) -> String {
81 match raw.as_bytes()[0] {
82 b'[' => raw[1..raw.len() - 1].to_owned(),
83 quote @ (b'\'' | b'"' | b'`') => raw[1..raw.len() - 1]
84 .replace(&format!("{0}{0}", quote as char), &(quote as char).to_string()),
85 _ => raw.to_owned(),
86 }
87}
88
89pub fn unparenthesized<'a, 'b>(mut expr: &'a Expr<'b>) -> &'a Expr<'b> {
91 while let Expr::Parenthesized(items) = expr {
92 let [item] = items.as_slice() else { break };
93 expr = item;
94 }
95 expr
96}
97
98pub fn alias_name(alias: &As<'_>) -> String {
100 let (As::As(alias) | As::Elided(alias)) = alias;
101 name(alias.0).to_ascii_lowercase()
102}
103
104pub fn values_names(width: usize) -> Vec<String> {
107 (1..=width).map(|position| format!("column{position}")).collect()
108}
109
110#[cfg(test)]
111#[path = "sql_tests.rs"]
112mod tests;