Any Java libraries out there that validate SQL syn

2020-01-25 09:37发布

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!

标签: java sql syntax
7条回答
劫难
2楼-- · 2020-01-25 09:56

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.

查看更多
欢心
3楼-- · 2020-01-25 09:58

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

查看更多
对你真心纯属浪费
4楼-- · 2020-01-25 10:03

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.

查看更多
家丑人穷心不美
5楼-- · 2020-01-25 10:06

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

查看更多
爷的心禁止访问
6楼-- · 2020-01-25 10:13

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.

查看更多
Anthone
7楼-- · 2020-01-25 10:19

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

查看更多
登录 后发表回答