Skip to content

Commit

Permalink
[#69] commit 파싱 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
myyrakle committed Mar 18, 2024
1 parent 87917e4 commit ea4e53d
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/ast/tcl/commit.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
use crate::ast::{SQLStatement, TCLStatement};

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CommitQuery {}

impl From<CommitQuery> for SQLStatement {
fn from(value: CommitQuery) -> SQLStatement {
SQLStatement::TCL(TCLStatement::Commit(value))
}
}
14 changes: 14 additions & 0 deletions src/parser/implements/tcl/commit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use std::error::Error;

use crate::ast::tcl::CommitQuery;
use crate::ast::SQLStatement;
use crate::parser::predule::{Parser, ParserContext};

impl Parser {
pub(crate) fn parse_commit_query(
&mut self,
_context: ParserContext,
) -> Result<SQLStatement, Box<dyn Error + Send>> {
Ok(CommitQuery {}.into())
}
}
1 change: 1 addition & 0 deletions src/parser/implements/tcl/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod begin;
pub mod commit;
4 changes: 4 additions & 0 deletions src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ impl Parser {
let query = self.parse_begin_query(context.clone())?;
statements.push(query);
}
Token::Commit => {
let query = self.parse_commit_query(context.clone())?;
statements.push(query);
}
_ => {
break;
}
Expand Down
2 changes: 1 addition & 1 deletion src/parser/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ pub(crate) mod update;

pub(crate) mod other;

pub(crate) mod begin_transaction;
pub(crate) mod tcl;
40 changes: 39 additions & 1 deletion src/parser/test/begin_transaction.rs → src/parser/test/tcl.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#![cfg(test)]

use crate::{
ast::{tcl::BeginTransactionQuery, SQLStatement},
ast::{
tcl::{BeginTransactionQuery, CommitQuery},
SQLStatement,
},
parser::predule::{Parser, ParserContext},
};

Expand Down Expand Up @@ -53,3 +56,38 @@ pub fn begin_transaction() {
assert_eq!(result.unwrap(), vec![tc.expected], "{}", tc.name);
}
}

#[test]
pub fn commit() {
struct TestCase {
name: String,
input: String,
expected: SQLStatement,
want_err: bool,
}

let test_cases = vec![TestCase {
name: "정상적인 Commit 명령".to_owned(),
input: "COMMIT;".to_owned(),
expected: CommitQuery {}.into(),
want_err: false,
}];

for tc in test_cases {
let mut parser = Parser::new(tc.input).unwrap();

let result = parser.parse(ParserContext::default());

if tc.want_err {
assert!(
result.is_err(),
"{} - expected error, got {:?}",
tc.name,
result
);
continue;
}

assert_eq!(result.unwrap(), vec![tc.expected], "{}", tc.name);
}
}

0 comments on commit ea4e53d

Please sign in to comment.