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)]
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 {
row: Row {
id: 45,
username: String::from("user"),
email: String::from("user@example.org"),
},
}
.into();
let result = statement.execute().display();

View file

@ -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 {
row: Row {
id,
username,
email,
},
}))
}

View file

@ -1,15 +1,17 @@
---
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 {
row: Row {
id: 1,
username: "username",
email: "email@example.com",
},
},
),
],
)

View file

@ -9,10 +9,12 @@ Ok(
),
Statement(
Insert {
row: Row {
id: 1,
username: "user",
email: "email@test.com",
},
},
),
Statement(
Select,

View file

@ -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 {
Statement::Insert { row } => {
let Row {
id,
username,
email,
} => StatementExecuteResult {
} = row;
StatementExecuteResult {
msg: format!("insert {id:?} {username:?} {email:?}"),
},
}
}
Statement::Select => StatementExecuteResult {
msg: String::from("select"),
},