]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7027 add MeasureDao#selectBySnapshotAndMetrics 652/head
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Tue, 24 Nov 2015 13:16:57 +0000 (14:16 +0100)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Wed, 25 Nov 2015 14:33:07 +0000 (15:33 +0100)
sonar-db/src/main/java/org/sonar/db/measure/MeasureDao.java
sonar-db/src/main/java/org/sonar/db/measure/MeasureMapper.java
sonar-db/src/main/resources/org/sonar/db/measure/MeasureMapper.xml
sonar-db/src/test/java/org/sonar/db/measure/MeasureDaoTest.java

index 13b27d687680eb461c8f5ca31579426342cb35a3..23458f0b8828332d36e49976bb555ba1000b70e2 100644 (file)
@@ -93,6 +93,19 @@ public class MeasureDao implements Dao {
     });
   }
 
+  /**
+   * Used by plugin Developer Cockpit
+   */
+  public List<MeasureDto> selectBySnapshotAndMetrics(final DbSession dbSession, final long snapshotId, Collection<Integer> metricIds) {
+    return DatabaseUtils.executeLargeInputs(metricIds, new Function<List<Integer>, List<MeasureDto>>() {
+      @Override
+      @Nonnull
+      public List<MeasureDto> apply(@Nonnull List<Integer> input) {
+        return mapper(dbSession).selectBySnapshotAndMetrics(snapshotId, input);
+      }
+    });
+  }
+
   public void insert(DbSession session, MeasureDto measureDto) {
     mapper(session).insert(measureDto);
   }
index fc6debc0a62d896eec57403d7d213eb6608fc036..a5b096001f8b8177ba5610dece2589a524257e26 100644 (file)
@@ -34,6 +34,8 @@ public interface MeasureMapper {
 
   List<MeasureDto> selectByDeveloperForSnapshotAndMetrics(@Param("developerId") long developerId, @Param("snapshotId") long snapshotId, @Param("metricIds") List<Integer> metricIds);
 
+  List<MeasureDto> selectBySnapshotAndMetrics(@Param("snapshotId") long snapshotId, @Param("metricIds") List<Integer> input);
+
   @CheckForNull
   MeasureDto selectByComponentAndMetric(@Param("componentKey") String componentKey, @Param("metricKey") String metricKey);
 
index 7874e1e2d2071f6634f9fd98ae9f935531a0fbf7..f2d223963745b72b42dede949400ea475f406da9 100644 (file)
     </where>
   </select>
 
+  <select id="selectBySnapshotAndMetrics" parameterType="map" resultType="Measure">
+    SELECT
+    <include refid="measureColumns"/>
+    FROM project_measures pm
+    <where>
+      pm.snapshot_id = #{snapshotId}
+      AND pm.rule_id is NULL
+      AND pm.characteristic_id is NULL
+      AND pm.person_id is NULL
+      AND
+      <foreach item="metricId" index="index" collection="metricIds" open="(" separator=" or " close=")">
+        pm.metric_id=#{metricId}
+      </foreach>
+    </where>
+  </select>
+
   <select id="countByComponentAndMetric" parameterType="map" resultType="long">
     SELECT count(pm.id)
     FROM project_measures pm
index 60538b15b0ec438336edafae6c729b8db3a16cd6..87136d9ca755eb520993fd4ce4b60aaae8025c8f 100644 (file)
@@ -358,6 +358,39 @@ public class MeasureDaoTest {
     assertThat(measureDtos).extracting("id").containsOnly(32L);
   }
 
+  @Test
+  public void selectBySnapshotAndMetrics_returns_empty_when_single_metric_id_does_not_exist() {
+    db.prepareDbUnit(getClass(), "with_some_measures_for_developer.xml");
+
+    List<MeasureDto> measureDtos = underTest.selectBySnapshotAndMetrics(db.getSession(),
+        SNAPSHOT_ID,
+        ImmutableList.of(666));
+
+    assertThat(measureDtos).isEmpty();
+  }
+
+  @Test
+  public void selectBySnapshotAndMetrics_returns_only_measures_not_for_developer() {
+    db.prepareDbUnit(getClass(), "with_some_measures_for_developer.xml");
+
+    List<MeasureDto> measureDtos = underTest.selectBySnapshotAndMetrics(db.getSession(),
+        SNAPSHOT_ID,
+        ImmutableList.of(AUTHORS_BY_LINE_METRIC_ID, COVERAGE_LINE_HITS_DATA_METRIC_ID, NCLOC_METRIC_ID));
+
+    assertThat(measureDtos).extracting("id").containsOnly(20L, 21L, 22L);
+  }
+
+  @Test
+  public void selectBySnapshotAndMetrics_returns_only_measures_not_for_developer_and_with_specified_metric_id() {
+    db.prepareDbUnit(getClass(), "with_some_measures_for_developer.xml");
+
+    List<MeasureDto> measureDtos = underTest.selectBySnapshotAndMetrics(db.getSession(),
+        SNAPSHOT_ID,
+        ImmutableList.of(NCLOC_METRIC_ID));
+
+    assertThat(measureDtos).extracting("id").containsOnly(22L);
+  }
+
   @Test
   public void selectByDeveloperForSnapshotAndMetrics_returns_empty_when_single_metric_id_does_not_exist() {
     db.prepareDbUnit(getClass(), "with_some_measures_for_developer.xml");