When I take select_parser.py from pyparsing, and run it with pyparsing 2.2.0 using this code:
query="select z.a, b from test_table left join test2_table where 1=1 and b in (select bb from foo)"
for key, val in select_stmt.parseString(query, parseAll=True).items():
print "%s: %s" % (key, val)
I get
$ python select_parser.pyparsing.py
where_expr: [['1', '=', '1'], 'AND', ['b', 'IN', ['SELECT', [['bb']], 'FROM', 'foo']]]
from: [['test_table', ['LEFT', 'JOIN'], 'test2_table', []]]
columns: [['z.a'], ['b']]
Although there is a named element "table" in the original definition:
single_source = ( (Group(database_name("database") + "." + table_name("table*")) | table_name("table*")) +
there is no dict key that comes out with the name "table".
Perhaps the "from" element consumed things first? I don't understand the exact logic of how the named elements get populated, and I haven't gotten a clear idea from reading the docs (several talks, etc.).
How can I use select_parser.py to get all the table names in a SQL query?
Note: the right answer here is a list (or set): test_table, test2_table, foo.
I could go through "from" and ditch lists, but that seems hacky, I don't know if it would work, and it doesn't seem like how pyparsing is supposed to work.
I see this question, this one, and this one, but I don't understand how they help here.