So I’m porting an old SQL generator that creates rather amazingly complex queries at run-time, let me hop in to some code to get the point across quickly:
def query_information(main_id, lookups) when is_list(lookups)
Used like:
lookups = [:name, :first_name, :last_name, :middle_initial, :employee_email, :personal_email] # and more, this lookup list is generated dynamically at runtime and can be fairly huge
results = query_information(main_id, lookups) |> Repo.one!()
name = results.name # Results is a map keyed on the lookups with the data
Basically query_information needs to take a large amount of ‘things’ to look up, and this would be fairly trivial if they were all in one table for ecto, however I’m working with a very very old database and querying even just ‘name’ involves 4 tables and 3 joins and a lot of where conditions, and trying to get something like the ethnicity is huge, requiring 3 sub-queries, a couple SQL case statements, and over a dozen joins, it is a bit of hell yes.
And to top it off, the database is slow, even just doing a simple query on a single table (like ‘username’ if you already have the main_id, which you usually don’t, so you require a minimum of one join there, usually more) is not what one would call ‘instant’.
Now interestingly actually running the query is not too slow, it is the sending of the query ‘to’ the server and getting the data back, so need to minimize the sql calls as much as possible, hence this previous amalgamation of horror.
So the old one in another language created a string of SQL, not too hard to do actually considering that the database has one oddity going for it, every single field in every single table is prefixed with the table name, so the ‘id’ column of table ‘blah’ is blah_id, so what the horror query builder did was process all joins that would be necessary for the requested things then make an utterly massive select statement and 'when’s and so much more, all using the field names, no tables needing to be referenced anywhere.
So… I’ve been tasked with re-creating it in Elixir, and I’d prefer not to make the potentially unsafe string horror and would prefer Ecto, however I’m having issues coming up with a good design.
I’m stuck even just on the first step where I have to setup all the joins based on the information. First I iterate the lookups list and have each command return the tables it will need to join at the top level to, then I uniq that, then I need to join them all based on criteria that each needs talking to another, in most cases it is the main_id, in some others it will be querying other fields.
The main issue I am having is the query syntax, to be able to reference prior joined tables I need to pass the bindings in, however I do not know how many bindings the join has at that point unless I pass a counter around, nor would that counter be useful since it requires a static binding name. If these lookups were known at compile-time for a given call this would be trivial in macro’s, however it seems what I am needing is either the expression syntax to not take bindings at all (so I can just reference the globally unique field names as there are no identical ones, except in the few cases a table joins itself, yes that happens too), or I need the bindings to be able to reference a specific table, say by name or something, but I cannot find a way to do this…
Please help… I’ve been fighting this all day… ![]()


















