tags:

views:

1611

answers:

5

I'm trying to build a .NET Managed data provider and I need to be able to parse SQL commands.

What are good, free (as in beer) objects to do this? - I haven't seen any standard .NET assemblies for this yet.

Thanks, Eli.

+2  A: 

Try General SQL Parser

zaca
Please note that the above link to GSP is no longer valid. This is where you can find it now: http://www.sqlparser.com/
Tom Juergens
+6  A: 

There's a OO parser generator called ANTLR that might help you.

What is ANTLR? ANTLR, ANother Tool for Language Recognition, is a language tool that provides a framework for constructing recognizers, interpreters, compilers, and translators from grammatical descriptions containing actions in a variety of target languages. ANTLR provides excellent support for tree construction, tree walking, translation, error recovery, and error reporting.

In fact, there are a pile of grammars already encoded for ANTLR, including several SQL dialects: Grammar List

(for posterity: note that the "General SQL Parser" also recommended in these answers is not free)

Chris Wuestefeld
+1  A: 

What is your data provider talking to? If you're implementing your own data engine then so be it, but in general your provider should just be a connector and pass the sql on to some other engine.

Joel Coehoorn
I'm writing my own engine, so no help there.
Eli
That's fine. I had to ask.
Joel Coehoorn
+1  A: 

You could also try

Microsoft.Data.Schema.ScriptDom
Microsoft.Data.Schema.ScriptDom.Sql

and the TSql100Parser and related classes, although the documentation really isn't all that helpful (not many examples available). Here are the two most useful links I've found so far:

http://blogs.msdn.com/gertd/archive/2008/08/21/getting-to-the-crown-jewels.aspx
http://stackoverflow.com/questions/1779737/using-the-tsqlparser

It seems to me, though, that even though the parser and generator are complete, it would take quite a bit of work to get to the same level of comfort provided by General SQL Parser, and the license for GSP is not that expensive. In addition, the MS classes only support TSQL, whereas GSP can also work with other variants.

Tom Juergens
Can I use Microsoft.Data.Schema.ScriptDom for SQL Parser Oracle ??
alhambraeidos
Sorry, I've been away a bit. It should work with basic Oracle SQL, but you probably won't get far once you start using more advanced, Oracle-specific syntax which differs from TSQL.
Tom Juergens