Parsing/Tokenizing a String Containing a SQL Command

Posted by Alan Storm on Stack Overflow See other posts from Stack Overflow or by Alan Storm
Published on 2010-03-13T19:04:08Z Indexed on 2010/03/13 19:15 UTC
Read the original article Hit count: 386

Filed under:
|
|
|
|

Are there any open source libraries (any language, python/PHP preferred) that will tokenize/parse an ANSI SQL string into its various components?

That is, if I had the following string

SELECT a.foo, b.baz, a.bar FROM TABLE_A a LEFT JOIN TABLE_B b ON a.id = b.id WHERE baz = 'snafu';

I'd get back a data structure/object something like

//fake PHPish $results['select-columns'] = Array[a.foo,b.baz,a.bar]; $results['tables'] = Array[TABLE_A,TABLE_B]; $results['table-aliases'] = Array[a=>TABLE_A, b=>TABLE_B]; //etc...

Restated, I'm looking for the code in a database package that teases the SQL command apart so that the engine knows what to do with it. Searching the internet turns up a lot of results on how to parse a string WITH SQL. That's not what I want.

I realize I could glop through an open source database's code to find what I want, but I was hoping for something a little more ready made, (although if you know where in the MySQL, PostgreSQL, SQLite source to look, feel free to pass it along)

Thanks!

© Stack Overflow or respective owner

Related posts about php

Related posts about python