SQL parser library - get table names from a query

I am looking for a C / C ++ SQL parsing library that can provide me with the names of the tables that the query depends on.

What I expect:

SELECT * FROM TABLEA NATURAL JOIN TABLEB 

Result: TABLEA, TABLEB

Of course, the above example is extremely simple. I already wrote my own parser (based on Boost.Spirit) that processes a subset of the SQL grammar, but I need a parser that can handle complex (recursive, etc.) queries.

Do you know anything useful for this purpose?

I found http://www.sqlparser.com - it is commercial, but it does exactly what I need. I also ended up in PostgreSQL sources, with no effect.

+4
source share
1 answer

Antlr can create a good SQL parser (C ++ can be for it), and several SQL grammars are available for it: http://www.antlr3.org/grammar/list.html

If all that interests you is the names of the tables, then taking one of these grammars and adding a semantic action that collects these names should be fairly simple.

With experience with Antlr and Bison / Yacc and Lex / Flex, I definitely recommend Antlr. It is written in Java, but the target language can be C ++ - the generated code is actually readable, looks like it is written by a person. Debugging generated by the Antlr parser is quite normal, which cannot be said about those that were created by Bison ..

There are other options, such as, for example, Lemon and sqlite grammar, look at this question if you want: SQL parser in C

+3
source

All Articles