[#3623] Add ParamType.NAMED_OR_INLINED to render named parameters only if a name is given
This commit is contained in:
parent
8f813f4893
commit
ebe9d22b2f
@ -262,13 +262,25 @@ public interface DSLContext {
|
||||
* variables as named parameters.
|
||||
* <p>
|
||||
* This is the same as calling
|
||||
* <code>renderContext().namedParams(true).render(part)</code>
|
||||
* <code>renderContext().paramType(NAMED).render(part)</code>
|
||||
*
|
||||
* @param part The {@link QueryPart} to be rendered
|
||||
* @return The rendered SQL
|
||||
*/
|
||||
String renderNamedParams(QueryPart part);
|
||||
|
||||
/**
|
||||
* Render a QueryPart in the context of this <code>DSLContext</code>, rendering bind
|
||||
* variables as named parameters, or inlined parameters if they have no name.
|
||||
* <p>
|
||||
* This is the same as calling
|
||||
* <code>renderContext().paramType(NAMED_OR_INLINED).render(part)</code>
|
||||
*
|
||||
* @param part The {@link QueryPart} to be rendered
|
||||
* @return The rendered SQL
|
||||
*/
|
||||
String renderNamedOrInlinedParams(QueryPart part);
|
||||
|
||||
/**
|
||||
* Render a QueryPart in the context of this <code>DSLContext</code>, inlining all bind
|
||||
* variables.
|
||||
|
||||
@ -43,11 +43,13 @@ package org.jooq.impl;
|
||||
import static org.jooq.Clause.FIELD;
|
||||
import static org.jooq.Clause.FIELD_VALUE;
|
||||
import static org.jooq.conf.ParamType.INLINED;
|
||||
import static org.jooq.conf.ParamType.NAMED_OR_INLINED;
|
||||
|
||||
import org.jooq.Clause;
|
||||
import org.jooq.Context;
|
||||
import org.jooq.DataType;
|
||||
import org.jooq.Param;
|
||||
import org.jooq.tools.StringUtils;
|
||||
|
||||
/**
|
||||
* A base implementation for {@link Param}
|
||||
@ -133,6 +135,8 @@ abstract class AbstractParam<T> extends AbstractField<T> implements Param<T> {
|
||||
}
|
||||
|
||||
final boolean isInline(Context<?> context) {
|
||||
return isInline() || context.paramType() == INLINED;
|
||||
return isInline()
|
||||
|| (context.paramType() == INLINED)
|
||||
|| (context.paramType() == NAMED_OR_INLINED && StringUtils.isBlank(paramName));
|
||||
}
|
||||
}
|
||||
|
||||
@ -475,6 +475,8 @@ abstract class AbstractQuery extends AbstractQueryPart implements Query, Attacha
|
||||
return create().renderInlined(this);
|
||||
case NAMED:
|
||||
return create().renderNamedParams(this);
|
||||
case NAMED_OR_INLINED:
|
||||
return create().renderNamedOrInlinedParams(this);
|
||||
}
|
||||
|
||||
throw new IllegalArgumentException("ParamType not supported: " + paramType);
|
||||
|
||||
@ -42,6 +42,7 @@ package org.jooq.impl;
|
||||
|
||||
import static org.jooq.conf.ParamType.INLINED;
|
||||
import static org.jooq.conf.ParamType.NAMED;
|
||||
import static org.jooq.conf.ParamType.NAMED_OR_INLINED;
|
||||
import static org.jooq.impl.DSL.field;
|
||||
import static org.jooq.impl.DSL.fieldByName;
|
||||
import static org.jooq.impl.DSL.queryPart;
|
||||
@ -374,6 +375,11 @@ public class DefaultDSLContext implements DSLContext, Serializable {
|
||||
return renderContext().paramType(NAMED).render(part);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String renderNamedOrInlinedParams(QueryPart part) {
|
||||
return renderContext().paramType(NAMED_OR_INLINED).render(part);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String renderInlined(QueryPart part) {
|
||||
return renderContext().paramType(INLINED).render(part);
|
||||
|
||||
@ -46,6 +46,7 @@ import static org.jooq.SQLDialect.CUBRID;
|
||||
import static org.jooq.SQLDialect.POSTGRES;
|
||||
import static org.jooq.conf.ParamType.INLINED;
|
||||
import static org.jooq.conf.ParamType.NAMED;
|
||||
import static org.jooq.conf.ParamType.NAMED_OR_INLINED;
|
||||
import static org.jooq.conf.SettingsTools.reflectionCaching;
|
||||
import static org.jooq.conf.SettingsTools.updatablePrimaryKeys;
|
||||
import static org.jooq.impl.DSL.concat;
|
||||
@ -1270,7 +1271,7 @@ final class Utils {
|
||||
else if (sqlChars[i] == '?' && substituteIndex < substitutes.size()) {
|
||||
QueryPart substitute = substitutes.get(substituteIndex++);
|
||||
|
||||
if (render.paramType() == INLINED || render.paramType() == NAMED) {
|
||||
if (render.paramType() == INLINED || render.paramType() == NAMED || render.paramType() == NAMED_OR_INLINED) {
|
||||
render.visit(substitute);
|
||||
}
|
||||
else {
|
||||
|
||||
@ -60,8 +60,8 @@ import static org.jooq.SQLDialect.POSTGRES;
|
||||
import static org.jooq.SQLDialect.SQLITE;
|
||||
// ...
|
||||
// ...
|
||||
import static org.jooq.conf.ParamType.INLINED;
|
||||
import static org.jooq.conf.ParamType.NAMED;
|
||||
import static org.jooq.conf.ParamType.NAMED_OR_INLINED;
|
||||
import static org.jooq.impl.DSL.name;
|
||||
import static org.jooq.impl.DSL.using;
|
||||
import static org.jooq.tools.StringUtils.leftPad;
|
||||
@ -313,7 +313,7 @@ class Val<T> extends AbstractParam<T> {
|
||||
* {@link RenderContext#namedParams()}
|
||||
*/
|
||||
private final String getBindVariable(RenderContext context) {
|
||||
if (context.paramType() == NAMED) {
|
||||
if (context.paramType() == NAMED || context.paramType() == NAMED_OR_INLINED) {
|
||||
int index = context.nextIndex();
|
||||
|
||||
if (StringUtils.isBlank(getParamName())) {
|
||||
@ -631,7 +631,7 @@ class Val<T> extends AbstractParam<T> {
|
||||
final void bind0(BindContext context) {
|
||||
|
||||
// [#1302] Bind value only if it was not explicitly forced to be inlined
|
||||
if (!isInline() && context.paramType() != INLINED) {
|
||||
if (!isInline(context)) {
|
||||
context.bindValue(value, this);
|
||||
}
|
||||
}
|
||||
|
||||
@ -38,7 +38,8 @@
|
||||
|
||||
- question marks
|
||||
- named parameters
|
||||
- inlined values
|
||||
- named or inlined parameters
|
||||
- inlined parameters
|
||||
|
||||
This value is overridden by statementType == STATIC_STATEMENT, in
|
||||
case of which, this defaults to INLINED
|
||||
@ -128,6 +129,9 @@
|
||||
|
||||
<!-- Execute statements with named parameters -->
|
||||
<enumeration value="NAMED"/>
|
||||
|
||||
<!-- Execute statements with named parameters, if a name is given, or inlined parameters otherwise -->
|
||||
<enumeration value="NAMED_OR_INLINED"/>
|
||||
|
||||
<!-- Execute statements with inlined parameters -->
|
||||
<enumeration value="INLINED"/>
|
||||
|
||||
@ -44,9 +44,11 @@ import static org.jooq.SQLDialect.MYSQL;
|
||||
import static org.jooq.conf.ParamType.INDEXED;
|
||||
import static org.jooq.conf.ParamType.INLINED;
|
||||
import static org.jooq.conf.ParamType.NAMED;
|
||||
import static org.jooq.conf.ParamType.NAMED_OR_INLINED;
|
||||
import static org.jooq.conf.RenderKeywordStyle.LOWER;
|
||||
import static org.jooq.conf.RenderKeywordStyle.UPPER;
|
||||
import static org.jooq.conf.StatementType.STATIC_STATEMENT;
|
||||
import static org.jooq.impl.DSL.param;
|
||||
import static org.jooq.impl.DSL.val;
|
||||
|
||||
import org.jooq.DSLContext;
|
||||
@ -72,13 +74,14 @@ public class RenderTest extends AbstractTest {
|
||||
assertEquals("select ?, ? from dual", q.getSQL(false));
|
||||
assertEquals("select 1, 'A' from dual", q.getSQL(true));
|
||||
assertEquals("select ?, ? from dual", q.getSQL(INDEXED));
|
||||
assertEquals("select :1, :2 from dual", q.getSQL(NAMED));
|
||||
assertEquals("select :var, :2 from dual", q.getSQL(NAMED));
|
||||
assertEquals("select :var, 'A' from dual", q.getSQL(NAMED_OR_INLINED));
|
||||
assertEquals("select 1, 'A' from dual", q.getSQL(INLINED));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetSQL() {
|
||||
Query q = create.select(val(1), val("A"));
|
||||
Query q = create.select(param("var", 1), val("A"));
|
||||
testGetSQL0(q, "select ?, ? from dual");
|
||||
}
|
||||
|
||||
@ -86,7 +89,7 @@ public class RenderTest extends AbstractTest {
|
||||
public void testGetSQLWithParamTypeINDEXED() {
|
||||
Query q =
|
||||
DSL.using(SQLDialect.MYSQL, new Settings().withParamType(INDEXED))
|
||||
.select(val(1), val("A"));
|
||||
.select(param("var", 1), val("A"));
|
||||
|
||||
testGetSQL0(q, "select ?, ? from dual");
|
||||
}
|
||||
@ -96,7 +99,7 @@ public class RenderTest extends AbstractTest {
|
||||
Query q =
|
||||
DSL.using(SQLDialect.MYSQL, new Settings().withParamType(INDEXED)
|
||||
.withStatementType(STATIC_STATEMENT))
|
||||
.select(val(1), val("A"));
|
||||
.select(param("var", 1), val("A"));
|
||||
|
||||
testGetSQL0(q, "select 1, 'A' from dual");
|
||||
}
|
||||
@ -105,17 +108,36 @@ public class RenderTest extends AbstractTest {
|
||||
public void testGetSQLWithParamTypeNAMED() {
|
||||
Query q =
|
||||
DSL.using(SQLDialect.MYSQL, new Settings().withParamType(NAMED))
|
||||
.select(val(1), val("A"));
|
||||
.select(param("var", 1), val("A"));
|
||||
|
||||
testGetSQL0(q, "select :1, :2 from dual");
|
||||
testGetSQL0(q, "select :var, :2 from dual");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetSQLWithParamTypeNAMEDandStatementTypeSTATIC() {
|
||||
Query q =
|
||||
DSL.using(SQLDialect.MYSQL, new Settings().withParamType(NAMED)
|
||||
.withStatementType(STATIC_STATEMENT))
|
||||
.select(val(1), val("A"));
|
||||
.withStatementType(STATIC_STATEMENT))
|
||||
.select(param("var", 1), val("A"));
|
||||
|
||||
testGetSQL0(q, "select 1, 'A' from dual");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetSQLWithParamTypeNAMED_OR_INLINED() {
|
||||
Query q =
|
||||
DSL.using(SQLDialect.MYSQL, new Settings().withParamType(NAMED_OR_INLINED))
|
||||
.select(param("var", 1), val("A"));
|
||||
|
||||
testGetSQL0(q, "select :var, 'A' from dual");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetSQLWithParamTypeNAMED_OR_INLINEDandStatementTypeSTATIC() {
|
||||
Query q =
|
||||
DSL.using(SQLDialect.MYSQL, new Settings().withParamType(NAMED_OR_INLINED)
|
||||
.withStatementType(STATIC_STATEMENT))
|
||||
.select(param("var", 1), val("A"));
|
||||
|
||||
testGetSQL0(q, "select 1, 'A' from dual");
|
||||
}
|
||||
@ -124,7 +146,7 @@ public class RenderTest extends AbstractTest {
|
||||
public void testGetSQLWithParamTypeINLINED() {
|
||||
Query q =
|
||||
DSL.using(SQLDialect.MYSQL, new Settings().withParamType(INLINED))
|
||||
.select(val(1), val("A"));
|
||||
.select(param("var", 1), val("A"));
|
||||
|
||||
testGetSQL0(q, "select 1, 'A' from dual");
|
||||
}
|
||||
@ -133,8 +155,8 @@ public class RenderTest extends AbstractTest {
|
||||
public void testGetSQLWithParamTypeINLINEDandStatementTypeSTATIC() {
|
||||
Query q =
|
||||
DSL.using(SQLDialect.MYSQL, new Settings().withParamType(INLINED)
|
||||
.withStatementType(STATIC_STATEMENT))
|
||||
.select(val(1), val("A"));
|
||||
.withStatementType(STATIC_STATEMENT))
|
||||
.select(param("var", 1), val("A"));
|
||||
|
||||
testGetSQL0(q, "select 1, 'A' from dual");
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user