使用动态运算符创建Ecto查询 [英] Create Ecto query with dynamic operators

查看:84
本文介绍了使用动态运算符创建Ecto查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何在Ecto查询中使用动态字段和值,例如:

I know how I can use dynamic fields and values into Ecto query, for example:

field = :age
value = 20
Ecto.Query.where(App.User, [x], field(x, ^field) < ^value)

但是,可以动态定义运算符(在此示例中为< )吗?以及如何?

But, it is possible to define the operator (in this example <) dynamically? and how?

我已经尝试过使用 fragment 和内插字符串:

I have tried it with fragment and interpolated string:

operator = ">"
Ecto.Query.where(App.User, [x], fragment("? #{operator} ?", field(x, ^field), ^value))

但是它引发了一个例外: Ecto.Query.CompileError)以防止SQL注入攻击,fragment(...)不允许使用字符串...

but it raises the exception: Ecto.Query.CompileError) to prevent SQL injection attacks, fragment(...) does not allow strings...

推荐答案

您可以进行一些元编程.的问题,支持自定义宏.

You can, with a bit of metaprogramming. ecto supports custom macros.

defmacrop custom_where(t, f, v, sign) do
  {sign, [context: Elixir, import: Kernel],
   [
     {:field, [], [t, {:^, [], [f]}]},
     {:^, [], [v]}
   ]}
end

并像使用它

field = :age
value = 20

Ecto.Query.where(User, [x],
  custom_where(x, field, value, :<))
#⇒ #Ecto.Query<from u0 in User, where: u0.age < ^20>

这里的诀窍是我们愚弄注入AST,因此它不会执行检查.

The trick here is that we fool ecto injecting an AST inplace, so it does not perform checks.

这篇关于使用动态运算符创建Ecto查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆