pglass/sqlitis

Support table aliases

pglass opened this issue · 0 comments

Blocks #22

If we try sqlitis 'select t.name from foo t', it could do two things,

  1. Resolve table aliases: It would return select([foo.c.name]). This would mean maintaining a dict mapping alias -> table name (and whatever that entails for subqueries, separate maps and maybe a hierarchical lookup if table aliases in the query are available to subqueries)

  2. Output Python variable reassignments, and leave the table aliases alone. This would produce multiple python statements (but conflicting alias names among subqueries is another thing to handle).

    t = foo.alias("t")
    select([t.c.name])

Initial thoughts:

  • The second option is truer to the original SQL query in a way. When a SQLAlchemy query object is printed, it prints out the equivalent SQL. That SQL will contain all the original table aliases if we go with the second option, although I'm not sure how important that is to sqlitis users... They might just care to get runnable python. And whether SQLAlchemy is aware of the table aliases should make no difference in the query results (or does it?).
  • On the second option, I feel like I don't want multi-statement output when there is a suitable single-statement equivalent (which there is in this case). But single vs multi statement output could always be made configurable. There are likely other situations where we will need multiple statements (creating tables, inserts, etc) so maybe I should just embrace the multi-statement output.
  • Table aliases kind of infect everything: columns, where clauses, subqueries, and any other place a column or table name can be used. So it needs a comprehensive solution.