aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-db-core
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@sonarsource.com>2018-07-26 16:10:57 +0200
committerSonarTech <sonartech@sonarsource.com>2018-08-02 20:21:33 +0200
commitc23d313b8e9e49aff7634a788655f508bb8f9a1e (patch)
treeef3a3cda6581bf17d170fccb7786f074c6df82f8 /server/sonar-db-core
parent3689d8d96db2956b8a3fcc1b29720314d4096970 (diff)
downloadsonarqube-c23d313b8e9e49aff7634a788655f508bb8f9a1e.tar.gz
sonarqube-c23d313b8e9e49aff7634a788655f508bb8f9a1e.zip
SONAR-11085 Prettify SQL TRACE logs
Diffstat (limited to 'server/sonar-db-core')
-rw-r--r--server/sonar-db-core/src/main/java/org/sonar/db/profiling/ProfilingPreparedStatementHandler.java4
-rw-r--r--server/sonar-db-core/src/main/java/org/sonar/db/profiling/ProfilingStatementHandler.java2
-rw-r--r--server/sonar-db-core/src/main/java/org/sonar/db/profiling/SqlLogFormatter.java24
-rw-r--r--server/sonar-db-core/src/test/java/org/sonar/db/profiling/SqlLogFormatterTest.java40
4 files changed, 42 insertions, 28 deletions
diff --git a/server/sonar-db-core/src/main/java/org/sonar/db/profiling/ProfilingPreparedStatementHandler.java b/server/sonar-db-core/src/main/java/org/sonar/db/profiling/ProfilingPreparedStatementHandler.java
index 51e3372f5b7..ad46941053e 100644
--- a/server/sonar-db-core/src/main/java/org/sonar/db/profiling/ProfilingPreparedStatementHandler.java
+++ b/server/sonar-db-core/src/main/java/org/sonar/db/profiling/ProfilingPreparedStatementHandler.java
@@ -44,9 +44,9 @@ class ProfilingPreparedStatementHandler implements InvocationHandler {
try {
result = InvocationUtils.invokeQuietly(statement, method, args);
} finally {
- profiler.addContext("sql", SqlLogFormatter.formatSql(sql));
+ profiler.addContext("sql", SqlLogFormatter.reformatSql(sql));
if (sqlParams.length > 0) {
- profiler.addContext("params", SqlLogFormatter.formatParams(sqlParams));
+ profiler.addContext("params", SqlLogFormatter.reformatParams(sqlParams));
}
profiler.stopTrace("");
}
diff --git a/server/sonar-db-core/src/main/java/org/sonar/db/profiling/ProfilingStatementHandler.java b/server/sonar-db-core/src/main/java/org/sonar/db/profiling/ProfilingStatementHandler.java
index 1e2d022a556..c8ac8688776 100644
--- a/server/sonar-db-core/src/main/java/org/sonar/db/profiling/ProfilingStatementHandler.java
+++ b/server/sonar-db-core/src/main/java/org/sonar/db/profiling/ProfilingStatementHandler.java
@@ -41,7 +41,7 @@ class ProfilingStatementHandler implements InvocationHandler {
result = InvocationUtils.invokeQuietly(statement, method, args);
} finally {
String sql = (String) args[0];
- profiler.addContext("sql", SqlLogFormatter.formatSql(sql));
+ profiler.addContext("sql", SqlLogFormatter.reformatSql(sql));
profiler.stopTrace("");
}
return result;
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 d1c6bd35771..5563b091030 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;
@@ -35,11 +34,24 @@ public class SqlLogFormatter {
// only statics
}
- public static String formatSql(String sql) {
- return StringUtils.replaceChars(sql, '\n', ' ');
+ public static String reformatSql(String sql) {
+ 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) {
+ public static String reformatParam(@Nullable Object param) {
if (param == null) {
return PARAM_NULL;
}
@@ -47,13 +59,13 @@ public class SqlLogFormatter {
return NEWLINE_PATTERN.matcher(abbreviated).replaceAll("\\\\n");
}
- public static String formatParams(Object[] params) {
+ public static String reformatParams(Object[] params) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < params.length; i++) {
if (i > 0) {
sb.append(", ");
}
- sb.append(formatParam(params[i]));
+ sb.append(reformatParam(params[i]));
}
return sb.toString();
}
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 ce3ac0925f4..461309ac49a 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
@@ -27,31 +27,33 @@ import static org.assertj.core.api.Assertions.assertThat;
public class SqlLogFormatterTest {
@Test
- public void formatSql() {
- assertThat(SqlLogFormatter.formatSql("select *")).isEqualTo("select *");
+ public void reformatSql() {
+ assertThat(SqlLogFormatter.reformatSql("")).isEqualTo("");
+ assertThat(SqlLogFormatter.reformatSql("select *")).isEqualTo("select *");
+ assertThat(SqlLogFormatter.reformatSql("select *\nfrom issues")).isEqualTo("select * from issues");
+ assertThat(SqlLogFormatter.reformatSql("select *\n from issues")).isEqualTo("select * from issues");
+ assertThat(SqlLogFormatter.reformatSql("select *\n from issues")).isEqualTo("select * from issues");
+ assertThat(SqlLogFormatter.reformatSql("select *\n from issues")).isEqualTo("select * from issues");
+ assertThat(SqlLogFormatter.reformatSql("select *\n\t\t from \tissues")).isEqualTo("select * from issues");
}
@Test
- public void formatSql_removes_newlines() {
- assertThat(SqlLogFormatter.formatSql("select *\nfrom issues")).isEqualTo("select * from issues");
+ public void reformatParam() {
+ assertThat(SqlLogFormatter.reformatParam(null)).isEqualTo("[null]");
+ assertThat(SqlLogFormatter.reformatParam("")).isEqualTo("");
+ assertThat(SqlLogFormatter.reformatParam("foo")).isEqualTo("foo");
+ assertThat(SqlLogFormatter.reformatParam("foo bar ")).isEqualTo("foo bar ");
}
@Test
- public void formatParam() {
- assertThat(SqlLogFormatter.formatParam(null)).isEqualTo("[null]");
- assertThat(SqlLogFormatter.formatParam("")).isEqualTo("");
- assertThat(SqlLogFormatter.formatParam("foo")).isEqualTo("foo");
+ public void reformatParam_escapes_newlines() {
+ assertThat(SqlLogFormatter.reformatParam("foo\n bar\nbaz")).isEqualTo("foo\\n bar\\nbaz");
}
@Test
- public void formatParam_escapes_newlines() {
- assertThat(SqlLogFormatter.formatParam("foo\nbar\nbaz")).isEqualTo("foo\\nbar\\nbaz");
- }
-
- @Test
- public void formatParam_truncates_if_too_long() {
+ public void reformatParam_truncates_if_too_long() {
String param = repeat("a", SqlLogFormatter.PARAM_MAX_WIDTH + 10);
- String formattedParam = SqlLogFormatter.formatParam(param);
+ String formattedParam = SqlLogFormatter.reformatParam(param);
assertThat(formattedParam)
.hasSize(SqlLogFormatter.PARAM_MAX_WIDTH)
.endsWith("...")
@@ -59,14 +61,14 @@ public class SqlLogFormatterTest {
}
@Test
- public void formatParams() {
- String formattedParams = SqlLogFormatter.formatParams(new Object[] {"foo", 42, null, true});
+ public void reformatParams() {
+ String formattedParams = SqlLogFormatter.reformatParams(new Object[] {"foo", 42, null, true});
assertThat(formattedParams).isEqualTo("foo, 42, [null], true");
}
@Test
- public void formatParams_returns_blank_if_zero_params() {
- String formattedParams = SqlLogFormatter.formatParams(new Object[0]);
+ public void reformatParams_returns_blank_if_zero_params() {
+ String formattedParams = SqlLogFormatter.reformatParams(new Object[0]);
assertThat(formattedParams).isEqualTo("");
}