rmoorman

rmoorman

Building a dynamic query using ecto's `dynamic` macro

Summary

I am currently trying to get a better understanding of elixir macros and in doing so encountered the section about dynamic queries in the ecto documentation. The example, however, leads to an unwanted true and part in the ecto query where and I am trying to have a look at how to solve that.

The trimmed down documentation example

So I tried out the example given and it seems to add a superfluous true and in front of the where clause. I trimmed the example down and put it inside a testcase:

defmodule MyMacroTest do
  use ExUnit.Case, async: true
  import Ecto.Query

  def filter_where(params) do
    Enum.reduce(params, dynamic(true), fn
      {"author", value}, dynamic ->
        dynamic([p], ^dynamic and p.author == ^value)
    end)
  end

  @q from(p in "post")

  test "author" do
    query = %{"author" => "foo"}
    assert inspect(where(@q, ^filter_where(query))) ==
             inspect(where(@q, [p], p.author == ^query["author"]))
  end
end

which fails with

     Assertion with == failed
     code:  assert inspect(where(@q, ^filter_where(query))) == inspect(where(@q, [p], p.author == ^query["author"]))
     left:  "#Ecto.Query<from p0 in \"post\", where: true and p0.author == ^\"foo\">"
     right: "#Ecto.Query<from p0 in \"post\", where: p0.author == ^\"foo\">"

The fancy if statement :see_no_evil:

Switching out the dynamic(true) within the call to Enum.reduce/3 with nil and using if seems to work

defmodule MyMacro2Test do
  use ExUnit.Case, async: true
  import Ecto.Query

  def filter_where(params) do
    Enum.reduce(params, nil, fn
      {"author", value}, dynamic ->
        if dynamic do
          dynamic([p], ^dynamic and p.author == ^value)
        else
          dynamic([p], p.author == ^value)
        end
    end)
  end

  @q from(p in "post")

  test "author" do
    query = %{"author" => "foo"}
    assert inspect(where(@q, ^filter_where(query))) ==
             inspect(where(@q, [p], p.author == ^query["author"]))
  end
end
Finished in 0.04 seconds (0.04s async, 0.00s sync)
1 test, 0 failures

Even though it works, it is not a thing of beauty and given that the dynamic macro basically outputs AST similar to what I would like, writing another macro appears to be the correct thing to do.

But here is the part I seem to struggle with

I now don’t know how to exactly extract that conditional into a macro. I guess I will need a macro because I need to wrap the dynamic macro and pass through the arguments in the same syntax.
Currently stuck with this version

defmodule MyMacroTest3 do
  use ExUnit.Case, async: true
  import Ecto.Query

  defmacro dynamic_and(dynamic, bindings, expr) do
    quote do
      if unquote(dynamic) do
        dynamic(unquote(bindings), unquote(dynamic) and unquote(expr))
      else
        dynamic(unquote(bindings), unquote(expr))
      end
    end
  end

  def filter_where(params) do
    Enum.reduce(params, nil, fn
      {"author", value}, dynamic ->
        dynamic_and(^dynamic, [p], p.author == ^value)
    end)
  end

  @q from(p in "post")

  test "author" do
    query = %{"author" => "foo"}
    assert inspect(where(@q, ^filter_where(query))) ==
             inspect(where(@q, [p], p.author == ^query["author"]))
  end
end

which gives me a compilation error about a misplaced operator ^dynamic

    The pin operator ^ is supported only inside matches or inside custom macros. Make sure you are inside a match or all necessary macros have been required
    │
 18 │         dynamic_and(^dynamic, [p], p.author == ^value)

I tried multiple variations of pinning and unquote/not unquote but I somehow currently fail to see where to look next for the correct way of solving this.
I also thought that a potential solution could involve returning some AST directly, or maybe it would be better to try to construct an %Ecto.Query.DynamicExpr struct myself.
Also got myself a copy of that Metaprogramming book and I am currently reading it hoping that it might help, and even got some bots involved :sweat_smile:

Anyway, I would really apreciate any pointers that could be thrown at me and thank you very much in advance! :slight_smile:

Marked As Solved

LostKobrakai

LostKobrakai

The solution here is Enum.map, Enum.reduce/2 over Enum.reduce/3:

params
|> Enum.map(fn 
  {"author", value} -> dynamic([p], p.author == ^value)
end)
|> Enum.reduce(fn a, b -> dynamic(^a and ^b) end)

Also Liked

gregvaughn

gregvaughn

I love the Enum.reduce/2 approach, but from past experience, I recommend you protect against an empty list, because it’ll raise.

Last Post!

rmoorman

rmoorman

@gregvaughn I will take care to correctly check the list of valid query conditions before proceeding with the rest of the filter logic. Good advice! Thank you.

Where Next?

Popular in Questions Top

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New

Other popular topics Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New

We're in Beta

About us Mission Statement