sql/statements/
cursor.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;
16
17use serde::Serialize;
18use sqlparser::ast::ObjectName;
19use sqlparser_derive::{Visit, VisitMut};
20
21use crate::statements::query::Query;
22
23/// Represents a DECLARE CURSOR statement
24///
25/// This statement will carry a SQL query
26#[derive(Debug, Clone, PartialEq, Eq, Visit, VisitMut, Serialize)]
27pub struct DeclareCursor {
28    pub cursor_name: ObjectName,
29    pub query: Box<Query>,
30}
31
32impl Display for DeclareCursor {
33    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
34        write!(f, "DECLARE {} CURSOR FOR {}", self.cursor_name, self.query)
35    }
36}
37
38/// Represents a FETCH FROM cursor statement
39#[derive(Debug, Clone, PartialEq, Eq, Visit, VisitMut, Serialize)]
40pub struct FetchCursor {
41    pub cursor_name: ObjectName,
42    pub fetch_size: u64,
43}
44
45impl Display for FetchCursor {
46    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
47        write!(f, "FETCH {} FROM {}", self.fetch_size, self.cursor_name)
48    }
49}
50
51/// Represents a CLOSE cursor statement
52#[derive(Debug, Clone, PartialEq, Eq, Visit, VisitMut, Serialize)]
53pub struct CloseCursor {
54    pub cursor_name: ObjectName,
55}
56
57impl Display for CloseCursor {
58    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
59        write!(f, "CLOSE {}", self.cursor_name)
60    }
61}