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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::{
|
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};
|
use insta::{assert_debug_snapshot, assert_snapshot};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_execute_insert_statement() {
|
fn test_execute_insert_statement() {
|
||||||
let statement: Command = Statement::Insert {
|
let statement: Command = Statement::Insert {
|
||||||
id: 45,
|
row: Row {
|
||||||
username: String::from("user"),
|
id: 45,
|
||||||
email: String::from("user@example.org"),
|
username: String::from("user"),
|
||||||
|
email: String::from("user@example.org"),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
.into();
|
.into();
|
||||||
let result = statement.execute().display();
|
let result = statement.execute().display();
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ use std::collections::VecDeque;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
command::{Command, CommandParseError, ExpectedToken},
|
command::{Command, CommandParseError, ExpectedToken},
|
||||||
statements::Statement,
|
statements::{Row, Statement},
|
||||||
tokens::{Location, Token, TokenData, tokenize},
|
tokens::{Location, Token, TokenData, tokenize},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -230,9 +230,11 @@ fn parse_insert_command(
|
||||||
expect_semicolon(tokens)?;
|
expect_semicolon(tokens)?;
|
||||||
|
|
||||||
Ok(Command::Statement(Statement::Insert {
|
Ok(Command::Statement(Statement::Insert {
|
||||||
id,
|
row: Row {
|
||||||
username,
|
id,
|
||||||
email,
|
username,
|
||||||
|
email,
|
||||||
|
},
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,16 @@
|
||||||
---
|
---
|
||||||
source: src/parser.rs
|
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(
|
Ok(
|
||||||
[
|
[
|
||||||
Statement(
|
Statement(
|
||||||
Insert {
|
Insert {
|
||||||
id: 1,
|
row: Row {
|
||||||
username: "username",
|
id: 1,
|
||||||
email: "email@example.com",
|
username: "username",
|
||||||
|
email: "email@example.com",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
|
|
|
||||||
|
|
@ -9,9 +9,11 @@ Ok(
|
||||||
),
|
),
|
||||||
Statement(
|
Statement(
|
||||||
Insert {
|
Insert {
|
||||||
id: 1,
|
row: Row {
|
||||||
username: "user",
|
id: 1,
|
||||||
email: "email@test.com",
|
username: "user",
|
||||||
|
email: "email@test.com",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
Statement(
|
Statement(
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,13 @@
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct Row {
|
||||||
|
pub id: i64,
|
||||||
|
pub username: String,
|
||||||
|
pub email: String,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum Statement {
|
pub enum Statement {
|
||||||
Insert {
|
Insert { row: Row },
|
||||||
id: i64,
|
|
||||||
username: String,
|
|
||||||
email: String,
|
|
||||||
},
|
|
||||||
Select,
|
Select,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -15,13 +18,16 @@ pub struct StatementExecuteResult {
|
||||||
impl Statement {
|
impl Statement {
|
||||||
pub fn execute(&self) -> StatementExecuteResult {
|
pub fn execute(&self) -> StatementExecuteResult {
|
||||||
match self {
|
match self {
|
||||||
Statement::Insert {
|
Statement::Insert { row } => {
|
||||||
id,
|
let Row {
|
||||||
username,
|
id,
|
||||||
email,
|
username,
|
||||||
} => StatementExecuteResult {
|
email,
|
||||||
msg: format!("insert {id:?} {username:?} {email:?}"),
|
} = row;
|
||||||
},
|
StatementExecuteResult {
|
||||||
|
msg: format!("insert {id:?} {username:?} {email:?}"),
|
||||||
|
}
|
||||||
|
}
|
||||||
Statement::Select => StatementExecuteResult {
|
Statement::Select => StatementExecuteResult {
|
||||||
msg: String::from("select"),
|
msg: String::from("select"),
|
||||||
},
|
},
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue