有没有验证 SQL 语法的 Java 库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/141499/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Any Java libraries out there that validate SQL syntax?
提问by awied
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.
我想知道是否有任何 Java 库可用于验证 SQL 查询的语法。我知道与常见的 SQL 规范有很多偏差,所以它可能只适用于 SQL:2006 之类的东西,但这肯定就足够了。
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!
谢谢!
采纳答案by Horcrux7
I don't think there are such libraries. The SQL syntax has too many derivatives.
我不认为有这样的图书馆。SQL 语法有太多的派生词。
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.
一个可能的解决方案是使用像SmallSQL这样的开源纯 Java DBMS 的一部分。在这个项目中,您可以创建SQLParser 的一个实例。可以很容易地删除对连接所需的引用。
回答by Tim Howland
回答by PhiLho
Perhaps you can use Antlr, it has a number of SQL grammarsand a Java library, as well as plugins for various Java IDEs.
也许您可以使用 Antlr,它有许多SQL 语法和一个Java 库,以及各种 Java IDE 的插件。
Or as advised, use the parser of open source SQL utilities like SQuirreL SQL Client.
或者按照建议,使用开源 SQL 实用程序的解析器,如SQuirreL SQL Client。
回答by Constantin
Apache Derbyis 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.
Apache Derby是一个完全用 Java 实现的开源 SQL 数据库,可在Apache 许可,版本 2.0 下使用。它以前称为IBM Cloudscape。
You may try to reuse it's parsing code from org.apache.derby.impl.sql
.
您可以尝试重用它的解析代码org.apache.derby.impl.sql
。
回答by Kevin
Try JSQL parser.
试试JSQL 解析器。
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.
除了验证之外,您还可以获得查询的有意义的表示。
例如,这允许您只接受“某些”命令;操作
查询,“美化”它等。
回答by Vladislav Rastrusny
回答by James Wang
General SQL Parsercan do offline SQL syntax check. Supported databases: Oracle, DB2, MySQL, SQL Server, Teradata and PostgreSQL.
一般 SQL Parser可以做离线 SQL 语法检查。支持的数据库:Oracle、DB2、MySQL、SQL Server、Teradata 和 PostgreSQL。