Skip to main content

linkrs_query/parser/parsing/ddl_parser/
drop_parser.rs

1use crate::parser::ast::stmt::*;
2use crate::parser::core::error::{ParseError, ParseErrorKind};
3use crate::parser::parsing::parse_context::ParseContext;
4use crate::parser::TokenKind;
5
6use super::consume_space_or_graph;
7use super::DdlParser;
8
9impl DdlParser {
10    pub fn parse_drop_statement(&mut self, ctx: &mut ParseContext) -> Result<Stmt, ParseError> {
11        let start_span = ctx.current_span();
12        ctx.expect_token(TokenKind::Drop)?;
13
14        if ctx.check_keyword("FULLTEXT") {
15            return crate::parser::parsing::fulltext_parser::parse_drop_fulltext_index_after_drop(
16                ctx,
17            );
18        }
19
20        if ctx.check_keyword("VECTOR") {
21            return crate::parser::parsing::vector_parser::parse_drop_vector_index_after_drop(ctx);
22        }
23
24        // `DROP SPACE` and `DROP GRAPH` are equivalent namespace operations.
25        if consume_space_or_graph(ctx)? {
26            let mut if_exists = false;
27            if ctx.match_token(TokenKind::If) {
28                ctx.expect_token(TokenKind::Exists)?;
29                if_exists = true;
30            }
31            let space_name = ctx.expect_dcl_name()?;
32            return Ok(Stmt::Drop(DropStmt {
33                span: start_span,
34                target: DropTarget::Space(space_name),
35                if_exists,
36            }));
37        } else if ctx.match_token(TokenKind::Tag) {
38            if ctx.check_token(TokenKind::Index) {
39                ctx.next_token();
40                let mut if_exists = false;
41                if ctx.match_token(TokenKind::If) {
42                    ctx.expect_token(TokenKind::Exists)?;
43                    if_exists = true;
44                }
45                let index_name = ctx.expect_identifier()?;
46                let space_name = if ctx.match_token(TokenKind::On) {
47                    Some(ctx.expect_identifier()?)
48                } else {
49                    None
50                };
51                return Ok(Stmt::Drop(DropStmt {
52                    span: start_span,
53                    target: DropTarget::TagIndex {
54                        space_name: space_name.unwrap_or_default(),
55                        index_name,
56                    },
57                    if_exists,
58                }));
59            } else {
60                let mut if_exists = false;
61                if ctx.match_token(TokenKind::If) {
62                    ctx.expect_token(TokenKind::Exists)?;
63                    if_exists = true;
64                }
65                let mut tag_names = vec![ctx.expect_identifier()?];
66                while ctx.match_token(TokenKind::Comma) {
67                    tag_names.push(ctx.expect_identifier()?);
68                }
69                return Ok(Stmt::Drop(DropStmt {
70                    span: start_span,
71                    target: DropTarget::Tags(tag_names),
72                    if_exists,
73                }));
74            }
75        } else if ctx.check_token(TokenKind::Edge) {
76            ctx.next_token();
77            if ctx.check_token(TokenKind::Index) {
78                ctx.next_token();
79                let mut if_exists = false;
80                if ctx.match_token(TokenKind::If) {
81                    ctx.expect_token(TokenKind::Exists)?;
82                    if_exists = true;
83                }
84                let index_name = ctx.expect_identifier()?;
85                let space_name = if ctx.match_token(TokenKind::On) {
86                    Some(ctx.expect_identifier()?)
87                } else {
88                    None
89                };
90                return Ok(Stmt::Drop(DropStmt {
91                    span: start_span,
92                    target: DropTarget::EdgeIndex {
93                        space_name: space_name.unwrap_or_default(),
94                        index_name,
95                    },
96                    if_exists,
97                }));
98            } else {
99                let mut if_exists = false;
100                if ctx.match_token(TokenKind::If) {
101                    ctx.expect_token(TokenKind::Exists)?;
102                    if_exists = true;
103                }
104                let mut edge_names = vec![ctx.expect_identifier()?];
105                while ctx.match_token(TokenKind::Comma) {
106                    edge_names.push(ctx.expect_identifier()?);
107                }
108                return Ok(Stmt::Drop(DropStmt {
109                    span: start_span,
110                    target: DropTarget::Edges(edge_names),
111                    if_exists,
112                }));
113            }
114        } else if ctx.match_token(TokenKind::Index) {
115            let mut if_exists = false;
116            if ctx.match_token(TokenKind::If) {
117                ctx.expect_token(TokenKind::Exists)?;
118                if_exists = true;
119            }
120            let index_name = ctx.expect_identifier()?;
121            let space_name = if ctx.match_token(TokenKind::On) {
122                Some(ctx.expect_identifier()?)
123            } else {
124                None
125            };
126            return Ok(Stmt::Drop(DropStmt {
127                span: start_span,
128                target: DropTarget::TagIndex {
129                    space_name: space_name.unwrap_or_default(),
130                    index_name,
131                },
132                if_exists,
133            }));
134        } else if ctx.match_token(TokenKind::User) {
135            let mut if_exists = false;
136            if ctx.match_token(TokenKind::If) {
137                ctx.expect_token(TokenKind::Exists)?;
138                if_exists = true;
139            }
140            let username = ctx.expect_dcl_name()?;
141
142            let end_span = ctx.current_span();
143            let span = ctx.merge_span(start_span.start, end_span.end);
144
145            return Ok(Stmt::DropUser(DropUserStmt {
146                span,
147                username,
148                if_exists,
149            }));
150        } else if ctx.match_token(TokenKind::Sequence) {
151            let mut if_exists = false;
152            if ctx.match_token(TokenKind::If) {
153                ctx.expect_token(TokenKind::Exists)?;
154                if_exists = true;
155            }
156            let seq_name = ctx.expect_identifier()?;
157            let end_span = ctx.current_span();
158            let span = ctx.merge_span(start_span.start, end_span.end);
159            return Ok(Stmt::Drop(DropStmt {
160                span,
161                target: DropTarget::Sequence(seq_name),
162                if_exists,
163            }));
164        } else if ctx.check_keyword("MACRO") {
165            ctx.consume_keyword("MACRO")?;
166            let mut if_exists = false;
167            if ctx.match_token(TokenKind::If) {
168                ctx.expect_token(TokenKind::Exists)?;
169                if_exists = true;
170            }
171            let name = ctx.expect_identifier()?;
172            let end_span = ctx.current_span();
173            let span = ctx.merge_span(start_span.start, end_span.end);
174            return Ok(Stmt::DropMacro(DropMacroStmt {
175                span,
176                name,
177                if_exists,
178            }));
179        } else if ctx.check_keyword("TYPE") {
180            ctx.consume_keyword("TYPE")?;
181            let mut if_exists = false;
182            if ctx.match_token(TokenKind::If) {
183                ctx.expect_token(TokenKind::Exists)?;
184                if_exists = true;
185            }
186            let name = ctx.expect_identifier()?;
187            let end_span = ctx.current_span();
188            let span = ctx.merge_span(start_span.start, end_span.end);
189            return Ok(Stmt::DropType(DropTypeStmt {
190                span,
191                name,
192                if_exists,
193            }));
194        } else {
195            return Err(ParseError::new(
196                ParseErrorKind::UnexpectedToken,
197                "Expected SPACE, GRAPH, TAG, EDGE, INDEX, SEQUENCE, MACRO, TYPE, or USER"
198                    .to_string(),
199                ctx.current_position(),
200            ));
201        }
202    }
203}