maybe it’s not the way to go, but this was my initial guess.
I have been writing a parser for a reduced query dialect that I have partially inherited and much expanded, allowing users write things like:
plant where accession.code='2018.0047'
it’s not ready, but the missing intermediate steps are clear, except the final one: how do I have the result executed?
I am targeting as result the quote
representation of the equivalent Ecto.Query.from
query. for the above example the equivalent as far as I am concerned would be:
from(p in "plant", select: [:id], join: a in "accession", on: a.id==p.accession_id, where: a.code=="2018.0047")
I have been looking into the structures returned by the __schema__
functions, and all looks quite doable, I mean I know how to extract the table name from the modules, and owner and related modules and keys from the association given its name, so let’s assume that my parser does return this value:
{:from, [context: Elixir, import: Ecto.Query],
[
{:in, [context: Elixir, import: Kernel], [{:p, [], Elixir}, "plant"]},
[
select: [:id],
join: {:in, [context: Elixir, import: Kernel],
[{:a, [], Elixir}, "accession"]},
on: {:==, [context: Elixir, import: Kernel],
[
{{:., [], [{:a, [], Elixir}, :id]}, [], []},
{{:., [], [{:p, [], Elixir}, :accession_id]}, [], []}
]},
where: {:==, [context: Elixir, import: Kernel],
[{{:., [], [{:a, [], Elixir}, :code]}, [], []}, "2018.0047"]}
]
]}
how do I get Ecto to execute it?