How to parse SQL Queries and sub queries using sqlparser into python -
want parse sql join query, select sub query python. using sqlparse library. not parse sub query. how can parse whole query.
e.g:
query = "select id,fname,lname,address res_users r left join res_partner p on p.id=r.partner_id name = (select name res_partner id = 1)" query_tokens = sqlparse.parse(query)[0].tokens
i not parse select name res_partner id = 1
sub query.
not elegant, works:
import sqlparse sqlparse.sql import where, comparison, parenthesis query = """ select id,fname,lname,address res_users r left join res_partner p on p.id=r.partner_id name = (select name res_partner id = 1)""" query_tokens = sqlparse.parse(query)[0] = next(token token in query_tokens.tokens if isinstance(token, where)) condition = next(token token in where.tokens if isinstance(token, comparison)) subquery = next(token token in condition.tokens if isinstance(token, parenthesis)) print subquery
prints:
(select name res_partner id = 1)
Comments
Post a Comment