]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8325 Refactore MeasureDao#selectByQuery to improve readybility 1351/head
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 3 Nov 2016 14:07:44 +0000 (15:07 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 3 Nov 2016 14:36:47 +0000 (15:36 +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/java/org/sonar/db/measure/MeasureQuery.java
sonar-db/src/main/resources/org/sonar/db/measure/MeasureMapper.xml
sonar-db/src/test/java/org/sonar/db/measure/MeasureDaoTest.java

index c298bb18ff1cf7bccbc3be499c848eae54e3d9f9..6d8071926d340a4b8dd91d5cdc250c2cc87a73e4 100644 (file)
@@ -25,12 +25,10 @@ import java.util.Collection;
 import java.util.Collections;
 import java.util.List;
 import java.util.Optional;
-import org.apache.ibatis.session.ResultHandler;
 import org.sonar.db.Dao;
 import org.sonar.db.DbSession;
 
 import static org.sonar.db.DatabaseUtils.executeLargeInputs;
-import static org.sonar.db.DatabaseUtils.executeLargeInputsWithoutOutput;
 
 public class MeasureDao implements Dao {
 
@@ -59,46 +57,23 @@ public class MeasureDao implements Dao {
     if (query.returnsEmpty()) {
       return Collections.emptyList();
     }
-    if (query.getComponentUuids() != null) {
+    if (query.isOnComponents()) {
       return executeLargeInputs(
         query.getComponentUuids(),
         componentUuids -> {
           MeasureQuery pageQuery = MeasureQuery.copyWithSubsetOfComponentUuids(query, componentUuids);
-          return mapper(dbSession).selectByQuery(pageQuery);
+          return mapper(dbSession).selectByQueryOnComponents(pageQuery);
         });
-    } else if (query.getProjectUuids() != null) {
-      return executeLargeInputs(
-        query.getProjectUuids(),
-        projectUuids -> {
-          MeasureQuery pageQuery = MeasureQuery.copyWithSubsetOfProjectUuids(query, projectUuids);
-          return mapper(dbSession).selectByQuery(pageQuery);
-        });
-    }
-    return mapper(dbSession).selectByQuery(query);
-  }
-
-  public void selectByQuery(DbSession dbSession, MeasureQuery query, ResultHandler resultHandler) {
-    if (query.returnsEmpty()) {
-      return;
     }
-    if (query.getComponentUuids() != null) {
-      executeLargeInputsWithoutOutput(
-        query.getComponentUuids(),
-        componentUuids -> {
-          MeasureQuery pageQuery = MeasureQuery.copyWithSubsetOfComponentUuids(query, componentUuids);
-          mapper(dbSession).selectByQuery(pageQuery, resultHandler);
-          return null;
-        });
-    } else if (query.getProjectUuids() != null) {
-      executeLargeInputsWithoutOutput(
+    if (query.isOnProjects()) {
+      return executeLargeInputs(
         query.getProjectUuids(),
         projectUuids -> {
           MeasureQuery pageQuery = MeasureQuery.copyWithSubsetOfProjectUuids(query, projectUuids);
-          mapper(dbSession).selectByQuery(pageQuery, resultHandler);
-          return null;
+          return mapper(dbSession).selectByQueryOnProjects(pageQuery);
         });
     }
-    mapper(dbSession).selectByQuery(query, resultHandler);
+    return mapper(dbSession).selectByQueryOnSingleComponent(query);
   }
 
   public List<PastMeasureDto> selectPastMeasures(DbSession dbSession,
index 68cfe0c20501b218af0d36859b40abc7d101038f..e945719372c8c1f8bd3361e9412fffed2d6e6ef4 100644 (file)
@@ -26,7 +26,11 @@ import org.apache.ibatis.session.ResultHandler;
 
 public interface MeasureMapper {
 
-  List<MeasureDto> selectByQuery(@Param("query") MeasureQuery query);
+  List<MeasureDto> selectByQueryOnProjects(@Param("query") MeasureQuery query);
+
+  List<MeasureDto> selectByQueryOnComponents(@Param("query") MeasureQuery query);
+
+  List<MeasureDto> selectByQueryOnSingleComponent(@Param("query") MeasureQuery query);
 
   void selectByQuery(@Param("query") MeasureQuery query, ResultHandler resultHandler);
 
index 00b724df17555645e06c2be66bd6e5c3205939ab..119d197e2d67a27bb98ddbb623ba3cc2c7f48f18 100644 (file)
@@ -136,7 +136,7 @@ public class MeasureQuery {
       return false;
     }
     MeasureQuery that = (MeasureQuery) o;
-    return Objects.equals(analysisUuid, that.analysisUuid) &&
+    return analysisUuid.equals(that.analysisUuid) &&
       Objects.equals(projectUuids, that.projectUuids) &&
       Objects.equals(componentUuids, that.componentUuids) &&
       Objects.equals(metricIds, that.metricIds) &&
index 0ed2c37a4ed0e73eaf3bb55d03d98dbe1fa24639..929ad1c44127834791e5fb0c6a4b59784507f00d 100644 (file)
     metric.name as metricKey
   </sql>
 
-  <select id="selectByQuery" parameterType="map" resultType="Measure">
-    select
-    <include refid="measureColumns"/>
-    from
-    project_measures pm
+  <select id="selectByQueryOnProjects" parameterType="map" resultType="Measure">
+    select <include refid="measureColumns"/> from project_measures pm
+    <include refid="selectByQueryCommonJoin"/>
+    where
+    <include refid="selectByQueryCommonFilters"/>
+    and analysis.component_uuid=pm.component_uuid
+    and analysis.component_uuid in
+    <foreach item="projectUuid" collection="query.getProjectUuids()" open="(" separator="," close=")">
+      #{projectUuid}
+    </foreach>
+  </select>
+
+  <select id="selectByQueryOnComponents" parameterType="map" resultType="Measure">
+    select <include refid="measureColumns"/> from project_measures pm
+    inner join projects p on p.project_uuid=analysis.component_uuid and p.uuid=pm.component_uuid
+      and p.project_uuid=#{query.projectUuid}
+      and p.uuid in
+      <foreach item="componentUuid" collection="query.getComponentUuids()" open="(" separator="," close=")">
+        #{componentUuid}
+      </foreach>
+    <include refid="selectByQueryCommonJoin"/>
+    where
+    <include refid="selectByQueryCommonFilters"/>
+  </select>
+
+  <select id="selectByQueryOnSingleComponent" parameterType="map" resultType="Measure">
+    select <include refid="measureColumns"/> from project_measures pm
+    inner join projects p on p.project_uuid=analysis.component_uuid
+      and p.uuid=pm.component_uuid
+      and p.uuid=#{query.componentUuid}
+    <include refid="selectByQueryCommonJoin"/>
+    where
+    <include refid="selectByQueryCommonFilters"/>
+  </select>
+
+  <sql id="selectByQueryCommonJoin">
     inner join snapshots analysis on analysis.uuid = pm.analysis_uuid
-    <if test="query.isOnComponents()">
-      inner join projects p on p.project_uuid=analysis.component_uuid and p.uuid=pm.component_uuid
-        and p.project_uuid=#{query.projectUuid}
-        and p.uuid in
-        <foreach item="componentUuid" collection="query.getComponentUuids()" open="(" separator="," close=")">
-          #{componentUuid}
-        </foreach>
-    </if>
-    <if test="query.isOnSingleComponent()">
-      inner join projects p on p.project_uuid=analysis.component_uuid and p.uuid=pm.component_uuid
-        and p.uuid=#{query.componentUuid}
-    </if>
     <if test="query.getMetricKeys() != null">
       inner join metrics m on m.id = pm.metric_id
     </if>
-    where
+  </sql>
+
+  <sql id="selectByQueryCommonFilters">
     <if test="query.getAnalysisUuid() == null">
       analysis.islast=${_true}
     </if>
     <if test="query.getAnalysisUuid() != null">
       analysis.uuid = #{query.analysisUuid}
     </if>
-    <if test="query.isOnProjects()">
-      and analysis.component_uuid=pm.component_uuid
-      and analysis.component_uuid in
-      <foreach item="projectUuid" collection="query.getProjectUuids()" open="(" separator="," close=")">
-        #{projectUuid}
-      </foreach>
-    </if>
     <if test="query.getMetricIds() != null">
       and pm.metric_id in
       <foreach item="metricId" collection="query.getMetricIds()" open="(" separator="," close=")">#{metricId}</foreach>
@@ -78,7 +92,7 @@
         and person_id is null
       </otherwise>
     </choose>
-  </select>
+  </sql>
 
   <select id="selectPastMeasures" parameterType="map" resultType="org.sonar.db.measure.PastMeasureDto">
     select pm.id as id, pm.metric_id as metricId, pm.person_id as personId, pm.value as value
index af492d3666fd1130f674c0c31c5ca44bebbcc5a4..db37a1afc32378f2babe585a86ca740acccd02ac 100644 (file)
@@ -19,7 +19,6 @@
  */
 package org.sonar.db.measure;
 
-import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 import java.util.Optional;
@@ -198,103 +197,6 @@ public class MeasureDaoTest {
     verifyMeasures(MeasureQuery.builder().setProjectUuids(asList(project1.uuid(), project2.uuid())).setMetricId(NCLOC_METRIC_ID).setAnalysisUuid(OTHER_ANALYSIS_UUID), "P1_M3");
   }
 
-  @Test
-  public void selectByQuery_with_handler() {
-    ComponentDto project1 = db.components().insertProject();
-    ComponentDto module = db.components().insertComponent(ComponentTesting.newModuleDto(project1));
-    db.components().insertComponent(newFileDto(module).setUuid("C1"));
-    db.components().insertComponent(newFileDto(module).setUuid("C2"));
-    insertAnalysis(LAST_ANALYSIS_UUID, project1.uuid(), true);
-    insertAnalysis(OTHER_ANALYSIS_UUID, project1.uuid(), false);
-
-    String project2LastAnalysisUuid = "P2_LAST_ANALYSIS";
-    ComponentDto project2 = db.components().insertProject();
-    insertAnalysis(project2LastAnalysisUuid, project2.uuid(), true);
-
-    // project 1
-    insertMeasure("P1_M1", LAST_ANALYSIS_UUID, project1.uuid(), NCLOC_METRIC_ID);
-    insertMeasure("P1_M1", LAST_ANALYSIS_UUID, project1.uuid(), COVERAGE_METRIC_ID);
-    insertMeasure("P1_M3", OTHER_ANALYSIS_UUID, project1.uuid(), NCLOC_METRIC_ID);
-    // project 2
-    insertMeasure("P2_M1", project2LastAnalysisUuid, project2.uuid(), NCLOC_METRIC_ID);
-    insertMeasure("P2_M2", project2LastAnalysisUuid, project2.uuid(), COVERAGE_METRIC_ID);
-    // component C1
-    insertMeasure("M1", OTHER_ANALYSIS_UUID, "C1", NCLOC_METRIC_ID);
-    insertMeasure("M2", LAST_ANALYSIS_UUID, "C1", NCLOC_METRIC_ID);
-    insertMeasure("M3", LAST_ANALYSIS_UUID, "C1", COVERAGE_METRIC_ID);
-    insertMeasureOnPerson("M4", LAST_ANALYSIS_UUID, "C1", NCLOC_METRIC_ID, A_PERSON_ID);
-    insertMeasureOnPerson("M5", OTHER_ANALYSIS_UUID, "C1", NCLOC_METRIC_ID, 123L);
-    // component C2
-    insertMeasure("M6", LAST_ANALYSIS_UUID, "C2", NCLOC_METRIC_ID);
-    db.commit();
-
-    verifyZeroMeasuresWithHandler(MeasureQuery.builder().setComponentUuids(project1.uuid(), emptyList()));
-    verifyZeroMeasuresWithHandler(MeasureQuery.builder().setComponentUuid("MISSING_COMPONENT"));
-    verifyZeroMeasuresWithHandler(MeasureQuery.builder().setProjectUuids(emptyList()));
-    verifyZeroMeasuresWithHandler(MeasureQuery.builder().setProjectUuids(singletonList("MISSING_COMPONENT")));
-
-    // all measures of component C1 of last analysis
-    verifyMeasuresWithHandler(MeasureQuery.builder().setComponentUuid("C1"), "M2", "M3");
-    // all measures of component C1 of non last analysis
-    verifyMeasuresWithHandler(MeasureQuery.builder().setComponentUuid("C1").setAnalysisUuid(OTHER_ANALYSIS_UUID), "M1");
-    // all measures of component C1 of last analysis by UUID
-    verifyMeasuresWithHandler(MeasureQuery.builder().setComponentUuid("C1").setAnalysisUuid(LAST_ANALYSIS_UUID), "M2", "M3");
-
-    // ncloc measure of component C1 of last analysis
-    verifyMeasuresWithHandler(MeasureQuery.builder().setComponentUuid("C1").setMetricId(NCLOC_METRIC_ID), "M2");
-    // ncloc measure of component C1 of non last analysis
-    verifyMeasuresWithHandler(MeasureQuery.builder().setComponentUuid("C1").setAnalysisUuid(OTHER_ANALYSIS_UUID).setMetricId(NCLOC_METRIC_ID), "M1");
-    // ncloc measure of component C1 of last analysis by UUID
-    verifyMeasuresWithHandler(MeasureQuery.builder().setComponentUuid("C1").setAnalysisUuid(LAST_ANALYSIS_UUID).setMetricId(NCLOC_METRIC_ID), "M2");
-
-    // multiple measures of component C1 of last analysis
-    verifyMeasuresWithHandler(MeasureQuery.builder().setComponentUuid("C1").setMetricIds(asList(NCLOC_METRIC_ID, COVERAGE_METRIC_ID)), "M2", "M3");
-    // multiple measures of component C1 of non last analysis
-    verifyMeasuresWithHandler(MeasureQuery.builder().setComponentUuid("C1").setAnalysisUuid(OTHER_ANALYSIS_UUID).setMetricIds(asList(NCLOC_METRIC_ID, COVERAGE_METRIC_ID)), "M1");
-    // multiple measures of component C1 of last analysis by UUID
-    verifyMeasuresWithHandler(MeasureQuery.builder().setComponentUuid("C1").setAnalysisUuid(LAST_ANALYSIS_UUID).setMetricIds(asList(NCLOC_METRIC_ID, COVERAGE_METRIC_ID)), "M2",
-      "M3");
-
-    // missing measure of component C1 of last analysis
-    verifyZeroMeasuresWithHandler(MeasureQuery.builder().setComponentUuid("C1").setMetricId(COMPLEXITY_METRIC_ID));
-    // missing measure of component C1 of non last analysis
-    verifyZeroMeasuresWithHandler(MeasureQuery.builder().setComponentUuid("C1").setAnalysisUuid(OTHER_ANALYSIS_UUID).setMetricId(COMPLEXITY_METRIC_ID));
-    // missing measure of component C1 of last analysis by UUID
-    verifyZeroMeasuresWithHandler(MeasureQuery.builder().setComponentUuid("C1").setAnalysisUuid(LAST_ANALYSIS_UUID).setMetricId(COMPLEXITY_METRIC_ID));
-
-    // ncloc measures of components C1, C2 and C3 (which does not exist) of last analysis
-    verifyMeasuresWithHandler(MeasureQuery.builder().setComponentUuids(project1.uuid(), asList("C1", "C2", "C3")), "M2", "M3", "M6");
-    // ncloc measures of components C1, C2 and C3 (which does not exist) of non last analysis
-    verifyMeasuresWithHandler(MeasureQuery.builder().setComponentUuids(project1.uuid(), asList("C1", "C2", "C3")).setAnalysisUuid(OTHER_ANALYSIS_UUID), "M1");
-    // ncloc measures of components C1, C2 and C3 (which does not exist) of last analysis by UUID
-    verifyMeasuresWithHandler(MeasureQuery.builder().setComponentUuids(project1.uuid(), asList("C1", "C2", "C3")).setAnalysisUuid(LAST_ANALYSIS_UUID), "M2", "M3", "M6");
-
-    // measures of missing developer of component C1 of last analysis
-    verifyZeroMeasuresWithHandler(MeasureQuery.builder().setComponentUuid("C1").setPersonId(123L));
-    // measures of missing developer of component C1 of non last analysis
-    verifyMeasuresWithHandler(MeasureQuery.builder().setComponentUuid("C1").setAnalysisUuid(OTHER_ANALYSIS_UUID).setPersonId(123L), "M5");
-    // measures of missing developer of component C1 of last analysis by UUID
-    verifyZeroMeasuresWithHandler(MeasureQuery.builder().setComponentUuid("C1").setAnalysisUuid(LAST_ANALYSIS_UUID).setPersonId(123L));
-
-    // developer measures of component C1 of last analysis
-    verifyMeasuresWithHandler(MeasureQuery.builder().setComponentUuid("C1").setPersonId(A_PERSON_ID), "M4");
-    // developer measures of component C1 of non last analysis
-    verifyZeroMeasuresWithHandler(MeasureQuery.builder().setComponentUuid("C1").setAnalysisUuid(OTHER_ANALYSIS_UUID).setPersonId(A_PERSON_ID));
-    // developer measures of component C1 of last analysis by UUID
-    verifyMeasuresWithHandler(MeasureQuery.builder().setComponentUuid("C1").setAnalysisUuid(LAST_ANALYSIS_UUID).setPersonId(A_PERSON_ID), "M4");
-
-    // projects measures of last analysis
-    verifyMeasuresWithHandler(MeasureQuery.builder().setProjectUuids(singletonList(project1.uuid())).setMetricId(NCLOC_METRIC_ID), "P1_M1");
-    verifyMeasuresWithHandler(MeasureQuery.builder().setProjectUuids(asList(project1.uuid(), project2.uuid())).setMetricIds(asList(NCLOC_METRIC_ID, COVERAGE_METRIC_ID)),
-      "P1_M1", "P2_M1", "P2_M2", "P2_M2");
-    verifyMeasuresWithHandler(MeasureQuery.builder().setProjectUuids(asList(project1.uuid(), project2.uuid(), "UNKNOWN")).setMetricId(NCLOC_METRIC_ID), "P1_M1", "P2_M1");
-
-    // projects measures of none last analysis
-    verifyMeasuresWithHandler(MeasureQuery.builder().setProjectUuids(singletonList(project1.uuid())).setMetricId(NCLOC_METRIC_ID).setAnalysisUuid(OTHER_ANALYSIS_UUID), "P1_M3");
-    verifyMeasuresWithHandler(MeasureQuery.builder().setProjectUuids(asList(project1.uuid(), project2.uuid())).setMetricId(NCLOC_METRIC_ID).setAnalysisUuid(OTHER_ANALYSIS_UUID),
-      "P1_M3");
-  }
-
   @Test
   public void selectSingle() {
     ComponentDto project = db.components().insertProject();
@@ -364,26 +266,10 @@ public class MeasureDaoTest {
     assertThat(measures).extracting(MeasureDto::getData).containsOnly(expectedIds);
   }
 
-  private void verifyMeasuresWithHandler(MeasureQuery.Builder query, String... expectedIds) {
-    List<MeasureDto> measures = getMeasuresWithHandler(query);
-    assertThat(measures).extracting(MeasureDto::getData).containsOnly(expectedIds);
-  }
-
   private void verifyZeroMeasures(MeasureQuery.Builder query) {
     assertThat(underTest.selectByQuery(db.getSession(), query.build())).isEmpty();
   }
 
-  private void verifyZeroMeasuresWithHandler(MeasureQuery.Builder query) {
-    List<MeasureDto> measures = getMeasuresWithHandler(query);
-    assertThat(measures).isEmpty();
-  }
-
-  private List<MeasureDto> getMeasuresWithHandler(MeasureQuery.Builder query) {
-    List<MeasureDto> measures = new ArrayList<>();
-    underTest.selectByQuery(db.getSession(), query.build(), resultContext -> measures.add((MeasureDto) resultContext.getResultObject()));
-    return measures;
-  }
-
   private void insertMeasure(String id, String analysisUuid, String componentUuid, int metricId) {
     insertMeasure(id, analysisUuid, componentUuid, null, metricId);
   }