Is there a way of abstracting fragments from a query in Ecto?

Hello everyone. Is there a way of abstracting fragments from a query? For example, lets say that I have this query:

from(..., order_by: [
    fragment(
        """
            CASE 
                WHEN ? = 'elixir' THEN 1,
                WHEN ? = 'ruby' THEN 2,
                WHEN ? = 'gleam' THEN 3
            END
        """,
        language.name
    ),
    fragment("""
            CASE 
                WHEN ? = 'something' THEN 1,
                WHEN ? = 'anotherthing' THEN 2,
            END
        """,
        language.somestuff
    )
]

Let’s say I want to do this order_by in 2 different queries. Is there a way to abstract this?

Thank you for your help :pray:

1 Like

https://hexdocs.pm/ecto/3.12.4/Ecto.Query.API.html#fragment/1-defining-custom-functions-using-macros-and-fragment

3 Likes

Thank you @LostKobrakai :pray: