diff --git a/njord/db/insert.db b/njord/db/insert.db index 01495941..8f648606 100644 Binary files a/njord/db/insert.db and b/njord/db/insert.db differ diff --git a/njord/db/update.db b/njord/db/update.db new file mode 100644 index 00000000..e69de29b diff --git a/njord/src/condition.rs b/njord/src/condition.rs index 028b62b1..0ed8a256 100644 --- a/njord/src/condition.rs +++ b/njord/src/condition.rs @@ -46,6 +46,10 @@ pub enum Condition { And(Box, Box), /// Logical OR condition. Or(Box, Box), + /// In condition: column IN (value1, value2, ...). + In(String, Vec), + /// Not in condition: column NOT IN (value1, value2, ...). + NotIn(String, Vec), } impl Condition { @@ -113,6 +117,22 @@ impl Condition { } Condition::And(left, right) => format!("({}) AND ({})", left.build(), right.build()), Condition::Or(left, right) => format!("({}) OR ({})", left.build(), right.build()), + Condition::In(column, values) => { + let values = values + .iter() + .map(|v| format!("'{}'", v)) + .collect::>() + .join(", "); + format!("{} IN ({})", column, values) + } + Condition::NotIn(column, values) => { + let values = values + .iter() + .map(|v| format!("'{}'", v)) + .collect::>() + .join(", "); + format!("{} NOT IN ({})", column, values) + } } } } diff --git a/njord/tests/sqlite_test.rs b/njord/tests/sqlite_test.rs index 29375cc1..b21858b4 100644 --- a/njord/tests/sqlite_test.rs +++ b/njord/tests/sqlite_test.rs @@ -539,3 +539,42 @@ fn update_with_sub_queries() { Err(e) => panic!("Failed to UPDATE: {:?}", e), }; } + +fn select_in() { + let db_relative_path = "./db/select.db"; + let db_path = Path::new(&db_relative_path); + let conn = sqlite::open(db_path); + + let columns = vec![ + Column::Text("id".to_string()), + Column::Text("username".to_string()), + Column::Text("email".to_string()), + Column::Text("address".to_string()), + ]; + + let condition = Condition::And( + Box::new(Condition::In( + "username".to_string(), + vec!["mjovanc".to_string(), "otheruser".to_string()], + )), + Box::new(Condition::NotIn( + "username".to_string(), + vec!["chasewillden".to_string()], + )), + ); + + match conn { + Ok(c) => { + let result = sqlite::select(&c, columns) + .from(User::default()) + .where_clause(condition) + .build(); + + match result { + Ok(r) => assert_eq!(r.len(), 2), + Err(e) => panic!("Failed to SELECT: {:?}", e), + }; + } + Err(e) => panic!("Failed to SELECT: {:?}", e), + }; +}