From e49f905d8931cf5ee8c5586c8db780bf5f68c531 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Thu, 20 Oct 2016 15:12:48 +0200 Subject: [PATCH] SONAR-8221 Fix issue on MySQL when indexing project measures without analysis --- .../component/es/ProjectMeasuresDoc.java | 3 +- .../es/ProjectMeasuresResultSetIterator.java | 37 ++++++++++++++++--- .../ws/SearchProjectsActionTest.java | 5 ++- 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/component/es/ProjectMeasuresDoc.java b/server/sonar-server/src/main/java/org/sonar/server/component/es/ProjectMeasuresDoc.java index 0b14e976cb8..67f0590af77 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/component/es/ProjectMeasuresDoc.java +++ b/server/sonar-server/src/main/java/org/sonar/server/component/es/ProjectMeasuresDoc.java @@ -104,11 +104,12 @@ public class ProjectMeasuresDoc extends BaseDoc { return this; } + @CheckForNull public String getQualityGate() { return getField(ProjectMeasuresIndexDefinition.FIELD_QUALITY_GATE); } - public ProjectMeasuresDoc setQualityGate(String s) { + public ProjectMeasuresDoc setQualityGate(@Nullable String s) { setField(ProjectMeasuresIndexDefinition.FIELD_QUALITY_GATE, s); return this; } diff --git a/server/sonar-server/src/main/java/org/sonar/server/component/es/ProjectMeasuresResultSetIterator.java b/server/sonar-server/src/main/java/org/sonar/server/component/es/ProjectMeasuresResultSetIterator.java index 2e9c1c1daa5..adffa8f9429 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/component/es/ProjectMeasuresResultSetIterator.java +++ b/server/sonar-server/src/main/java/org/sonar/server/component/es/ProjectMeasuresResultSetIterator.java @@ -28,6 +28,7 @@ import java.util.Date; import java.util.HashMap; import java.util.Map; import java.util.Optional; +import javax.annotation.CheckForNull; import javax.annotation.Nullable; import org.apache.commons.lang.StringUtils; import org.sonar.api.resources.Qualifiers; @@ -129,21 +130,24 @@ public class ProjectMeasuresResultSetIterator extends ResultSetIterator analysisUuid) { Measures measures = new Measures(); - try (PreparedStatement stmt = createMeasuresStatement(projectUuid, analysisUuid); + if (!analysisUuid.isPresent()) { + return measures; + } + try (PreparedStatement stmt = createMeasuresStatement(projectUuid, analysisUuid.get()); ResultSet rs = stmt.executeQuery()) { while (rs.next()) { readMeasure(rs, measures); @@ -195,6 +199,18 @@ public class ProjectMeasuresResultSetIterator extends ResultSetIterator getString(ResultSet rs, int index) { + try { + String value = rs.getString(index); + if (!rs.wasNull()) { + return Optional.of(value); + } + return Optional.empty(); + } catch (SQLException e) { + throw new IllegalStateException("Fail to get string value", e); + } + } + private static class Measures { private Map numericMeasures = new HashMap<>(); private String qualityGateStatus; @@ -204,10 +220,19 @@ public class ProjectMeasuresResultSetIterator extends ResultSetIterator getNumericMeasures() { + return numericMeasures; + } + + Measures setQualityGateStatus(@Nullable String qualityGateStatus) { this.qualityGateStatus = qualityGateStatus; return this; } + + @CheckForNull + public String getQualityGateStatus() { + return qualityGateStatus; + } } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/component/ws/SearchProjectsActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/component/ws/SearchProjectsActionTest.java index 3e56eea30be..f98d2da0325 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/component/ws/SearchProjectsActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/component/ws/SearchProjectsActionTest.java @@ -238,7 +238,10 @@ public class SearchProjectsActionTest { httpRequest.setParam(Param.PAGE, String.valueOf(wsRequest.getPage())); httpRequest.setParam(Param.PAGE_SIZE, String.valueOf(wsRequest.getPageSize())); - httpRequest.setParam(PARAM_FILTER, wsRequest.getFilter()); + String filter = wsRequest.getFilter(); + if (filter != null) { + httpRequest.setParam(PARAM_FILTER, filter); + } try { return SearchProjectsWsResponse.parseFrom(httpRequest.execute().getInputStream()); -- 2.39.5