public class SqlPrettyWriter extends java.lang.Object implements SqlWriter
There are several options to control the format.
Option | Description | Default |
---|---|---|
SelectListItemsOnSeparateLines
|
Whether each item in the select clause is on its own 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 |
ParenthesizeAllExprs |
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 |
|
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 |
SelectListItemsOnSeparateLines
|
Whether each item in the select clause is on its own line. | false |
SubQueryStyle |
Style for formatting sub-queries. Values are:
Hyde ,
Black . |
Hyde |
LineLength |
Set the desired maximum length for lines (to look nice in editors, printouts, etc.). | 0 |
Modifier and Type | Class and Description |
---|---|
private static class |
SqlPrettyWriter.Bean
Helper class which exposes the get/set methods of an object as
properties.
|
protected class |
SqlPrettyWriter.FrameImpl
Implementation of
SqlWriter.Frame . |
SqlWriter.Frame, SqlWriter.FrameType, SqlWriter.FrameTypeEnum, SqlWriter.SubQueryStyle
Modifier and Type | Field and Description |
---|---|
protected boolean |
alwaysUseParentheses |
private SqlPrettyWriter.Bean |
bean |
private boolean |
caseClausesOnNewLines |
private int |
charCount |
private boolean |
clauseStartsLine |
private int |
currentIndent |
private static SqlPrettyWriter.Bean |
DEFAULT_BEAN
Bean holding the default property values.
|
private SqlDialect |
dialect |
private com.google.common.collect.ImmutableList.Builder<java.lang.Integer> |
dynamicParameters |
protected SqlPrettyWriter.FrameImpl |
frame |
private int |
indentation |
private boolean |
keywordsLowerCase |
private int |
lineLength |
private java.util.Deque<SqlPrettyWriter.FrameImpl> |
listStack |
protected static CalciteLogger |
LOGGER |
private boolean |
needWhitespace |
protected java.lang.String |
nextWhitespace |
protected static java.lang.String |
NL |
protected java.io.PrintWriter |
pw |
private boolean |
quoteAllIdentifiers |
private boolean |
selectListExtraIndentFlag |
private boolean |
selectListItemsOnSeparateLines |
private SqlWriter.SubQueryStyle |
subQueryStyle |
private java.io.StringWriter |
sw |
private boolean |
updateSetListNewline |
private boolean |
whereListItemsOnSeparateLines |
private boolean |
windowDeclListNewline |
private boolean |
windowNewline |
Constructor and Description |
---|
SqlPrettyWriter(SqlDialect dialect) |
SqlPrettyWriter(SqlDialect dialect,
boolean alwaysUseParentheses) |
SqlPrettyWriter(SqlDialect dialect,
boolean alwaysUseParentheses,
java.io.PrintWriter pw) |
Modifier and Type | Method and Description |
---|---|
protected SqlPrettyWriter.FrameImpl |
createListFrame(SqlWriter.FrameType frameType,
java.lang.String keyword,
java.lang.String open,
java.lang.String close)
Creates a list frame.
|
void |
describe(java.io.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(SqlWriter.Frame frame)
Ends a list.
|
void |
fetchOffset(SqlNode fetch,
SqlNode offset)
Prints the OFFSET/FETCH clause.
|
java.lang.String |
format(SqlNode node) |
private SqlPrettyWriter.Bean |
getBean()
Returns an object which encapsulates each property as a get/set method.
|
SqlDialect |
getDialect()
Returns the dialect of SQL.
|
int |
getIndentation()
Returns the offset for each level of indentation.
|
int |
getLineLength() |
void |
identifier(java.lang.String name,
boolean quoted)
Prints an identifier, quoting as necessary.
|
(package private) void |
indent(int indent) |
boolean |
inQuery()
Returns whether we are currently in a query context (SELECT, INSERT,
UNION, INTERSECT, EXCEPT, and the ORDER BY operator).
|
boolean |
isAlwaysUseParentheses()
Returns whether to enclose all expressions in parentheses, even if the
operator has high enough precedence that the parentheses are not
required.
|
boolean |
isClauseStartsLine()
Returns whether this writer should start each clause (e.g.
|
boolean |
isKeywordsLowerCase()
Returns whether to output all keywords (e.g.
|
boolean |
isQuoteAllIdentifiers()
Returns whether this writer should quote all identifiers, even those
that do not contain mixed-case identifiers or punctuation.
|
boolean |
isSelectListExtraIndentFlag() |
boolean |
isSelectListItemsOnSeparateLines()
Returns whether the items in the SELECT clause should each be on a
separate line.
|
boolean |
isWhereListItemsOnSeparateLines() |
void |
keyword(java.lang.String s)
Prints a sequence of keywords.
|
void |
literal(java.lang.String s)
Prints a literal, exactly as provided.
|
private void |
maybeWhitespace(java.lang.String s) |
private static boolean |
needWhitespaceAfter(java.lang.String s) |
private static boolean |
needWhitespaceBefore(java.lang.String s) |
void |
newlineAndIndent()
Prints a new line, and indents.
|
void |
print(int x)
Prints an integer.
|
void |
print(java.lang.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(java.lang.String sep)
Writes a list separator, unless the separator is "," and this is the
first occurrence in the list.
|
void |
sep(java.lang.String sep,
boolean printFirst)
Writes a list separator.
|
void |
setAlwaysUseParentheses(boolean b) |
void |
setCaseClausesOnNewLines(boolean caseClausesOnNewLines)
Sets whether the WHEN, THEN and ELSE clauses of a CASE expression appear
at the start of a new line.
|
void |
setClauseStartsLine(boolean clauseStartsLine)
Sets whether a clause (FROM, WHERE, GROUP BY, HAVING, WINDOW, ORDER BY)
starts a new line.
|
void |
setFormatOptions(SqlFormatOptions options) |
void |
setIndentation(int indentation)
Sets the number of spaces indentation.
|
void |
setKeywordsLowerCase(boolean b)
Sets whether to print keywords (SELECT, AS, etc.) in lower-case.
|
void |
setLineLength(int lineLength) |
void |
setNeedWhitespace(boolean needWhitespace)
Sets whether whitespace is needed before the next token.
|
void |
setQuoteAllIdentifiers(boolean b)
Sets whether to quote all identifiers, even those which would be correct
according to the rules of the
SqlDialect if quotation marks were
omitted. |
void |
setSelectListExtraIndentFlag(boolean b)
Sets whether to use a fix for SELECT list indentations.
|
void |
setSelectListItemsOnSeparateLines(boolean b)
Sets whether each item in a SELECT list, GROUP BY list, or ORDER BY list
is on its own line.
|
void |
setSettings(java.util.Properties properties)
Sets settings from a properties object.
|
void |
setSubQueryStyle(SqlWriter.SubQueryStyle subQueryStyle)
Sets the sub-query style.
|
void |
setWhereListItemsOnSeparateLines(boolean b)
Sets whether to print a newline before each AND or OR (whichever is
higher level) in WHERE clauses.
|
void |
setWindowDeclListNewline(boolean windowDeclListNewline) |
void |
setWindowNewline(boolean windowNewline) |
SqlWriter.Frame |
startFunCall(java.lang.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,
java.lang.String open,
java.lang.String close)
Starts a list.
|
protected SqlWriter.Frame |
startList(SqlWriter.FrameType frameType,
java.lang.String keyword,
java.lang.String open,
java.lang.String close)
Starts a list.
|
SqlWriter.Frame |
startList(java.lang.String open,
java.lang.String close)
Starts a list.
|
protected boolean |
tooLong(java.lang.String s) |
SqlString |
toSqlString()
Returns the contents of this writer as a 'certified kocher' SQL string.
|
java.lang.String |
toString() |
protected void |
whiteSpace() |
protected static final CalciteLogger LOGGER
private static final SqlPrettyWriter.Bean DEFAULT_BEAN
protected static final java.lang.String NL
private final SqlDialect dialect
private final java.io.StringWriter sw
protected final java.io.PrintWriter pw
private final java.util.Deque<SqlPrettyWriter.FrameImpl> listStack
private com.google.common.collect.ImmutableList.Builder<java.lang.Integer> dynamicParameters
protected SqlPrettyWriter.FrameImpl frame
private boolean needWhitespace
protected java.lang.String nextWhitespace
protected boolean alwaysUseParentheses
private boolean keywordsLowerCase
private SqlPrettyWriter.Bean bean
private boolean quoteAllIdentifiers
private int indentation
private boolean clauseStartsLine
private boolean selectListItemsOnSeparateLines
private boolean selectListExtraIndentFlag
private int currentIndent
private boolean windowDeclListNewline
private boolean updateSetListNewline
private boolean windowNewline
private SqlWriter.SubQueryStyle subQueryStyle
private boolean whereListItemsOnSeparateLines
private boolean caseClausesOnNewLines
private int lineLength
private int charCount
public SqlPrettyWriter(SqlDialect dialect, boolean alwaysUseParentheses, java.io.PrintWriter pw)
public SqlPrettyWriter(SqlDialect dialect, boolean alwaysUseParentheses)
public SqlPrettyWriter(SqlDialect dialect)
public void setCaseClausesOnNewLines(boolean caseClausesOnNewLines)
public void setSubQueryStyle(SqlWriter.SubQueryStyle subQueryStyle)
SqlWriter.SubQueryStyle.HYDE
.public void setWindowNewline(boolean windowNewline)
public void setWindowDeclListNewline(boolean windowDeclListNewline)
public int getIndentation()
SqlWriter
getIndentation
in interface SqlWriter
public boolean isAlwaysUseParentheses()
SqlWriter
For 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 SqlWriter
public boolean inQuery()
SqlWriter
public boolean isQuoteAllIdentifiers()
SqlWriter
isQuoteAllIdentifiers
in interface SqlWriter
public boolean isClauseStartsLine()
SqlWriter
isClauseStartsLine
in interface SqlWriter
public boolean isSelectListItemsOnSeparateLines()
SqlWriter
isSelectListItemsOnSeparateLines
in interface SqlWriter
public boolean isWhereListItemsOnSeparateLines()
public boolean isSelectListExtraIndentFlag()
public boolean isKeywordsLowerCase()
SqlWriter
isKeywordsLowerCase
in interface SqlWriter
public int getLineLength()
public void resetSettings()
SqlWriter
resetSettings
in interface SqlWriter
public void reset()
SqlWriter
SqlWriter.resetSettings()
private SqlPrettyWriter.Bean getBean()
public void setIndentation(int indentation)
getIndentation()
public void describe(java.io.PrintWriter pw, boolean omitDefaults)
pw
- WriteromitDefaults
- Whether to omit properties whose value is the same as
the defaultpublic void setSettings(java.util.Properties properties)
public void setClauseStartsLine(boolean clauseStartsLine)
public void setSelectListItemsOnSeparateLines(boolean b)
public void setSelectListExtraIndentFlag(boolean b)
SELECT A as A B as B C as C D
SELECT A as A B as B C as C D
public void setKeywordsLowerCase(boolean b)
public void setWhereListItemsOnSeparateLines(boolean b)
public void setAlwaysUseParentheses(boolean b)
public void newlineAndIndent()
SqlWriter
newlineAndIndent
in interface SqlWriter
void indent(int indent)
public void setQuoteAllIdentifiers(boolean b)
SqlDialect
if quotation marks were
omitted.
Default true.
protected SqlPrettyWriter.FrameImpl createListFrame(SqlWriter.FrameType frameType, java.lang.String keyword, java.lang.String open, java.lang.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, java.lang.String keyword, java.lang.String open, java.lang.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(SqlWriter.Frame frame)
SqlWriter
endList
in interface SqlWriter
frame
- The frame which was created by SqlWriter.startList(java.lang.String, java.lang.String)
.public java.lang.String format(SqlNode node)
public java.lang.String toString()
toString
in class java.lang.Object
public SqlString toSqlString()
SqlWriter
toSqlString
in interface SqlWriter
public SqlDialect getDialect()
SqlWriter
getDialect
in interface SqlWriter
public void literal(java.lang.String s)
SqlWriter
public void keyword(java.lang.String s)
SqlWriter
keyword("SELECT")
,
keyword("CHARACTER SET")
.private void maybeWhitespace(java.lang.String s)
private static boolean needWhitespaceBefore(java.lang.String s)
private static boolean needWhitespaceAfter(java.lang.String s)
protected void whiteSpace()
protected boolean tooLong(java.lang.String s)
public void print(java.lang.String s)
SqlWriter
public void print(int x)
SqlWriter
public void identifier(java.lang.String name, boolean quoted)
SqlWriter
identifier
in interface SqlWriter
name
- 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 SqlWriter
public void fetchOffset(SqlNode fetch, SqlNode offset)
SqlWriter
fetchOffset
in interface SqlWriter
public SqlWriter.Frame startFunCall(java.lang.String funName)
SqlWriter
startFunCall
in interface SqlWriter
SqlWriter.endFunCall(Frame)
public void endFunCall(SqlWriter.Frame frame)
SqlWriter
endFunCall
in interface SqlWriter
frame
- FrameSqlWriter.startFunCall(String)
public SqlWriter.Frame startList(java.lang.String open, java.lang.String close)
SqlWriter
public SqlWriter.Frame startList(SqlWriter.FrameTypeEnum frameType)
SqlWriter
public SqlWriter.Frame startList(SqlWriter.FrameType frameType, java.lang.String open, java.lang.String close)
SqlWriter
public void sep(java.lang.String sep)
SqlWriter
public void sep(java.lang.String sep, boolean printFirst)
SqlWriter
public void setNeedWhitespace(boolean needWhitespace)
SqlWriter
setNeedWhitespace
in interface SqlWriter
public void setLineLength(int lineLength)
public void setFormatOptions(SqlFormatOptions options)
Copyright © 2012–2019 The Apache Software Foundation. All rights reserved.