From b0a48708ff1f414d27d41a801d675be36485c7b0 Mon Sep 17 00:00:00 2001 From: jvech Date: Mon, 29 Jul 2024 07:54:47 -0500 Subject: add: multiple comment filtering added --- src/cli.rs | 2 +- src/database.rs | 34 +++++++++++++++++++++++++++------- 2 files changed, 28 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/cli.rs b/src/cli.rs index 5cd8285..e862034 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -49,7 +49,7 @@ pub enum Commands { /// Filter registers by note (sql match syntax) #[arg(short='n', long)] - filter_note: Option, + filter_note: Option>, }, diff --git a/src/database.rs b/src/database.rs index fe20bb2..24298ff 100644 --- a/src/database.rs +++ b/src/database.rs @@ -88,7 +88,7 @@ impl Database { pub fn view_history( conn: Connection, agent_filter: Option, - note_filter: Option) -> Result<()> + note_filter: Option>) -> Result<()> { let mut hist_query: String = " SELECT date(r.register_date, 'auto'), a.name, r.amount, r.note @@ -97,24 +97,44 @@ impl Database { ON a.id = r.agent_id ".to_owned(); + let mut filter_note_string = String::new(); let mut stmt: Statement; let mut regs: Rows; + if let Some(notes) = note_filter.clone() { + let mut i: u32 = 0; + for note in notes.iter() { + if i == 0 && agent_filter != None { + filter_note_string.push_str(" AND (note LIKE '"); + } else if i == 0 && agent_filter == None { + filter_note_string.push_str(" (note LIKE '"); + } else { + filter_note_string.push_str(" OR note LIKE '"); + } + filter_note_string.push_str(note.as_str()); + filter_note_string.push_str("'"); + i+=1; + } + filter_note_string.push(')'); + } + match (agent_filter, note_filter) { - (Some(name), Some(note)) => { - hist_query.push_str("WHERE name = ?1 and note LIKE ?2"); + (Some(name), Some(_)) => { + hist_query.push_str("WHERE name = ?1"); + hist_query.push_str(filter_note_string.as_str()); stmt = conn.prepare(hist_query.as_str())?; - regs = stmt.query([name, note])?; + regs = stmt.query([name])?; }, (Some(name), None) => { hist_query.push_str("WHERE name = ?1"); stmt = conn.prepare(hist_query.as_str())?; regs = stmt.query([name])?; }, - (None, Some(note)) => { - hist_query.push_str("WHERE note LIKE ?1"); + (None, Some(_)) => { + hist_query.push_str("WHERE"); + hist_query.push_str(filter_note_string.as_str()); stmt = conn.prepare(hist_query.as_str())?; - regs = stmt.query([note])?; + regs = stmt.query([])?; }, (None, None) => { stmt = conn.prepare(hist_query.as_str())?; -- cgit v1.2.3-70-g09d2