sql/parsers/
prepare_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::keywords::Keyword;
17use sqlparser::tokenizer::Token;
18
19use crate::error::{Result, SyntaxSnafu};
20use crate::parser::ParserContext;
21
22impl ParserContext<'_> {
23    /// Parses MySQL style 'PREPARE stmt_name FROM stmt' into a (stmt_name, stmt) tuple.
24    /// Only use for MySQL. for PostgreSQL, use `sqlparser::parser::Parser::parse_prepare` instead.
25    pub(crate) fn parse_mysql_prepare(&mut self) -> Result<(String, String)> {
26        self.parser
27            .expect_keyword(Keyword::PREPARE)
28            .context(SyntaxSnafu)?;
29        let stmt_name = self.parser.parse_identifier().context(SyntaxSnafu)?;
30        self.parser
31            .expect_keyword(Keyword::FROM)
32            .context(SyntaxSnafu)?;
33        let next_token = self.parser.peek_token();
34        let stmt = match next_token.token {
35            Token::SingleQuotedString(s) | Token::DoubleQuotedString(s) => {
36                let _ = self.parser.next_token();
37                s
38            }
39            _ => self
40                .parser
41                .expected("string literal", next_token)
42                .context(SyntaxSnafu)?,
43        };
44        Ok((stmt_name.value, stmt))
45    }
46}