]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8221 Fix issue on MySQL when indexing project measures without analysis 1323/head
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 20 Oct 2016 13:12:48 +0000 (15:12 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 20 Oct 2016 14:01:03 +0000 (16:01 +0200)
server/sonar-server/src/main/java/org/sonar/server/component/es/ProjectMeasuresDoc.java
server/sonar-server/src/main/java/org/sonar/server/component/es/ProjectMeasuresResultSetIterator.java
server/sonar-server/src/test/java/org/sonar/server/component/ws/SearchProjectsActionTest.java

index 0b14e976cb8be6709a18e9c047282153752d1574..67f0590af774bbecabc053d9bf677f175fb0b870 100644 (file)
@@ -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;
   }
index 2e9c1c1daa54ca20546152bc00d2fcb4b19225c0..adffa8f9429a07f3d814bf468b03d5907dcb5716 100644 (file)
@@ -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<ProjectM
   @Override
   protected ProjectMeasuresDoc read(ResultSet rs) throws SQLException {
     String projectUuid = rs.getString(1);
-    Measures measures = selectMeasures(projectUuid, rs.getString(4));
+    Measures measures = selectMeasures(projectUuid, getString(rs, 4));
     ProjectMeasuresDoc doc = new ProjectMeasuresDoc()
       .setId(projectUuid)
       .setKey(rs.getString(2))
       .setName(rs.getString(3))
-      .setQualityGate(measures.qualityGateStatus)
-      .setMeasuresFromMap(measures.numericMeasures);
+      .setQualityGate(measures.getQualityGateStatus())
+      .setMeasuresFromMap(measures.getNumericMeasures());
     long analysisDate = rs.getLong(5);
     doc.setAnalysedAt(rs.wasNull() ? null : new Date(analysisDate));
     return doc;
   }
 
-  private Measures selectMeasures(String projectUuid, @Nullable String analysisUuid) {
+  private Measures selectMeasures(String projectUuid, Optional<String> 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<ProjectM
     }
   }
 
+  private static Optional<String> 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<String, Object> numericMeasures = new HashMap<>();
     private String qualityGateStatus;
@@ -204,10 +220,19 @@ public class ProjectMeasuresResultSetIterator extends ResultSetIterator<ProjectM
       return this;
     }
 
-    Measures setQualityGateStatus(String qualityGateStatus) {
+    public Map<String, Object> getNumericMeasures() {
+      return numericMeasures;
+    }
+
+    Measures setQualityGateStatus(@Nullable String qualityGateStatus) {
       this.qualityGateStatus = qualityGateStatus;
       return this;
     }
+
+    @CheckForNull
+    public String getQualityGateStatus() {
+      return qualityGateStatus;
+    }
   }
 
 }
index 3e56eea30be17a1f605aba673d2b03b8b881725c..f98d2da03250dc52df194197c173a6116c534b3f 100644 (file)
@@ -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());