sea-query icon indicating copy to clipboard operation
sea-query copied to clipboard

[Feature Request] Allow SimplExpr in like expressions

Open DontBreakAlex opened this issue 2 years ago • 4 comments

Motivation

This would allow building dynamic like expressions like this:

fn build_like(expr: Expr, value: sea_query::Value) -> SimpleExpr {
	expr.like(Expr::value('%').concat(Expr::value(value)).concat(Expr::value('%')))
}

DontBreakAlex avatar Oct 06 '22 16:10 DontBreakAlex

Hey @DontBreakAlex, intesting! I don't know we can actually do this, select * from posts where title like '%' || 'abc' || '%'. However, since || concat operator only works on Postgres. So, I'm hesitate to allow SimpleExpr in like expression. Afterall, the so called like expression is a string. So, why not expr.like(format!("%{}%", val))?

billy1624 avatar Oct 07 '22 03:10 billy1624

Hi @billy1624, thanks for your interest. The issue is that my external interface accepts Values (not Expr::Value) for all kinds of various filters, and converting it back to a string is a bit barbarous:

expr.like(format!("%{}%",
                  if let Value::String(str) = str {
	                  if let Some(str) = str { *str } else { "".to_string() }
                  } else { "".to_string() }
))

DontBreakAlex avatar Oct 07 '22 09:10 DontBreakAlex

This would also allow writing this:

Expr::expr(Func::lower([expr])).like(Func::lower.args([other_expr]))

DontBreakAlex avatar Oct 28 '22 15:10 DontBreakAlex