-
Notifications
You must be signed in to change notification settings - Fork 13k
A new option (shrinkWhitespacesInSql) to remove extra whitespaces from SQL #1901
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
Changes from 2 commits
9ec145e
dc8b67c
95b8ec9
08635fd
7c3ff97
660cef1
18d1464
2c5a986
45fbebc
a3bef57
7e7e057
6df767d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -113,6 +113,7 @@ public class Configuration { | |
| protected boolean callSettersOnNulls; | ||
| protected boolean useActualParamName = true; | ||
| protected boolean returnInstanceForEmptyRow; | ||
| protected boolean minifySqlEnabled; | ||
|
||
|
|
||
| protected String logPrefix; | ||
| protected Class<? extends Log> logImpl; | ||
|
|
@@ -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; | ||
| } | ||
|
|
||
| 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"; | ||
harawata marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| @BeforeAll | ||
harawata marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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(); | ||
| } | ||
harawata marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| 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); | ||
|
||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.