From: Sébastien Lesaint Date: Mon, 4 Jul 2016 15:30:20 +0000 (+0200) Subject: SONAR-7780 add MeasureQuery.analysisUuid X-Git-Tag: 6.0-RC1~126 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=65e2080c62fccb8add6af7b3a7cd792562833604;p=sonarqube.git SONAR-7780 add MeasureQuery.analysisUuid required by Governance --- 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 2e27a2c9509..ebaaa1042e8 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 @@ -27,6 +27,7 @@ import java.util.List; import java.util.Optional; import org.sonar.db.Dao; import org.sonar.db.DbSession; +import org.sonar.db.version.Select; import static org.sonar.db.DatabaseUtils.executeLargeInputs; @@ -37,6 +38,19 @@ public class MeasureDao implements Dao { return Optional.ofNullable(Iterables.getOnlyElement(measures, null)); } + /** + * Selects the measures of either the last analysis (when {@link MeasureQuery#analysisUuid} is {@code null}) or of the + * specified analysis (given by {@link MeasureQuery#analysisUuid}) for the component UUIDs specified in + * {@link MeasureQuery#componentUuids}. + *

+ * In addition, this method returns measures which are not associated to any developer, unless one is specified in + * {@link MeasureQuery#personId}. + *

+ *

+ * Returned measure can optionally be filtered metric (either by specifying {@link MeasureQuery#metricIds} + * or {@link MeasureQuery#metricKeys}). + *

+ */ public List selectByQuery(DbSession dbSession, MeasureQuery query) { if (query.returnsEmpty()) { return Collections.emptyList(); @@ -50,6 +64,19 @@ public class MeasureDao implements Dao { }); } + public List selectByQuery(DbSession dbSession, MeasureQuery query, Select.RowHandler rowHandler) { + if (query.returnsEmpty()) { + return Collections.emptyList(); + } + if (query.getComponentUuids() == null) { + return mapper(dbSession).selectByQuery(query, rowHandler); + } + return executeLargeInputs(query.getComponentUuids(), componentUuids -> { + MeasureQuery pageQuery = MeasureQuery.copyWithSubsetOfComponentUuids(query, componentUuids); + return mapper(dbSession).selectByQuery(pageQuery, rowHandler); + }); + } + public List 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 734ab6e7d49..7a6aa3a34bd 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 @@ -21,11 +21,14 @@ package org.sonar.db.measure; import java.util.List; import org.apache.ibatis.annotations.Param; +import org.sonar.db.version.Select; public interface MeasureMapper { List selectByQuery(@Param("query") MeasureQuery query); + List selectByQuery(@Param("query") MeasureQuery query, Select.RowHandler rowHandler); + List selectPastMeasures(@Param("componentUuid") String componentUuid, @Param("analysisUuid") String analysisUuid, @Param("metricIds") List metricIds); void insert(MeasureDto measureDto); diff --git a/sonar-db/src/main/java/org/sonar/db/measure/MeasureQuery.java b/sonar-db/src/main/java/org/sonar/db/measure/MeasureQuery.java index 2d6604b8e68..80b49427fcc 100644 --- a/sonar-db/src/main/java/org/sonar/db/measure/MeasureQuery.java +++ b/sonar-db/src/main/java/org/sonar/db/measure/MeasureQuery.java @@ -21,56 +21,51 @@ package org.sonar.db.measure; import java.util.Collection; import java.util.List; +import java.util.Objects; import javax.annotation.CheckForNull; import javax.annotation.Nullable; import static com.google.common.base.Preconditions.checkState; import static java.util.Collections.singleton; import static java.util.Collections.singletonList; +import static java.util.Objects.requireNonNull; public class MeasureQuery { - - @CheckForNull private final String analysisUuid; - private final List componentUuids; - @CheckForNull private final Collection metricIds; - @CheckForNull private final Collection metricKeys; - @CheckForNull private final Long personId; private MeasureQuery(Builder builder) { - this(builder.componentUuids, builder.analysisUuid, builder.metricIds, builder.metricKeys, builder.personId); + this(builder.analysisUuid, builder.componentUuids, builder.metricIds, builder.metricKeys, builder.personId); } - private MeasureQuery(List componentUuids, - @Nullable String analysisUuid, + private MeasureQuery(@Nullable String analysisUuid, + List componentUuids, @Nullable Collection metricIds, @Nullable Collection metricKeys, @Nullable Long personId) { - checkState(componentUuids != null, "Component UUIDs must be set"); + requireNonNull(componentUuids, "Component UUIDs must be set"); checkState(metricIds == null || metricKeys == null, "Metric IDs and keys must not be set both"); - this.componentUuids = componentUuids; this.analysisUuid = analysisUuid; + this.componentUuids = componentUuids; this.metricIds = metricIds; this.metricKeys = metricKeys; this.personId = personId; } - public List getComponentUuids() { - return componentUuids; - } - - @CheckForNull public String getAnalysisUuid() { return analysisUuid; } + public List getComponentUuids() { + return componentUuids; + } + @CheckForNull public Collection getMetricIds() { return metricIds; @@ -92,17 +87,38 @@ public class MeasureQuery { || (metricKeys != null && metricKeys.isEmpty()); } + @Override + public boolean equals(@Nullable Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + MeasureQuery that = (MeasureQuery) o; + return Objects.equals(analysisUuid, that.analysisUuid) && + Objects.equals(componentUuids, that.componentUuids) && + Objects.equals(metricIds, that.metricIds) && + Objects.equals(metricKeys, that.metricKeys) && + Objects.equals(personId, that.personId); + } + + @Override + public int hashCode() { + return Objects.hash(analysisUuid, componentUuids, metricIds, metricKeys, personId); + } + public static Builder builder() { return new Builder(); } static MeasureQuery copyWithSubsetOfComponentUuids(MeasureQuery query, List componentUuids) { - return new MeasureQuery(componentUuids, query.analysisUuid, query.metricIds, query.metricKeys, query.personId); + return new MeasureQuery(query.analysisUuid, componentUuids, query.metricIds, query.metricKeys, query.personId); } public static final class Builder { - private List componentUuids; private String analysisUuid; + private List componentUuids; private Collection metricIds; private Collection metricKeys; private Long personId; @@ -111,6 +127,11 @@ public class MeasureQuery { // see MeasureQuery#builder() } + public Builder setAnalysisUuid(String analysisUuid) { + this.analysisUuid = analysisUuid; + return this; + } + public Builder setComponentUuids(List componentUuids) { this.componentUuids = componentUuids; return this; @@ -121,11 +142,6 @@ public class MeasureQuery { return this; } - public Builder setAnalysisUuid(String s) { - this.analysisUuid = s; - return this; - } - /** * All the measures are returned if parameter is {@code null}. */ diff --git a/sonar-db/src/main/resources/org/sonar/db/measure/MeasureMapper.xml b/sonar-db/src/main/resources/org/sonar/db/measure/MeasureMapper.xml index 7e7c0dca712..26a8278ede0 100644 --- a/sonar-db/src/main/resources/org/sonar/db/measure/MeasureMapper.xml +++ b/sonar-db/src/main/resources/org/sonar/db/measure/MeasureMapper.xml @@ -26,37 +26,43 @@