Class SqlPrettyWriter

java.lang.Object
org.apache.calcite.sql.pretty.SqlPrettyWriter
All Implemented Interfaces:
SqlWriter

public class SqlPrettyWriter extends Object implements SqlWriter
Pretty printer for SQL statements.

There are several options to control the format.

Formatting options
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 (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.

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:

  • Field Details

  • Constructor Details

    • SqlPrettyWriter

      public SqlPrettyWriter(SqlWriterConfig config, StringBuilder buf)
      Creates a writer with the given configuration and a given buffer to write to.
    • SqlPrettyWriter

      public 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

      @Deprecated public SqlPrettyWriter(SqlDialect dialect, SqlWriterConfig config)
      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 public SqlPrettyWriter(SqlDialect dialect, boolean alwaysUseParentheses)
      Deprecated.
    • SqlPrettyWriter

      @Deprecated public SqlPrettyWriter(SqlDialect dialect)
      Deprecated.
      Creates a writer with a given dialect, the default configuration and a private print writer.
    • SqlPrettyWriter

      public SqlPrettyWriter(SqlWriterConfig config)
      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

      public static SqlWriterConfig config()
      Creates a SqlWriterConfig with Calcite's SQL dialect.
    • setCaseClausesOnNewLines

      @Deprecated public void setCaseClausesOnNewLines(boolean caseClausesOnNewLines)
      Deprecated.
    • setSubQueryStyle

      @Deprecated public void setSubQueryStyle(SqlWriter.SubQueryStyle subQueryStyle)
      Deprecated.
    • setWindowNewline

      @Deprecated public void setWindowNewline(boolean windowNewline)
      Deprecated.
    • setWindowDeclListNewline

      @Deprecated public void setWindowDeclListNewline(boolean windowDeclListNewline)
      Deprecated.
    • getIndentation

      @Deprecated public int getIndentation()
      Deprecated.
      Description copied from interface: SqlWriter
      Returns the offset for each level of indentation. Default 4.
      Specified by:
      getIndentation in interface SqlWriter
    • isAlwaysUseParentheses

      @Deprecated public boolean 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 to a + (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 interface SqlWriter
    • 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).
      Specified by:
      inQuery in interface SqlWriter
    • isQuoteAllIdentifiers

      @Deprecated public boolean 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 interface SqlWriter
      Returns:
      whether to quote all identifiers
    • isClauseStartsLine

      @Deprecated public boolean 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 interface SqlWriter
      Returns:
      whether to start each clause on a new line
    • isSelectListItemsOnSeparateLines

      @Deprecated public boolean 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 interface SqlWriter
      Returns:
      whether to put each SELECT clause item on a new line
    • isWhereListItemsOnSeparateLines

      @Deprecated public boolean isWhereListItemsOnSeparateLines()
      Deprecated.
    • isSelectListExtraIndentFlag

      @Deprecated public boolean isSelectListExtraIndentFlag()
      Deprecated.
    • isKeywordsLowerCase

      @Deprecated public boolean 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 interface SqlWriter
      Returns:
      whether to output SQL keywords in lower case
    • getLineLength

      @Deprecated public int getLineLength()
      Deprecated.
    • resetSettings

      public void resetSettings()
      Description copied from interface: SqlWriter
      Resets all properties to their default values.
      Specified by:
      resetSettings in interface SqlWriter
    • reset

      public void reset()
      Description copied from interface: SqlWriter
      Resets this writer so that it can format another expression. Does not affect formatting preferences (see SqlWriter.resetSettings()
      Specified by:
      reset in interface SqlWriter
    • setIndentation

      @Deprecated public void setIndentation(int indentation)
      Deprecated.
    • describe

      public void describe(PrintWriter pw, boolean omitDefaults)
      Prints the property settings of this pretty-writer to a writer.
      Parameters:
      pw - Writer
      omitDefaults - Whether to omit properties whose value is the same as the default
    • setSettings

      public void setSettings(Properties properties)
      Sets settings from a properties object.
    • setClauseStartsLine

      @Deprecated public void setClauseStartsLine(boolean clauseStartsLine)
      Deprecated.
    • setSelectListItemsOnSeparateLines

      @Deprecated public void setSelectListItemsOnSeparateLines(boolean b)
      Deprecated.
    • setSelectListExtraIndentFlag

      @Deprecated public void setSelectListExtraIndentFlag(boolean selectListExtraIndentFlag)
      Deprecated.
    • setKeywordsLowerCase

      @Deprecated public void setKeywordsLowerCase(boolean keywordsLowerCase)
      Deprecated.
    • setWhereListItemsOnSeparateLines

      @Deprecated public void setWhereListItemsOnSeparateLines(boolean whereListItemsOnSeparateLines)
      Deprecated.
    • setAlwaysUseParentheses

      @Deprecated public void setAlwaysUseParentheses(boolean alwaysUseParentheses)
      Deprecated.
    • newlineAndIndent

      public void newlineAndIndent()
      Description copied from interface: SqlWriter
      Prints a new line, and indents.
      Specified by:
      newlineAndIndent in interface SqlWriter
    • newlineAndIndent

      public void newlineAndIndent(int indent)
    • setQuoteAllIdentifiers

      @Deprecated public void setQuoteAllIdentifiers(boolean quoteAllIdentifiers)
      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 list
      keyword - The keyword to be printed at the start of the list
      open - The string to print at the start of the list
      close - 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

      public void endList(@Nullable SqlWriter.Frame frame)
      Description copied from interface: SqlWriter
      Ends a list.
      Specified by:
      endList in interface SqlWriter
      Parameters:
      frame - The frame which was created by SqlWriter.startList(java.lang.String, java.lang.String).
    • format

      public String format(SqlNode node)
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • toSqlString

      public SqlString toSqlString()
      Description copied from interface: SqlWriter
      Returns the contents of this writer as a 'certified kocher' SQL string.
      Specified by:
      toSqlString in interface SqlWriter
      Returns:
      SQL string
    • getDialect

      public SqlDialect getDialect()
      Description copied from interface: SqlWriter
      Returns the dialect of SQL.
      Specified by:
      getDialect in interface SqlWriter
      Returns:
      SQL dialect
    • literal

      public void literal(String s)
      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.
      Specified by:
      literal in interface SqlWriter
    • keyword

      public void keyword(String s)
      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").
      Specified by:
      keyword in interface SqlWriter
    • whiteSpace

      protected void whiteSpace()
    • tooLong

      protected boolean tooLong(String s)
    • print

      public void print(String s)
      Description copied from interface: SqlWriter
      Prints a string, preceded by whitespace if necessary.
      Specified by:
      print in interface SqlWriter
    • print

      public void print(int x)
      Description copied from interface: SqlWriter
      Prints an integer.
      Specified by:
      print in interface SqlWriter
      Parameters:
      x - Integer
    • identifier

      public void identifier(String name, boolean quoted)
      Description copied from interface: SqlWriter
      Prints an identifier, quoting as necessary.
      Specified by:
      identifier in interface SqlWriter
      Parameters:
      name - The identifier name
      quoted - 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 interface SqlWriter
    • fetchOffset

      public void fetchOffset(@Nullable SqlNode fetch, @Nullable SqlNode offset)
      Description copied from interface: SqlWriter
      Prints the OFFSET/FETCH clause.
      Specified by:
      fetchOffset in interface SqlWriter
    • topN

      public void topN(@Nullable SqlNode fetch, @Nullable SqlNode offset)
      Description copied from interface: SqlWriter
      Prints the TOP(n) clause.
      Specified by:
      topN in interface SqlWriter
      See Also:
    • startFunCall

      public SqlWriter.Frame startFunCall(String funName)
      Description copied from interface: SqlWriter
      Starts a list which is a call to a function.
      Specified by:
      startFunCall in interface SqlWriter
      See Also:
    • endFunCall

      public void endFunCall(SqlWriter.Frame frame)
      Description copied from interface: SqlWriter
      Ends a list which is a call to a function.
      Specified by:
      endFunCall in interface SqlWriter
      Parameters:
      frame - Frame
      See Also:
    • startList

      public SqlWriter.Frame startList(String open, String close)
      Description copied from interface: SqlWriter
      Starts a list.
      Specified by:
      startList in interface SqlWriter
    • startList

      public SqlWriter.Frame startList(SqlWriter.FrameTypeEnum frameType)
      Description copied from interface: SqlWriter
      Starts a list with no opening string.
      Specified by:
      startList in interface SqlWriter
      Parameters:
      frameType - Type of list. For example, a SELECT list will be governed according to SELECT-list formatting preferences.
    • startList

      public SqlWriter.Frame startList(SqlWriter.FrameType frameType, String open, String close)
      Description copied from interface: SqlWriter
      Starts a list.
      Specified by:
      startList in interface SqlWriter
      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
    • list

      public SqlWriter list(SqlWriter.FrameTypeEnum frameType, Consumer<SqlWriter> action)
      Description copied from interface: SqlWriter
      Writes a list.
      Specified by:
      list in interface SqlWriter
    • list

      public SqlWriter list(SqlWriter.FrameTypeEnum frameType, SqlBinaryOperator sepOp, SqlNodeList list)
      Description copied from interface: SqlWriter
      Writes a list separated by a binary operator (AND, OR, or COMMA).
      Specified by:
      list in interface SqlWriter
    • sep

      public void sep(String sep)
      Description copied from interface: SqlWriter
      Writes a list separator, unless the separator is "," and this is the first occurrence in the list.
      Specified by:
      sep in interface SqlWriter
      Parameters:
      sep - List separator, typically ",".
    • sep

      public void sep(String sep, boolean printFirst)
      Description copied from interface: SqlWriter
      Writes a list separator.
      Specified by:
      sep in interface SqlWriter
      Parameters:
      sep - List separator, typically ","
      printFirst - Whether to print the first occurrence of the 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 interface SqlWriter
    • setLineLength

      @Deprecated public void setLineLength(int lineLength)
      Deprecated.
    • setFormatOptions

      public void setFormatOptions(@Nullable SqlFormatOptions options)