]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-11085 Prettify logs of SQL requests
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Fri, 3 Aug 2018 11:20:08 +0000 (13:20 +0200)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Sun, 5 Aug 2018 21:17:31 +0000 (23:17 +0200)
server/sonar-db-core/src/main/java/org/sonar/db/profiling/SqlLogFormatter.java
server/sonar-db-core/src/test/java/org/sonar/db/profiling/SqlLogFormatterTest.java

index fafc22ca46f404bd62f16ab9bc4ff3212786f254..aaeac772ea280508dd70299d00a1b58c8b9f1603 100644 (file)
@@ -21,7 +21,6 @@ package org.sonar.db.profiling;
 
 import java.util.regex.Pattern;
 import javax.annotation.Nullable;
-import org.apache.commons.lang.StringUtils;
 
 import static org.apache.commons.lang.StringUtils.abbreviate;
 
@@ -36,7 +35,20 @@ public class SqlLogFormatter {
   }
 
   public static String formatSql(String sql) {
-    return StringUtils.replaceChars(sql, '\n', ' ');
+    char[] chars = sql.toCharArray();
+    StringBuilder result = new StringBuilder(chars.length);
+
+    for (int i = 0; i < chars.length; i++) {
+      char c = chars[i];
+      if (c == '\n' || c == '\t') {
+        c = ' ';
+      }
+      if (Character.isWhitespace(c) && i > 0 && Character.isWhitespace(chars[i - 1])) {
+        continue;
+      }
+      result.append(c);
+    }
+    return result.toString();
   }
 
   public static String formatParam(@Nullable Object param) {
index c3f6f528c01a77b0b6e08b47d6a1d06eff15600d..364693608f3804a9cb63da3181b8e3df6a58ad3a 100644 (file)
@@ -28,12 +28,13 @@ public class SqlLogFormatterTest {
 
   @Test
   public void formatSql() {
+    assertThat(SqlLogFormatter.formatSql("")).isEqualTo("");
     assertThat(SqlLogFormatter.formatSql("select *")).isEqualTo("select *");
-  }
-
-  @Test
-  public void formatSql_removes_newlines() {
     assertThat(SqlLogFormatter.formatSql("select *\nfrom issues")).isEqualTo("select * from issues");
+    assertThat(SqlLogFormatter.formatSql("select *\n from issues")).isEqualTo("select * from issues");
+    assertThat(SqlLogFormatter.formatSql("select *\n   from issues")).isEqualTo("select * from issues");
+    assertThat(SqlLogFormatter.formatSql("select    *\n   from    issues")).isEqualTo("select * from issues");
+    assertThat(SqlLogFormatter.formatSql("select    *\n\t\t  from  \tissues")).isEqualTo("select * from issues");
   }
 
   @Test