joehua87

joehua87

Remove `and true` in dynamic query reduce

When using reduce in dynamic query, like discussed in the Ecto docs here, it produce output like this:

where: true and (true and e0.avg_rating > ^3)

The problems it that it make expression longer and harder to maintain test
Does anyone have ideas to reduce this to:

where: e0.avg_rating > ^3

Marked As Solved

chrismcg

chrismcg

I wouldn’t use inspect here as it’s representation could change (see [1] for an example of this).

I think you could make the initial value of reduce nil and then pattern match on that rather than dynamic(true) representation.

e.g.

defp join_exp(nil, exp) -> exp
defp join_exp(acc, exp) -> dynamic(^acc and ^exp)

[1] Multi-letter sigils by wojtekmach · Pull Request #9826 · elixir-lang/elixir · GitHub

Also Liked

sb8244

sb8244

Author of Real-Time Phoenix

I’ve thought about this for a recent project but didn’t implement it, because I decided it wasn’t worth the code for SQL vanity purpose.

In your reducer function, you could case on the accumulator condition and, if nil, produce a query without the and. The benefit of using “true and” is that your reducer doesn’t need to care what the accumulator is.

I haven’t tried it but I think this would work.

joehua87

joehua87

Thank you @sb8244! It’s simpler than I thought, just made it :slight_smile:

Before:

defp parse_item({compare_type, value}, acc, field_name) do
  case compare_type do
    :eq -> dynamic([p], ^acc and field(p, ^field_name) == ^value)
    :gt -> dynamic([p], ^acc and field(p, ^field_name) > ^value)
    :lt -> dynamic([p], ^acc and field(p, ^field_name) < ^value)
    :gte -> dynamic([p], ^acc and field(p, ^field_name) >= ^value)
    :lte -> dynamic([p], ^acc and field(p, ^field_name) <= ^value)
    _ -> acc
  end
end

After:

defp parse_item({compare_type, value}, acc, field_name) do
  case compare_type do
    :eq -> join_exp(acc, dynamic([p], field(p, ^field_name) == ^value))
    :gt -> join_exp(acc, dynamic([p], field(p, ^field_name) > ^value))
    :lt -> join_exp(acc, dynamic([p], field(p, ^field_name) < ^value))
    :gte -> join_exp(acc, dynamic([p], field(p, ^field_name) >= ^value))
    :lte -> join_exp(acc, dynamic([p], field(p, ^field_name) <= ^value))
    _ -> acc
  end
end

defp join_exp(acc, exp) do
  case inspect(acc) do
    "dynamic([], true)" ->
      exp

    _ ->
      dynamic(^acc and ^exp)
  end
end
dimitarvp

dimitarvp

Relying on inspect's output is awful but sometimes a fact of life. :slight_smile:

Where Next?

Popular in Questions Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
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
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New

Other popular topics Top

peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
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
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

We're in Beta

About us Mission Statement