diff options
author | jvech <jmvalenciae@unal.edu.co> | 2024-07-29 07:54:47 -0500 |
---|---|---|
committer | jvech <jmvalenciae@unal.edu.co> | 2024-07-29 07:54:47 -0500 |
commit | b0a48708ff1f414d27d41a801d675be36485c7b0 (patch) | |
tree | a43a65a6773f25b7042f622bdd0f2d10bdd54648 | |
parent | 18d46f7a8550fd0773df6ca838a0caf6f4b657c4 (diff) |
add: multiple comment filtering added
-rw-r--r-- | src/cli.rs | 2 | ||||
-rw-r--r-- | src/database.rs | 34 |
2 files changed, 28 insertions, 8 deletions
@@ -49,7 +49,7 @@ pub enum Commands { /// Filter registers by note (sql match syntax) #[arg(short='n', long)] - filter_note: Option<String>, + filter_note: Option<Vec<String>>, }, 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<String>, - note_filter: Option<String>) -> Result<()> + note_filter: Option<Vec<String>>) -> 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())?; |