Question

I'm not sure if this even exists or not, so I figured I would tap the wisdom of others..

I was wondering if there are any Java libraries out there that can be used to validate a SQL query's syntax. I know that there are many deviations from common SQL spec, so it would probably only work against something like SQL:2006, but that would certainly suffice.

My goal is to use this for unit-testing purposes without needing to attempt the execution against the DB. I know it's of limited use, but it would still be useful.

Thanks!

Was it helpful?

Solution

I don't think there are such libraries. The SQL syntax has too many derivatives.

A possible solution would be to use parts of an open source pure Java DBMS like SmallSQL. In this project you can create an instance of the SQLParser. The needed references to the connection can be removed very easily.

OTHER TIPS

Perhaps you can use Antlr, it has a number of SQL grammars and a Java library, as well as plugins for various Java IDEs.

Or as advised, use the parser of open source SQL utilities like SQuirreL SQL Client.

General SQL Parser can do offline SQL syntax check. Supported databases: Oracle, DB2, MySQL, SQL Server, Teradata and PostgreSQL.

Try JSQL parser.

In addition to validating, you obtain a meaningful representation of the query.
This allows you, for example, to only accept "certain" commands; manipulate
the query, "prettify" it, etc.

You might be able to extract the parsing code out from HSQL, which is java and open source.

Apache Derby is an open source SQL database implemented entirely in Java and available under the Apache License, Version 2.0. It was formerly known as IBM Cloudscape.

You may try to reuse it's parsing code from org.apache.derby.impl.sql.

With JOOQ you will never make a mistake in SQL. Java compiler will take care of that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top