sql/statements/
explain.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 std::fmt::{Display, Formatter};
16
17use serde::Serialize;
18use sqlparser::ast::{AnalyzeFormat, Statement as SpStatement};
19use sqlparser_derive::{Visit, VisitMut};
20
21use crate::error::Error;
22
23/// Explain statement.
24#[derive(Debug, Clone, PartialEq, Eq, Visit, VisitMut, Serialize)]
25pub struct Explain {
26    pub inner: SpStatement,
27}
28
29impl Explain {
30    pub fn format(&self) -> Option<AnalyzeFormat> {
31        match self.inner {
32            SpStatement::Explain { format, .. } => format,
33            _ => None,
34        }
35    }
36}
37
38impl TryFrom<SpStatement> for Explain {
39    type Error = Error;
40
41    fn try_from(value: SpStatement) -> Result<Self, Self::Error> {
42        Ok(Explain { inner: value })
43    }
44}
45
46impl Display for Explain {
47    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
48        write!(f, "{}", self.inner)
49    }
50}