aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-db
diff options
context:
space:
mode:
authorJulien Lancelot <julien.lancelot@sonarsource.com>2016-11-04 07:53:17 +0100
committerJulien Lancelot <julien.lancelot@sonarsource.com>2016-11-04 07:53:17 +0100
commitba16e00db95723d24f9d1d0271d472a0ce1e2c75 (patch)
tree84677d75a4c7dc76cca1317d10fe188842219183 /sonar-db
parent349dd4f5035eb4ca5208a21e62aa8451efcf5fb4 (diff)
downloadsonarqube-ba16e00db95723d24f9d1d0271d472a0ce1e2c75.tar.gz
sonarqube-ba16e00db95723d24f9d1d0271d472a0ce1e2c75.zip
SONAR-8325 Restore selectByQuery with handler
Diffstat (limited to 'sonar-db')
-rw-r--r--sonar-db/src/main/java/org/sonar/db/measure/MeasureDao.java27
-rw-r--r--sonar-db/src/main/java/org/sonar/db/measure/MeasureMapper.java6
-rw-r--r--sonar-db/src/test/java/org/sonar/db/measure/MeasureDaoTest.java114
3 files changed, 146 insertions, 1 deletions
diff --git a/sonar-db/src/main/java/org/sonar/db/measure/MeasureDao.java b/sonar-db/src/main/java/org/sonar/db/measure/MeasureDao.java
index 6d8071926d3..9deba33c76c 100644
--- a/sonar-db/src/main/java/org/sonar/db/measure/MeasureDao.java
+++ b/sonar-db/src/main/java/org/sonar/db/measure/MeasureDao.java
@@ -25,10 +25,12 @@ 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 {
@@ -76,6 +78,31 @@ public class MeasureDao implements Dao {
return mapper(dbSession).selectByQueryOnSingleComponent(query);
}
+ public void selectByQuery(DbSession dbSession, MeasureQuery query, ResultHandler resultHandler) {
+ if (query.returnsEmpty()) {
+ return;
+ }
+ if (query.isOnComponents()) {
+ executeLargeInputsWithoutOutput(
+ query.getComponentUuids(),
+ componentUuids -> {
+ MeasureQuery pageQuery = MeasureQuery.copyWithSubsetOfComponentUuids(query, componentUuids);
+ mapper(dbSession).selectByQueryOnComponents(pageQuery, resultHandler);
+ return null;
+ });
+ }
+ if (query.isOnProjects()) {
+ executeLargeInputsWithoutOutput(
+ query.getProjectUuids(),
+ projectUuids -> {
+ MeasureQuery pageQuery = MeasureQuery.copyWithSubsetOfProjectUuids(query, projectUuids);
+ mapper(dbSession).selectByQueryOnProjects(pageQuery, resultHandler);
+ return null;
+ });
+ }
+ mapper(dbSession).selectByQueryOnSingleComponent(query, resultHandler);
+ }
+
public List<PastMeasureDto> selectPastMeasures(DbSession dbSession,
String componentUuid,
String analysisUuid,
diff --git a/sonar-db/src/main/java/org/sonar/db/measure/MeasureMapper.java b/sonar-db/src/main/java/org/sonar/db/measure/MeasureMapper.java
index e945719372c..7b385815f20 100644
--- a/sonar-db/src/main/java/org/sonar/db/measure/MeasureMapper.java
+++ b/sonar-db/src/main/java/org/sonar/db/measure/MeasureMapper.java
@@ -32,7 +32,11 @@ public interface MeasureMapper {
List<MeasureDto> selectByQueryOnSingleComponent(@Param("query") MeasureQuery query);
- void selectByQuery(@Param("query") MeasureQuery query, ResultHandler resultHandler);
+ void selectByQueryOnProjects(@Param("query") MeasureQuery query, ResultHandler resultHandler);
+
+ void selectByQueryOnComponents(@Param("query") MeasureQuery query, ResultHandler resultHandler);
+
+ void selectByQueryOnSingleComponent(@Param("query") MeasureQuery query, ResultHandler resultHandler);
List<PastMeasureDto> selectPastMeasures(@Param("componentUuid") String componentUuid, @Param("analysisUuid") String analysisUuid, @Param("metricIds") List<Integer> metricIds);
diff --git a/sonar-db/src/test/java/org/sonar/db/measure/MeasureDaoTest.java b/sonar-db/src/test/java/org/sonar/db/measure/MeasureDaoTest.java
index db37a1afc32..af492d3666f 100644
--- a/sonar-db/src/test/java/org/sonar/db/measure/MeasureDaoTest.java
+++ b/sonar-db/src/test/java/org/sonar/db/measure/MeasureDaoTest.java
@@ -19,6 +19,7 @@
*/
package org.sonar.db.measure;
+import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
@@ -198,6 +199,103 @@ public class MeasureDaoTest {
}
@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();
db.components().insertComponent(newFileDto(project).setUuid("C1"));
@@ -266,10 +364,26 @@ 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);
}