Thanks to visit codestin.com
Credit goes to docs.rs

sqlparser/dialect/
sqlite.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18#[cfg(not(feature = "std"))]
19use alloc::boxed::Box;
20
21use crate::ast::BinaryOperator;
22use crate::ast::{Expr, Statement};
23use crate::dialect::Dialect;
24use crate::keywords::Keyword;
25use crate::parser::{Parser, ParserError};
26
27/// A [`Dialect`] for [SQLite](https://www.sqlite.org)
28///
29/// This dialect allows columns in a
30/// [`CREATE TABLE`](https://sqlite.org/lang_createtable.html) statement with no
31/// type specified, as in `CREATE TABLE t1 (a)`. In the AST, these columns will
32/// have the data type [`Unspecified`](crate::ast::DataType::Unspecified).
33#[derive(Debug)]
34pub struct SQLiteDialect {}
35
36impl Dialect for SQLiteDialect {
37    // see https://www.sqlite.org/lang_keywords.html
38    // parse `...`, [...] and "..." as identifier
39    // TODO: support depending on the context tread '...' as identifier too.
40    fn is_delimited_identifier_start(&self, ch: char) -> bool {
41        ch == '`' || ch == '"' || ch == '['
42    }
43
44    fn identifier_quote_style(&self, _identifier: &str) -> Option<char> {
45        Some('`')
46    }
47
48    fn is_identifier_start(&self, ch: char) -> bool {
49        // See https://www.sqlite.org/draft/tokenreq.html
50        ch.is_ascii_lowercase()
51            || ch.is_ascii_uppercase()
52            || ch == '_'
53            || ('\u{007f}'..='\u{ffff}').contains(&ch)
54    }
55
56    fn supports_filter_during_aggregation(&self) -> bool {
57        true
58    }
59
60    fn supports_start_transaction_modifier(&self) -> bool {
61        true
62    }
63
64    fn is_identifier_part(&self, ch: char) -> bool {
65        self.is_identifier_start(ch) || ch.is_ascii_digit()
66    }
67
68    fn parse_statement(&self, parser: &mut Parser) -> Option<Result<Statement, ParserError>> {
69        if parser.parse_keyword(Keyword::REPLACE) {
70            parser.prev_token();
71            Some(parser.parse_insert())
72        } else {
73            None
74        }
75    }
76
77    fn parse_infix(
78        &self,
79        parser: &mut crate::parser::Parser,
80        expr: &crate::ast::Expr,
81        _precedence: u8,
82    ) -> Option<Result<crate::ast::Expr, ParserError>> {
83        // Parse MATCH and REGEXP as operators
84        // See <https://www.sqlite.org/lang_expr.html#the_like_glob_regexp_match_and_extract_operators>
85        for (keyword, op) in [
86            (Keyword::REGEXP, BinaryOperator::Regexp),
87            (Keyword::MATCH, BinaryOperator::Match),
88        ] {
89            if parser.parse_keyword(keyword) {
90                let left = Box::new(expr.clone());
91                let right = Box::new(parser.parse_expr().unwrap());
92                return Some(Ok(Expr::BinaryOp { left, op, right }));
93            }
94        }
95        None
96    }
97
98    fn supports_in_empty_list(&self) -> bool {
99        true
100    }
101
102    fn supports_limit_comma(&self) -> bool {
103        true
104    }
105
106    fn supports_asc_desc_in_column_definition(&self) -> bool {
107        true
108    }
109
110    fn supports_dollar_placeholder(&self) -> bool {
111        true
112    }
113}