Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A new option (shrinkWhitespacesInSql) to remove extra whitespaces from SQL #1901

Merged
merged 12 commits into from May 18, 2020
Merged
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.isShrinkWhitespacesInSql()) {
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
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
@@ -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();
harawata marked this conversation as resolved.
Show resolved Hide resolved
}
}
9 changes: 9 additions & 0 deletions src/main/java/org/apache/ibatis/session/Configuration.java
Expand Up @@ -113,6 +113,7 @@ public class Configuration {
protected boolean callSettersOnNulls;
protected boolean useActualParamName = true;
protected boolean returnInstanceForEmptyRow;
protected boolean shrinkWhitespacesInSql;

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 isShrinkWhitespacesInSql() {
return shrinkWhitespacesInSql;
}

public void setShrinkWhitespacesInSql(boolean shrinkWhitespacesInSql) {
this.shrinkWhitespacesInSql = shrinkWhitespacesInSql;
}

public String getDatabaseId() {
return databaseId;
}
Expand Down
57 changes: 57 additions & 0 deletions src/test/java/org/apache/ibatis/builder/SqlSourceBuilderTest.java
@@ -0,0 +1,57 @@
/*
* 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 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.BeforeEach;
import org.junit.jupiter.api.Test;

public class SqlSourceBuilderTest {

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

@BeforeEach
void setUp() {
configuration = new Configuration();

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() {
String expected = "\\t\\n\\n SELECT * \\n FROM user\\n \\t WHERE user_id = 1\\n\\t";
harawata marked this conversation as resolved.
Show resolved Hide resolved

configuration.setShrinkWhitespacesInSql(true);
SqlSource sqlSource = sqlSourceBuilder.parse(sqlFromXml, null, null);
BoundSql boundSql = sqlSource.getBoundSql(null);
String actual = boundSql.getSql();

Assertions.assertEquals(expected, actual);
}
}