]> source.dussan.org Git - sonarqube.git/commitdiff
Add selectComponentsByQualifiers 561/head
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 1 Oct 2015 15:25:26 +0000 (17:25 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Fri, 2 Oct 2015 14:06:32 +0000 (16:06 +0200)
sonar-db/src/main/java/org/sonar/db/component/ComponentDao.java
sonar-db/src/main/java/org/sonar/db/component/ComponentMapper.java
sonar-db/src/main/resources/org/sonar/db/component/ComponentMapper.xml
sonar-db/src/test/java/org/sonar/db/component/ComponentDaoTest.java

index 4f4a9cc49cac16f5b14a768830bbf2b885b474e3..4b8f62b0e065915dbd818116863c2f4951c7a99a 100644 (file)
@@ -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() + "%");
index 774c14a378daf6276c90924886bd8aa1bad91ef4..42a59729461380e93ac45ccd7018e72ad9a54934 100644 (file)
@@ -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);
+
 }
index 9d295b933833076ae4194fa3a137776cc58d657c..a47afccf978e5cc6703d7fbe57259335e1f57f46 100644 (file)
     </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"/>
index a282e4e25da92b20e3bea512ec0b935a69bf317e..8c52572027b3573c7cd616bdfbb9afcbfd256946 100644 (file)
@@ -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
@@ -303,6 +305,26 @@ public class ComponentDaoTest {
     assertThat(underTest.existsById(111L, dbSession)).isFalse();
   }
 
+  @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");