[#6525] Add Setting for formatting options

This commit is contained in:
lukaseder 2017-08-28 14:50:36 +02:00
parent a02484603c
commit b2e81d227a
6 changed files with 218 additions and 12 deletions

View File

@ -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 >}}
*

View File

@ -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;
}
}

View File

@ -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.
* <p>
@ -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;

View File

@ -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;
}
/**

View File

@ -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<RenderContext> implements Ren
private int alias;
private int indent;
private Deque<Integer> indentLock;
private int printMargin = 80;
private boolean separator;
private boolean newline;
private int skipUpdateCounts;
@ -98,6 +98,12 @@ class DefaultRenderContext extends AbstractContext<RenderContext> 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<RenderContext> 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<RenderContext> 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<RenderContext> 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<RenderContext> 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<RenderContext> 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<RenderContext> implements Ren
@Override
public final RenderContext formatPrintMargin(int margin) {
printMargin = margin;
cachedPrintMargin = margin;
return this;
}

View File

@ -48,6 +48,10 @@ This is set to "QUOTED" by default for backwards-compatibility]]></jxb:javadoc><
<annotation><appinfo><jxb:property><jxb:javadoc><![CDATA[Whether rendered SQL should be pretty-printed.]]></jxb:javadoc></jxb:property></appinfo></annotation>
</element>
<element name="renderFormatting" type="jooq-runtime:RenderFormatting" minOccurs="0" maxOccurs="1">
<annotation><appinfo><jxb:property><jxb:javadoc><![CDATA[All sorts of formatting flags / settings.]]></jxb:javadoc></jxb:property></appinfo></annotation>
</element>
<element name="renderScalarSubqueriesForStoredFunctions" type="boolean" minOccurs="0" maxOccurs="1" default="false">
<annotation><appinfo><jxb:property><jxb:javadoc><![CDATA[Whether stored function calls should be wrapped in scalar subqueries.
<p>
@ -345,6 +349,24 @@ Either &lt;input/> or &lt;inputExpression/> must be provided]]></jxb:javadoc></j
</restriction>
</simpleType>
<complexType name="RenderFormatting">
<annotation><appinfo><jxb:class><jxb:javadoc><![CDATA[All sorts of formatting flags / settings.]]></jxb:javadoc></jxb:class></appinfo></annotation>
<all>
<element name="newline" type="string" minOccurs="0" maxOccurs="1" default="&#10;">
<annotation><appinfo><jxb:property><jxb:javadoc><![CDATA[The character to be used for line breaks.]]></jxb:javadoc></jxb:property></appinfo></annotation>
</element>
<element name="indentation" type="string" minOccurs="0" maxOccurs="1" default=" ">
<annotation><appinfo><jxb:property><jxb:javadoc><![CDATA[The characters to be used for indentation.]]></jxb:javadoc></jxb:property></appinfo></annotation>
</element>
<element name="printMargin" type="int" minOccurs="0" maxOccurs="1" default="80">
<annotation><appinfo><jxb:property><jxb:javadoc><![CDATA[The print margin after which (some) formatted elements will break lines.]]></jxb:javadoc></jxb:property></appinfo></annotation>
</element>
</all>
</complexType>
<simpleType name="BackslashEscaping">
<restriction base="string">