sql/parsers/
insert_parser.rs

1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use snafu::ResultExt;
16use sqlparser::ast::Statement as SpStatement;
17
18use crate::error::{self, Result};
19use crate::parser::ParserContext;
20use crate::statements::insert::Insert;
21use crate::statements::statement::Statement;
22
23/// INSERT/REPLACE statement parser implementation
24impl ParserContext<'_> {
25    pub(crate) fn parse_insert(&mut self) -> Result<Statement> {
26        let token = self.parser.next_token();
27        let spstatement = self
28            .parser
29            .parse_insert(token)
30            .context(error::SyntaxSnafu)?;
31
32        match spstatement {
33            SpStatement::Insert { .. } => {
34                Ok(Statement::Insert(Box::new(Insert { inner: spstatement })))
35            }
36            unexp => error::UnsupportedSnafu {
37                keyword: unexp.to_string(),
38            }
39            .fail(),
40        }
41    }
42
43    pub(crate) fn parse_replace(&mut self) -> Result<Statement> {
44        let token = self.parser.next_token();
45        let spstatement = self
46            .parser
47            .parse_insert(token)
48            .context(error::SyntaxSnafu)?;
49
50        match spstatement {
51            SpStatement::Insert(mut insert_stmt) => {
52                insert_stmt.replace_into = true;
53                Ok(Statement::Insert(Box::new(Insert {
54                    inner: SpStatement::Insert(insert_stmt),
55                })))
56            }
57            unexp => error::UnsupportedSnafu {
58                keyword: unexp.to_string(),
59            }
60            .fail(),
61        }
62    }
63}
64
65#[cfg(test)]
66mod tests {
67    use std::assert_matches::assert_matches;
68
69    use super::*;
70    use crate::dialect::GreptimeDbDialect;
71    use crate::parser::ParseOptions;
72
73    #[test]
74    pub fn test_parse_insert() {
75        let sql = r"INSERT INTO table_1 VALUES (
76            'test1',1,'true',
77            'test2',2,'false')
78         ";
79        let result =
80            ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}, ParseOptions::default())
81                .unwrap();
82        assert_eq!(1, result.len());
83        assert_matches!(result[0], Statement::Insert { .. })
84    }
85
86    #[test]
87    pub fn test_parse_invalid_insert() {
88        let sql = r"INSERT INTO table_1 VALUES ("; // intentionally a bad sql
89        let result =
90            ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}, ParseOptions::default());
91        assert!(result.is_err(), "result is: {result:?}");
92    }
93}