-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[#69] 트랜잭션 명령어 파서 구현
- Loading branch information
Showing
14 changed files
with
241 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 BeginTransactionQuery {} | ||
|
||
impl From<BeginTransactionQuery> for SQLStatement { | ||
fn from(value: BeginTransactionQuery) -> SQLStatement { | ||
SQLStatement::TCL(TCLStatement::BeginTransaction(value)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 RollbackQuery {} | ||
|
||
impl From<RollbackQuery> for SQLStatement { | ||
fn from(value: RollbackQuery) -> SQLStatement { | ||
SQLStatement::TCL(TCLStatement::Rollback(value)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ pub mod ddl; | |
pub mod debug; | ||
pub mod dml; | ||
pub mod other; | ||
pub mod tcl; | ||
pub mod utils; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
use std::error::Error; | ||
|
||
use crate::ast::tcl::BeginTransactionQuery; | ||
use crate::ast::SQLStatement; | ||
use crate::errors::predule::ParsingError; | ||
use crate::lexer::tokens::Token; | ||
use crate::parser::predule::{Parser, ParserContext}; | ||
|
||
impl Parser { | ||
pub(crate) fn parse_begin_query( | ||
&mut self, | ||
_context: ParserContext, | ||
) -> Result<SQLStatement, Box<dyn Error + Send>> { | ||
if !self.has_next_token() { | ||
return Err(ParsingError::boxed("E2001 need more tokens")); | ||
} | ||
|
||
let current_token = self.get_next_token(); | ||
|
||
if current_token != Token::Transaction { | ||
return Err(ParsingError::boxed("E2002 Expected BEGIN")); | ||
} | ||
|
||
Ok(BeginTransactionQuery {}.into()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub mod begin; | ||
pub mod commit; | ||
pub mod rollback; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
use std::error::Error; | ||
|
||
use crate::ast::tcl::RollbackQuery; | ||
use crate::ast::SQLStatement; | ||
use crate::parser::predule::{Parser, ParserContext}; | ||
|
||
impl Parser { | ||
pub(crate) fn parse_rollback_query( | ||
&mut self, | ||
_context: ParserContext, | ||
) -> Result<SQLStatement, Box<dyn Error + Send>> { | ||
Ok(RollbackQuery {}.into()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,5 @@ pub(crate) mod select; | |
pub(crate) mod update; | ||
|
||
pub(crate) mod other; | ||
|
||
pub(crate) mod tcl; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
#![cfg(test)] | ||
|
||
use crate::{ | ||
ast::{ | ||
tcl::{BeginTransactionQuery, CommitQuery, RollbackQuery}, | ||
SQLStatement, | ||
}, | ||
parser::predule::{Parser, ParserContext}, | ||
}; | ||
|
||
#[test] | ||
pub fn begin_transaction() { | ||
struct TestCase { | ||
name: String, | ||
input: String, | ||
expected: SQLStatement, | ||
want_err: bool, | ||
} | ||
|
||
let test_cases = vec![ | ||
TestCase { | ||
name: "정상적인 트랜잭션 시작".to_owned(), | ||
input: "BEGIN TRANSACTION;".to_owned(), | ||
expected: BeginTransactionQuery {}.into(), | ||
want_err: false, | ||
}, | ||
TestCase { | ||
name: "begin만 있는 경우".to_owned(), | ||
input: "BEGIN;".to_owned(), | ||
expected: Default::default(), | ||
want_err: true, | ||
}, | ||
TestCase { | ||
name: "begin 이후에 기대하지 않은 입력이 있는 경우".to_owned(), | ||
input: "BEGIN TRANSITION;".to_owned(), | ||
expected: Default::default(), | ||
want_err: true, | ||
}, | ||
]; | ||
|
||
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); | ||
} | ||
} | ||
|
||
#[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); | ||
} | ||
} | ||
|
||
#[test] | ||
pub fn rollback() { | ||
struct TestCase { | ||
name: String, | ||
input: String, | ||
expected: SQLStatement, | ||
want_err: bool, | ||
} | ||
|
||
let test_cases = vec![TestCase { | ||
name: "정상적인 ROLLBACK 명령".to_owned(), | ||
input: "ROLLBACK;".to_owned(), | ||
expected: RollbackQuery {}.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); | ||
} | ||
} |