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

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -