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:
SelectListItemsOnSeparateLinesreplaced bySelectFolding,GroupByFolding, andOrderByFolding;UpdateSetListNewlinereplaced byUpdateSetFolding;WindowDeclListNewlinereplaced byWindowFolding;WindowNewlinereplaced byOverFolding;ValuesListNewlinereplaced byValuesFolding.
-
Nested Class Summary
Nested ClassesNested classes/interfaces inherited from interface org.apache.calcite.sql.SqlWriter
SqlWriter.Frame, SqlWriter.FrameType, SqlWriter.FrameTypeEnum, SqlWriter.SubQueryStyle -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected @Nullable SqlPrettyWriter.FrameImplprotected static final CalciteLoggerprotected @Nullable Stringprotected static final String -
Constructor Summary
ConstructorsConstructorDescriptionCreates 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 SqlWriterConfigconfig()Creates aSqlWriterConfigwith Calcite's SQL dialect.protected SqlPrettyWriter.FrameImplcreateListFrame(SqlWriter.FrameType frameType, @Nullable String keyword, String open, String close) Creates a list frame.voiddescribe(PrintWriter pw, boolean omitDefaults) Prints the property settings of this pretty-writer to a writer.voiddynamicParam(int index) Prints a dynamic parameter (e.g.voidendFunCall(SqlWriter.Frame frame) Ends a list which is a call to a function.voidendList(@Nullable SqlWriter.Frame frame) Ends a list.voidfetchOffset(@Nullable SqlNode fetch, @Nullable SqlNode offset) Prints the OFFSET/FETCH clause.Returns the dialect of SQL.intDeprecated.intDeprecated.voididentifier(String name, boolean quoted) Prints an identifier, quoting as necessary.booleaninQuery()Returns whether we are currently in a query context (SELECT, INSERT, UNION, INTERSECT, EXCEPT, and the ORDER BY operator).booleanDeprecated.booleanDeprecated.booleanDeprecated.booleanDeprecated.booleanDeprecated.booleanDeprecated.booleanDeprecated.voidPrints a sequence of keywords.list(SqlWriter.FrameTypeEnum frameType, Consumer<SqlWriter> action) Writes a list.list(SqlWriter.FrameTypeEnum frameType, SqlBinaryOperator sepOp, SqlNodeList list) voidPrints a literal, exactly as provided.voidPrints a new line, and indents.voidnewlineAndIndent(int indent) voidprint(int x) Prints an integer.voidPrints a string, preceded by whitespace if necessary.voidreset()Resets this writer so that it can format another expression.voidResets all properties to their default values.voidWrites a list separator, unless the separator is "," and this is the first occurrence in the list.voidWrites a list separator.voidsetAlwaysUseParentheses(boolean alwaysUseParentheses) Deprecated.voidsetCaseClausesOnNewLines(boolean caseClausesOnNewLines) Deprecated.voidsetClauseStartsLine(boolean clauseStartsLine) Deprecated.voidsetFormatOptions(@Nullable SqlFormatOptions options) voidsetIndentation(int indentation) Deprecated.voidsetKeywordsLowerCase(boolean keywordsLowerCase) Deprecated.voidsetLineLength(int lineLength) Deprecated.voidsetNeedWhitespace(boolean needWhitespace) Sets whether whitespace is needed before the next token.voidsetQuoteAllIdentifiers(boolean quoteAllIdentifiers) Deprecated.voidsetSelectListExtraIndentFlag(boolean selectListExtraIndentFlag) Deprecated.voidsetSelectListItemsOnSeparateLines(boolean b) Deprecated.voidsetSettings(Properties properties) Sets settings from a properties object.voidsetSubQueryStyle(SqlWriter.SubQueryStyle subQueryStyle) Deprecated.voidsetWhereListItemsOnSeparateLines(boolean whereListItemsOnSeparateLines) Deprecated.voidsetWindowDeclListNewline(boolean windowDeclListNewline) Deprecated.voidsetWindowNewline(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.FramestartList(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 booleanvoidPrints 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 aSqlWriterConfigwith Calcite's SQL dialect. -
setCaseClausesOnNewLines
Deprecated. -
setSubQueryStyle
Deprecated. -
setWindowNewline
Deprecated. -
setWindowDeclListNewline
Deprecated. -
getIndentation
Deprecated.Description copied from interface:SqlWriterReturns the offset for each level of indentation. Default 4.- Specified by:
getIndentationin interfaceSqlWriter
-
isAlwaysUseParentheses
Deprecated.Description copied from interface:SqlWriterReturns 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) * cbecause 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:
isAlwaysUseParenthesesin interfaceSqlWriter
-
inQuery
public boolean inQuery()Description copied from interface:SqlWriterReturns whether we are currently in a query context (SELECT, INSERT, UNION, INTERSECT, EXCEPT, and the ORDER BY operator). -
isQuoteAllIdentifiers
Deprecated.Description copied from interface:SqlWriterReturns whether this writer should quote all identifiers, even those that do not contain mixed-case identifiers or punctuation.- Specified by:
isQuoteAllIdentifiersin interfaceSqlWriter- Returns:
- whether to quote all identifiers
-
isClauseStartsLine
Deprecated.Description copied from interface:SqlWriterReturns whether this writer should start each clause (e.g. GROUP BY) on a new line.- Specified by:
isClauseStartsLinein interfaceSqlWriter- Returns:
- whether to start each clause on a new line
-
isSelectListItemsOnSeparateLines
Deprecated.Description copied from interface:SqlWriterReturns whether the items in the SELECT clause should each be on a separate line.- Specified by:
isSelectListItemsOnSeparateLinesin interfaceSqlWriter- Returns:
- whether to put each SELECT clause item on a new line
-
isWhereListItemsOnSeparateLines
Deprecated. -
isSelectListExtraIndentFlag
Deprecated. -
isKeywordsLowerCase
Deprecated.Description copied from interface:SqlWriterReturns whether to output all keywords (e.g. SELECT, GROUP BY) in lower case.- Specified by:
isKeywordsLowerCasein interfaceSqlWriter- Returns:
- whether to output SQL keywords in lower case
-
getLineLength
Deprecated. -
resetSettings
public void resetSettings()Description copied from interface:SqlWriterResets all properties to their default values.- Specified by:
resetSettingsin interfaceSqlWriter
-
reset
public void reset()Description copied from interface:SqlWriterResets 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:SqlWriterPrints a new line, and indents.- Specified by:
newlineAndIndentin 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:SqlWriterEnds a list.- Specified by:
endListin interfaceSqlWriter- Parameters:
frame- The frame which was created bySqlWriter.startList(java.lang.String, java.lang.String).
-
format
-
toString
-
toSqlString
Description copied from interface:SqlWriterReturns the contents of this writer as a 'certified kocher' SQL string.- Specified by:
toSqlStringin interfaceSqlWriter- Returns:
- SQL string
-
getDialect
Description copied from interface:SqlWriterReturns the dialect of SQL.- Specified by:
getDialectin interfaceSqlWriter- Returns:
- SQL dialect
-
literal
Description copied from interface:SqlWriterPrints 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:SqlWriterPrints 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:SqlWriterPrints a string, preceded by whitespace if necessary. -
print
public void print(int x) Description copied from interface:SqlWriterPrints an integer. -
identifier
Description copied from interface:SqlWriterPrints an identifier, quoting as necessary.- Specified by:
identifierin 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:SqlWriterPrints a dynamic parameter (e.g.?for default JDBC)- Specified by:
dynamicParamin interfaceSqlWriter
-
fetchOffset
Description copied from interface:SqlWriterPrints the OFFSET/FETCH clause.- Specified by:
fetchOffsetin interfaceSqlWriter
-
topN
Description copied from interface:SqlWriterPrints the TOP(n) clause. -
startFunCall
Description copied from interface:SqlWriterStarts a list which is a call to a function.- Specified by:
startFunCallin interfaceSqlWriter- See Also:
-
endFunCall
Description copied from interface:SqlWriterEnds a list which is a call to a function.- Specified by:
endFunCallin interfaceSqlWriter- Parameters:
frame- Frame- See Also:
-
startList
Description copied from interface:SqlWriterStarts a list. -
startList
Description copied from interface:SqlWriterStarts a list with no opening string. -
startList
Description copied from interface:SqlWriterStarts a list. -
list
Description copied from interface:SqlWriterWrites a list. -
list
Description copied from interface:SqlWriter -
sep
Description copied from interface:SqlWriterWrites a list separator, unless the separator is "," and this is the first occurrence in the list. -
sep
Description copied from interface:SqlWriterWrites a list separator. -
setNeedWhitespace
public void setNeedWhitespace(boolean needWhitespace) Description copied from interface:SqlWriterSets whether whitespace is needed before the next token.- Specified by:
setNeedWhitespacein interfaceSqlWriter
-
setLineLength
Deprecated. -
setFormatOptions
-