To Reproduce
I ran the function below to get the columns but "range" and "section" didn't return as expected.
def find_selected_columns(select_query):
tokens = sqlparse.parse(query)[0].tokens
found_select = False
for token in tokens:
if found_select:
if isinstance(token, sqlparse.sql.IdentifierList):
return [
col.value.split(" ")[-1].strip("`").rpartition('.')[-1]
for col in token.tokens
if isinstance(col, sqlparse.sql.Identifier)
]
else:
found_select = token.match(
sqlparse.tokens.Keyword.DML, ["select", "SELECT"])
raise Exception("Could not find a select statement. Weird query :)")
Expected behavior
Columns "range" and "section" needs to be returned.
Versions (please complete the following information):
Describe the bug select_query = 'select locid, locname, drn, pipelineid, pipelineshortname, loctype, subtype, facilitytype, town, range, section, segmentmilepost, milemarker, segmentname, locpro, zone, dirflow, statuscode, state, county, unsubscribedcapacity, latitude, longitude, pbgroupcode, enableparkloan, updwnpartydrn, scadastationcode, updatedt, createon, getdate() ap_import_time from table1'
To Reproduce I ran the function below to get the columns but "range" and "section" didn't return as expected.
def find_selected_columns(select_query): tokens = sqlparse.parse(query)[0].tokens found_select = False for token in tokens: if found_select: if isinstance(token, sqlparse.sql.IdentifierList): return [ col.value.split(" ")[-1].strip("`").rpartition('.')[-1] for col in token.tokens if isinstance(col, sqlparse.sql.Identifier) ] else: found_select = token.match( sqlparse.tokens.Keyword.DML, ["select", "SELECT"]) raise Exception("Could not find a select statement. Weird query :)")
Expected behavior Columns "range" and "section" needs to be returned.
Versions (please complete the following information):