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
18use crate::ast::Statement;
19use crate::dialect::Dialect;
20use crate::keywords::Keyword;
21use crate::parser::{Parser, ParserError};
22
23/// A [`Dialect`] for [SQLite](https://www.sqlite.org)
24///
25/// This dialect allows columns in a
26/// [`CREATE TABLE`](https://sqlite.org/lang_createtable.html) statement with no
27/// type specified, as in `CREATE TABLE t1 (a)`. In the AST, these columns will
28/// have the data type [`Unspecified`](crate::ast::DataType::Unspecified).
29#[derive(Debug)]
30pub struct SQLiteDialect {}
31
32impl Dialect for SQLiteDialect {
33    // see https://www.sqlite.org/lang_keywords.html
34    // parse `...`, [...] and "..." as identifier
35    // TODO: support depending on the context tread '...' as identifier too.
36    fn is_delimited_identifier_start(&self, ch: char) -> bool {
37        ch == '`' || ch == '"' || ch == '['
38    }
39
40    fn identifier_quote_style(&self, _identifier: &str) -> Option<char> {
41        Some('`')
42    }
43
44    fn is_identifier_start(&self, ch: char) -> bool {
45        // See https://www.sqlite.org/draft/tokenreq.html
46        ch.is_ascii_lowercase()
47            || ch.is_ascii_uppercase()
48            || ch == '_'
49            || ('\u{007f}'..='\u{ffff}').contains(&ch)
50    }
51
52    fn supports_filter_during_aggregation(&self) -> bool {
53        true
54    }
55
56    fn supports_start_transaction_modifier(&self) -> bool {
57        true
58    }
59
60    fn is_identifier_part(&self, ch: char) -> bool {
61        self.is_identifier_start(ch) || ch.is_ascii_digit()
62    }
63
64    fn parse_statement(&self, parser: &mut Parser) -> Option<Result<Statement, ParserError>> {
65        if parser.parse_keyword(Keyword::REPLACE) {
66            parser.prev_token();
67            Some(parser.parse_insert())
68        } else {
69            None
70        }
71    }
72
73    fn supports_in_empty_list(&self) -> bool {
74        true
75    }
76
77    fn supports_limit_comma(&self) -> bool {
78        true
79    }
80
81    fn supports_asc_desc_in_column_definition(&self) -> bool {
82        true
83    }
84
85    fn supports_dollar_placeholder(&self) -> bool {
86        true
87    }
88}