public class SqlPrettyWriter extends 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 |
|---|---|
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 |
protected SqlPrettyWriter.FrameImpl |
frame |
protected static CalciteLogger |
LOGGER |
protected String |
nextWhitespace |
protected static String |
NL |
protected PrintWriter |
pw |
| Constructor and Description |
|---|
SqlPrettyWriter(SqlDialect dialect) |
SqlPrettyWriter(SqlDialect dialect,
boolean alwaysUseParentheses) |
SqlPrettyWriter(SqlDialect dialect,
boolean alwaysUseParentheses,
PrintWriter pw) |
| Modifier and Type | Method and Description |
|---|---|
protected SqlPrettyWriter.FrameImpl |
createListFrame(SqlWriter.FrameType frameType,
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 |
endFunCall(SqlWriter.Frame frame)
Ends a list which is a call to a function.
|
void |
endList(SqlWriter.Frame frame)
Ends a list.
|
String |
format(SqlNode node) |
SqlDialect |
getDialect()
Returns the dialect of SQL.
|
int |
getIndentation()
Returns the offset for each level of indentation.
|
int |
getLineLength() |
void |
identifier(String name)
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()
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(String s)
Prints a sequence of keywords.
|
void |
literal(String s)
Prints a literal, exactly as provided.
|
void |
newlineAndIndent()
Prints a new line, and indents.
|
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 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(Properties properties)
Sets settings from a properties object.
|
void |
setSubqueryStyle(SqlWriter.SubqueryStyle subqueryStyle)
Sets the subquery 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(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,
String keyword,
String open,
String close)
Starts a list.
|
SqlWriter.Frame |
startList(String open,
String close)
Starts a list.
|
protected boolean |
tooLong(String s) |
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 final PrintWriter pw
protected SqlPrettyWriter.FrameImpl frame
protected String nextWhitespace
protected boolean alwaysUseParentheses
public SqlPrettyWriter(SqlDialect dialect, boolean alwaysUseParentheses, 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()
SqlWritergetIndentation in interface SqlWriterpublic 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()
SqlWriterpublic boolean isQuoteAllIdentifiers()
SqlWriterisQuoteAllIdentifiers in interface SqlWriterpublic boolean isClauseStartsLine()
SqlWriterisClauseStartsLine in interface SqlWriterpublic boolean isSelectListItemsOnSeparateLines()
SqlWriterisSelectListItemsOnSeparateLines in interface SqlWriterpublic boolean isWhereListItemsOnSeparateLines()
public boolean isSelectListExtraIndentFlag()
public boolean isKeywordsLowerCase()
SqlWriterisKeywordsLowerCase in interface SqlWriterpublic int getLineLength()
public void resetSettings()
SqlWriterresetSettings in interface SqlWriterpublic void reset()
SqlWriterSqlWriter.resetSettings()public void setIndentation(int indentation)
getIndentation()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)
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()
SqlWriternewlineAndIndent in interface SqlWriterpublic void setQuoteAllIdentifiers(boolean b)
SqlDialect if quotation marks were
omitted.
Default true.
protected SqlPrettyWriter.FrameImpl createListFrame(SqlWriter.FrameType frameType, 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, 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(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)
SqlWriteridentifier in interface SqlWriterpublic 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 void sep(String sep)
SqlWriterpublic void sep(String sep, boolean printFirst)
SqlWriterpublic void setNeedWhitespace(boolean needWhitespace)
SqlWritersetNeedWhitespace in interface SqlWriterpublic void setLineLength(int lineLength)
public void setFormatOptions(SqlFormatOptions options)
Copyright © 2012–2015 The Apache Software Foundation. All rights reserved.