diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2015-10-01 17:25:26 +0200 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2015-10-02 16:06:32 +0200 |
commit | a2a92b33b659e5a386cdc64d9d0c1bcbab7eae9b (patch) | |
tree | 0938978ecaa49dde5cc3576f38f1ebfaf3b396a8 | |
parent | 1494dac2272c73ca4d13c25e895716ec5c356897 (diff) | |
download | sonarqube-a2a92b33b659e5a386cdc64d9d0c1bcbab7eae9b.tar.gz sonarqube-a2a92b33b659e5a386cdc64d9d0c1bcbab7eae9b.zip |
Add selectComponentsByQualifiers
4 files changed, 51 insertions, 2 deletions
diff --git a/sonar-db/src/main/java/org/sonar/db/component/ComponentDao.java b/sonar-db/src/main/java/org/sonar/db/component/ComponentDao.java index 4f4a9cc49ca..4b8f62b0e06 100644 --- a/sonar-db/src/main/java/org/sonar/db/component/ComponentDao.java +++ b/sonar-db/src/main/java/org/sonar/db/component/ComponentDao.java @@ -25,8 +25,10 @@ import com.google.common.base.Optional; import com.google.common.collect.Lists; import java.util.Collection; import java.util.Collections; +import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Set; import javax.annotation.Nonnull; import javax.annotation.Nullable; import org.apache.ibatis.session.RowBounds; @@ -37,6 +39,7 @@ import org.sonar.db.DatabaseUtils; import org.sonar.db.DbSession; import org.sonar.db.RowNotFoundException; +import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.collect.Maps.newHashMapWithExpectedSize; import static org.sonar.db.DatabaseUtils.executeLargeInputs; @@ -251,6 +254,16 @@ public class ComponentDao implements Dao { return mapper(dbSession).selectByProjectUuid(projectUuid); } + /** + * Retrieve enabled components keys with given qualifiers + * + * Used by Views plugin + */ + public Set<ComponentDto> selectComponentsByQualifiers(DbSession dbSession, Set<String> qualifiers) { + checkArgument(!qualifiers.isEmpty(), "Qualifiers cannot be empty"); + return new HashSet<>(mapper(dbSession).selectComponentsByQualifiers(qualifiers)); + } + private static void addPartialQueryParameterIfNotNull(Map<String, Object> parameters, @Nullable String keyOrNameFilter) { if (keyOrNameFilter != null) { parameters.put("query", "%" + keyOrNameFilter.toUpperCase() + "%"); diff --git a/sonar-db/src/main/java/org/sonar/db/component/ComponentMapper.java b/sonar-db/src/main/java/org/sonar/db/component/ComponentMapper.java index 774c14a378d..42a59729461 100644 --- a/sonar-db/src/main/java/org/sonar/db/component/ComponentMapper.java +++ b/sonar-db/src/main/java/org/sonar/db/component/ComponentMapper.java @@ -57,6 +57,8 @@ public interface ComponentMapper { List<String> selectExistingUuids(@Param("uuids") Collection<String> uuids); + List<ComponentDto> selectComponentsByQualifiers(@Param("qualifiers") Collection<String> qualifiers); + List<ComponentDto> selectByQuery(ComponentQuery query, RowBounds rowBounds); int countByQuery(ComponentQuery query); @@ -131,4 +133,5 @@ public interface ComponentMapper { void update(ComponentDto componentDto); void delete(long componentId); + } diff --git a/sonar-db/src/main/resources/org/sonar/db/component/ComponentMapper.xml b/sonar-db/src/main/resources/org/sonar/db/component/ComponentMapper.xml index 9d295b93383..a47afccf978 100644 --- a/sonar-db/src/main/resources/org/sonar/db/component/ComponentMapper.xml +++ b/sonar-db/src/main/resources/org/sonar/db/component/ComponentMapper.xml @@ -229,6 +229,17 @@ </where> </select> + <select id="selectComponentsByQualifiers" resultType="Component"> + SELECT + <include refid="componentColumns"/> + FROM projects p + <where> + <foreach collection="qualifiers" open="(" close=")" item="qualifier" separator="OR "> + p.qualifier=#{qualifier} + </foreach> + </where> + </select> + <select id="selectComponents" resultType="Component"> select <include refid="componentColumns"/> diff --git a/sonar-db/src/test/java/org/sonar/db/component/ComponentDaoTest.java b/sonar-db/src/test/java/org/sonar/db/component/ComponentDaoTest.java index a282e4e25da..8c52572027b 100644 --- a/sonar-db/src/test/java/org/sonar/db/component/ComponentDaoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/component/ComponentDaoTest.java @@ -36,7 +36,9 @@ import org.sonar.db.RowNotFoundException; import org.sonar.test.DbTests; import static com.google.common.collect.Lists.newArrayList; +import static com.google.common.collect.Sets.newHashSet; import static java.util.Collections.singleton; +import static java.util.Collections.singletonList; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.guava.api.Assertions.assertThat; import static org.sonar.db.component.ComponentTesting.newDeveloper; @@ -178,7 +180,7 @@ public class ComponentDaoTest { public void get_by_keys() { db.prepareDbUnit(getClass(), "shared.xml"); - List<ComponentDto> results = underTest.selectByKeys(dbSession, Collections.singletonList("org.struts:struts-core:src/org/struts/RequestContext.java")); + List<ComponentDto> results = underTest.selectByKeys(dbSession, singletonList("org.struts:struts-core:src/org/struts/RequestContext.java")); assertThat(results).hasSize(1); ComponentDto result = results.get(0); @@ -192,7 +194,7 @@ public class ComponentDaoTest { assertThat(result.language()).isEqualTo("java"); assertThat(result.parentProjectId()).isEqualTo(2); - assertThat(underTest.selectByKeys(dbSession, Collections.singletonList("unknown"))).isEmpty(); + assertThat(underTest.selectByKeys(dbSession, singletonList("unknown"))).isEmpty(); } @Test @@ -304,6 +306,26 @@ public class ComponentDaoTest { } @Test + public void select_component_keys_by_qualifiers() { + db.prepareDbUnit(getClass(), "shared.xml"); + + assertThat(underTest.selectComponentsByQualifiers(dbSession, newHashSet("TRK"))).extracting("kee").containsOnly("org.struts:struts", "org.disabled.project"); + assertThat(underTest.selectComponentsByQualifiers(dbSession, newHashSet("BRC"))).extracting("kee").containsOnly("org.struts:struts-core"); + assertThat(underTest.selectComponentsByQualifiers(dbSession, newHashSet("DIR"))).extracting("kee").containsOnly("org.struts:struts-core:src/org/struts"); + assertThat(underTest.selectComponentsByQualifiers(dbSession, newHashSet("FIL"))).extracting("kee").containsOnly("org.struts:struts-core:src/org/struts/RequestContext.java"); + assertThat(underTest.selectComponentsByQualifiers(dbSession, newHashSet("unknown"))).isEmpty(); + } + + @Test + public void fail_with_IAE_select_component_keys_by_qualifiers_on_empty_qualifier() throws Exception { + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("Qualifiers cannot be empty"); + + db.prepareDbUnit(getClass(), "shared.xml"); + underTest.selectComponentsByQualifiers(dbSession, Collections.<String>emptySet()); + } + + @Test public void find_sub_projects_by_component_keys() { db.prepareDbUnit(getClass(), "multi-modules.xml"); |