abdulsattar

abdulsattar

How does this work?

query = from u in "users",
          where: u.age > 18,
          select: u.name

I get that from is a macro, which essentially takes two arguments:

query = from(u in "users",
          [where: u.age > 18,
          select: u.name])

which makes it more mysterious. u in "users" what does it do? The keyword list has u.age, where does the u come from? How is throwing u not defined exception?

The macro based syntax is also suprising:

"users"
|> where([u], u.age > 18)
|> select([:name])

How does where([u], u.age > 18) not fail with u not defined?

Showing Posts 1 to 2

LostKobrakai

LostKobrakai

Elixir is not directly using what you wrote for the ecto query, but ecto transforms the AST of your code into something that makes sense to elixir.

minhajuddin

minhajuddin

A macro doesn’t execute the code passed in, It gets an AST of the code and it can then do whatever it wants. I haven’t looked at how Ecto actually does this, I am assuming that it gets a hold of the AST and transforms it into a SQL query based on the operators that are being used and then it executes the code. One way to inspect it is by looking at the struct that was returned:

iex(7)> q = from(u in User, where: u.id == 3, order_by: [asc: :id]) |> Map.from_struct
%{
  assocs: [],
  distinct: nil,
  from: {"users", UA.Accounts.User},
  group_bys: [],
  havings: [],
  joins: [],
  limit: nil,
  lock: nil,
  offset: nil,
  order_bys: [
    %Ecto.Query.QueryExpr{
      expr: [asc: {{:., [], [{:&, [], [0]}, :id]}, [], []}],
      file: "iex",
      line: 7,
      params: []
    }
  ],
  prefix: nil,
  preloads: [],
  select: nil,
  sources: nil,
  updates: [],
  wheres: [
    %Ecto.Query.BooleanExpr{
      expr: {:==, [],
       [
         {{:., [], [{:&, [], [0]}, :id]}, [], []},
         %Ecto.Query.Tagged{tag: nil, type: {0, :id}, value: 3}
       ]},
      file: "iex",
      line: 7,
      op: :and,
      params: []
    }
  ]
}

Here is an example that should give you more clarith around macros, the expression 1 + 2 is never evaluated:

defmodule M do
  defmacro add({:+, _, [op1, op2]}) do
    quote do
      "Add #{unquote(op1)} and #{unquote(op2)} yourself!"
    end
  end
end

defmodule Test do
  def test do
    require M
    M.add(1 + 3)
  end
end

Test.test() |> IO.puts
# => Add 1 and 3 yourself!
— 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
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
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
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

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
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 & 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
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews