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 _ = self.parser.next_token();
27        let spstatement = self.parser.parse_insert().context(error::SyntaxSnafu)?;
28
29        match spstatement {
30            SpStatement::Insert { .. } => {
31                Ok(Statement::Insert(Box::new(Insert { inner: spstatement })))
32            }
33            unexp => error::UnsupportedSnafu {
34                keyword: unexp.to_string(),
35            }
36            .fail(),
37        }
38    }
39
40    pub(crate) fn parse_replace(&mut self) -> Result<Statement> {
41        let _ = self.parser.next_token();
42        let spstatement = self.parser.parse_insert().context(error::SyntaxSnafu)?;
43
44        match spstatement {
45            SpStatement::Insert(mut insert_stmt) => {
46                insert_stmt.replace_into = true;
47                Ok(Statement::Insert(Box::new(Insert {
48                    inner: SpStatement::Insert(insert_stmt),
49                })))
50            }
51            unexp => error::UnsupportedSnafu {
52                keyword: unexp.to_string(),
53            }
54            .fail(),
55        }
56    }
57}
58
59#[cfg(test)]
60mod tests {
61    use std::assert_matches::assert_matches;
62
63    use super::*;
64    use crate::dialect::GreptimeDbDialect;
65    use crate::parser::ParseOptions;
66
67    #[test]
68    pub fn test_parse_insert() {
69        let sql = r"INSERT INTO table_1 VALUES (
70            'test1',1,'true',
71            'test2',2,'false')
72         ";
73        let result =
74            ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}, ParseOptions::default())
75                .unwrap();
76        assert_eq!(1, result.len());
77        assert_matches!(result[0], Statement::Insert { .. })
78    }
79
80    #[test]
81    pub fn test_parse_invalid_insert() {
82        let sql = r"INSERT INTO table_1 VALUES ("; // intentionally a bad sql
83        let result =
84            ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}, ParseOptions::default());
85        assert!(result.is_err(), "result is: {result:?}");
86    }
87}