refactor(insert): separate row struct
This commit is contained in:
parent
7ccefac553
commit
db7db63fe3
5 changed files with 44 additions and 27 deletions
|
|
@ -112,16 +112,21 @@ impl From<ScanError> for CommandParseError {
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::{
|
||||
command::Command, meta_commands::MetaCommand, parser::parse, statements::Statement,
|
||||
command::Command,
|
||||
meta_commands::MetaCommand,
|
||||
parser::parse,
|
||||
statements::{Row, Statement},
|
||||
};
|
||||
use insta::{assert_debug_snapshot, assert_snapshot};
|
||||
|
||||
#[test]
|
||||
fn test_execute_insert_statement() {
|
||||
let statement: Command = Statement::Insert {
|
||||
id: 45,
|
||||
username: String::from("user"),
|
||||
email: String::from("user@example.org"),
|
||||
row: Row {
|
||||
id: 45,
|
||||
username: String::from("user"),
|
||||
email: String::from("user@example.org"),
|
||||
},
|
||||
}
|
||||
.into();
|
||||
let result = statement.execute().display();
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ use std::collections::VecDeque;
|
|||
|
||||
use crate::{
|
||||
command::{Command, CommandParseError, ExpectedToken},
|
||||
statements::Statement,
|
||||
statements::{Row, Statement},
|
||||
tokens::{Location, Token, TokenData, tokenize},
|
||||
};
|
||||
|
||||
|
|
@ -230,9 +230,11 @@ fn parse_insert_command(
|
|||
expect_semicolon(tokens)?;
|
||||
|
||||
Ok(Command::Statement(Statement::Insert {
|
||||
id,
|
||||
username,
|
||||
email,
|
||||
row: Row {
|
||||
id,
|
||||
username,
|
||||
email,
|
||||
},
|
||||
}))
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,14 +1,16 @@
|
|||
---
|
||||
source: src/parser.rs
|
||||
expression: "parse(file.clone(),\nString::from(r#\"insert 1 \"username\" \"email@example.com\"\"#))"
|
||||
expression: "parse(file.clone(),\nString::from(r#\"insert 1 \"username\" \"email@example.com\";\"#))"
|
||||
---
|
||||
Ok(
|
||||
[
|
||||
Statement(
|
||||
Insert {
|
||||
id: 1,
|
||||
username: "username",
|
||||
email: "email@example.com",
|
||||
row: Row {
|
||||
id: 1,
|
||||
username: "username",
|
||||
email: "email@example.com",
|
||||
},
|
||||
},
|
||||
),
|
||||
],
|
||||
|
|
|
|||
|
|
@ -9,9 +9,11 @@ Ok(
|
|||
),
|
||||
Statement(
|
||||
Insert {
|
||||
id: 1,
|
||||
username: "user",
|
||||
email: "email@test.com",
|
||||
row: Row {
|
||||
id: 1,
|
||||
username: "user",
|
||||
email: "email@test.com",
|
||||
},
|
||||
},
|
||||
),
|
||||
Statement(
|
||||
|
|
|
|||
|
|
@ -1,10 +1,13 @@
|
|||
#[derive(Debug)]
|
||||
pub struct Row {
|
||||
pub id: i64,
|
||||
pub username: String,
|
||||
pub email: String,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Statement {
|
||||
Insert {
|
||||
id: i64,
|
||||
username: String,
|
||||
email: String,
|
||||
},
|
||||
Insert { row: Row },
|
||||
Select,
|
||||
}
|
||||
|
||||
|
|
@ -15,13 +18,16 @@ pub struct StatementExecuteResult {
|
|||
impl Statement {
|
||||
pub fn execute(&self) -> StatementExecuteResult {
|
||||
match self {
|
||||
Statement::Insert {
|
||||
id,
|
||||
username,
|
||||
email,
|
||||
} => StatementExecuteResult {
|
||||
msg: format!("insert {id:?} {username:?} {email:?}"),
|
||||
},
|
||||
Statement::Insert { row } => {
|
||||
let Row {
|
||||
id,
|
||||
username,
|
||||
email,
|
||||
} = row;
|
||||
StatementExecuteResult {
|
||||
msg: format!("insert {id:?} {username:?} {email:?}"),
|
||||
}
|
||||
}
|
||||
Statement::Select => StatementExecuteResult {
|
||||
msg: String::from("select"),
|
||||
},
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue