[#2528] Combining renderFormatted with inlined bind variables will
change bind values when they contain newlines
This commit is contained in:
parent
1fe63caa1e
commit
ca19cb98e9
@ -530,4 +530,25 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, UU, I, IPK, T7
|
||||
assertTrue(s.getSQL().contains("ON"));
|
||||
assertTrue(s.getSQL().contains("ORDER BY"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRenderFormattedAndInlinedWithNewlines() throws Exception {
|
||||
// [#2528] When inlining bind values, formatting SQL might change the
|
||||
// values, in case values contain newlines
|
||||
DSLContext create = create(new Settings()
|
||||
.withRenderFormatted(true)
|
||||
.withStatementType(StatementType.STATIC_STATEMENT));
|
||||
|
||||
String value = "foo\nbar\n\n baz";
|
||||
|
||||
create.update(TBook())
|
||||
.set(TBook_TITLE(), value)
|
||||
.where(TBook_ID().eq(1))
|
||||
.execute();
|
||||
|
||||
assertEquals(value, create.select(TBook_TITLE())
|
||||
.from(TBook())
|
||||
.where(TBook_ID().eq(1))
|
||||
.fetchOne(TBook_TITLE()));
|
||||
}
|
||||
}
|
||||
|
||||
@ -2102,6 +2102,11 @@ public abstract class jOOQAbstractTest<
|
||||
new BatchTests(this).testBatchDelete();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRenderFormattedAndInlinedWithNewlines() throws Exception {
|
||||
new RenderAndBindTests(this).testRenderFormattedAndInlinedWithNewlines();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNamedParams() throws Exception {
|
||||
new RenderAndBindTests(this).testNamedParams();
|
||||
|
||||
@ -86,6 +86,14 @@ public interface RenderContext extends Context<RenderContext> {
|
||||
*/
|
||||
RenderContext sql(String sql);
|
||||
|
||||
/**
|
||||
* Append some SQL to the context's contained {@link StringBuilder}.
|
||||
* <p>
|
||||
* Set <code>literal = true</code> to indicate that the
|
||||
* <code>RenderContext</code> shall not format the argument SQL.
|
||||
*/
|
||||
RenderContext sql(String sql, boolean literal);
|
||||
|
||||
/**
|
||||
* Append some SQL to the context's contained {@link StringBuilder}.
|
||||
*/
|
||||
|
||||
@ -146,14 +146,20 @@ class DefaultRenderContext extends AbstractContext<RenderContext> implements Ren
|
||||
|
||||
@Override
|
||||
public final RenderContext sql(String s) {
|
||||
if (s != null && cachedRenderFormatted) {
|
||||
sql.append(NEWLINE.matcher(s).replaceAll("$0" + indentation()));
|
||||
return sql(s, s == null || !cachedRenderFormatted);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final RenderContext sql(String s, boolean literal) {
|
||||
if (literal) {
|
||||
sql.append(s);
|
||||
}
|
||||
else {
|
||||
sql.append(s);
|
||||
sql.append(NEWLINE.matcher(s).replaceAll("$0" + indentation()));
|
||||
}
|
||||
|
||||
return this;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -486,7 +486,7 @@ class Val<T> extends AbstractParam<T> {
|
||||
}
|
||||
}
|
||||
else if (ArrayRecord.class.isAssignableFrom(type)) {
|
||||
context.sql(val.toString());
|
||||
context.sql(val.toString(), true);
|
||||
}
|
||||
else if (EnumType.class.isAssignableFrom(type)) {
|
||||
toSQL(context, ((EnumType) val).getLiteral());
|
||||
@ -501,7 +501,7 @@ class Val<T> extends AbstractParam<T> {
|
||||
// - UUID
|
||||
else {
|
||||
context.sql("'")
|
||||
.sql(escape(val))
|
||||
.sql(escape(val), true)
|
||||
.sql("'");
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user