From 9b6ce3f07555a86a8f525144c6a4479327fde6f6 Mon Sep 17 00:00:00 2001 From: Iwao AVE! Date: Sat, 1 Mar 2025 04:44:06 +0900 Subject: [PATCH 1/2] `TrimSqlNode` should add an extra space when concatenating its contents like `MixedSqlNode` does This was the [key difference](https://github.com/mybatis/mybatis-3/pull/3349#issuecomment-2567497103) that broke #3349 . --- .../org/apache/ibatis/scripting/xmltags/TrimSqlNode.java | 5 ++++- .../ibatis/builder/xml/dynamic/DynamicSqlSourceTest.java | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/apache/ibatis/scripting/xmltags/TrimSqlNode.java b/src/main/java/org/apache/ibatis/scripting/xmltags/TrimSqlNode.java index 1a1053794ec..9178b829547 100644 --- a/src/main/java/org/apache/ibatis/scripting/xmltags/TrimSqlNode.java +++ b/src/main/java/org/apache/ibatis/scripting/xmltags/TrimSqlNode.java @@ -90,7 +90,7 @@ public FilteredDynamicContext(DynamicContext delegate) { public void applyAll() { sqlBuffer = new StringBuilder(sqlBuffer.toString().trim()); String trimmedUppercaseSql = sqlBuffer.toString().toUpperCase(Locale.ENGLISH); - if (trimmedUppercaseSql.length() > 0) { + if (!trimmedUppercaseSql.isEmpty()) { applyPrefix(sqlBuffer, trimmedUppercaseSql); applySuffix(sqlBuffer, trimmedUppercaseSql); } @@ -99,6 +99,9 @@ public void applyAll() { @Override public void appendSql(String sql) { + if (sqlBuffer.length() > 0) { + sqlBuffer.append(" "); + } sqlBuffer.append(sql); } diff --git a/src/test/java/org/apache/ibatis/builder/xml/dynamic/DynamicSqlSourceTest.java b/src/test/java/org/apache/ibatis/builder/xml/dynamic/DynamicSqlSourceTest.java index e66ddd15a2c..b9aa1575dee 100644 --- a/src/test/java/org/apache/ibatis/builder/xml/dynamic/DynamicSqlSourceTest.java +++ b/src/test/java/org/apache/ibatis/builder/xml/dynamic/DynamicSqlSourceTest.java @@ -214,7 +214,7 @@ void shouldTrimWHEREInsteadOfORForSecondCondition() throws Exception { @Test void shouldTrimWHEREInsteadOfANDForBothConditions() throws Exception { - final String expected = "SELECT * FROM BLOG WHERE ID = ? OR NAME = ?"; + final String expected = "SELECT * FROM BLOG WHERE ID = ? OR NAME = ?"; DynamicSqlSource source = createDynamicSqlSource(new TextSqlNode("SELECT * FROM BLOG"), new WhereSqlNode(new Configuration(), mixedContents(new IfSqlNode(mixedContents(new TextSqlNode(" and ID = ? ")), "true"), @@ -236,7 +236,7 @@ void shouldTrimNoWhereClause() throws Exception { @Test void shouldTrimSETInsteadOfCOMMAForBothConditions() throws Exception { - final String expected = "UPDATE BLOG SET ID = ?, NAME = ?"; + final String expected = "UPDATE BLOG SET ID = ?, NAME = ?"; DynamicSqlSource source = createDynamicSqlSource(new TextSqlNode("UPDATE BLOG"), new SetSqlNode(new Configuration(), mixedContents(new IfSqlNode(mixedContents(new TextSqlNode(" ID = ?, ")), "true"), From 3bf602f7fe7270a3ce77cdcb54c3900d2727f6a2 Mon Sep 17 00:00:00 2001 From: Iwao AVE! Date: Thu, 13 Mar 2025 05:41:21 +0900 Subject: [PATCH 2/2] Updated the test case to emphasize the inconsistency with `TrimSqlNode` --- .../ibatis/scripting/xmltags/XMLScriptBuilderTest.java | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/test/java/org/apache/ibatis/scripting/xmltags/XMLScriptBuilderTest.java b/src/test/java/org/apache/ibatis/scripting/xmltags/XMLScriptBuilderTest.java index d85bb234bba..c911de3a24f 100644 --- a/src/test/java/org/apache/ibatis/scripting/xmltags/XMLScriptBuilderTest.java +++ b/src/test/java/org/apache/ibatis/scripting/xmltags/XMLScriptBuilderTest.java @@ -30,16 +30,14 @@ void shouldWhereInsertWhitespace() throws Exception { String xml = """ """; SqlSource sqlSource = new XMLScriptBuilder(new Configuration(), new XPathParser(xml).evalNode("/script")) .parseScriptNode(); - assertThat(sqlSource.getBoundSql(1).getSql()) - .containsPattern("(?m)^\\s*select \\* from user\\s+WHERE\\s+id = 1\\s+and id > 0\\s*$"); + assertThat(sqlSource.getBoundSql(1).getSql()).containsPattern( + "(?m)^\\s*select \\* from user\\s+WHERE\\s+id = 1\\s+and id > 0\\s+and id = 1\\s+and id > 0\\s*$"); } }