Class SqlPrettyWriter
- All Implemented Interfaces:
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 bySelectFolding
,GroupByFolding
, andOrderByFolding
;UpdateSetListNewline
replaced byUpdateSetFolding
;WindowDeclListNewline
replaced byWindowFolding
;WindowNewline
replaced byOverFolding
;ValuesListNewline
replaced byValuesFolding
.
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.apache.calcite.sql.SqlWriter
SqlWriter.Frame, SqlWriter.FrameType, SqlWriter.FrameTypeEnum, SqlWriter.SubQueryStyle
-
Field Summary
Modifier and TypeFieldDescriptionprotected @Nullable SqlPrettyWriter.FrameImpl
protected static final CalciteLogger
protected @Nullable String
protected static final String
-
Constructor Summary
ConstructorDescriptionCreates 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. -
Method Summary
Modifier and TypeMethodDescriptionstatic SqlWriterConfig
config()
Creates aSqlWriterConfig
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.Returns the dialect of SQL.int
Deprecated.int
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
Deprecated.boolean
Deprecated.boolean
Deprecated.boolean
Deprecated.boolean
Deprecated.boolean
Deprecated.boolean
Deprecated.void
Prints a sequence of keywords.list
(SqlWriter.FrameTypeEnum frameType, Consumer<SqlWriter> action) Writes a list.list
(SqlWriter.FrameTypeEnum frameType, SqlBinaryOperator sepOp, SqlNodeList list) void
Prints a literal, exactly as provided.void
Prints a new line, and indents.void
newlineAndIndent
(int indent) void
print
(int x) Prints an integer.void
Prints a string, preceded by whitespace if necessary.void
reset()
Resets this writer so that it can format another expression.void
Resets all properties to their default values.void
Writes a list separator, unless the separator is "," and this is the first occurrence in the list.void
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.startFunCall
(String funName) Starts a list which is a call to a function.Starts a list.startList
(SqlWriter.FrameTypeEnum frameType) Starts a list with no opening string.protected SqlWriter.Frame
startList
(SqlWriter.FrameType frameType, @Nullable String keyword, String open, String close) Starts a list.startList
(SqlWriter.FrameType frameType, String open, String close) Starts a list.protected boolean
void
Prints the TOP(n) clause.Returns the contents of this writer as a 'certified kocher' SQL string.toString()
protected void
-
Field Details
-
LOGGER
-
NL
-
frame
-
nextWhitespace
-
-
Constructor Details
-
SqlPrettyWriter
Creates a writer with the given configuration and a given buffer to write to. -
SqlPrettyWriter
Creates a writer with the given configuration and dialect, and a given print writer (or a private print writer if it is null). -
SqlPrettyWriter
Deprecated.Creates a writer with the given configuration and a private print writer. -
SqlPrettyWriter
@Deprecated public SqlPrettyWriter(SqlDialect dialect, boolean alwaysUseParentheses, PrintWriter pw) Deprecated. -
SqlPrettyWriter
Deprecated. -
SqlPrettyWriter
Deprecated.Creates a writer with a given dialect, the default configuration and a private print writer. -
SqlPrettyWriter
Creates a writer with the given configuration, and a private builder. -
SqlPrettyWriter
public SqlPrettyWriter()Creates a writer with the default configuration.- See Also:
-
-
Method Details
-
config
Creates aSqlWriterConfig
with Calcite's SQL dialect. -
setCaseClausesOnNewLines
Deprecated. -
setSubQueryStyle
Deprecated. -
setWindowNewline
Deprecated. -
setWindowDeclListNewline
Deprecated. -
getIndentation
Deprecated.Description copied from interface:SqlWriter
Returns the offset for each level of indentation. Default 4.- Specified by:
getIndentation
in interfaceSqlWriter
-
isAlwaysUseParentheses
Deprecated.Description copied from interface:SqlWriter
Returns 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
(a + b) * c
because the '*' operator has higher precedence than the '+' operator, and so without the parentheses, the expression would be equivalent toa + (b * c)
. The fully-parenthesized expression,((a + b) * c)
is unambiguous even if you don't know the precedence of every operator.- Specified by:
isAlwaysUseParentheses
in interfaceSqlWriter
-
inQuery
public boolean inQuery()Description copied from interface:SqlWriter
Returns whether we are currently in a query context (SELECT, INSERT, UNION, INTERSECT, EXCEPT, and the ORDER BY operator). -
isQuoteAllIdentifiers
Deprecated.Description copied from interface:SqlWriter
Returns whether this writer should quote all identifiers, even those that do not contain mixed-case identifiers or punctuation.- Specified by:
isQuoteAllIdentifiers
in interfaceSqlWriter
- Returns:
- whether to quote all identifiers
-
isClauseStartsLine
Deprecated.Description copied from interface:SqlWriter
Returns whether this writer should start each clause (e.g. GROUP BY) on a new line.- Specified by:
isClauseStartsLine
in interfaceSqlWriter
- Returns:
- whether to start each clause on a new line
-
isSelectListItemsOnSeparateLines
Deprecated.Description copied from interface:SqlWriter
Returns whether the items in the SELECT clause should each be on a separate line.- Specified by:
isSelectListItemsOnSeparateLines
in interfaceSqlWriter
- Returns:
- whether to put each SELECT clause item on a new line
-
isWhereListItemsOnSeparateLines
Deprecated. -
isSelectListExtraIndentFlag
Deprecated. -
isKeywordsLowerCase
Deprecated.Description copied from interface:SqlWriter
Returns whether to output all keywords (e.g. SELECT, GROUP BY) in lower case.- Specified by:
isKeywordsLowerCase
in interfaceSqlWriter
- Returns:
- whether to output SQL keywords in lower case
-
getLineLength
Deprecated. -
resetSettings
public void resetSettings()Description copied from interface:SqlWriter
Resets all properties to their default values.- Specified by:
resetSettings
in interfaceSqlWriter
-
reset
public void reset()Description copied from interface:SqlWriter
Resets this writer so that it can format another expression. Does not affect formatting preferences (seeSqlWriter.resetSettings()
-
setIndentation
Deprecated. -
describe
Prints the property settings of this pretty-writer to a writer.- Parameters:
pw
- WriteromitDefaults
- Whether to omit properties whose value is the same as the default
-
setSettings
Sets settings from a properties object. -
setClauseStartsLine
Deprecated. -
setSelectListItemsOnSeparateLines
Deprecated. -
setSelectListExtraIndentFlag
Deprecated. -
setKeywordsLowerCase
Deprecated. -
setWhereListItemsOnSeparateLines
Deprecated. -
setAlwaysUseParentheses
Deprecated. -
newlineAndIndent
public void newlineAndIndent()Description copied from interface:SqlWriter
Prints a new line, and indents.- Specified by:
newlineAndIndent
in interfaceSqlWriter
-
newlineAndIndent
public void newlineAndIndent(int indent) -
setQuoteAllIdentifiers
Deprecated. -
createListFrame
protected SqlPrettyWriter.FrameImpl createListFrame(SqlWriter.FrameType frameType, @Nullable String keyword, String open, String close) Creates a list frame.Derived classes should override this method to specify the indentation of the list.
- Parameters:
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 list- Returns:
- A frame
-
startList
protected SqlWriter.Frame startList(SqlWriter.FrameType frameType, @Nullable String keyword, String open, String close) Starts a list.- Parameters:
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.
-
endList
Description copied from interface:SqlWriter
Ends a list.- Specified by:
endList
in interfaceSqlWriter
- Parameters:
frame
- The frame which was created bySqlWriter.startList(java.lang.String, java.lang.String)
.
-
format
-
toString
-
toSqlString
Description copied from interface:SqlWriter
Returns the contents of this writer as a 'certified kocher' SQL string.- Specified by:
toSqlString
in interfaceSqlWriter
- Returns:
- SQL string
-
getDialect
Description copied from interface:SqlWriter
Returns the dialect of SQL.- Specified by:
getDialect
in interfaceSqlWriter
- Returns:
- SQL dialect
-
literal
Description copied from interface:SqlWriter
Prints a literal, exactly as provided. Does not attempt to indent or convert to upper or lower case. Does not add quotation marks. Adds preceding whitespace if necessary. -
keyword
Description copied from interface:SqlWriter
Prints a sequence of keywords. Must not start or end with space, but may contain a space. For example,keyword("SELECT")
,keyword("CHARACTER SET")
. -
whiteSpace
protected void whiteSpace() -
tooLong
-
print
Description copied from interface:SqlWriter
Prints a string, preceded by whitespace if necessary. -
print
public void print(int x) Description copied from interface:SqlWriter
Prints an integer. -
identifier
Description copied from interface:SqlWriter
Prints an identifier, quoting as necessary.- Specified by:
identifier
in interfaceSqlWriter
- Parameters:
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 quoted
-
dynamicParam
public void dynamicParam(int index) Description copied from interface:SqlWriter
Prints a dynamic parameter (e.g.?
for default JDBC)- Specified by:
dynamicParam
in interfaceSqlWriter
-
fetchOffset
Description copied from interface:SqlWriter
Prints the OFFSET/FETCH clause.- Specified by:
fetchOffset
in interfaceSqlWriter
-
topN
Description copied from interface:SqlWriter
Prints the TOP(n) clause. -
startFunCall
Description copied from interface:SqlWriter
Starts a list which is a call to a function.- Specified by:
startFunCall
in interfaceSqlWriter
- See Also:
-
endFunCall
Description copied from interface:SqlWriter
Ends a list which is a call to a function.- Specified by:
endFunCall
in interfaceSqlWriter
- Parameters:
frame
- Frame- See Also:
-
startList
Description copied from interface:SqlWriter
Starts a list. -
startList
Description copied from interface:SqlWriter
Starts a list with no opening string. -
startList
Description copied from interface:SqlWriter
Starts a list. -
list
Description copied from interface:SqlWriter
Writes a list. -
list
Description copied from interface:SqlWriter
-
sep
Description copied from interface:SqlWriter
Writes a list separator, unless the separator is "," and this is the first occurrence in the list. -
sep
Description copied from interface:SqlWriter
Writes a list separator. -
setNeedWhitespace
public void setNeedWhitespace(boolean needWhitespace) Description copied from interface:SqlWriter
Sets whether whitespace is needed before the next token.- Specified by:
setNeedWhitespace
in interfaceSqlWriter
-
setLineLength
Deprecated. -
setFormatOptions
-