joehua87

joehua87

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

Showing Posts 1 to 5

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 OP

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:

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

joehua87

joehua87 OP

Thanks :slight_smile:

— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews