Skip to content

Commit

Permalink
Support DROP TABLE statement
Browse files Browse the repository at this point in the history
  • Loading branch information
PakhomovAlexander committed Oct 22, 2024
1 parent b38a3ee commit a9ee074
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
4 changes: 3 additions & 1 deletion agenda.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@

Today we are going to:
- [x] WHERE statement support in parser
- [x] Maybe CREATE TABLE
- [ ] DROP TABLE
- [ ] INSERT INTO support
- [ ] Function call support
- [ ] Maybe CREATE TABLE and DROP TABLE and INSERT INTO support



Expand Down
30 changes: 30 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ pub enum Op {
Where,

CreateTable,
DropTable,

ColumnDefinition,
}
Expand Down Expand Up @@ -132,6 +133,7 @@ impl<'a> Parser<'a> {
}
Some(Ok(Token::Select)) => self.parse_select(min_bp),
Some(Ok(Token::Create)) => self.parse_create(min_bp),
Some(Ok(Token::Drop)) => self.parse_drop(min_bp),
s => panic!("Unexpected token: {:?}", s),
};

Expand Down Expand Up @@ -179,6 +181,26 @@ impl<'a> Parser<'a> {
lhs
}

fn parse_drop(&mut self, min_bp: u8) -> Node {
match self.lexer.next() {
Some(Ok(Token::Table)) => self.parse_drop_table(min_bp),
s => panic!("Unexpected token: {:?}", s),
}
}

fn parse_drop_table(&mut self, _min_bp: u8) -> Node {
let lhs = match self.lexer.next() {
Some(Ok(Token::Identifier {
first_name,
second_name: None,
third_name: None,
})) => Literal::identifier(first_name),
s => panic!("Unexpected token: {:?}", s),
};

Node::Prefix(Op::DropTable, vec![Node::Leaf(lhs)])
}

fn parse_create(&mut self, min_bp: u8) -> Node {
match self.lexer.next() {
Some(Ok(Token::Table)) => self.parse_create_table(min_bp),
Expand Down Expand Up @@ -671,4 +693,12 @@ mod tests {
)
);
}

#[test]
fn drop_table() {
assert_eq!(
parse("drop table table1"),
prefix(Op::DropTable, leaf(id("table1")),)
);
}
}

0 comments on commit a9ee074

Please sign in to comment.