Ecto bindingless query operations

Is there a bindingless way to create an Ecto where clause such that it can filter for tests other than equality?

The docs provide the following example:

from Post,
  where: [category: "fresh and new"],
  order_by: [desc: :published_at],
  select: [:id, :title, :body]

What if I wanted to instead filter based off of date (ie all records dated after a specific date) - I know that I can do something like:

from p in Post, 
   where: p.submitted_time > important_date 

If I want to assemble the key, operator and value dynamically, using bindingless syntax, is that possible?

I don’t think you can assemble the query dynamically AND without using bindings, but you can assemble it dynamically using dynamic/2.

My original approach at the problem set was this

def build_query(%Queries{options: opts}) when is_list(opts) do
    base_query =
      from r in Record,
        order_by: r.submitted_time

    Enum.reduce(opts, base_query, fn prop, q ->
      process_filter(q, prop)

def process_filter(query, {_key, _value} = w) do
    from x in query,
      where: ^[w]

I’m not sure if the above will work… but the key here, is that I’m trying to pass the key into the query builder such that the function doesn’t need to be aware of the schema, so I have to be bindingless.

You can use field/2 to access a field dynamically.

1 Like

Thanks - I think this is exactly what I needed!