onurozgurozkan

onurozgurozkan

How to change `or` and `and` condition in dynamic method?

Hello,

Newbie alert :grinning:

My company tries to move LARAVEL + VUE to Phoenix. We try to build a dynamic query system for Ecto.

I use dynamic method (macro) for building filter system. The SQL condition (“and” or “or”) come from the JSON. I need to pass condition (string or atom or some type) to dynamic method because I don’t want to write some function twice.

  def build_query(dynamic, "or", _data_type, field, _id, ">", value) do
    dynamic([q], ^dynamic or field(q, ^field) > ^value)
  end

  def build_query(dynamic, "and", _data_type, field, _id, ">", value) do
    dynamic([q], ^dynamic and field(q, ^field) > ^value)
  end

What I want,

  def build_query(dynamic, condition, _data_type, field, _id, ">", value) do
    dynamic([q], ^dynamic ^condition field(q, ^field) > ^value)
  end

But I get invalid query expression error.

** (Ecto.Query.CompileError) `condition()` is not a valid query expression.

What is the best way to pass a variable to dynamic as and and or and change it?

Regards.

First Post!

LostKobrakai

LostKobrakai

There is no good way to do that. What you already have is the way to go.

Edit:

dynamic(fragment("? ? ?", 1, literal(^"OR"), 2))

This would work, but generally you want to avoid raw SQL where possible.

Where Next?

Popular in Discussions Top

scouten
I’m looking for a host for the server part of a small (personal) side project that I’m working on. It’s currently written in Node.js and ...
New
ben-pr-p
In general I’ve been sticking to this community style guide GitHub - christopheradams/elixir_style_guide: A community driven style guide ...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
Fl4m3Ph03n1x
Background A few days ago I was listening to The future of Elixir from Elixir Talks, with Dave Thomas (@pragdave ) and Brian Mitchell. I...
New
crispinb
On reading dhh’s latest The One Person Framework it strikes me that Phoenix with LiveView is already pretty much this. However, never hav...
New
fireproofsocks
I’ve been working on an Elixir project that has required a lot of scripting. I usually reach for Elixir because I like it more (and in th...
New
matthias_toepp
I’d love to hear what people think about Wisp, the new Gleam web framework started by Gleam’s primary creator Louis Pilfold. Gleam, alon...
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
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
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
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42533 114
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement