From 45bc4794dfec69b5495de09778ad3eb4081b279d Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Sun, 12 Jul 2015 17:13:04 +0200 Subject: [PATCH] Merge SqlUtil and DatabaseUtils --- .../sonar/server/db/migrations/SqlUtil.java | 102 ------------------ .../db/migrations/v36/ViolationConverter.java | 24 ++--- .../v36/ViolationMigrationStep.java | 4 +- .../ReplaceIssueFiltersProjectKeyByUuid.java | 4 +- .../issue/index/IssueResultSetIterator.java | 8 +- .../server/db/migrations/SqlUtilTest.java | 43 -------- .../main/java/org/sonar/db/DatabaseUtils.java | 70 ++++++++++++ .../java/org/sonar/db/DatabaseUtilsTest.java | 17 +++ 8 files changed, 107 insertions(+), 165 deletions(-) delete mode 100644 server/sonar-server/src/main/java/org/sonar/server/db/migrations/SqlUtil.java delete mode 100644 server/sonar-server/src/test/java/org/sonar/server/db/migrations/SqlUtilTest.java diff --git a/server/sonar-server/src/main/java/org/sonar/server/db/migrations/SqlUtil.java b/server/sonar-server/src/main/java/org/sonar/server/db/migrations/SqlUtil.java deleted file mode 100644 index f528218723f..00000000000 --- a/server/sonar-server/src/main/java/org/sonar/server/db/migrations/SqlUtil.java +++ /dev/null @@ -1,102 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.server.db.migrations; - -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Timestamp; -import java.util.Date; - -import javax.annotation.CheckForNull; - -import org.sonar.api.utils.log.Logger; - -public class SqlUtil { - - private SqlUtil() { - // only static methods - } - - /** - * 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/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v36/ViolationConverter.java b/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v36/ViolationConverter.java index 92416181edb..a8423b7efeb 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v36/ViolationConverter.java +++ b/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v36/ViolationConverter.java @@ -33,12 +33,12 @@ import org.apache.commons.dbutils.handlers.AbstractListHandler; import org.sonar.api.rule.Severity; import org.sonar.api.utils.internal.Uuids; import org.sonar.db.Database; -import org.sonar.server.db.migrations.SqlUtil; import com.google.common.base.Objects; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Lists; import com.google.common.collect.Maps; +import org.sonar.db.DatabaseUtils; class ViolationConverter implements Callable { @@ -272,7 +272,7 @@ class ViolationConverter implements Callable { Map map = Maps.newHashMap(); map.put(CREATED_AT, rs.getTimestamp(CREATED_AT)); map.put(UPDATED_AT, rs.getTimestamp(UPDATED_AT)); - map.put(USER_ID, SqlUtil.getLong(rs, USER_ID)); + map.put(USER_ID, DatabaseUtils.getLong(rs, USER_ID)); map.put(REVIEW_TEXT, rs.getString(REVIEW_TEXT)); return map; } @@ -284,26 +284,26 @@ class ViolationConverter implements Callable { @Override protected Map handleRow(ResultSet rs) throws SQLException { Map map = Maps.newHashMap(); - map.put(REVIEW_ID, SqlUtil.getLong(rs, REVIEW_ID)); - map.put(PROJECT_ID, SqlUtil.getLong(rs, PROJECT_ID)); - map.put(ROOT_PROJECT_ID, SqlUtil.getLong(rs, ROOT_PROJECT_ID)); - map.put(RULE_ID, SqlUtil.getLong(rs, RULE_ID)); - map.put(SEVERITY, Objects.firstNonNull(SEVERITIES.get(SqlUtil.getInt(rs, "failureLevel")), SEVERITY_MAJOR)); + map.put(REVIEW_ID, DatabaseUtils.getLong(rs, REVIEW_ID)); + map.put(PROJECT_ID, DatabaseUtils.getLong(rs, PROJECT_ID)); + map.put(ROOT_PROJECT_ID, DatabaseUtils.getLong(rs, ROOT_PROJECT_ID)); + map.put(RULE_ID, DatabaseUtils.getLong(rs, RULE_ID)); + map.put(SEVERITY, Objects.firstNonNull(SEVERITIES.get(DatabaseUtils.getInt(rs, "failureLevel")), SEVERITY_MAJOR)); map.put(MESSAGE, rs.getString(MESSAGE)); - map.put(LINE, SqlUtil.getInt(rs, LINE)); - map.put(COST, SqlUtil.getDouble(rs, COST)); + map.put(LINE, DatabaseUtils.getInt(rs, LINE)); + map.put(COST, DatabaseUtils.getDouble(rs, COST)); map.put(CHECKSUM, rs.getString(CHECKSUM)); map.put(CREATED_AT, rs.getTimestamp(CREATED_AT)); map.put(REVIEW_RESOLUTION, rs.getString(REVIEW_RESOLUTION)); map.put(REVIEW_SEVERITY, Objects.firstNonNull(rs.getString(REVIEW_SEVERITY), SEVERITY_MAJOR)); map.put(REVIEW_STATUS, rs.getString(REVIEW_STATUS)); - map.put(REVIEW_REPORTER_ID, SqlUtil.getLong(rs, REVIEW_REPORTER_ID)); - map.put(REVIEW_ASSIGNEE_ID, SqlUtil.getLong(rs, REVIEW_ASSIGNEE_ID)); + map.put(REVIEW_REPORTER_ID, DatabaseUtils.getLong(rs, REVIEW_REPORTER_ID)); + map.put(REVIEW_ASSIGNEE_ID, DatabaseUtils.getLong(rs, REVIEW_ASSIGNEE_ID)); map.put(REVIEW_DATA, rs.getString(REVIEW_DATA)); map.put(REVIEW_MANUAL_SEVERITY, rs.getBoolean(REVIEW_MANUAL_SEVERITY)); map.put(REVIEW_UPDATED_AT, rs.getTimestamp(REVIEW_UPDATED_AT)); map.put(REVIEW_MANUAL_VIOLATION, rs.getBoolean(REVIEW_MANUAL_VIOLATION)); - map.put(PLAN_ID, SqlUtil.getLong(rs, PLAN_ID)); + map.put(PLAN_ID, DatabaseUtils.getLong(rs, PLAN_ID)); return map; } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v36/ViolationMigrationStep.java b/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v36/ViolationMigrationStep.java index 1c6ee020642..2584e207311 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v36/ViolationMigrationStep.java +++ b/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v36/ViolationMigrationStep.java @@ -26,8 +26,8 @@ import org.sonar.api.utils.MessageException; import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; import org.sonar.db.Database; +import org.sonar.db.DatabaseUtils; import org.sonar.server.db.migrations.MigrationStep; -import org.sonar.server.db.migrations.SqlUtil; /** * Used in the Active Record Migration 401 @@ -58,7 +58,7 @@ public class ViolationMigrationStep implements MigrationStep { } } catch (SQLException e) { LOGGER.error(FAILURE_MESSAGE, e); - SqlUtil.log(LOGGER, e); + DatabaseUtils.log(LOGGER, e); throw MessageException.of(FAILURE_MESSAGE); } catch (Exception e) { diff --git a/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v50/ReplaceIssueFiltersProjectKeyByUuid.java b/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v50/ReplaceIssueFiltersProjectKeyByUuid.java index 0a1e2003d7b..b0854b468d3 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v50/ReplaceIssueFiltersProjectKeyByUuid.java +++ b/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v50/ReplaceIssueFiltersProjectKeyByUuid.java @@ -29,11 +29,11 @@ import javax.annotation.Nullable; import org.apache.commons.dbutils.DbUtils; import org.sonar.api.utils.System2; import org.sonar.db.Database; +import org.sonar.db.DatabaseUtils; import org.sonar.server.db.migrations.BaseDataChange; import org.sonar.server.db.migrations.MassUpdate; import org.sonar.server.db.migrations.Select; import org.sonar.server.db.migrations.SqlStatement; -import org.sonar.server.db.migrations.SqlUtil; /** * Used in the Active Record Migration 710 @@ -108,7 +108,7 @@ public class ReplaceIssueFiltersProjectKeyByUuid extends BaseDataChange { try { rs = pstmt.executeQuery(); if (rs.next()) { - String projectUuid = SqlUtil.getString(rs, "uuid"); + String projectUuid = DatabaseUtils.getString(rs, "uuid"); if (projectUuid != null) { newFields.append("projectUuids=").append(projectUuid); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/issue/index/IssueResultSetIterator.java b/server/sonar-server/src/main/java/org/sonar/server/issue/index/IssueResultSetIterator.java index 301150d331b..e60c4b65a32 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/issue/index/IssueResultSetIterator.java +++ b/server/sonar-server/src/main/java/org/sonar/server/issue/index/IssueResultSetIterator.java @@ -33,13 +33,13 @@ import javax.annotation.Nullable; import org.apache.commons.lang.StringUtils; import org.sonar.api.resources.Scopes; import org.sonar.api.rule.RuleKey; +import org.sonar.db.DatabaseUtils; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.ResultSetIterator; -import org.sonar.server.db.migrations.SqlUtil; import static org.sonar.api.utils.DateUtils.longToDate; -import static org.sonar.server.db.migrations.SqlUtil.getLong; +import static org.sonar.db.DatabaseUtils.getLong; /** * Scrolls over table ISSUES and reads documents to populate @@ -154,9 +154,9 @@ class IssueResultSetIterator extends ResultSetIterator { doc.setTechnicalUpdateDate(new Date(rs.getLong(3))); doc.setActionPlanKey(rs.getString(4)); doc.setAssignee(rs.getString(5)); - doc.setEffortToFix(SqlUtil.getDouble(rs, 6)); + doc.setEffortToFix(DatabaseUtils.getDouble(rs, 6)); doc.setAttributes(rs.getString(7)); - doc.setLine(SqlUtil.getInt(rs, 8)); + doc.setLine(DatabaseUtils.getInt(rs, 8)); doc.setMessage(rs.getString(9)); doc.setResolution(rs.getString(10)); doc.setSeverity(rs.getString(11)); diff --git a/server/sonar-server/src/test/java/org/sonar/server/db/migrations/SqlUtilTest.java b/server/sonar-server/src/test/java/org/sonar/server/db/migrations/SqlUtilTest.java deleted file mode 100644 index 2e18a4531b5..00000000000 --- a/server/sonar-server/src/test/java/org/sonar/server/db/migrations/SqlUtilTest.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.server.db.migrations; - -import org.junit.Test; -import org.sonar.api.utils.log.Logger; - -import java.sql.SQLException; - -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; - -public class SqlUtilTest { - - @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); - - Logger logger = mock(Logger.class); - SqlUtil.log(logger, root); - - verify(logger).error("SQL error: {}. Message: {}", "456", "this is next"); - } -} 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"); + } } -- 2.39.5