Skip to main content

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