Skip to main content

sqlive/
sql.rs

1//! SQLite tokens used to inspect CREATE statements without matching comments or strings.
2
3use sqlite3_parser::ast::{As, Expr};
4
5pub use crate::token_scan::scan;
6
7/// A token of SQL text.
8#[derive(Clone, Debug, Eq, PartialEq)]
9pub enum Token {
10    /// An unquoted word, with ASCII letters lowercased.
11    Word(String),
12    /// A name in double quotes, backticks or brackets, without the quotes.
13    Quoted(String),
14    /// A string in single quotes, without the quotes.
15    String(String),
16    /// A punctuation character.
17    Symbol(char),
18}
19
20impl Token {
21    /// Whether this is the unquoted word `word`, in any letter case.
22    pub fn word(&self, word: &str) -> bool {
23        matches!(self, Self::Word(value) if value.eq_ignore_ascii_case(word))
24    }
25}
26
27/// The tokens of `sql`, without its comments and whitespace.
28pub 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
33/// The tokens that the texts `scanned` hold. Quoted contents keep their case,
34/// unquoted words fold ASCII case, and numeric and operator punctuation stay
35/// separate.
36fn 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
72/// Quote an identifier, never a value or SQL fragment.
73pub fn identifier(name: &str) -> String {
74    format!("\"{}\"", name.replace('"', "\"\""))
75}
76
77/// A name the parser read, without the quotes SQLite allows around it:
78/// double quotes, backticks, brackets, or single quotes where SQLite reads
79/// a string as a name.
80pub 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
89/// `expr` without the parentheses around it, each holding one expression.
90pub 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
98/// The name, lowercase, SQLite gives a result column that `alias` names.
99pub fn alias_name(alias: &As<'_>) -> String {
100    let (As::As(alias) | As::Elided(alias)) = alias;
101    name(alias.0).to_ascii_lowercase()
102}
103
104/// The names SQLite gives the columns of a VALUES list whose first row has
105/// `width` expressions: column1, column2, and so on.
106pub 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;