Package org.apache.calcite.util
Class StackWriter
java.lang.Object
java.io.Writer
java.io.FilterWriter
org.apache.calcite.util.StackWriter
- All Implemented Interfaces:
Closeable,Flushable,Appendable,AutoCloseable
A helper class for generating formatted text. StackWriter keeps track of
nested formatting state like indentation level and quote escaping. Typically,
it is inserted between a PrintWriter and the real Writer; directives are
passed straight through the PrintWriter via the write method, as in the
following example:
StringWriter sw = new StringWriter(); StackWriter stackw = new StackWriter(sw, StackWriter.INDENT_SPACE4); PrintWriter pw = new PrintWriter(stackw); pw.write(StackWriter.INDENT); pw.print("execute remote(link_name,"); pw.write(StackWriter.OPEN_SQL_STRING_LITERAL); pw.println(); pw.write(StackWriter.INDENT); pw.println("select * from t where c > 'alabama'"); pw.write(StackWriter.OUTDENT); pw.write(StackWriter.CLOSE_SQL_STRING_LITERAL); pw.println(");"); pw.write(StackWriter.OUTDENT); pw.close(); System.out.println(sw.toString());
which produces the following output:
execute remote(link_name,' select * from t where c > ''alabama'' ');
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intDirective for ending an SQL identifier.static final intDirective for ending an SQL string literal.static final intDirective for increasing the indentation level.static final StringFour-space indentation.static final StringTab indentation.static final intDirective for beginning an SQL identifier.static final intDirective for beginning an SQL string literal.static final intDirective for decreasing the indentation level.Fields inherited from class java.io.FilterWriter
out -
Constructor Summary
ConstructorsConstructorDescriptionStackWriter(Writer writer, String indentation) Creates a new StackWriter on top of an existing Writer, with the specified string to be used for each level of indentation. -
Method Summary
Modifier and TypeMethodDescriptionstatic voidprintSqlIdentifier(PrintWriter pw, String s) Writes an SQL identifier.static voidWrites an SQL string literal.voidwrite(char[] cbuf, int off, int len) voidwrite(int c) voidMethods inherited from class java.io.FilterWriter
close, flush
-
Field Details
-
INDENT
public static final int INDENTDirective for increasing the indentation level.- See Also:
-
OUTDENT
public static final int OUTDENTDirective for decreasing the indentation level.- See Also:
-
OPEN_SQL_STRING_LITERAL
public static final int OPEN_SQL_STRING_LITERALDirective for beginning an SQL string literal.- See Also:
-
CLOSE_SQL_STRING_LITERAL
public static final int CLOSE_SQL_STRING_LITERALDirective for ending an SQL string literal.- See Also:
-
OPEN_SQL_IDENTIFIER
public static final int OPEN_SQL_IDENTIFIERDirective for beginning an SQL identifier.- See Also:
-
CLOSE_SQL_IDENTIFIER
public static final int CLOSE_SQL_IDENTIFIERDirective for ending an SQL identifier.- See Also:
-
INDENT_TAB
Tab indentation.- See Also:
-
INDENT_SPACE4
Four-space indentation.- See Also:
-
-
Constructor Details
-
StackWriter
Creates a new StackWriter on top of an existing Writer, with the specified string to be used for each level of indentation.- Parameters:
writer- underlying writerindentation- indentation unit such asINDENT_TABorINDENT_SPACE4
-
-
Method Details
-
write
- Overrides:
writein classFilterWriter- Throws:
IOException
-
write
- Overrides:
writein classFilterWriter- Throws:
IOException
-
write
- Overrides:
writein classFilterWriter- Throws:
IOException
-
printSqlStringLiteral
Writes an SQL string literal.- Parameters:
pw- PrintWriter on which to writes- text of literal
-
printSqlIdentifier
Writes an SQL identifier.- Parameters:
pw- PrintWriter on which to writes- identifier
-