0

I'm writing a code to expand a SELECT query that uses * or alias.* into SELECT query with defined column names.

The idea is to supply the query, table list and associated column list and return a query with all those columns defined within the query.

Example

table_names = ["my_table", "other_table"]
column_names = {
    "my_table": ["id", "column1", "column2"],
    "other_table": ["id", "column3", "column4"]
}
sql_query = """SELECT * FROM (SELECT * FROM my_table as m join other_table as o on o.id=m.id) AS subquery"""

result_query = """SELECT subquery.id,subquery.column1,subquery.column2,subquery.id,subquery.column3,subquery.column4 FROM (SELECT my_table.id,my_table.column1,my_table.column2,other_table.id,other_table.column3,other_table.column4 FROM my_table as m join other_table as o on o.id=m.id) AS subquery

I have tried getting the column list vie sql_metadata, but I was wondering if there's a library that can already do it.

If not, do we just get a list of tables and append the column list side by side?

Thanks in advance.

NOTE: I cannot directly connect to the DB and need to do this outside of the system with python

4
  • 1
    What do you mean you can't connect to the DB? How else would you get the column names? Do you know them beforehand? Are you just trying to replace the * like a wild card from your pre-defined column_names dictionary? It's not clear. Commented Jul 11, 2024 at 17:52
  • Yes exactly. I can query the DB via API call, but not with a direct connection. I can make Select queries to get the column names for a given table from INFORMATION_SCHEMA And yes, I'm just trying to replace the * or alias.* with the full column names Commented Jul 11, 2024 at 17:54
  • If you do have the option to query information_schema.columns, I'm not sure what the problem is then. If a client library knows and is able to expand/suggest the column names, that or pg_attribute is exactly how it gets that information. You can also add a limit 0 to the query and the db will do that same expansion for you, assembling an empty result structure with column names and types. Commented Jul 11, 2024 at 19:02
  • Would that be affected by structure of the query? If there are bunch of nested queries within it, will it take a long time, or is the engine smart enough to only query one row to get the data. I want to avoid taxing the system as the tables used are quite large. Commented Jul 11, 2024 at 20:45

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.