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

Modifying whitespace removal for logging #460

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions pom.xml
Expand Up @@ -101,6 +101,10 @@
<name>Tomáš Neuberg</name>
<email>neuberg@m-atelier.cz</email>
</contributor>
<contributor>
<name>Gabor Szabo</name>
<email>szgabsz91@gmail.com</email>
</contributor>
</contributors>

<scm>
Expand Down Expand Up @@ -333,6 +337,9 @@
<exclude>**/*.java</exclude>
</excludes>
</testResource>
<testResource>
<directory>${project.basedir}/src/test/resources</directory>
</testResource>
</testResources>
</build>

Expand Down
16 changes: 5 additions & 11 deletions src/main/java/org/apache/ibatis/logging/jdbc/BaseJdbcLogger.java
Expand Up @@ -15,16 +15,15 @@
*/
package org.apache.ibatis.logging.jdbc;

import org.apache.ibatis.logging.Log;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;

import org.apache.ibatis.logging.Log;

/**
* Base class for proxies to do logging
Expand Down Expand Up @@ -120,13 +119,8 @@ 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();
String regex = "\\s+(?=((\\\\[\\\\\"']|[^\\\\\"'])*[\"'](\\\\[\\\\\"']|[^\\\\\"'])*[\"'])*(\\\\[\\\\\"']|[^\\\\\"'])*$)";
return original.replaceAll(regex, " ").trim();
}

protected boolean isDebugEnabled() {
Expand Down
@@ -0,0 +1,50 @@
/**
* Copyright 2009-2015 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.logging.jdbc;

import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;
import java.util.Scanner;

import static org.junit.Assert.assertEquals;

public class BaseJdbcLoggerTest {

private BaseJdbcLogger baseJdbcLogger;

@Before
public void setUp() {
// We need only a default implementation for calling getResourceContent in the test
this.baseJdbcLogger = new BaseJdbcLogger(null, 0) {
};
}

@Test
public void shouldLeaveWhitespaceInsideQuotesAndApostrophesIntact() {
String originalQuery = getResourceContent("sample-query.txt");
String queryWithoutWhitespace = baseJdbcLogger.removeBreakingWhitespace(originalQuery);
assertEquals("SELECT field1 FROM Table WHERE field2 = ' ' AND field3 = \" \" AND field4=\" \\\"in quotes\\\" \" AND field5 = ' \\'in apostrophes\\' ';", queryWithoutWhitespace);
}

private static String getResourceContent(String name) {
InputStream inputStream = BaseJdbcLoggerTest.class.getResourceAsStream(name);
Scanner scanner = new Scanner(inputStream).useDelimiter("\\A");
return scanner.next();
}

}
@@ -0,0 +1,7 @@
SELECT
field1
FROM
Table
WHERE
field2 = ' ' AND field3 = " " AND
field4=" \"in quotes\" " AND field5 = ' \'in apostrophes\' ';