SeaQL / sea-query

🔱 A dynamic SQL query builder for MySQL, Postgres and SQLite
https://www.sea-ql.org
Other
1.18k stars 194 forks source link

[Feature Request] Allow SimplExpr in like expressions #464

Open DontBreakAlex opened 2 years ago

DontBreakAlex commented 2 years ago

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('%')))
}
billy1624 commented 2 years ago

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))?

DontBreakAlex commented 2 years ago

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 commented 2 years ago

This would also allow writing this:

Expr::expr(Func::lower([expr])).like(Func::lower.args([other_expr]))
elichai commented 2 weeks ago

However, since || concat operator only works on Postgres.

Is this true? as far as I can see sqlite does support concat: https://sqlite.org/lang_corefunc.html#concat and also it seems to be part of the SQL spec? am I misunderstanding something about this concat? (I'd like to use concat to SET the value in an UPDATE query)