refactor(insert): separate row struct

This commit is contained in:
Khaïs COLIN 2025-06-05 19:06:23 +02:00
parent 7ccefac553
commit db7db63fe3
Signed by: logistic-bot
SSH key fingerprint: SHA256:RlpiqKeXpcPFZZ4y9Ou4xi2M8OhRJovIwDlbCaMsuAo
5 changed files with 44 additions and 27 deletions

View file

@ -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();

View file

@ -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,
},
})) }))
} }

View file

@ -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",
},
}, },
), ),
], ],

View file

@ -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(

View file

@ -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"),
}, },