Skip to content

Commit

Permalink
[test] add test for parser
Browse files Browse the repository at this point in the history
Signed-off-by: reilly <tang.ruilin@foxmail.com>
  • Loading branch information
Tangruilin committed Oct 6, 2023
1 parent 2ebc9c3 commit 41d3519
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
22 changes: 19 additions & 3 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ use std::collections::BTreeMap;
use stmt::Statement;
use token::Token;

use self::stmt::{DropTableStmt, SetStmt, SetVariableType, TransactionIsolationLevel, UpdateStmt};
use self::stmt::{
DropTableStmt, ExplainStmt, SetStmt, SetVariableType, TransactionIsolationLevel, UpdateStmt,
};
use self::{
column::Column,
operator::Precedence,
Expand Down Expand Up @@ -415,6 +417,7 @@ impl Parser {
fn parse_delete_stmt(&mut self) -> Result<Statement> {
self.next_expected_keyword(Keyword::From)?;
let table = self.next_ident()?;
self.next_token();
Ok(Statement::Delete(DeleteTableStmt {
table,
r#where: self.parse_clause_where()?,
Expand Down Expand Up @@ -710,6 +713,7 @@ impl Parser {
}

fn parse_drop_stmt(&mut self) -> Result<Statement> {
self.next_expected_keyword(Keyword::Table)?;
Ok(Statement::DropTable(DropTableStmt {
table: self.next_ident()?,
}))
Expand Down Expand Up @@ -1128,6 +1132,7 @@ impl Parser {
loop {
let column = self.next_ident()?;
self.next_expected_token(Token::Equal)?;
self.next_token();
let expr = self.parse_expression(Precedence::Lowest)?;
if set.contains_key(&column) {
return Err(Error::OtherErr(fmt_err!(
Expand All @@ -1136,7 +1141,7 @@ impl Parser {
)));
}
set.insert(column, expr);
if self.peek_token == Token::Comma {
if self.peek_token != Token::Comma {
self.next_token();
break;
}
Expand All @@ -1149,7 +1154,10 @@ impl Parser {
}

fn parse_explain_stmt(&mut self) -> Result<Statement> {
unimplemented!()
self.next_token();
Ok(Statement::Explain(ExplainStmt {
statement: Box::new(self.parse_stmt()?),
}))
}

fn next_token(&mut self) -> &Token {
Expand Down Expand Up @@ -1692,6 +1700,14 @@ pub mod test {
}

test_parser! {
explain_wrong_sql: "explain nothing;" => Err(Error::ParseErr("src/parser/mod.rs:86 unexpected token: Token: Ident: nothing".to_owned())),
explain_base_sql: "explain drop table person" => Ok(Statement::Explain(ExplainStmt{ statement: Box::new(Statement::DropTable(DropTableStmt { table: "person".to_owned() }))})),
update_table_base: "update person set name = 'tangruilin' where id = 1;" => Ok(Statement::Update(UpdateStmt { table: "person".to_owned(), set: BTreeMap::from([
("name".to_owned(), Some(Expression::Literal(Literal::String("tangruilin".to_owned())))),
]), r#where: Some(Expression::Operation(Operation::Equal(Box::new(Expression::Field(None, "id".to_owned())), Box::new(Expression::Literal(Literal::Int(1))))))})),
drop_table_base: "drop table person;" => Ok(Statement::DropTable(DropTableStmt { table: "person".to_owned() })),
delete_table_base: "delete from person where id = 1;" => Ok(Statement::Delete(DeleteTableStmt { table: "person".to_owned(), r#where: Some(Expression::Operation(Operation::Equal(Box::new(Expression::Field(None, "id".to_owned())), Box::new(Expression::Literal(Literal::Int(1)))))) })),
delete_table_without_where: "delete from person;" => Ok(Statement::Delete(DeleteTableStmt { table: "person".to_owned(), r#where: None })),
insert_table_base: "insert into person (id, name, age) values (1, 'tangruilin', 14)" => Ok(Statement::Insert(InsertStmt {
table: "person".to_owned(),
columns: Some(["id".to_owned(), "name".to_owned(), "age".to_owned()].to_vec()),
Expand Down
6 changes: 4 additions & 2 deletions src/parser/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,10 @@ impl std::fmt::Display for BeginStmt {
}
}

#[derive(Eq, PartialEq, Debug)]
pub struct ExplainStmt {}
#[derive(PartialEq, Debug)]
pub struct ExplainStmt {
pub statement: Box<Statement>,
}
#[derive(PartialEq, Debug)]
pub struct CreateTableStmt {
pub columns: Vec<Column>,
Expand Down

0 comments on commit 41d3519

Please sign in to comment.