From 586d43c40fab309b0b21ff5b7c1fddceb603f864 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Fri, 3 Aug 2018 13:20:08 +0200 Subject: [PATCH] SONAR-11085 Prettify logs of SQL requests --- .../org/sonar/db/profiling/SqlLogFormatter.java | 16 ++++++++++++++-- .../sonar/db/profiling/SqlLogFormatterTest.java | 9 +++++---- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/server/sonar-db-core/src/main/java/org/sonar/db/profiling/SqlLogFormatter.java b/server/sonar-db-core/src/main/java/org/sonar/db/profiling/SqlLogFormatter.java index fafc22ca46f..aaeac772ea2 100644 --- a/server/sonar-db-core/src/main/java/org/sonar/db/profiling/SqlLogFormatter.java +++ b/server/sonar-db-core/src/main/java/org/sonar/db/profiling/SqlLogFormatter.java @@ -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) { diff --git a/server/sonar-db-core/src/test/java/org/sonar/db/profiling/SqlLogFormatterTest.java b/server/sonar-db-core/src/test/java/org/sonar/db/profiling/SqlLogFormatterTest.java index c3f6f528c01..364693608f3 100644 --- a/server/sonar-db-core/src/test/java/org/sonar/db/profiling/SqlLogFormatterTest.java +++ b/server/sonar-db-core/src/test/java/org/sonar/db/profiling/SqlLogFormatterTest.java @@ -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 -- 2.39.5