Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.ibatis.mapping.ParameterMapping;
import org.apache.ibatis.mapping.SqlSource;
import org.apache.ibatis.parsing.GenericTokenParser;
import org.apache.ibatis.parsing.StringParser;
import org.apache.ibatis.parsing.TokenHandler;
import org.apache.ibatis.reflection.MetaClass;
import org.apache.ibatis.reflection.MetaObject;
Expand All @@ -42,7 +43,12 @@ public SqlSourceBuilder(Configuration configuration) {
public SqlSource parse(String originalSql, Class<?> parameterType, Map<String, Object> additionalParameters) {
ParameterMappingTokenHandler handler = new ParameterMappingTokenHandler(configuration, parameterType, additionalParameters);
GenericTokenParser parser = new GenericTokenParser("#{", "}", handler);
String sql = parser.parse(originalSql);
String sql;
if (configuration.isMinifySqlEnabled()) {
sql = parser.parse(StringParser.removeBreakingWhitespace(originalSql));
} else {
sql = parser.parse(originalSql);
}
return new StaticSqlSource(configuration, sql, handler.getParameterMappings());
}

Expand Down
10 changes: 2 additions & 8 deletions src/main/java/org/apache/ibatis/logging/jdbc/BaseJdbcLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.stream.Collectors;

import org.apache.ibatis.logging.Log;
import org.apache.ibatis.parsing.StringParser;
import org.apache.ibatis.reflection.ArrayUtil;

/**
Expand Down Expand Up @@ -121,13 +121,7 @@ protected void clearColumnInfo() {
}

protected String removeBreakingWhitespace(String original) {
StringTokenizer whitespaceStripper = new StringTokenizer(original);
StringBuilder builder = new StringBuilder();
while (whitespaceStripper.hasMoreTokens()) {
builder.append(whitespaceStripper.nextToken());
builder.append(" ");
}
return builder.toString();
return StringParser.removeBreakingWhitespace(original);
}

protected boolean isDebugEnabled() {
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/org/apache/ibatis/parsing/StringParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2009-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ibatis.parsing;

import java.util.StringTokenizer;

public class StringParser {

public static String removeBreakingWhitespace(String original) {
StringTokenizer whitespaceStripper = new StringTokenizer(original);
StringBuilder builder = new StringBuilder();
while (whitespaceStripper.hasMoreTokens()) {
builder.append(whitespaceStripper.nextToken());
builder.append(" ");
}
return builder.toString().trim();
}
}
9 changes: 9 additions & 0 deletions src/main/java/org/apache/ibatis/session/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public class Configuration {
protected boolean callSettersOnNulls;
protected boolean useActualParamName = true;
protected boolean returnInstanceForEmptyRow;
protected boolean minifySqlEnabled;
Copy link
Member

@harawata harawata May 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'minifySql' implies a smart operation that does not change the functionality of the original SQL, but this could change the functionality if there are literals in the SQL (e.g. #459 ).
How about 'shrinkWhitespacesInSql'?

In any case, the suffix 'Enabled' is unnecessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I can see. The words you have presented are easier to understand than mine. I fix it quickly, thank you!!


protected String logPrefix;
protected Class<? extends Log> logImpl;
Expand Down Expand Up @@ -266,6 +267,14 @@ public void setReturnInstanceForEmptyRow(boolean returnEmptyInstance) {
this.returnInstanceForEmptyRow = returnEmptyInstance;
}

public boolean isMinifySqlEnabled() {
return minifySqlEnabled;
}

public void setMinifySqlEnabled(boolean minifySqlEnabled) {
this.minifySqlEnabled = minifySqlEnabled;
}

public String getDatabaseId() {
return databaseId;
}
Expand Down
62 changes: 62 additions & 0 deletions src/test/java/org/apache/ibatis/builder/SqlSourceBuilderTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 2009-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ibatis.builder;

import java.io.Reader;
import org.apache.ibatis.builder.xml.XMLConfigBuilder;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.SqlSource;
import org.apache.ibatis.session.Configuration;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

public class SqlSourceBuilderTest {

private static Configuration configuration;
private static SqlSourceBuilder sqlSourceBuilder;
String sqlFromXml = "SELECT * \n FROM user\n WHERE user_id = 1";

@BeforeAll
static void setUp() throws Exception {
// create an SqlSessionFactory
try (Reader reader = Resources
.getResourceAsReader("org/apache/ibatis/submitted/empty_row/mybatis-config.xml")) {
XMLConfigBuilder parser = new XMLConfigBuilder(reader, null, null);
configuration = parser.parse();
}

sqlSourceBuilder = new SqlSourceBuilder(configuration);
}

@Test
void testMinifySqlEnabledIsFalse() {
SqlSource sqlSource = sqlSourceBuilder.parse(sqlFromXml, null, null);
BoundSql boundSql = sqlSource.getBoundSql(null);
String actual = boundSql.getSql();
Assertions.assertEquals(sqlFromXml, actual);
}

@Test
void testMinifySqlEnabledIsTrue() {
configuration.setMinifySqlEnabled(true);
SqlSource sqlSource = sqlSourceBuilder.parse(sqlFromXml, null, null);
BoundSql boundSql = sqlSource.getBoundSql(null);
String actual = boundSql.getSql();
Assertions.assertNotEquals(sqlFromXml, actual);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change this to assertEquals and specify the expected output explicitly.

}
}