Skip to content

Commit 16495da

Browse files
authored
Merge pull request #3422 from harawata/trimsqlnode-add-space
`TrimSqlNode` should add an extra space when concatenating its contents like `MixedSqlNode` does
2 parents 848bbdc + 3bf602f commit 16495da

File tree

3 files changed

+10
-9
lines changed

3 files changed

+10
-9
lines changed

src/main/java/org/apache/ibatis/scripting/xmltags/TrimSqlNode.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public FilteredDynamicContext(DynamicContext delegate) {
9191
public void applyAll() {
9292
sqlBuffer = new StringBuilder(sqlBuffer.toString().trim());
9393
String trimmedUppercaseSql = sqlBuffer.toString().toUpperCase(Locale.ENGLISH);
94-
if (trimmedUppercaseSql.length() > 0) {
94+
if (!trimmedUppercaseSql.isEmpty()) {
9595
applyPrefix(sqlBuffer, trimmedUppercaseSql);
9696
applySuffix(sqlBuffer, trimmedUppercaseSql);
9797
}
@@ -100,6 +100,9 @@ public void applyAll() {
100100

101101
@Override
102102
public void appendSql(String sql) {
103+
if (sqlBuffer.length() > 0) {
104+
sqlBuffer.append(" ");
105+
}
103106
sqlBuffer.append(sql);
104107
}
105108

src/test/java/org/apache/ibatis/builder/xml/dynamic/DynamicSqlSourceTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ void shouldTrimWHEREInsteadOfORForSecondCondition() throws Exception {
214214

215215
@Test
216216
void shouldTrimWHEREInsteadOfANDForBothConditions() throws Exception {
217-
final String expected = "SELECT * FROM BLOG WHERE ID = ? OR NAME = ?";
217+
final String expected = "SELECT * FROM BLOG WHERE ID = ? OR NAME = ?";
218218
DynamicSqlSource source = createDynamicSqlSource(new TextSqlNode("SELECT * FROM BLOG"),
219219
new WhereSqlNode(new Configuration(),
220220
mixedContents(new IfSqlNode(mixedContents(new TextSqlNode(" and ID = ? ")), "true"),
@@ -236,7 +236,7 @@ void shouldTrimNoWhereClause() throws Exception {
236236

237237
@Test
238238
void shouldTrimSETInsteadOfCOMMAForBothConditions() throws Exception {
239-
final String expected = "UPDATE BLOG SET ID = ?, NAME = ?";
239+
final String expected = "UPDATE BLOG SET ID = ?, NAME = ?";
240240
DynamicSqlSource source = createDynamicSqlSource(new TextSqlNode("UPDATE BLOG"),
241241
new SetSqlNode(new Configuration(),
242242
mixedContents(new IfSqlNode(mixedContents(new TextSqlNode(" ID = ?, ")), "true"),

src/test/java/org/apache/ibatis/scripting/xmltags/XMLScriptBuilderTest.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,14 @@ void shouldWhereInsertWhitespace() throws Exception {
3232
String xml = """
3333
<script>
3434
select * from user
35-
<where>
36-
<if test="1==1">and id = 1</if>
37-
<if test="1==1">and id > 0</if>
38-
</where>
35+
<where><if test="1==1">and id = 1</if><if test="1==1">and id > 0</if></where>
36+
<if test="1==1">and id = 1</if><if test="1==1">and id > 0</if>
3937
</script>
4038
""";
4139
SqlSource sqlSource = new XMLScriptBuilder(new Configuration(), new XPathParser(xml).evalNode("/script"))
4240
.parseScriptNode();
43-
assertThat(sqlSource.getBoundSql(1).getSql())
44-
.containsPattern("(?m)^\\s*select \\* from user\\s+WHERE\\s+id = 1\\s+and id > 0\\s*$");
41+
assertThat(sqlSource.getBoundSql(1).getSql()).containsPattern(
42+
"(?m)^\\s*select \\* from user\\s+WHERE\\s+id = 1\\s+and id > 0\\s+and id = 1\\s+and id > 0\\s*$");
4543
}
4644

4745
@Test

0 commit comments

Comments
 (0)