[#8486] Add Settings.renderNamedParamPrefix to support dialect specific named parameter placeholders

This commit is contained in:
lukaseder 2019-04-08 11:16:48 +02:00
parent dfaddcaca8
commit af286d96b7
3 changed files with 69 additions and 2 deletions

View File

@ -52,6 +52,8 @@ public class Settings
@XmlElement(defaultValue = "QUOTED")
@XmlSchemaType(name = "string")
protected RenderNameStyle renderNameStyle = RenderNameStyle.QUOTED;
@XmlElement(defaultValue = ":")
protected String renderNamedParamPrefix = ":";
@XmlElement(defaultValue = "AS_IS")
@XmlSchemaType(name = "string")
protected RenderKeywordCase renderKeywordCase = RenderKeywordCase.AS_IS;
@ -327,6 +329,38 @@ public class Settings
this.renderNameStyle = value;
}
/**
* The prefix to use for named parameters.
* <p>
* Named parameter syntax defaults to <code>:name</code> (such as supported by Oracle, JPA, Spring), but
* vendor specific parameters may look differently. This flag can be used to determine the prefix to be
* used by named parameters, such as <code>@</code> for SQL Server's <code>@name</code> or <code>$</code>
* for PostgreSQL's <code>$name</code>.
* <p>
* "Named indexed" parameters can be obtained in the same way by specifingy {@code ParamType#NAMED} and not
* providing a name to parameters, resulting in <code>:1</code> or <code>@1</code> or <code>$1</code>, etc.
*
* @return
* possible object is
* {@link String }
*
*/
public String getRenderNamedParamPrefix() {
return renderNamedParamPrefix;
}
/**
* Sets the value of the renderNamedParamPrefix property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setRenderNamedParamPrefix(String value) {
this.renderNamedParamPrefix = value;
}
/**
* Whether the case of {@link org.jooq.Keyword} references should be modified in any way.
*
@ -1549,6 +1583,11 @@ public class Settings
return this;
}
public Settings withRenderNamedParamPrefix(String value) {
setRenderNamedParamPrefix(value);
return this;
}
public Settings withRenderKeywordCase(RenderKeywordCase value) {
setRenderKeywordCase(value);
return this;
@ -1817,6 +1856,11 @@ public class Settings
sb.append(renderNameStyle);
sb.append("</renderNameStyle>");
}
if ((renderNamedParamPrefix!= null)&&(!"".equals(renderNamedParamPrefix))) {
sb.append("<renderNamedParamPrefix>");
sb.append(renderNamedParamPrefix);
sb.append("</renderNamedParamPrefix>");
}
if (renderKeywordCase!= null) {
sb.append("<renderKeywordCase>");
sb.append(renderKeywordCase);
@ -2121,6 +2165,15 @@ public class Settings
return false;
}
}
if (renderNamedParamPrefix == null) {
if (other.renderNamedParamPrefix!= null) {
return false;
}
} else {
if (!renderNamedParamPrefix.equals(other.renderNamedParamPrefix)) {
return false;
}
}
if (renderKeywordCase == null) {
if (other.renderKeywordCase!= null) {
return false;
@ -2557,6 +2610,7 @@ public class Settings
result = ((prime*result)+((renderQuotedNames == null)? 0 :renderQuotedNames.hashCode()));
result = ((prime*result)+((renderNameCase == null)? 0 :renderNameCase.hashCode()));
result = ((prime*result)+((renderNameStyle == null)? 0 :renderNameStyle.hashCode()));
result = ((prime*result)+((renderNamedParamPrefix == null)? 0 :renderNamedParamPrefix.hashCode()));
result = ((prime*result)+((renderKeywordCase == null)? 0 :renderKeywordCase.hashCode()));
result = ((prime*result)+((renderKeywordStyle == null)? 0 :renderKeywordStyle.hashCode()));
result = ((prime*result)+((renderLocale == null)? 0 :renderLocale.hashCode()));

View File

@ -112,11 +112,12 @@ final class Val<T> extends AbstractParam<T> {
final String getBindVariable(Context<?> ctx) {
if (ctx.paramType() == NAMED || ctx.paramType() == NAMED_OR_INLINED) {
int index = ctx.nextIndex();
String prefix = StringUtils.defaultIfNull(ctx.settings().getRenderNamedParamPrefix(), ":");
if (StringUtils.isBlank(getParamName()))
return ":" + index;
return prefix + index;
else
return ":" + getParamName();
return prefix + getParamName();
}
else {
return "?";

View File

@ -58,6 +58,18 @@ This is set to "QUOTED" by default for backwards-compatibility.
@deprecated - 3.12.0 - [#5909] - Use {@link RenderQuotedNames} and {@link RenderNameCase} instead.]]></jxb:javadoc></jxb:property></appinfo></annotation>
</element>
<element name="renderNamedParamPrefix" type="string" minOccurs="0" maxOccurs="1" default=":">
<annotation><appinfo><jxb:property><jxb:javadoc><![CDATA[The prefix to use for named parameters.
<p>
Named parameter syntax defaults to <code>:name</code> (such as supported by Oracle, JPA, Spring), but
vendor specific parameters may look differently. This flag can be used to determine the prefix to be
used by named parameters, such as <code>@</code> for SQL Server's <code>@name</code> or <code>$</code>
for PostgreSQL's <code>$name</code>.
<p>
"Named indexed" parameters can be obtained in the same way by specifingy {@code ParamType#NAMED} and not
providing a name to parameters, resulting in <code>:1</code> or <code>@1</code> or <code>$1</code>, etc.]]></jxb:javadoc></jxb:property></appinfo></annotation>
</element>
<element name="renderKeywordCase" type="jooq-runtime:RenderKeywordCase" minOccurs="0" maxOccurs="1" default="AS_IS">
<annotation><appinfo><jxb:property><jxb:javadoc><![CDATA[Whether the case of {@link org.jooq.Keyword} references should be modified in any way.]]></jxb:javadoc></jxb:property></appinfo></annotation>
</element>