aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-db/src
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@sonarsource.com>2015-07-12 17:13:04 +0200
committerSimon Brandhof <simon.brandhof@sonarsource.com>2015-07-12 17:13:04 +0200
commit45bc4794dfec69b5495de09778ad3eb4081b279d (patch)
tree4198bf52d39f43bc0e1a6739bbd53d0ce8d46ad3 /sonar-db/src
parentaa5aedac205005477230665673a0f446d0cd3661 (diff)
downloadsonarqube-45bc4794dfec69b5495de09778ad3eb4081b279d.tar.gz
sonarqube-45bc4794dfec69b5495de09778ad3eb4081b279d.zip
Merge SqlUtil and DatabaseUtils
Diffstat (limited to 'sonar-db/src')
-rw-r--r--sonar-db/src/main/java/org/sonar/db/DatabaseUtils.java70
-rw-r--r--sonar-db/src/test/java/org/sonar/db/DatabaseUtilsTest.java17
2 files changed, 87 insertions, 0 deletions
diff --git a/sonar-db/src/main/java/org/sonar/db/DatabaseUtils.java b/sonar-db/src/main/java/org/sonar/db/DatabaseUtils.java
index 71eb3596d65..a42de7dcafd 100644
--- a/sonar-db/src/main/java/org/sonar/db/DatabaseUtils.java
+++ b/sonar-db/src/main/java/org/sonar/db/DatabaseUtils.java
@@ -25,11 +25,15 @@ import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
+import java.sql.Timestamp;
import java.util.Collection;
import java.util.Collections;
+import java.util.Date;
import java.util.List;
+import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import org.slf4j.LoggerFactory;
+import org.sonar.api.utils.log.Logger;
import static com.google.common.collect.Lists.newArrayList;
@@ -117,4 +121,70 @@ public class DatabaseUtils {
}
return sb.toString();
}
+
+ /**
+ * Logback does not log exceptions associated to {@link java.sql.SQLException#getNextException()}.
+ * See http://jira.qos.ch/browse/LOGBACK-775
+ */
+ public static void log(Logger logger, SQLException e) {
+ SQLException next = e.getNextException();
+ while (next != null) {
+ logger.error("SQL error: {}. Message: {}", next.getSQLState(), next.getMessage());
+ next = next.getNextException();
+ }
+ }
+
+ @CheckForNull
+ public static Long getLong(ResultSet rs, String columnName) throws SQLException {
+ long l = rs.getLong(columnName);
+ return rs.wasNull() ? null : l;
+ }
+
+ @CheckForNull
+ public static Double getDouble(ResultSet rs, String columnName) throws SQLException {
+ double d = rs.getDouble(columnName);
+ return rs.wasNull() ? null : d;
+ }
+
+ @CheckForNull
+ public static Integer getInt(ResultSet rs, String columnName) throws SQLException {
+ int i = rs.getInt(columnName);
+ return rs.wasNull() ? null : i;
+ }
+
+ @CheckForNull
+ public static String getString(ResultSet rs, String columnName) throws SQLException {
+ String s = rs.getString(columnName);
+ return rs.wasNull() ? null : s;
+ }
+
+ @CheckForNull
+ public static Long getLong(ResultSet rs, int columnIndex) throws SQLException {
+ long l = rs.getLong(columnIndex);
+ return rs.wasNull() ? null : l;
+ }
+
+ @CheckForNull
+ public static Double getDouble(ResultSet rs, int columnIndex) throws SQLException {
+ double d = rs.getDouble(columnIndex);
+ return rs.wasNull() ? null : d;
+ }
+
+ @CheckForNull
+ public static Integer getInt(ResultSet rs, int columnIndex) throws SQLException {
+ int i = rs.getInt(columnIndex);
+ return rs.wasNull() ? null : i;
+ }
+
+ @CheckForNull
+ public static String getString(ResultSet rs, int columnIndex) throws SQLException {
+ String s = rs.getString(columnIndex);
+ return rs.wasNull() ? null : s;
+ }
+
+ @CheckForNull
+ public static Date getDate(ResultSet rs, int columnIndex) throws SQLException {
+ Timestamp t = rs.getTimestamp(columnIndex);
+ return rs.wasNull() ? null : new Date(t.getTime());
+ }
}
diff --git a/sonar-db/src/test/java/org/sonar/db/DatabaseUtilsTest.java b/sonar-db/src/test/java/org/sonar/db/DatabaseUtilsTest.java
index 0eb5d438b91..fc850643379 100644
--- a/sonar-db/src/test/java/org/sonar/db/DatabaseUtilsTest.java
+++ b/sonar-db/src/test/java/org/sonar/db/DatabaseUtilsTest.java
@@ -32,6 +32,9 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.sonar.api.utils.System2;
+import org.sonar.api.utils.log.LogTester;
+import org.sonar.api.utils.log.LoggerLevel;
+import org.sonar.api.utils.log.Loggers;
import org.sonar.db.dialect.Oracle;
import org.sonar.test.DbTests;
@@ -48,6 +51,9 @@ public class DatabaseUtilsTest {
@Rule
public DbTester dbTester = DbTester.create(System2.INSTANCE);
+ @Rule
+ public LogTester logTester = new LogTester();
+
@Test
public void should_close_connection() throws Exception {
Connection connection = dbTester.openConnection();
@@ -201,4 +207,15 @@ public class DatabaseUtilsTest {
assertThat(outputs).isEmpty();
}
+
+ @Test
+ public void log_all_sql_exceptions() {
+ SQLException root = new SQLException("this is root", "123");
+ SQLException next = new SQLException("this is next", "456");
+ root.setNextException(next);
+
+ DatabaseUtils.log(Loggers.get(getClass()), root);
+
+ assertThat(logTester.logs(LoggerLevel.ERROR)).contains("SQL error: 456. Message: this is next");
+ }
}