diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2017-01-23 18:15:54 +0100 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@sonarsource.com> | 2017-01-23 18:50:26 +0100 |
commit | ea4563b2077d57220ea77190d537f7cb3c8adf17 (patch) | |
tree | 3e56daf7d51ff8e9572565f7698dbd16d31b63ac /sonar-db | |
parent | 2556c84850d9856cfdd85738dc092fe4cabfaea9 (diff) | |
download | sonarqube-ea4563b2077d57220ea77190d537f7cb3c8adf17.tar.gz sonarqube-ea4563b2077d57220ea77190d537f7cb3c8adf17.zip |
Support all qualifiers in ComponentDao#xxxProvisioned()
Diffstat (limited to 'sonar-db')
4 files changed, 76 insertions, 34 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 ad049fc90df..e4d2527af9b 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 @@ -28,20 +28,24 @@ import java.util.HashSet; import java.util.List; import java.util.Locale; import java.util.Map; -import java.util.Objects; import java.util.Set; +import javax.annotation.CheckForNull; import javax.annotation.Nullable; import org.apache.ibatis.session.ResultHandler; import org.apache.ibatis.session.RowBounds; import org.sonar.api.resources.Qualifiers; import org.sonar.api.resources.Scopes; import org.sonar.db.Dao; +import org.sonar.db.DatabaseUtils; import org.sonar.db.DbSession; import org.sonar.db.RowNotFoundException; +import org.sonar.db.WildcardPosition; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.collect.Maps.newHashMapWithExpectedSize; import static java.util.Collections.emptyList; +import static java.util.Objects.requireNonNull; +import static org.apache.commons.lang.StringUtils.isBlank; import static org.sonar.db.DatabaseUtils.executeLargeInputs; import static org.sonar.db.DatabaseUtils.executeLargeUpdates; @@ -205,20 +209,37 @@ public class ComponentDao implements Dao { return mapper(dbSession).selectAllRootsByOrganization(organizationUuid); } - public List<ComponentDto> selectProvisionedProjects(DbSession session, int offset, int limit, @Nullable String query) { - Map<String, Object> parameters = newHashMapWithExpectedSize(2); - addProjectQualifier(parameters); - addPartialQueryParameterIfNotNull(parameters, query); - - return mapper(session).selectProvisionedProjects(parameters, new RowBounds(offset, limit)); + /** + * Select a page of provisioned (root) components. Results are ordered by ascending name. + * + * @param dbSession + * @param textQuery optional text query to match component name or key + * @param qualifiers filter on qualifiers. Must not be null nor empty + * @param rowBounds pagination + */ + public List<ComponentDto> selectProvisioned(DbSession dbSession, @Nullable String textQuery, Set<String> qualifiers, RowBounds rowBounds) { + checkArgument(!qualifiers.isEmpty(), "qualifiers must not be empty"); + return mapper(dbSession).selectProvisioned(buildUpperLikeSql(textQuery), qualifiers, rowBounds); } - public int countProvisionedProjects(DbSession session, @Nullable String query) { - Map<String, Object> parameters = newHashMapWithExpectedSize(2); - addProjectQualifier(parameters); - addPartialQueryParameterIfNotNull(parameters, query); + /** + * Count number of provisioned (root) components. + * + * @param dbSession + * @param textQuery optional text query to match component name or key + * @param qualifiers filter on qualifiers. Must not be null nor empty + */ + public int countProvisioned(DbSession dbSession, @Nullable String textQuery, Set<String> qualifiers) { + checkArgument(!qualifiers.isEmpty(), "qualifiers must not be empty"); + return mapper(dbSession).countProvisioned(buildUpperLikeSql(textQuery), qualifiers); + } - return mapper(session).countProvisionedProjects(parameters); + @CheckForNull + private static String buildUpperLikeSql(@Nullable String textQuery) { + if (isBlank(textQuery)) { + return null; + } + return DatabaseUtils.buildLikeValue(textQuery.toUpperCase(Locale.ENGLISH), WildcardPosition.BEFORE_AND_AFTER); } public List<ComponentDto> selectGhostProjects(DbSession session, int offset, int limit, @Nullable String query) { @@ -244,7 +265,7 @@ public class ComponentDao implements Dao { * @param handler the action to be applied to every result */ public void selectForIndexing(DbSession session, @Nullable String projectUuid, ResultHandler handler) { - Objects.requireNonNull(handler); + requireNonNull(handler); mapper(session).selectForIndexing(projectUuid, handler); } 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 9ad0394deb4..101ac0f26c6 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 @@ -22,6 +22,7 @@ package org.sonar.db.component; import java.util.Collection; import java.util.List; import java.util.Map; +import java.util.Set; import javax.annotation.CheckForNull; import javax.annotation.Nullable; import org.apache.ibatis.annotations.Param; @@ -108,9 +109,9 @@ public interface ComponentMapper { */ List<String> selectProjectsFromView(@Param("viewUuidLikeQuery") String viewUuidLikeQuery, @Param("projectViewUuid") String projectViewUuid); - List<ComponentDto> selectProvisionedProjects(Map<String, Object> parameters, RowBounds rowBounds); + List<ComponentDto> selectProvisioned(@Nullable @Param("keyOrNameLike") String keyOrNameLike, @Param("qualifiers") Set<String> qualifiers, RowBounds rowBounds); - int countProvisionedProjects(Map<String, Object> parameters); + int countProvisioned(@Nullable @Param("keyOrNameLike") String keyOrNameLike, @Param("qualifiers") Set<String> qualifiers); List<ComponentDto> selectGhostProjects(Map<String, Object> parameters, RowBounds rowBounds); 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 6fba079f9d5..2f4a42d2835 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 @@ -368,30 +368,30 @@ </where> </select> - <select id="selectProvisionedProjects" parameterType="map" resultType="Component"> + <select id="selectProvisioned" parameterType="map" resultType="Component"> select <include refid="componentColumns"/> from projects p - <include refid="provisionClauses"/> + <include refid="provisionedClauses"/> </select> - <select id="countProvisionedProjects" parameterType="map" resultType="int"> + <select id="countProvisioned" parameterType="map" resultType="int"> select count(p.id) from projects p - <include refid="provisionClauses"/> + <include refid="provisionedClauses"/> </select> - <sql id="provisionClauses"> + <sql id="provisionedClauses"> left join snapshots s on s.component_uuid=p.uuid where s.id is null - and p.enabled=${_true} - and p.qualifier=#{qualifier} + and p.enabled = ${_true} + and p.qualifier in <foreach collection="qualifiers" open="(" close=")" item="qualifier" separator=",">#{qualifier}</foreach> and p.copy_component_uuid is null - <if test="query!=null"> + <if test="keyOrNameLike != null"> and ( - UPPER(p.name) like #{query} - or UPPER(p.kee) like #{query} + upper(p.name) like #{keyOrNameLike} ESCAPE '/' + or upper(p.kee) like #{keyOrNameLike} ESCAPE '/' ) </if> </sql> 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 8d2b2d83c66..9bbaaf20aa7 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 @@ -24,9 +24,11 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Map; +import java.util.Set; import javax.annotation.Nullable; import org.apache.ibatis.session.ResultContext; import org.apache.ibatis.session.ResultHandler; +import org.apache.ibatis.session.RowBounds; import org.assertj.core.api.ListAssert; import org.junit.Rule; import org.junit.Test; @@ -529,23 +531,41 @@ public class ComponentDaoTest { } @Test - public void select_provisioned_projects() { + public void select_provisioned() { db.prepareDbUnit(getClass(), "select_provisioned_projects.xml"); - List<ComponentDto> result = underTest.selectProvisionedProjects(dbSession, 0, 10, null); - ComponentDto project = result.get(0); + Set<String> projectQualifiers = newHashSet(Qualifiers.PROJECT); + assertProvisionedKeys(null, projectQualifiers, 0, 10).containsExactly("org.provisioned.project"); - assertThat(result).hasSize(1); - assertThat(project.getKey()).isEqualTo("org.provisioned.project"); + // pagination + assertProvisionedKeys(null, projectQualifiers, 2, 10).isEmpty(); + + // filter on qualifiers + assertProvisionedKeys(null, newHashSet("XXX"), 0, 10).isEmpty(); + assertProvisionedKeys(null, newHashSet(Qualifiers.PROJECT, "XXX"), 0, 10).containsExactly("org.provisioned.project"); + + // match key + assertProvisionedKeys("org.provisioned.project", projectQualifiers, 0, 10).containsExactly("org.provisioned.project"); + assertProvisionedKeys("ORG.provisioned.PROJECT", projectQualifiers, 0, 10).containsExactly("org.provisioned.project"); + assertProvisionedKeys("missing", projectQualifiers, 0, 10).isEmpty(); + assertProvisionedKeys("to be escaped '\"\\%", projectQualifiers, 0, 10).isEmpty(); + + // match name + assertProvisionedKeys("ned proj", projectQualifiers, 0, 10).containsExactly("org.provisioned.project"); + } + + private ListAssert<String> assertProvisionedKeys(@Nullable String textQuery, Set<String> qualifiers, int offset, int limit) { + List<ComponentDto> result = underTest.selectProvisioned(dbSession, textQuery, qualifiers, new RowBounds(offset, limit)); + return assertThat(result).extracting(ComponentDto::getKey); } @Test - public void count_provisioned_projects() { + public void count_provisioned() { db.prepareDbUnit(getClass(), "select_provisioned_projects.xml"); - int numberOfProjects = underTest.countProvisionedProjects(dbSession, null); - - assertThat(numberOfProjects).isEqualTo(1); + assertThat(underTest.countProvisioned(dbSession, null, newHashSet(Qualifiers.PROJECT))).isEqualTo(1); + assertThat(underTest.countProvisioned(dbSession, null, newHashSet(Qualifiers.VIEW))).isEqualTo(0); + assertThat(underTest.countProvisioned(dbSession, null, newHashSet(Qualifiers.VIEW, Qualifiers.PROJECT))).isEqualTo(1); } @Test |