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"), 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*$"); } }