Skip to content

Commit 63785f2

Browse files
authored
Merge pull request #1901 from elfhazard/feature/minifysql
A new option `shrinkWhitespacesInSql` that removes extra whitespaces from SQL
2 parents 89a7133 + 6df767d commit 63785f2

16 files changed

+177
-21
lines changed

src/main/java/org/apache/ibatis/builder/SqlSourceBuilder.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2019 the original author or authors.
2+
* Copyright 2009-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
1818
import java.util.ArrayList;
1919
import java.util.List;
2020
import java.util.Map;
21+
import java.util.StringTokenizer;
2122

2223
import org.apache.ibatis.mapping.ParameterMapping;
2324
import org.apache.ibatis.mapping.SqlSource;
@@ -42,10 +43,29 @@ public SqlSourceBuilder(Configuration configuration) {
4243
public SqlSource parse(String originalSql, Class<?> parameterType, Map<String, Object> additionalParameters) {
4344
ParameterMappingTokenHandler handler = new ParameterMappingTokenHandler(configuration, parameterType, additionalParameters);
4445
GenericTokenParser parser = new GenericTokenParser("#{", "}", handler);
45-
String sql = parser.parse(originalSql);
46+
String sql;
47+
if (configuration.isShrinkWhitespacesInSql()) {
48+
sql = parser.parse(removeExtraWhitespaces(originalSql));
49+
} else {
50+
sql = parser.parse(originalSql);
51+
}
4652
return new StaticSqlSource(configuration, sql, handler.getParameterMappings());
4753
}
4854

55+
public static String removeExtraWhitespaces(String original) {
56+
StringTokenizer tokenizer = new StringTokenizer(original);
57+
StringBuilder builder = new StringBuilder();
58+
boolean hasMoreTokens = tokenizer.hasMoreTokens();
59+
while (hasMoreTokens) {
60+
builder.append(tokenizer.nextToken());
61+
hasMoreTokens = tokenizer.hasMoreTokens();
62+
if (hasMoreTokens) {
63+
builder.append(' ');
64+
}
65+
}
66+
return builder.toString();
67+
}
68+
4969
private static class ParameterMappingTokenHandler extends BaseBuilder implements TokenHandler {
5070

5171
private List<ParameterMapping> parameterMappings = new ArrayList<>();

src/main/java/org/apache/ibatis/builder/xml/XMLConfigBuilder.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ private void settingsElement(Properties props) {
268268
configuration.setReturnInstanceForEmptyRow(booleanValueOf(props.getProperty("returnInstanceForEmptyRow"), false));
269269
configuration.setLogPrefix(props.getProperty("logPrefix"));
270270
configuration.setConfigurationFactory(resolveClass(props.getProperty("configurationFactory")));
271+
configuration.setShrinkWhitespacesInSql(booleanValueOf(props.getProperty("shrinkWhitespacesInSql"), false));
271272
}
272273

273274
private void environmentsElement(XNode context) throws Exception {

src/main/java/org/apache/ibatis/logging/jdbc/BaseJdbcLogger.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2019 the original author or authors.
2+
* Copyright 2009-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,9 +26,9 @@
2626
import java.util.List;
2727
import java.util.Map;
2828
import java.util.Set;
29-
import java.util.StringTokenizer;
3029
import java.util.stream.Collectors;
3130

31+
import org.apache.ibatis.builder.SqlSourceBuilder;
3232
import org.apache.ibatis.logging.Log;
3333
import org.apache.ibatis.reflection.ArrayUtil;
3434

@@ -120,14 +120,8 @@ protected void clearColumnInfo() {
120120
columnValues.clear();
121121
}
122122

123-
protected String removeBreakingWhitespace(String original) {
124-
StringTokenizer whitespaceStripper = new StringTokenizer(original);
125-
StringBuilder builder = new StringBuilder();
126-
while (whitespaceStripper.hasMoreTokens()) {
127-
builder.append(whitespaceStripper.nextToken());
128-
builder.append(" ");
129-
}
130-
return builder.toString();
123+
protected String removeExtraWhitespace(String original) {
124+
return SqlSourceBuilder.removeExtraWhitespaces(original);
131125
}
132126

133127
protected boolean isDebugEnabled() {

src/main/java/org/apache/ibatis/logging/jdbc/ConnectionLogger.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public Object invoke(Object proxy, Method method, Object[] params)
5050
}
5151
if ("prepareStatement".equals(method.getName()) || "prepareCall".equals(method.getName())) {
5252
if (isDebugEnabled()) {
53-
debug(" Preparing: " + removeBreakingWhitespace((String) params[0]), true);
53+
debug(" Preparing: " + removeExtraWhitespace((String) params[0]), true);
5454
}
5555
PreparedStatement stmt = (PreparedStatement) method.invoke(connection, params);
5656
stmt = PreparedStatementLogger.newInstance(stmt, statementLog, queryStack);

src/main/java/org/apache/ibatis/logging/jdbc/StatementLogger.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public Object invoke(Object proxy, Method method, Object[] params) throws Throwa
4848
}
4949
if (EXECUTE_METHODS.contains(method.getName())) {
5050
if (isDebugEnabled()) {
51-
debug(" Executing: " + removeBreakingWhitespace((String) params[0]), true);
51+
debug(" Executing: " + removeExtraWhitespace((String) params[0]), true);
5252
}
5353
if ("executeQuery".equals(method.getName())) {
5454
ResultSet rs = (ResultSet) method.invoke(statement, params);

src/main/java/org/apache/ibatis/session/Configuration.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ public class Configuration {
113113
protected boolean callSettersOnNulls;
114114
protected boolean useActualParamName = true;
115115
protected boolean returnInstanceForEmptyRow;
116+
protected boolean shrinkWhitespacesInSql;
116117

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

270+
public boolean isShrinkWhitespacesInSql() {
271+
return shrinkWhitespacesInSql;
272+
}
273+
274+
public void setShrinkWhitespacesInSql(boolean shrinkWhitespacesInSql) {
275+
this.shrinkWhitespacesInSql = shrinkWhitespacesInSql;
276+
}
277+
269278
public String getDatabaseId() {
270279
return databaseId;
271280
}

src/site/es/xdoc/configuration.xml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!--
33
4-
Copyright 2009-2019 the original author or authors.
4+
Copyright 2009-2020 the original author or authors.
55
66
Licensed under the Apache License, Version 2.0 (the "License");
77
you may not use this file except in compliance with the License.
@@ -547,6 +547,20 @@ SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader, environ
547547
Not set
548548
</td>
549549
</tr>
550+
<tr>
551+
<td>
552+
shrinkWhitespacesInSql
553+
</td>
554+
<td>
555+
Removes extra whitespace characters from the SQL. Note that this also affects literal strings in SQL. (Since 3.5.5)
556+
</td>
557+
<td>
558+
true | false
559+
</td>
560+
<td>
561+
false
562+
</td>
563+
</tr>
550564
</tbody>
551565
</table>
552566
<p>

src/site/ja/xdoc/configuration.xml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!--
33
4-
Copyright 2009-2019 the original author or authors.
4+
Copyright 2009-2020 the original author or authors.
55
66
Licensed under the Apache License, Version 2.0 (the "License");
77
you may not use this file except in compliance with the License.
@@ -572,6 +572,20 @@ SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader, environ
572572
未指定
573573
</td>
574574
</tr>
575+
<tr>
576+
<td>
577+
shrinkWhitespacesInSql
578+
</td>
579+
<td>
580+
SQL 内の余分な空白文字を削除します。リテラル文字列も対象となる点に注意してください。(導入されたバージョン: 3.5.5)
581+
</td>
582+
<td>
583+
true | false
584+
</td>
585+
<td>
586+
false
587+
</td>
588+
</tr>
575589
</tbody>
576590
</table>
577591
<p>

src/site/ko/xdoc/configuration.xml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!--
33
4-
Copyright 2009-2019 the original author or authors.
4+
Copyright 2009-2020 the original author or authors.
55
66
Licensed under the Apache License, Version 2.0 (the "License");
77
you may not use this file except in compliance with the License.
@@ -555,6 +555,20 @@ SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader, environ
555555
설정하지 않음
556556
</td>
557557
</tr>
558+
<tr>
559+
<td>
560+
shrinkWhitespacesInSql
561+
</td>
562+
<td>
563+
SQL에서 여분의 whitespace 문자들을 삭제한다. 이는 SQL의 리터럴 문자열에도 영향을 미친다. (Since 3.5.5)
564+
</td>
565+
<td>
566+
true | false
567+
</td>
568+
<td>
569+
false
570+
</td>
571+
</tr>
558572
</tbody>
559573
</table>
560574
<p>위 설정을 모두 사용한 setting 엘리먼트의 예제이다:</p>

src/site/xdoc/configuration.xml

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
<?xml version="1.0" encoding="UTF-8"?>
1+
<?xml version="1.0" encoding="UTF-8"?>
22
<!--
33
4-
Copyright 2009-2019 the original author or authors.
4+
Copyright 2009-2020 the original author or authors.
55
66
Licensed under the Apache License, Version 2.0 (the "License");
77
you may not use this file except in compliance with the License.
@@ -634,6 +634,20 @@ SqlSessionFactory factory =
634634
Not set
635635
</td>
636636
</tr>
637+
<tr>
638+
<td>
639+
shrinkWhitespacesInSql
640+
</td>
641+
<td>
642+
Removes extra whitespace characters from the SQL. Note that this also affects literal strings in SQL. (Since 3.5.5)
643+
</td>
644+
<td>
645+
true | false
646+
</td>
647+
<td>
648+
false
649+
</td>
650+
</tr>
637651
</tbody>
638652
</table>
639653
<p>

0 commit comments

Comments
 (0)