Checking if Ecto.Query has specific expression

Is there a possibility to check if the given Ecto Query has a specific expression?
For example, if query q has already expression order_by filled in

Take a look at: Ecto.Query — Ecto v3.8.3

You can access the order_bys like this:

iex(1)> query = from u in "users", order_by: [], select: [, u.age]
#Ecto.Query<from u0 in "users", order_by: [asc:],
 select: [, u0.age]>
iex(2)> query.order_bys
    expr: [asc: {{:., [], [{:&, [], [0]}, :name]}, [], []}],
    file: "iex",
    line: 12,
    params: []
1 Like

Always keep in mind that most parts of the query internals are implementation details and might change at any time.