onurozgurozkan
How to change `or` and `and` condition in dynamic method?
Hello,
Newbie alert ![]()
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
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.
0
Popular in Discussions
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
In general I’ve been sticking to this community style guide GitHub - christopheradams/elixir_style_guide: A community driven style guide ...
New
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
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
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
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
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
Hi!
In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir?
Searched the docs for ip address and the web, no good results.
Thanks!
New
I would like to know what is the best IDE for elixir development?
New
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
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
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
New
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
Chat & Discussions>Discussions
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
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #hex
- #security










