SQL Parsing library for Python

Juan Carlos Coto picture Juan Carlos Coto · Jan 24, 2013 · Viewed 9.7k times · Source

We need a SQL parsing or decomposing library for Python. We would like to be able to input a SQL text query and then get the query parts back as a result. It doesn't need to be fancy, or anything, but we would like to avoid doing the parsing ourselves. Ideally, we could do something like:

the_query = "select something from some_table where blah = 'thing' limit 15"
query_parts = the_library.parse(the_query)
print query_parts.limit().val()

>>> '15'

And this, too:

the_query = "select something from some_table where blah = 'thing'"
query_parts = the_library.parse(the_query)
print query_parts.limit().val()

>>> None

Can anyone give us any pointers for this? If the functionality is more limited, that's OK as well.

Thanks a lot!

Answer

dwurf picture dwurf · Jan 24, 2013

You might like to take a look at sqlparse

Blatantly stolen from their homepage:

>>> # Parsing
>>> res = sqlparse.parse('select * from "someschema"."mytable" where id = 1')
>>> res
<<< (<Statement 'select...' at 0x9ad08ec>,)
>>> stmt = res[0]
>>> stmt.to_unicode()  # converting it back to unicode
<<< u'select * from "someschema"."mytable" where id = 1'
>>> # This is how the internal representation looks like:
>>> stmt.tokens
<<<
(<DML 'select' at 0x9b63c34>,
 <Whitespace ' ' at 0x9b63e8c>,
 <Operator '*' at 0x9b63e64>,
 <Whitespace ' ' at 0x9b63c5c>,
 <Keyword 'from' at 0x9b63c84>,
 <Whitespace ' ' at 0x9b63cd4>,
 <Identifier '"somes...' at 0x9b5c62c>,
 <Whitespace ' ' at 0x9b63f04>,
 <Where 'where ...' at 0x9b5caac>)
>>>