Package org.apache.calcite.sql
Interface SqlWriter
- All Known Implementing Classes:
SqlPrettyWriter
public interface SqlWriter
A
SqlWriter is the target to construct a SQL statement from a
parse tree. It deals with dialect differences; for example, Oracle quotes
identifiers as "scott", while SQL Server quotes them as
[scott].-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfaceA Frame is a piece of generated text which shares a common indentation level.static interfaceFrame type.static enumEnumerates the types of frame.static enumStyle of formatting sub-queries. -
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionvoiddynamicParam(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.intReturns the offset for each level of indentation.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).booleanReturns whether to enclose all expressions in parentheses, even if the operator has high enough precedence that the parentheses are not required.booleanReturns whether this writer should start each clause (e.g.booleanReturns whether to output all keywords (e.g.booleanReturns whether this writer should quote all identifiers, even those that do not contain mixed-case identifiers or punctuation.booleanReturns whether the items in the SELECT clause should each be on a separate line.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.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.voidsetNeedWhitespace(boolean needWhitespace) Sets whether whitespace is needed before the next token.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.startList(SqlWriter.FrameType frameType, String open, String close) Starts a list.voidPrints the TOP(n) clause.Returns the contents of this writer as a 'certified kocher' SQL string.
-
Field Details
-
COMMA
Comma operator.Defined in
SqlWriterbecause it is only used while convertingSqlNodeto SQL; seelist(FrameTypeEnum, SqlBinaryOperator, SqlNodeList).The precedence of the comma operator is low but not zero. For instance, this ensures parentheses in
select x, (select * from foo order by z), y from t.
-
-
Method Details
-
reset
void reset()Resets this writer so that it can format another expression. Does not affect formatting preferences (seeresetSettings() -
resetSettings
void resetSettings()Resets all properties to their default values. -
getDialect
SqlDialect getDialect()Returns the dialect of SQL.- Returns:
- SQL dialect
-
toSqlString
SqlString toSqlString()Returns the contents of this writer as a 'certified kocher' SQL string.- Returns:
- SQL string
-
literal
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
Prints a sequence of keywords. Must not start or end with space, but may contain a space. For example,keyword("SELECT"),keyword("CHARACTER SET"). -
print
Prints a string, preceded by whitespace if necessary. -
print
@Pure void print(int x) Prints an integer.- Parameters:
x- Integer
-
identifier
Prints an identifier, quoting as necessary.- 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
void dynamicParam(int index) Prints a dynamic parameter (e.g.?for default JDBC) -
fetchOffset
Prints the OFFSET/FETCH clause. -
topN
Prints the TOP(n) clause. -
newlineAndIndent
void newlineAndIndent()Prints a new line, and indents. -
isQuoteAllIdentifiers
boolean isQuoteAllIdentifiers()Returns whether this writer should quote all identifiers, even those that do not contain mixed-case identifiers or punctuation.- Returns:
- whether to quote all identifiers
-
isClauseStartsLine
boolean isClauseStartsLine()Returns whether this writer should start each clause (e.g. GROUP BY) on a new line.- Returns:
- whether to start each clause on a new line
-
isSelectListItemsOnSeparateLines
boolean isSelectListItemsOnSeparateLines()Returns whether the items in the SELECT clause should each be on a separate line.- Returns:
- whether to put each SELECT clause item on a new line
-
isKeywordsLowerCase
boolean isKeywordsLowerCase()Returns whether to output all keywords (e.g. SELECT, GROUP BY) in lower case.- Returns:
- whether to output SQL keywords in lower case
-
startFunCall
Starts a list which is a call to a function.- See Also:
-
endFunCall
Ends a list which is a call to a function.- Parameters:
frame- Frame- See Also:
-
startList
Starts a list. -
startList
Starts a list with no opening string.- Parameters:
frameType- Type of list. For example, a SELECT list will be governed according to SELECT-list formatting preferences.
-
startList
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 start the list; typically "(" or the empty string.close- String to close the list
-
endList
Ends a list.- Parameters:
frame- The frame which was created bystartList(java.lang.String, java.lang.String).
-
list
Writes a list. -
list
-
sep
Writes a list separator, unless the separator is "," and this is the first occurrence in the list.- Parameters:
sep- List separator, typically ",".
-
sep
Writes a list separator.- Parameters:
sep- List separator, typically ","printFirst- Whether to print the first occurrence of the separator
-
setNeedWhitespace
@Pure void setNeedWhitespace(boolean needWhitespace) Sets whether whitespace is needed before the next token. -
getIndentation
int getIndentation()Returns the offset for each level of indentation. Default 4. -
isAlwaysUseParentheses
boolean isAlwaysUseParentheses()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) * 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. -
inQuery
boolean inQuery()Returns whether we are currently in a query context (SELECT, INSERT, UNION, INTERSECT, EXCEPT, and the ORDER BY operator).
-