From b2e81d227acc0ac53785b8268fcce15ca221c42b Mon Sep 17 00:00:00 2001 From: lukaseder Date: Mon, 28 Aug 2017 14:50:36 +0200 Subject: [PATCH] [#6525] Add Setting for formatting options --- .../java/org/jooq/conf/ObjectFactory.java | 8 ++ .../java/org/jooq/conf/RenderFormatting.java | 131 ++++++++++++++++++ .../src/main/java/org/jooq/conf/Settings.java | 30 ++++ .../java/org/jooq/conf/SettingsTools.java | 4 +- .../org/jooq/impl/DefaultRenderContext.java | 35 +++-- .../resources/xsd/jooq-runtime-3.10.0.xsd | 22 +++ 6 files changed, 218 insertions(+), 12 deletions(-) create mode 100644 jOOQ/src/main/java/org/jooq/conf/RenderFormatting.java diff --git a/jOOQ/src/main/java/org/jooq/conf/ObjectFactory.java b/jOOQ/src/main/java/org/jooq/conf/ObjectFactory.java index 9d174d1c14..4f6ebf35f7 100644 --- a/jOOQ/src/main/java/org/jooq/conf/ObjectFactory.java +++ b/jOOQ/src/main/java/org/jooq/conf/ObjectFactory.java @@ -72,6 +72,14 @@ public class ObjectFactory { return new MappedTable(); } + /** + * Create an instance of {@link RenderFormatting } + * + */ + public RenderFormatting createRenderFormatting() { + return new RenderFormatting(); + } + /** * Create an instance of {@link JAXBElement }{@code <}{@link Settings }{@code >}} * diff --git a/jOOQ/src/main/java/org/jooq/conf/RenderFormatting.java b/jOOQ/src/main/java/org/jooq/conf/RenderFormatting.java new file mode 100644 index 0000000000..0a884265bc --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/conf/RenderFormatting.java @@ -0,0 +1,131 @@ + + + + + + + + +package org.jooq.conf; + +import java.io.Serializable; +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlType; + + +/** + * All sorts of formatting flags / settings. + * + * + * + */ +@XmlAccessorType(XmlAccessType.FIELD) +@XmlType(name = "RenderFormatting", propOrder = { + +}) +@SuppressWarnings({ + "all" +}) +public class RenderFormatting + extends SettingsBase + implements Serializable, Cloneable +{ + + private final static long serialVersionUID = 31000L; + @XmlElement(defaultValue = "\n") + protected String newline = "\n"; + @XmlElement(defaultValue = " ") + protected String indentation = " "; + @XmlElement(defaultValue = "80") + protected Integer printMargin = 80; + + /** + * The character to be used for line breaks. + * + * @return + * possible object is + * {@link String } + * + */ + public String getNewline() { + return newline; + } + + /** + * Sets the value of the newline property. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setNewline(String value) { + this.newline = value; + } + + /** + * The characters to be used for indentation. + * + * @return + * possible object is + * {@link String } + * + */ + public String getIndentation() { + return indentation; + } + + /** + * Sets the value of the indentation property. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setIndentation(String value) { + this.indentation = value; + } + + /** + * The print margin after which (some) formatted elements will break lines. + * + * @return + * possible object is + * {@link Integer } + * + */ + public Integer getPrintMargin() { + return printMargin; + } + + /** + * Sets the value of the printMargin property. + * + * @param value + * allowed object is + * {@link Integer } + * + */ + public void setPrintMargin(Integer value) { + this.printMargin = value; + } + + public RenderFormatting withNewline(String value) { + setNewline(value); + return this; + } + + public RenderFormatting withIndentation(String value) { + setIndentation(value); + return this; + } + + public RenderFormatting withPrintMargin(Integer value) { + setPrintMargin(value); + return this; + } + +} diff --git a/jOOQ/src/main/java/org/jooq/conf/Settings.java b/jOOQ/src/main/java/org/jooq/conf/Settings.java index d10b5702cc..20a65c5459 100644 --- a/jOOQ/src/main/java/org/jooq/conf/Settings.java +++ b/jOOQ/src/main/java/org/jooq/conf/Settings.java @@ -48,6 +48,7 @@ public class Settings protected RenderKeywordStyle renderKeywordStyle = RenderKeywordStyle.AS_IS; @XmlElement(defaultValue = "false") protected Boolean renderFormatted = false; + protected RenderFormatting renderFormatting; @XmlElement(defaultValue = "false") protected Boolean renderScalarSubqueriesForStoredFunctions = false; @XmlElement(defaultValue = "DEFAULT") @@ -254,6 +255,30 @@ public class Settings this.renderFormatted = value; } + /** + * All sorts of formatting flags / settings. + * + * @return + * possible object is + * {@link RenderFormatting } + * + */ + public RenderFormatting getRenderFormatting() { + return renderFormatting; + } + + /** + * Sets the value of the renderFormatting property. + * + * @param value + * allowed object is + * {@link RenderFormatting } + * + */ + public void setRenderFormatting(RenderFormatting value) { + this.renderFormatting = value; + } + /** * Whether stored function calls should be wrapped in scalar subqueries. *

@@ -839,6 +864,11 @@ public class Settings return this; } + public Settings withRenderFormatting(RenderFormatting value) { + setRenderFormatting(value); + return this; + } + public Settings withRenderScalarSubqueriesForStoredFunctions(Boolean value) { setRenderScalarSubqueriesForStoredFunctions(value); return this; diff --git a/jOOQ/src/main/java/org/jooq/conf/SettingsTools.java b/jOOQ/src/main/java/org/jooq/conf/SettingsTools.java index 842b9cf9e1..c70fcbd905 100644 --- a/jOOQ/src/main/java/org/jooq/conf/SettingsTools.java +++ b/jOOQ/src/main/java/org/jooq/conf/SettingsTools.java @@ -202,7 +202,9 @@ public final class SettingsTools { * Clone some settings. */ public static final Settings clone(Settings settings) { - return (Settings) settings.clone(); + Settings result = (Settings) settings.clone(); + result.renderFormatting = (RenderFormatting) result.renderFormatting.clone(); + return result; } /** diff --git a/jOOQ/src/main/java/org/jooq/impl/DefaultRenderContext.java b/jOOQ/src/main/java/org/jooq/impl/DefaultRenderContext.java index 62ac95c856..43aa2d590e 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DefaultRenderContext.java +++ b/jOOQ/src/main/java/org/jooq/impl/DefaultRenderContext.java @@ -62,6 +62,7 @@ import org.jooq.QueryPart; import org.jooq.QueryPartInternal; import org.jooq.RenderContext; import org.jooq.SQLDialect; +import org.jooq.conf.RenderFormatting; import org.jooq.conf.RenderKeywordStyle; import org.jooq.conf.RenderNameStyle; import org.jooq.conf.Settings; @@ -88,7 +89,6 @@ class DefaultRenderContext extends AbstractContext implements Ren private int alias; private int indent; private Deque indentLock; - private int printMargin = 80; private boolean separator; private boolean newline; private int skipUpdateCounts; @@ -98,6 +98,12 @@ class DefaultRenderContext extends AbstractContext implements Ren RenderNameStyle cachedRenderNameStyle; boolean cachedRenderFormatted; + // [#6525] Cached values from Settings.renderFormatting + String cachedIndentation; + int cachedIndentWidth; + String cachedNewline; + int cachedPrintMargin; + DefaultRenderContext(Configuration configuration) { super(configuration, null); @@ -108,6 +114,15 @@ class DefaultRenderContext extends AbstractContext implements Ren this.cachedRenderKeywordStyle = settings.getRenderKeywordStyle(); this.cachedRenderFormatted = Boolean.TRUE.equals(settings.isRenderFormatted()); this.cachedRenderNameStyle = settings.getRenderNameStyle(); + + RenderFormatting formatting = settings.getRenderFormatting(); + if (formatting == null) + formatting = new RenderFormatting(); + + this.cachedNewline = formatting.getNewline() == null ? "\n" : formatting.getNewline(); + this.cachedIndentation = formatting.getIndentation() == null ? " " : formatting.getIndentation(); + this.cachedIndentWidth = cachedIndentation.length(); + this.cachedPrintMargin = formatting.getPrintMargin() == null ? 80 : formatting.getPrintMargin(); } DefaultRenderContext(RenderContext context) { @@ -222,7 +237,7 @@ class DefaultRenderContext extends AbstractContext implements Ren @Override public final RenderContext formatNewLine() { if (cachedRenderFormatted) { - sql("\n", true); + sql(cachedNewline, true); sql(indentation(), true); newline = true; @@ -233,15 +248,15 @@ class DefaultRenderContext extends AbstractContext implements Ren @Override public final RenderContext formatNewLineAfterPrintMargin() { - if (cachedRenderFormatted && printMargin > 0) - if (sql.length() - sql.lastIndexOf("\n") > printMargin) + if (cachedRenderFormatted && cachedPrintMargin > 0) + if (sql.length() - sql.lastIndexOf(cachedNewline) > cachedPrintMargin) formatNewLine(); return this; } private final String indentation() { - return StringUtils.leftPad("", indent, " "); + return StringUtils.leftPad("", indent, cachedIndentation); } @Override @@ -271,12 +286,12 @@ class DefaultRenderContext extends AbstractContext implements Ren @Override public final RenderContext formatIndentStart() { - return formatIndentStart(2); + return formatIndentStart(cachedIndentWidth); } @Override public final RenderContext formatIndentEnd() { - return formatIndentEnd(2); + return formatIndentEnd(cachedIndentWidth); } @Override @@ -302,13 +317,11 @@ class DefaultRenderContext extends AbstractContext implements Ren return indentLock; } - private static final Pattern NEW_LINE = Pattern.compile("[\\n\\r]"); - @Override public final RenderContext formatIndentLockStart() { if (cachedRenderFormatted) { indentLock().push(indent); - String[] lines = NEW_LINE.split(sql); + String[] lines = NEWLINE.split(sql); indent = lines[lines.length - 1].length(); } @@ -325,7 +338,7 @@ class DefaultRenderContext extends AbstractContext implements Ren @Override public final RenderContext formatPrintMargin(int margin) { - printMargin = margin; + cachedPrintMargin = margin; return this; } diff --git a/jOOQ/src/main/resources/xsd/jooq-runtime-3.10.0.xsd b/jOOQ/src/main/resources/xsd/jooq-runtime-3.10.0.xsd index 706c1ce51c..038e1aa714 100644 --- a/jOOQ/src/main/resources/xsd/jooq-runtime-3.10.0.xsd +++ b/jOOQ/src/main/resources/xsd/jooq-runtime-3.10.0.xsd @@ -48,6 +48,10 @@ This is set to "QUOTED" by default for backwards-compatibility]]>< + + + + @@ -345,6 +349,24 @@ Either <input/> or <inputExpression/> must be provided]]> + + + + + + + + + + + + + + + + + +