Java 寻找可嵌入的 SQL 美化程序或重新格式化程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/312552/
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
Looking for an embeddable SQL beautifier or reformatter
提问by Brian Matthews
I am looking for a Java open source beautifier or reformatter for SQL that I can use to clean up DDL statements that I am generating with openArchitectureWare.
我正在寻找用于 SQL 的 Java 开源美化器或重新格式化程序,我可以用它来清理我使用openArchitectureWare生成的 DDL 语句。
Nothing in the answer to "Online Code Beautifier And Formatter" is of use to me and I have not been able to get Simple SQL Formatterto work for me.
“在线代码美化器和格式化程序”的答案中没有任何内容对我有用,而且我无法让Simple SQL Formatter为我工作。
采纳答案by Robert J. Walker
UPDATE 2:
更新 2:
org.hibernate.jdbc.util.BasicFormatterImpl
got moved in release 4.0. It is now located at: org.hibernate.engine.jdbc.internal.BasicFormatterImpl
.
org.hibernate.jdbc.util.BasicFormatterImpl
在 4.0 版中移动。它现在位于:org.hibernate.engine.jdbc.internal.BasicFormatterImpl
。
UPDATE 1:
更新1:
Technology marches on. As noted by Alex, org.hibernate.pretty.Formatter
no longer exists as of version 3.3.2.GA. The replacement is org.hibernate.jdbc.util.BasicFormatterImpl
:
技术在前进。正如 Alex 所指出的,org.hibernate.pretty.Formatter
从 3.3.2.GA 版本开始不再存在。替换是org.hibernate.jdbc.util.BasicFormatterImpl
:
String formattedSQL = new BasicFormatterImpl().format(sql);
ORIGINAL ANSWER:
原始答案:
If you're using Hibernate, they've got one built-in: org.hibernate.pretty.Formatter
如果您使用的是 Hibernate,它们有一个内置的: org.hibernate.pretty.Formatter
String formattedSQL = new Formatter(sql).format();
回答by tcurdt
You could just use a SQL grammarand build the AST with antlr. Then you can output the tree in any format you like.
回答by Stephen Denne
Part of the eclipse Data Tools Platformis the SQL Development Tools Project.
Eclipse数据工具平台的一部分是SQL 开发工具项目。
The page describing how to use the SQL Query Parserhas an extremely brief use of SQLQuerySourceFormatwhich provides these options:
描述如何使用SQL 查询解析器的页面非常简短地使用了SQLQuerySourceFormat,它提供了以下选项:
- preserveSourceFormat = the option to preserve the input source formating when SQL source text is generated
- statementTerminator = the character separating multiple SQL statements
- hostVariablePrefix = the character that preceedes a host language variable
- parameterMarker = the character that identifies a host language parameter
- delimitedIdentifierQuote* = the character that encloses delimited identifiers whose writing in case will be preserved
- omitSchema = the current schema (omitted in SQL source, implicit to unqualified table references)
- qualifyIdentifiers = the flag describing how identifiers in the SQL source will be qualified
- preserveComments = the option to preserve comments in the parsed SQL source or/and the generated SQL source
- generateCommentsForStatementOnly = the option to generate comments for the SQL source only in the context of the complete statement, or if set to false, for single SQL Query objects outside the context of a statement as well
- preserveSourceFormat = 在生成 SQL 源文本时保留输入源格式的选项
- statementTerminator = 分隔多个 SQL 语句的字符
- hostVariablePrefix = 位于宿主语言变量之前的字符
- parameterMarker = 标识宿主语言参数的字符
- delimitedIdentifierQuote* = 包含分隔标识符的字符,其大小写将被保留
- omitSchema = 当前模式(在 SQL 源中省略,隐含于不合格的表引用)
- qualifyIdentifiers = 描述如何限定 SQL 源中的标识符的标志
- preserveComments = 在解析的 SQL 源或/和生成的 SQL 源中保留注释的选项
- generateCommentsForStatementOnly = 仅在完整语句的上下文中为 SQL 源生成注释的选项,或者如果设置为 false,也为语句上下文之外的单个 SQL 查询对象生成注释
回答by mike g
Perhaps jsqlparserwill work for you.
也许jsqlparser会为你工作。
Not as easy to find as you might think, as there are a fair few defunct projects out there. In fact i failed to find it so ended up doing my own thing (based on the h2 parser - you can contact me if all else fails). As a consequence i do not know if it has a beautifier, but writing one on top should be straight forward enough.
不像你想象的那么容易找到,因为那里有相当多的不复存在的项目。事实上,我没能找到它,所以最终做我自己的事情(基于 h2 解析器 - 如果一切都失败了,你可以联系我)。因此,我不知道它是否有美化剂,但在上面写一个应该足够直截了当。
Its based on a grammar and JavaCC, so probably a better option than reinventing this wheel with antlr in any event. You may find if you need to support various dialects of sql in complex statements that any approach based on a grammar will fail you.
它基于语法和 JavaCC,因此在任何情况下都可能比用 antlr 重新发明这个轮子更好的选择。您可能会发现,如果您需要在复杂的语句中支持各种 sql 方言,那么任何基于语法的方法都会使您失败。
回答by Guy
Have you considered:
你有没有考虑过:
They provide both an API version and a command line version (as well as an online version).
它们提供 API 版本和命令行版本(以及在线版本)。
No knowledge of costs though.
虽然不知道成本。
回答by Ferdeen
Would this work - SQL Formatter.
这行得通吗 - SQL 格式化程序。
回答by Stephan
With Hibernate v3.3.2.GA, org.hibernate.pretty.Formatterdoesn't exist anymore. You can use its replacement : org.hibernate.jdbc.util.BasicFormatterImpl
使用 Hibernate v3.3.2.GA,org.hibernate.pretty.Formatter不再存在。您可以使用它的替代品:org.hibernate.jdbc.util.BasicFormatterImpl
Use org.hibernate.engine.jdbc.internal.BasicFormatterImplfor Hibernate 4.0+.
将org.hibernate.engine.jdbc.internal.BasicFormatterImpl用于 Hibernate 4.0+。
Formatter f = new BasicFormatterImpl();
String formatted_sql_code = f.format(ugly_sql_code);
回答by James Wang
So this is definitely what you are looking for: A SQL formatter librarythat support Oracle, SQL Server, DB2, MySQL, Teradata and PostgreSQL.
所以这绝对是您要找的:支持 Oracle、SQL Server、DB2、MySQL、Teradata 和 PostgreSQL 的SQL 格式化程序库。