From 0414ebab9c1eb708eef9a054536e6205ea422d98 Mon Sep 17 00:00:00 2001 From: MrTan Date: Fri, 18 Sep 2020 22:46:57 +0800 Subject: [PATCH] add empty like check --- src/lib.rs | 66 ++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 49 insertions(+), 17 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index dce5630..67b2d50 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -849,7 +849,7 @@ impl SqlBuilder { S: ToString, T: ToString, { - let expr = format!("{} = '{}'", &field.to_string(), &esc(&value.to_string())); + let expr = format!("{} = '{}'", &field.to_string(), &esc(value)); self.sets.push(expr); self } @@ -1248,7 +1248,7 @@ impl SqlBuilder { { let mut cond = field.to_string(); cond.push_str(" LIKE '"); - cond.push_str(&esc(&mask.to_string())); + cond.push_str(&esc(mask)); cond.push('\''); self.and_where(&cond) } @@ -1277,8 +1277,9 @@ impl SqlBuilder { T: ToString, { let mut cond = field.to_string(); + if cond.is_empty() || mask.to_string().is_empty() { return self; } cond.push_str(" LIKE '%"); - cond.push_str(&esc(&mask.to_string())); + cond.push_str(&esc(mask)); cond.push('\''); self.and_where(&cond) } @@ -1307,8 +1308,9 @@ impl SqlBuilder { T: ToString, { let mut cond = field.to_string(); + if cond.is_empty() || mask.to_string().is_empty() { return self; } cond.push_str(" LIKE '"); - cond.push_str(&esc(&mask.to_string())); + cond.push_str(&esc(mask)); cond.push_str("%'"); self.and_where(&cond) } @@ -1337,8 +1339,9 @@ impl SqlBuilder { T: ToString, { let mut cond = field.to_string(); + if cond.is_empty() || mask.to_string().is_empty() { return self; } cond.push_str(" LIKE '%"); - cond.push_str(&esc(&mask.to_string())); + cond.push_str(&esc(mask)); cond.push_str("%'"); self.and_where(&cond) } @@ -1368,7 +1371,7 @@ impl SqlBuilder { { let mut cond = field.to_string(); cond.push_str(" NOT LIKE '"); - cond.push_str(&esc(&mask.to_string())); + cond.push_str(&esc(mask)); cond.push('\''); self.and_where(&cond) } @@ -1397,8 +1400,9 @@ impl SqlBuilder { T: ToString, { let mut cond = field.to_string(); + if cond.is_empty() || mask.to_string().is_empty() { return self; } cond.push_str(" NOT LIKE '%"); - cond.push_str(&esc(&mask.to_string())); + cond.push_str(&esc(mask)); cond.push('\''); self.and_where(&cond) } @@ -1427,8 +1431,9 @@ impl SqlBuilder { T: ToString, { let mut cond = field.to_string(); + if cond.is_empty() || mask.to_string().is_empty() { return self; } cond.push_str(" NOT LIKE '"); - cond.push_str(&esc(&mask.to_string())); + cond.push_str(&esc(mask)); cond.push_str("%'"); self.and_where(&cond) } @@ -1457,8 +1462,9 @@ impl SqlBuilder { T: ToString, { let mut cond = field.to_string(); + if cond.is_empty() || mask.to_string().is_empty() { return self; } cond.push_str(" NOT LIKE '%"); - cond.push_str(&esc(&mask.to_string())); + cond.push_str(&esc(mask)); cond.push_str("%'"); self.and_where(&cond) } @@ -1901,7 +1907,7 @@ impl SqlBuilder { { let mut cond = field.to_string(); cond.push_str(" LIKE '"); - cond.push_str(&esc(&mask.to_string())); + cond.push_str(&esc(mask)); cond.push('\''); self.or_where(&cond) } @@ -1931,8 +1937,9 @@ impl SqlBuilder { T: ToString, { let mut cond = field.to_string(); + if cond.is_empty() || mask.to_string().is_empty() { return self; } cond.push_str(" LIKE '%"); - cond.push_str(&esc(&mask.to_string())); + cond.push_str(&esc(mask)); cond.push('\''); self.or_where(&cond) } @@ -1962,8 +1969,9 @@ impl SqlBuilder { T: ToString, { let mut cond = field.to_string(); + if cond.is_empty() || mask.to_string().is_empty() { return self; } cond.push_str(" LIKE '"); - cond.push_str(&esc(&mask.to_string())); + cond.push_str(&esc(mask)); cond.push_str("%'"); self.or_where(&cond) } @@ -1993,8 +2001,9 @@ impl SqlBuilder { T: ToString, { let mut cond = field.to_string(); + if cond.is_empty() || mask.to_string().is_empty() { return self; } cond.push_str(" LIKE '%"); - cond.push_str(&esc(&mask.to_string())); + cond.push_str(&esc(mask)); cond.push_str("%'"); self.or_where(&cond) } @@ -2025,7 +2034,7 @@ impl SqlBuilder { { let mut cond = field.to_string(); cond.push_str(" NOT LIKE '"); - cond.push_str(&esc(&mask.to_string())); + cond.push_str(&esc(mask)); cond.push('\''); self.or_where(&cond) } @@ -2055,8 +2064,9 @@ impl SqlBuilder { T: ToString, { let mut cond = field.to_string(); + if cond.is_empty() || mask.to_string().is_empty() { return self; } cond.push_str(" NOT LIKE '%"); - cond.push_str(&esc(&mask.to_string())); + cond.push_str(&esc(mask)); cond.push('\''); self.or_where(&cond) } @@ -2086,8 +2096,9 @@ impl SqlBuilder { T: ToString, { let mut cond = field.to_string(); + if cond.is_empty() || mask.to_string().is_empty() { return self; } cond.push_str(" NOT LIKE '"); - cond.push_str(&esc(&mask.to_string())); + cond.push_str(&esc(mask)); cond.push_str("%'"); self.or_where(&cond) } @@ -2117,8 +2128,9 @@ impl SqlBuilder { T: ToString, { let mut cond = field.to_string(); + if cond.is_empty() || mask.to_string().is_empty() { return self; } cond.push_str(" NOT LIKE '%"); - cond.push_str(&esc(&mask.to_string())); + cond.push_str(&esc(mask)); cond.push_str("%'"); self.or_where(&cond) } @@ -3447,6 +3459,26 @@ mod tests { Ok(()) } + + #[test] + fn test_empty_like() -> Result<(), Box> { + let sql = SqlBuilder::select_from("books") + .and_where_like_any("", "") + .and_where_like_left("", "") + .and_where_like_right("", "") + .and_where_not_like_any("", "") + .and_where_not_like_left("", "") + .and_where_not_like_right("", "") + .or_where_like_any("", "") + .or_where_like_left("", "") + .or_where_like_right("", "") + .or_where_not_like_any("", "") + .or_where_not_like_left("", "") + .or_where_not_like_right("", "") + .sql()?; + assert_eq!(&sql, "SELECT * FROM books;"); + Ok(()) + } } //#[cfg(test)]