leaf
I am building a query that use dynamics and then interpolate it in the join. I build it successfully. But I want to include dynamics for multiple fields .The reduce function iterates over the map take the fields and build a dynamic query. Here is my code so far:
Enum.reduce(additional_join, true, fn {field, map}, query ->
query =
if Map.has_key?(map, "$in") do
query = dynamic([q], field(q, ^String.to_atom(field)) in ^map["$in"] and ^query)
else
query
end
if Map.has_key?(map, "$between_equal") do
query =
dynamic(
[q],
field(q, ^FatHelper.string_to_existing_atom(field)) >= ^Enum.min(map["$between_equal"]) and
field(q, ^FatHelper.string_to_existing_atom(field)) <= ^Enum.max(map["$between_equal"]) and
^query
)
else
query
end
end)
These are the params:
"$additional_on_clauses" => %{
"event_date" => %{"$between_equal" => [start_date, end_date]}
}
"$additional_on_clauses" => %{
"infection_category_id" => %{"$in" => category_ids},
"event_date" => %{"$between_equal" => [start_date, end_date]}
}
It only takes the last map in the above params and returns the query. I think reduce is a better way but i don’t have any idea how to use the dynamics inside of it.
I want dynamics to include both the above maps inside additional_on_clauses.
If I take the common part of the query outside of the reduce function and pass dynamics in the acc It won’t work because Enum not implemented for dynamics. dynamic([q, c], q.id == c.infection_id). I can’t use this as an accumulator.
Is there any other way I can build composite dynamic query with multiple dynamic conditions.?
Any help/suggestion will be much appreciated.
Thanks
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixir-ls
- #ai
- #elixirconf-us
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 8- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
al2o3cr
Note: I’m assuming that that first code block is surrounded in some kind of
Enum.reducestructure. If it isn’t, please post the rest of the code.Probably want to pass the result of the previous iteration here instead of
true.peerreynders
I’m not entirely sure I understand all the nuances of your particular use case - but this seems to work:
leaf
Thanks for your answer.
sorry if it’s too vague to understand. Let me explain more:
Background:
I am writing this join query that takes multiple options for
onin joins and build a query dynamically. The fields can change so I am trying to implement a dynamic solution. I need some method that iterates over these params:And build a dynamic query expression that I interpolate later in my join clause like this:
The problem is my current implementation make dynamic query for just one map inside the
"$additional_on_clauses". And ignore the other one.The params inside
"$additional_on_clauses"can vary. And field names also change. The only thing constant will be$in,$between_equal. Based on these two I will build queries dynamically.Hope this helps.
peerreynders
Not sure what you mean by ignoring something - looking at
I thought you want something like
provided
The code I showed will act in exactly that manner.
peerreynders
This is what is unclear - what do you mean by both the above maps inside
additional_on_clauses?The code you supplied can only process
or
So when you say both the above maps it sounds like you are referring to
and
in
Now I’m wondering what
actually means because
evaluates to
because the last
"$additional_on_clauses"value will replace the first"$additional_on_clauses"value.leaf
Thanks for your time.
I am talking about
Ecto Dynamics. https://hexdocs.pm/ecto/Ecto.Query.html#dynamic/2I am using them inside Enum.reduce statement. So when I pass this map:
}
}
First it takes this first map and make a dynamic query like this:
And then
Enum.reducetakes the second map in the next iteration and build query like this:But I want these two to be combined like this inside
Enum.reduce.so far I have this:
But It only returns query for the last map which in this case is this one:
I understand the behaviour thats how Enum.reduce works. But Is there a way I can combine these two together.
The fields can be dynamic so may be next time the fields will b
end_dateinstead ofevent_dateandidinstead ofinfection_category_id. So i can’t hard code these.So is there a way beside
Enum.reduceor with Enum.reduce that I can get the desired result.Thanks
peerreynders
This code strongly suggests that the problem is somewhere else in your code (something that you aren’t showing or even looking at):
And to my earlier point changing to this:
has the following result:
leaf
Thanks for your suggestion I updated the code and it seems to be working so the issue is was on my side.