cjbottaro

cjbottaro

Ecto.Query.API.fragment and string interpolation

I know this has been asked before, but I still can’t get it to work. I have code like this…

def case_stmt(input) do
  [c1, c2, c3, c4] = generate_clauses(input)

  Ecto.Query.dynamic([foo: a, bar: b], fragment("""
    CASE WHEN ? THEN 1 ELSE 0 END +
    CASE WHEN ? THEN 1 ELSE 0 END +
    CASE WHEN ? THEN 1 ELSE 0 END +
    CASE WHEN ? THEN 1 ELSE 0 END
  """, ^c1, ^c2, ^c3, ^c4))
end

Which works fine when the number of generated clauses is exactly 4. What about when it’s an unknown amount? I want to build up the SQL string using reduce.

There is a really good blog post about making SQL case statements with Ecto:

The problem is that it doesn’t work when you pass it a variable; it only works when you pass a literal.

Any ideas on how to make this work? My use case is user defined queries that are stored in a database. All the user input is validated before making the db records of course. “Just make a case clause for each field the user can search on.” doesn’t work because the schema itself is user defined.

Thanks for the help!

Marked As Solved

cjbottaro

cjbottaro

Figured it out after chasing down those ideas, but ultimately went a different route (I think).

clauses = generate_clauses(input)

clause_count = Enum.reduce(clauses, nil, fn clause, acc ->
  q = Ecto.Query.dynamic(
    [foo: a, bar: b],
    fragment("CASE WHEN ? THEN 1 ELSE 0 END", ^clause)
  )
  
  if acc do
    Ecto.Query.dynamic([foo: a, bar: b], ^q + ^acc)
  else
    q
  end
end)

Ecto.Query.from(..., select_merge: ^%{clause_count: clause_count})

I need it done in SQL because I’m going to use that count in a where clause.

Very cool! I had been knee deep in Ecto docs for the past couple of days and still managed to miss that. Going to have to tuck that away in my head for later use. I tried using it here with a like a fragment("sum(?)", splice(clauses)) but it didn’t work cuz that’s not how sql sum works… :grimacing:

Also Liked

dimitarvp

dimitarvp

Why not separate them and do the summing in Elixir? You can also just use select_merge with dummy map keys and then use the resulting map’s values and sum that, similarly to this thread: Aggregate multiple dynamically selected columns in Ecto (without group by) - #3 by zackmichener

def case_stmt(input) do
   clauses = generate_clauses(input)
   bindings = [foo: a, bar: b]

   Enum.reduce(clauses, false, fn clause, query ->
     Ecto.Query.dynamic(
       bindings,
       fragment("CASE WHEN ? THEN 1 ELSE 0 END", ^clause) or ^query
     )
   end)
end
LostKobrakai

LostKobrakai

This might help with variable number of elements, where you don‘t know the number of elements at compile time. Not sure if it works with CASE though.

dimitarvp

dimitarvp

Oh that’s pretty clever. Nice job!

Where Next?

Popular in Questions Top

nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
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
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&query=perfume&page=2, I would like to get: ...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
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
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
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

We're in Beta

About us Mission Statement