public class SqlPrettyWriter extends Object implements SqlWriter
There are several options to control the format.
| Option | Description | Default |
|---|---|---|
SqlWriterConfig.clauseStartsLine() ClauseStartsLine} |
Whether a clause (FROM, WHERE, GROUP BY,
HAVING, WINDOW, ORDER BY) starts a new line.
SELECT is always at the start of a line. |
true |
ClauseEndsLine |
Whether a clause (SELECT, FROM, WHERE,
GROUP BY, HAVING, WINDOW, ORDER BY) is
followed by a new line. |
false |
CaseClausesOnNewLines |
Whether the WHEN, THEN and ELSE clauses of a CASE expression appear at the start of a new line. | false |
Indentation |
Number of spaces to indent | 4 |
KeywordsLowerCase |
Whether to print keywords (SELECT, AS, etc.) in lower-case. | false |
AlwaysUseParentheses |
Whether to enclose all expressions in parentheses, even if the operator has high enough precedence that the parentheses are not required. For example, the parentheses are required in the expression
|
false |
QuoteAllIdentifiers |
Whether to quote all identifiers, even those which would be correct
according to the rules of the SqlDialect if quotation marks were
omitted. |
true |
SubQueryStyle |
Style for formatting sub-queries. Values are:
Hyde,
Black. |
Hyde |
LineLength |
The desired maximum length for lines (to look nice in editors, printouts, etc.). | -1 (no maximum) |
FoldLength |
The line length at which lines are folded or chopped down
(see LineFolding). Only has an effect if clauses are marked
CHOP or
FOLD. |
80 |
LineFolding |
How long lines are to be handled. Options are lines are WIDE (do not wrap), FOLD (wrap if long), CHOP (chop down if long), and TALL (wrap always). | WIDE |
SelectFolding |
How the SELECT clause is to be folded. |
LineFolding |
FromFolding |
How the FROM clause and nested JOIN clauses are to be
folded. |
LineFolding |
WhereFolding |
How the WHERE clause is to be folded. |
LineFolding |
GroupByFolding |
How the GROUP BY clause is to be folded. |
LineFolding |
HavingFolding |
How the HAVING clause is to be folded. |
LineFolding |
OrderByFolding |
How the ORDER BY clause is to be folded. |
LineFolding |
WindowFolding |
How the WINDOW clause is to be folded. |
LineFolding |
OverFolding |
How window declarations in the WINDOW clause
and in the OVER clause of aggregate functions are to be folded. |
LineFolding |
ValuesFolding |
How lists of values in the VALUES clause are to be folded. |
LineFolding |
UpdateSetFolding |
How assignments in the SET clause of an UPDATE statement
are to be folded. |
LineFolding |
The following options exist for backwards compatibility. They are
used if LineFolding and clause-specific
options such as SelectFolding are not
specified:
SelectListItemsOnSeparateLines
replaced by SelectFolding,
GroupByFolding, and
OrderByFolding;
UpdateSetListNewline
replaced by UpdateSetFolding;
WindowDeclListNewline
replaced by WindowFolding;
WindowNewline
replaced by OverFolding;
ValuesListNewline
replaced by ValuesFolding.
| Modifier and Type | Class and Description |
|---|---|
protected class |
SqlPrettyWriter.FrameImpl
Implementation of
SqlWriter.Frame. |
SqlWriter.Frame, SqlWriter.FrameType, SqlWriter.FrameTypeEnum, SqlWriter.SubQueryStyle| Modifier and Type | Field and Description |
|---|---|
protected @Nullable SqlPrettyWriter.FrameImpl |
frame |
protected static CalciteLogger |
LOGGER |
protected @Nullable String |
nextWhitespace |
protected static String |
NL |
| Constructor and Description |
|---|
SqlPrettyWriter()
Creates a writer with the default configuration.
|
SqlPrettyWriter(SqlDialect dialect)
Deprecated.
|
SqlPrettyWriter(SqlDialect dialect,
boolean alwaysUseParentheses)
Deprecated.
|
SqlPrettyWriter(SqlDialect dialect,
boolean alwaysUseParentheses,
PrintWriter pw)
Deprecated.
|
SqlPrettyWriter(SqlDialect dialect,
SqlWriterConfig config)
Deprecated.
|
SqlPrettyWriter(SqlDialect dialect,
SqlWriterConfig config,
StringBuilder buf)
Creates a writer with the given configuration and dialect,
and a given print writer (or a private print writer if it is null).
|
SqlPrettyWriter(SqlWriterConfig config)
Creates a writer with the given configuration,
and a private builder.
|
SqlPrettyWriter(SqlWriterConfig config,
StringBuilder buf)
Creates a writer with the given configuration
and a given buffer to write to.
|
| Modifier and Type | Method and Description |
|---|---|
static SqlWriterConfig |
config()
Creates a
SqlWriterConfig with Calcite's SQL dialect. |
protected SqlPrettyWriter.FrameImpl |
createListFrame(SqlWriter.FrameType frameType,
@Nullable String keyword,
String open,
String close)
Creates a list frame.
|
void |
describe(PrintWriter pw,
boolean omitDefaults)
Prints the property settings of this pretty-writer to a writer.
|
void |
dynamicParam(int index)
Prints a dynamic parameter (e.g.
|
void |
endFunCall(SqlWriter.Frame frame)
Ends a list which is a call to a function.
|
void |
endList(@Nullable SqlWriter.Frame frame)
Ends a list.
|
void |
fetchOffset(@Nullable SqlNode fetch,
@Nullable SqlNode offset)
Prints the OFFSET/FETCH clause.
|
String |
format(SqlNode node) |
SqlDialect |
getDialect()
Returns the dialect of SQL.
|
int |
getIndentation()
Deprecated.
|
int |
getLineLength()
Deprecated.
|
void |
identifier(String name,
boolean quoted)
Prints an identifier, quoting as necessary.
|
boolean |
inQuery()
Returns whether we are currently in a query context (SELECT, INSERT,
UNION, INTERSECT, EXCEPT, and the ORDER BY operator).
|
boolean |
isAlwaysUseParentheses()
Deprecated.
|
boolean |
isClauseStartsLine()
Deprecated.
|
boolean |
isKeywordsLowerCase()
Deprecated.
|
boolean |
isQuoteAllIdentifiers()
Deprecated.
|
boolean |
isSelectListExtraIndentFlag()
Deprecated.
|
boolean |
isSelectListItemsOnSeparateLines()
Deprecated.
|
boolean |
isWhereListItemsOnSeparateLines()
Deprecated.
|
void |
keyword(String s)
Prints a sequence of keywords.
|
SqlWriter |
list(SqlWriter.FrameTypeEnum frameType,
Consumer<SqlWriter> action)
Writes a list.
|
SqlWriter |
list(SqlWriter.FrameTypeEnum frameType,
SqlBinaryOperator sepOp,
SqlNodeList list)
|
void |
literal(String s)
Prints a literal, exactly as provided.
|
void |
newlineAndIndent()
Prints a new line, and indents.
|
void |
newlineAndIndent(int indent) |
void |
print(int x)
Prints an integer.
|
void |
print(String s)
Prints a string, preceded by whitespace if necessary.
|
void |
reset()
Resets this writer so that it can format another expression.
|
void |
resetSettings()
Resets all properties to their default values.
|
void |
sep(String sep)
Writes a list separator, unless the separator is "," and this is the
first occurrence in the list.
|
void |
sep(String sep,
boolean printFirst)
Writes a list separator.
|
void |
setAlwaysUseParentheses(boolean alwaysUseParentheses)
Deprecated.
|
void |
setCaseClausesOnNewLines(boolean caseClausesOnNewLines)
Deprecated.
|
void |
setClauseStartsLine(boolean clauseStartsLine)
Deprecated.
|
void |
setFormatOptions(@Nullable SqlFormatOptions options) |
void |
setIndentation(int indentation)
Deprecated.
|
void |
setKeywordsLowerCase(boolean keywordsLowerCase)
Deprecated.
|
void |
setLineLength(int lineLength)
Deprecated.
|
void |
setNeedWhitespace(boolean needWhitespace)
Sets whether whitespace is needed before the next token.
|
void |
setQuoteAllIdentifiers(boolean quoteAllIdentifiers)
Deprecated.
|
void |
setSelectListExtraIndentFlag(boolean selectListExtraIndentFlag)
Deprecated.
|
void |
setSelectListItemsOnSeparateLines(boolean b)
Deprecated.
|
void |
setSettings(Properties properties)
Sets settings from a properties object.
|
void |
setSubQueryStyle(SqlWriter.SubQueryStyle subQueryStyle)
Deprecated.
|
void |
setWhereListItemsOnSeparateLines(boolean whereListItemsOnSeparateLines)
Deprecated.
|
void |
setWindowDeclListNewline(boolean windowDeclListNewline)
Deprecated.
|
void |
setWindowNewline(boolean windowNewline)
Deprecated.
|
SqlWriter.Frame |
startFunCall(String funName)
Starts a list which is a call to a function.
|
SqlWriter.Frame |
startList(SqlWriter.FrameTypeEnum frameType)
Starts a list with no opening string.
|
SqlWriter.Frame |
startList(SqlWriter.FrameType frameType,
String open,
String close)
Starts a list.
|
protected SqlWriter.Frame |
startList(SqlWriter.FrameType frameType,
@Nullable String keyword,
String open,
String close)
Starts a list.
|
SqlWriter.Frame |
startList(String open,
String close)
Starts a list.
|
protected boolean |
tooLong(String s) |
void |
topN(@Nullable SqlNode fetch,
@Nullable SqlNode offset)
Prints the TOP(n) clause.
|
SqlString |
toSqlString()
Returns the contents of this writer as a 'certified kocher' SQL string.
|
String |
toString() |
protected void |
whiteSpace() |
protected static final CalciteLogger LOGGER
protected static final String NL
protected @Nullable SqlPrettyWriter.FrameImpl frame
protected @Nullable String nextWhitespace
public SqlPrettyWriter(SqlWriterConfig config, StringBuilder buf)
public SqlPrettyWriter(SqlDialect dialect, SqlWriterConfig config, StringBuilder buf)
@Deprecated public SqlPrettyWriter(SqlDialect dialect, SqlWriterConfig config)
@Deprecated public SqlPrettyWriter(SqlDialect dialect, boolean alwaysUseParentheses, PrintWriter pw)
@Deprecated public SqlPrettyWriter(SqlDialect dialect, boolean alwaysUseParentheses)
@Deprecated public SqlPrettyWriter(SqlDialect dialect)
public SqlPrettyWriter(SqlWriterConfig config)
public SqlPrettyWriter()
config()public static SqlWriterConfig config()
SqlWriterConfig with Calcite's SQL dialect.@Deprecated public void setCaseClausesOnNewLines(boolean caseClausesOnNewLines)
@Deprecated public void setSubQueryStyle(SqlWriter.SubQueryStyle subQueryStyle)
@Deprecated public void setWindowNewline(boolean windowNewline)
@Deprecated public void setWindowDeclListNewline(boolean windowDeclListNewline)
@Deprecated public int getIndentation()
SqlWritergetIndentation in interface SqlWriter@Deprecated public boolean isAlwaysUseParentheses()
SqlWriterFor example, the parentheses are required in the expression (a +
b) * c because the '*' operator has higher precedence than the '+'
operator, and so without the parentheses, the expression would be
equivalent to a + (b * c). The fully-parenthesized
expression, ((a + b) * c) is unambiguous even if you don't
know the precedence of every operator.
isAlwaysUseParentheses in interface SqlWriterpublic boolean inQuery()
SqlWriter@Deprecated public boolean isQuoteAllIdentifiers()
SqlWriterisQuoteAllIdentifiers in interface SqlWriter@Deprecated public boolean isClauseStartsLine()
SqlWriterisClauseStartsLine in interface SqlWriter@Deprecated public boolean isSelectListItemsOnSeparateLines()
SqlWriterisSelectListItemsOnSeparateLines in interface SqlWriter@Deprecated public boolean isWhereListItemsOnSeparateLines()
@Deprecated public boolean isSelectListExtraIndentFlag()
@Deprecated public boolean isKeywordsLowerCase()
SqlWriterisKeywordsLowerCase in interface SqlWriter@Deprecated public int getLineLength()
public void resetSettings()
SqlWriterresetSettings in interface SqlWriterpublic void reset()
SqlWriterSqlWriter.resetSettings()@Deprecated public void setIndentation(int indentation)
public void describe(PrintWriter pw, boolean omitDefaults)
pw - WriteromitDefaults - Whether to omit properties whose value is the same as
the defaultpublic void setSettings(Properties properties)
@Deprecated public void setClauseStartsLine(boolean clauseStartsLine)
@Deprecated public void setSelectListItemsOnSeparateLines(boolean b)
@Deprecated public void setSelectListExtraIndentFlag(boolean selectListExtraIndentFlag)
@Deprecated public void setKeywordsLowerCase(boolean keywordsLowerCase)
@Deprecated public void setWhereListItemsOnSeparateLines(boolean whereListItemsOnSeparateLines)
@Deprecated public void setAlwaysUseParentheses(boolean alwaysUseParentheses)
public void newlineAndIndent()
SqlWriternewlineAndIndent in interface SqlWriterpublic void newlineAndIndent(int indent)
@Deprecated public void setQuoteAllIdentifiers(boolean quoteAllIdentifiers)
protected SqlPrettyWriter.FrameImpl createListFrame(SqlWriter.FrameType frameType, @Nullable String keyword, String open, String close)
Derived classes should override this method to specify the indentation of the list.
frameType - What type of listkeyword - The keyword to be printed at the start of the listopen - The string to print at the start of the listclose - The string to print at the end of the listprotected SqlWriter.Frame startList(SqlWriter.FrameType frameType, @Nullable String keyword, String open, String close)
frameType - Type of list. For example, a SELECT list will be
governed according to SELECT-list formatting preferences.open - String to print at the start of the list; typically "(" or
the empty string.close - String to print at the end of the list.public void endList(@Nullable SqlWriter.Frame frame)
SqlWriterendList in interface SqlWriterframe - The frame which was created by SqlWriter.startList(java.lang.String, java.lang.String).public SqlString toSqlString()
SqlWritertoSqlString in interface SqlWriterpublic SqlDialect getDialect()
SqlWritergetDialect in interface SqlWriterpublic void literal(String s)
SqlWriterpublic void keyword(String s)
SqlWriterkeyword("SELECT"),
keyword("CHARACTER SET").protected void whiteSpace()
protected boolean tooLong(String s)
public void print(String s)
SqlWriterpublic void print(int x)
SqlWriterpublic void identifier(String name, boolean quoted)
SqlWriteridentifier in interface SqlWritername - The identifier namequoted - Whether this identifier was quoted in the original sql statement,
this may not be the only factor to decide whether this identifier
should be quotedpublic void dynamicParam(int index)
SqlWriter? for default JDBC)dynamicParam in interface SqlWriterpublic void fetchOffset(@Nullable SqlNode fetch, @Nullable SqlNode offset)
SqlWriterfetchOffset in interface SqlWriterpublic void topN(@Nullable SqlNode fetch, @Nullable SqlNode offset)
SqlWritertopN in interface SqlWriterSqlWriter.fetchOffset(org.apache.calcite.sql.SqlNode, org.apache.calcite.sql.SqlNode)public SqlWriter.Frame startFunCall(String funName)
SqlWriterstartFunCall in interface SqlWriterSqlWriter.endFunCall(Frame)public void endFunCall(SqlWriter.Frame frame)
SqlWriterendFunCall in interface SqlWriterframe - FrameSqlWriter.startFunCall(String)public SqlWriter.Frame startList(String open, String close)
SqlWriterpublic SqlWriter.Frame startList(SqlWriter.FrameTypeEnum frameType)
SqlWriterpublic SqlWriter.Frame startList(SqlWriter.FrameType frameType, String open, String close)
SqlWriterpublic SqlWriter list(SqlWriter.FrameTypeEnum frameType, Consumer<SqlWriter> action)
SqlWriterpublic SqlWriter list(SqlWriter.FrameTypeEnum frameType, SqlBinaryOperator sepOp, SqlNodeList list)
SqlWriterpublic void sep(String sep)
SqlWriterpublic void sep(String sep, boolean printFirst)
SqlWriterpublic void setNeedWhitespace(boolean needWhitespace)
SqlWritersetNeedWhitespace in interface SqlWriter@Deprecated public void setLineLength(int lineLength)
public void setFormatOptions(@Nullable SqlFormatOptions options)
Copyright © 2012-2022 Apache Software Foundation. All Rights Reserved.