|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.eigenbase.sql.pretty.SqlPrettyWriter
public class SqlPrettyWriter
Pretty printer for SQL statements.
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 |
Nested Class Summary | |
---|---|
private static class |
SqlPrettyWriter.Bean
Helper class which exposes the get/set methods of an object as properties. |
protected class |
SqlPrettyWriter.FrameImpl
Implementation of SqlWriter.Frame . |
Nested classes/interfaces inherited from interface org.eigenbase.sql.SqlWriter |
---|
SqlWriter.Frame, SqlWriter.FrameType, SqlWriter.FrameTypeEnum, SqlWriter.SubqueryStyle |
Constructor Summary | |
---|---|
SqlPrettyWriter(SqlDialect dialect)
|
|
SqlPrettyWriter(SqlDialect dialect,
boolean alwaysUseParentheses)
|
|
SqlPrettyWriter(SqlDialect dialect,
boolean alwaysUseParentheses,
PrintWriter pw)
|
Method Summary | |
---|---|
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)
|
private SqlPrettyWriter.Bean |
getBean()
Returns an object which encapsulates each property as a get/set method. |
SqlDialect |
getDialect()
|
int |
getIndentation()
Returns the offset for each level of indentation. |
int |
getLineLength()
|
void |
identifier(String name)
Prints an identifier, quoting as necessary. |
(package private) void |
indent(int indent)
|
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()
|
boolean |
isKeywordsLowerCase()
|
boolean |
isQuoteAllIdentifiers()
|
boolean |
isSelectListExtraIndentFlag()
|
boolean |
isSelectListItemsOnSeparateLines()
|
boolean |
isWhereListItemsOnSeparateLines()
|
void |
keyword(String s)
Prints a sequence of keywords. |
void |
literal(String s)
Prints a literal, exactly as provided. |
private void |
maybeWhitespace(String s)
|
private static boolean |
needWhitespaceAfter(String s)
|
private static boolean |
needWhitespaceBefore(String s)
|
void |
newlineAndIndent()
Prints a new line, and indents. |
void |
print(int x)
|
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 |
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)
|
private static String |
spaces(int i)
Returns a string of N spaces. |
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)
|
String |
toString()
|
protected void |
whiteSpace()
|
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
protected static final EigenbaseLogger logger
private static final SqlPrettyWriter.Bean defaultBean
protected static final String NL
private static final String[] spaces
private final SqlDialect dialect
private final StringWriter sw
protected final PrintWriter pw
private final Stack<SqlPrettyWriter.FrameImpl> listStack
protected SqlPrettyWriter.FrameImpl frame
private boolean needWhitespace
protected String nextWhitespace
protected boolean alwaysUseParentheses
private boolean keywordsLowerCase
private SqlPrettyWriter.Bean bean
private boolean quoteAllIdentifiers
private int indentation
private boolean clauseStartsLine
private boolean selectListItemsOnSeparateLines
private boolean selectListExtraIndentFlag
private int currentIndent
private boolean windowDeclListNewline
private boolean updateSetListNewline
private boolean windowNewline
private SqlWriter.SubqueryStyle subqueryStyle
private boolean whereListItemsOnSeparateLines
private boolean caseClausesOnNewLines
private int lineLength
private int charCount
Constructor Detail |
---|
public SqlPrettyWriter(SqlDialect dialect, boolean alwaysUseParentheses, PrintWriter pw)
public SqlPrettyWriter(SqlDialect dialect, boolean alwaysUseParentheses)
public SqlPrettyWriter(SqlDialect dialect)
Method Detail |
---|
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()
SqlWriter
getIndentation
in interface SqlWriter
public boolean isAlwaysUseParentheses()
SqlWriter
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.
isAlwaysUseParentheses
in interface SqlWriter
public boolean inQuery()
SqlWriter
inQuery
in interface SqlWriter
public boolean isQuoteAllIdentifiers()
isQuoteAllIdentifiers
in interface SqlWriter
public boolean isClauseStartsLine()
isClauseStartsLine
in interface SqlWriter
public boolean isSelectListItemsOnSeparateLines()
isSelectListItemsOnSeparateLines
in interface SqlWriter
public boolean isWhereListItemsOnSeparateLines()
public boolean isSelectListExtraIndentFlag()
public boolean isKeywordsLowerCase()
isKeywordsLowerCase
in interface SqlWriter
public int getLineLength()
public void resetSettings()
SqlWriter
resetSettings
in interface SqlWriter
public void reset()
SqlWriter
SqlWriter.resetSettings()
reset
in interface SqlWriter
private SqlPrettyWriter.Bean getBean()
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()
SqlWriter
newlineAndIndent
in interface SqlWriter
void indent(int indent)
public 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 list
private static String spaces(int i)
protected 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)
SqlWriter
endList
in interface SqlWriter
frame
- The frame which was created by SqlWriter.startList(java.lang.String, java.lang.String)
.public String format(SqlNode node)
public String toString()
toString
in class Object
public SqlDialect getDialect()
getDialect
in interface SqlWriter
public void literal(String s)
SqlWriter
literal
in interface SqlWriter
public void keyword(String s)
SqlWriter
keyword("SELECT")
,
keyword("CHARACTER SET")
.
keyword
in interface SqlWriter
private void maybeWhitespace(String s)
private static boolean needWhitespaceBefore(String s)
private static boolean needWhitespaceAfter(String s)
protected void whiteSpace()
protected boolean tooLong(String s)
public void print(String s)
SqlWriter
print
in interface SqlWriter
public void print(int x)
print
in interface SqlWriter
public void identifier(String name)
SqlWriter
identifier
in interface SqlWriter
public SqlWriter.Frame startFunCall(String funName)
SqlWriter
startFunCall
in interface SqlWriter
SqlWriter.endFunCall(Frame)
public void endFunCall(SqlWriter.Frame frame)
SqlWriter
endFunCall
in interface SqlWriter
SqlWriter.startFunCall(String)
public SqlWriter.Frame startList(String open, String close)
SqlWriter
startList
in interface SqlWriter
public SqlWriter.Frame startList(SqlWriter.FrameTypeEnum frameType)
SqlWriter
startList
in interface SqlWriter
frameType
- Type of list. For example, a SELECT list will bepublic SqlWriter.Frame startList(SqlWriter.FrameType frameType, String open, String close)
SqlWriter
startList
in interface SqlWriter
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.public void sep(String sep)
SqlWriter
sep
in interface SqlWriter
sep
- List separator, typically ",".public void sep(String sep, boolean printFirst)
SqlWriter
sep
in interface SqlWriter
sep
- List separator, typically ","printFirst
- Whether to print the first occurrence of the separatorpublic void setNeedWhitespace(boolean needWhitespace)
SqlWriter
setNeedWhitespace
in interface SqlWriter
public void setLineLength(int lineLength)
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |