Skip to content

Commit 3e7d8f0

Browse files
committed
add empty like check
1 parent 423afb6 commit 3e7d8f0

1 file changed

Lines changed: 57 additions & 17 deletions

File tree

src/lib.rs

Lines changed: 57 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -849,7 +849,7 @@ impl SqlBuilder {
849849
S: ToString,
850850
T: ToString,
851851
{
852-
let expr = format!("{} = '{}'", &field.to_string(), &esc(&value.to_string()));
852+
let expr = format!("{} = '{}'", &field.to_string(), &esc(value));
853853
self.sets.push(expr);
854854
self
855855
}
@@ -1247,8 +1247,9 @@ impl SqlBuilder {
12471247
T: ToString,
12481248
{
12491249
let mut cond = field.to_string();
1250+
if cond.is_empty() || mask.to_string().is_empty() { return self; }
12501251
cond.push_str(" LIKE '");
1251-
cond.push_str(&esc(&mask.to_string()));
1252+
cond.push_str(&esc(mask));
12521253
cond.push('\'');
12531254
self.and_where(&cond)
12541255
}
@@ -1277,8 +1278,9 @@ impl SqlBuilder {
12771278
T: ToString,
12781279
{
12791280
let mut cond = field.to_string();
1281+
if cond.is_empty() || mask.to_string().is_empty() || mask.to_string().is_empty() { return self; }
12801282
cond.push_str(" LIKE '%");
1281-
cond.push_str(&esc(&mask.to_string()));
1283+
cond.push_str(&esc(mask));
12821284
cond.push('\'');
12831285
self.and_where(&cond)
12841286
}
@@ -1307,8 +1309,9 @@ impl SqlBuilder {
13071309
T: ToString,
13081310
{
13091311
let mut cond = field.to_string();
1312+
if cond.is_empty() || mask.to_string().is_empty() { return self; }
13101313
cond.push_str(" LIKE '");
1311-
cond.push_str(&esc(&mask.to_string()));
1314+
cond.push_str(&esc(mask));
13121315
cond.push_str("%'");
13131316
self.and_where(&cond)
13141317
}
@@ -1337,8 +1340,9 @@ impl SqlBuilder {
13371340
T: ToString,
13381341
{
13391342
let mut cond = field.to_string();
1343+
if cond.is_empty() || mask.to_string().is_empty() { return self; }
13401344
cond.push_str(" LIKE '%");
1341-
cond.push_str(&esc(&mask.to_string()));
1345+
cond.push_str(&esc(mask));
13421346
cond.push_str("%'");
13431347
self.and_where(&cond)
13441348
}
@@ -1367,8 +1371,9 @@ impl SqlBuilder {
13671371
T: ToString,
13681372
{
13691373
let mut cond = field.to_string();
1374+
if cond.is_empty() || mask.to_string().is_empty() { return self; }
13701375
cond.push_str(" NOT LIKE '");
1371-
cond.push_str(&esc(&mask.to_string()));
1376+
cond.push_str(&esc(mask));
13721377
cond.push('\'');
13731378
self.and_where(&cond)
13741379
}
@@ -1397,8 +1402,9 @@ impl SqlBuilder {
13971402
T: ToString,
13981403
{
13991404
let mut cond = field.to_string();
1405+
if cond.is_empty() || mask.to_string().is_empty() { return self; }
14001406
cond.push_str(" NOT LIKE '%");
1401-
cond.push_str(&esc(&mask.to_string()));
1407+
cond.push_str(&esc(mask));
14021408
cond.push('\'');
14031409
self.and_where(&cond)
14041410
}
@@ -1427,8 +1433,9 @@ impl SqlBuilder {
14271433
T: ToString,
14281434
{
14291435
let mut cond = field.to_string();
1436+
if cond.is_empty() || mask.to_string().is_empty() { return self; }
14301437
cond.push_str(" NOT LIKE '");
1431-
cond.push_str(&esc(&mask.to_string()));
1438+
cond.push_str(&esc(mask));
14321439
cond.push_str("%'");
14331440
self.and_where(&cond)
14341441
}
@@ -1457,8 +1464,9 @@ impl SqlBuilder {
14571464
T: ToString,
14581465
{
14591466
let mut cond = field.to_string();
1467+
if cond.is_empty() || mask.to_string().is_empty() { return self; }
14601468
cond.push_str(" NOT LIKE '%");
1461-
cond.push_str(&esc(&mask.to_string()));
1469+
cond.push_str(&esc(mask));
14621470
cond.push_str("%'");
14631471
self.and_where(&cond)
14641472
}
@@ -1900,8 +1908,9 @@ impl SqlBuilder {
19001908
T: ToString,
19011909
{
19021910
let mut cond = field.to_string();
1911+
if cond.is_empty() || mask.to_string().is_empty() { return self; }
19031912
cond.push_str(" LIKE '");
1904-
cond.push_str(&esc(&mask.to_string()));
1913+
cond.push_str(&esc(mask));
19051914
cond.push('\'');
19061915
self.or_where(&cond)
19071916
}
@@ -1931,8 +1940,9 @@ impl SqlBuilder {
19311940
T: ToString,
19321941
{
19331942
let mut cond = field.to_string();
1943+
if cond.is_empty() || mask.to_string().is_empty() { return self; }
19341944
cond.push_str(" LIKE '%");
1935-
cond.push_str(&esc(&mask.to_string()));
1945+
cond.push_str(&esc(mask));
19361946
cond.push('\'');
19371947
self.or_where(&cond)
19381948
}
@@ -1962,8 +1972,9 @@ impl SqlBuilder {
19621972
T: ToString,
19631973
{
19641974
let mut cond = field.to_string();
1975+
if cond.is_empty() || mask.to_string().is_empty() { return self; }
19651976
cond.push_str(" LIKE '");
1966-
cond.push_str(&esc(&mask.to_string()));
1977+
cond.push_str(&esc(mask));
19671978
cond.push_str("%'");
19681979
self.or_where(&cond)
19691980
}
@@ -1993,8 +2004,9 @@ impl SqlBuilder {
19932004
T: ToString,
19942005
{
19952006
let mut cond = field.to_string();
2007+
if cond.is_empty() || mask.to_string().is_empty() { return self; }
19962008
cond.push_str(" LIKE '%");
1997-
cond.push_str(&esc(&mask.to_string()));
2009+
cond.push_str(&esc(mask));
19982010
cond.push_str("%'");
19992011
self.or_where(&cond)
20002012
}
@@ -2024,8 +2036,9 @@ impl SqlBuilder {
20242036
T: ToString,
20252037
{
20262038
let mut cond = field.to_string();
2039+
if cond.is_empty() || mask.to_string().is_empty() { return self; }
20272040
cond.push_str(" NOT LIKE '");
2028-
cond.push_str(&esc(&mask.to_string()));
2041+
cond.push_str(&esc(mask));
20292042
cond.push('\'');
20302043
self.or_where(&cond)
20312044
}
@@ -2055,8 +2068,9 @@ impl SqlBuilder {
20552068
T: ToString,
20562069
{
20572070
let mut cond = field.to_string();
2071+
if cond.is_empty() || mask.to_string().is_empty() { return self; }
20582072
cond.push_str(" NOT LIKE '%");
2059-
cond.push_str(&esc(&mask.to_string()));
2073+
cond.push_str(&esc(mask));
20602074
cond.push('\'');
20612075
self.or_where(&cond)
20622076
}
@@ -2086,8 +2100,9 @@ impl SqlBuilder {
20862100
T: ToString,
20872101
{
20882102
let mut cond = field.to_string();
2103+
if cond.is_empty() || mask.to_string().is_empty() { return self; }
20892104
cond.push_str(" NOT LIKE '");
2090-
cond.push_str(&esc(&mask.to_string()));
2105+
cond.push_str(&esc(mask));
20912106
cond.push_str("%'");
20922107
self.or_where(&cond)
20932108
}
@@ -2117,8 +2132,9 @@ impl SqlBuilder {
21172132
T: ToString,
21182133
{
21192134
let mut cond = field.to_string();
2135+
if cond.is_empty() || mask.to_string().is_empty() { return self; }
21202136
cond.push_str(" NOT LIKE '%");
2121-
cond.push_str(&esc(&mask.to_string()));
2137+
cond.push_str(&esc(mask));
21222138
cond.push_str("%'");
21232139
self.or_where(&cond)
21242140
}
@@ -3447,6 +3463,30 @@ mod tests {
34473463

34483464
Ok(())
34493465
}
3466+
3467+
#[test]
3468+
fn test_empty_like() -> Result<(), Box<dyn Error + Send + Sync>> {
3469+
let sql = SqlBuilder::select_from("books")
3470+
.and_where_like("", "")
3471+
.and_where_like_any("", "")
3472+
.and_where_like_left("", "")
3473+
.and_where_like_right("", "")
3474+
.and_where_not_like("", "")
3475+
.and_where_not_like_any("", "")
3476+
.and_where_not_like_left("", "")
3477+
.and_where_not_like_right("", "")
3478+
.or_where_like("", "")
3479+
.or_where_like_any("", "")
3480+
.or_where_like_left("", "")
3481+
.or_where_like_right("", "")
3482+
.or_where_not_like("", "")
3483+
.or_where_not_like_any("", "")
3484+
.or_where_not_like_left("", "")
3485+
.or_where_not_like_right("", "")
3486+
.sql()?;
3487+
assert_eq!(&sql, "SELECT * FROM books;");
3488+
Ok(())
3489+
}
34503490
}
34513491

34523492
//#[cfg(test)]

0 commit comments

Comments
 (0)