]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9181 WS api/projects/provisioned relies on ComponentDao.selectByQuery(...)
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Mon, 4 Sep 2017 16:10:10 +0000 (18:10 +0200)
committerStas Vilchik <stas.vilchik@sonarsource.com>
Mon, 11 Sep 2017 09:28:29 +0000 (11:28 +0200)
server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentMapper.java
server/sonar-db-dao/src/main/resources/org/sonar/db/component/ComponentMapper.xml
server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentDaoTest.java
server/sonar-server/src/main/java/org/sonar/server/project/ws/ProvisionedAction.java

index 8121fee14472eec87c31eb891e62e92f0e710fd4..0f1d4fb8de027a6f59e58c2f2e2c1aba39210dfe 100644 (file)
@@ -240,31 +240,6 @@ public class ComponentDao implements Dao {
     return mapper(dbSession).selectAllRootsByOrganization(organizationUuid);
   }
 
-  /**
-   * Select a page of provisioned (root) components. Results are ordered by ascending name.
-   * @param dbSession
-   * @param organizationUuid uuid of the organization
-   * @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, String organizationUuid, @Nullable String textQuery, Set<String> qualifiers, RowBounds rowBounds) {
-    checkArgument(!qualifiers.isEmpty(), "qualifiers must not be empty");
-    return mapper(dbSession).selectProvisioned(organizationUuid, buildUpperLikeSql(textQuery), qualifiers, rowBounds);
-  }
-
-  /**
-   * Count number of provisioned (root) components.
-   * @param dbSession
-   * @param organizationUuid uuid of the organization
-   * @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, String organizationUuid, @Nullable String textQuery, Set<String> qualifiers) {
-    checkArgument(!qualifiers.isEmpty(), "qualifiers must not be empty");
-    return mapper(dbSession).countProvisioned(organizationUuid, buildUpperLikeSql(textQuery), qualifiers);
-  }
-
   public List<ComponentDto> selectGhostProjects(DbSession session, String organizationUuid, @Nullable String query, int offset, int limit) {
     return mapper(session).selectGhostProjects(organizationUuid, buildUpperLikeSql(query), new RowBounds(offset, limit));
   }
index c476910b2ed44d9c5d068149200acb17c79e2b26..53ebb044c5a48852ed3bd9d35ba550a6abbd7c0d 100644 (file)
@@ -22,7 +22,6 @@ package org.sonar.db.component;
 
 import java.util.Collection;
 import java.util.List;
-import java.util.Set;
 import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
 import org.apache.ibatis.annotations.Param;
@@ -117,11 +116,6 @@ public interface ComponentMapper {
    */
   List<String> selectProjectsFromView(@Param("viewUuidLikeQuery") String viewUuidLikeQuery, @Param("projectViewUuid") String projectViewUuid);
 
-  List<ComponentDto> selectProvisioned(@Param("organizationUuid") String organizationUuid, @Nullable @Param("keyOrNameLike") String keyOrNameLike,
-    @Param("qualifiers") Set<String> qualifiers, RowBounds rowBounds);
-
-  int countProvisioned(@Param("organizationUuid") String organizationUuid, @Nullable @Param("keyOrNameLike") String keyOrNameLike, @Param("qualifiers") Set<String> qualifiers);
-
   List<ComponentDto> selectGhostProjects(@Param("organizationUuid") String organizationUuid, @Nullable @Param("query") String query, RowBounds rowBounds);
 
   long countGhostProjects(@Param("organizationUuid") String organizationUuid, @Nullable @Param("query") String query);
index 0e189b06595ee6b872037e7c71483a5f286c6b13..29c5724e0ad6a8488142e7a4df156dc2b97e201c 100644 (file)
     </where>
   </select>
 
-  <select id="selectProvisioned" parameterType="map" resultType="Component">
-    select
-    <include refid="componentColumns"/>
-    from projects p
-    <include refid="provisionedClauses"/>
-  </select>
-
-  <select id="countProvisioned" parameterType="map" resultType="int">
-    select count(p.id)
-    from projects p
-    <include refid="provisionedClauses"/>
-  </select>
-
-  <sql id="provisionedClauses">
-    left join snapshots s on s.component_uuid=p.uuid
-    where
-    s.id is null
-    and p.organization_uuid = #{organizationUuid,jdbcType=VARCHAR}
-    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="keyOrNameLike != null">
-      and (
-      upper(p.name) like #{keyOrNameLike} ESCAPE '/'
-      or upper(p.kee) like #{keyOrNameLike} ESCAPE '/'
-      )
-    </if>
-  </sql>
-
   <select id="selectGhostProjects" parameterType="map" resultType="Component">
     select distinct
       <include refid="componentColumns"/>
index 490dcc5210dcbabfc088c7ed8aade6fd3231cf41..dd3c15cbd352425ce3bca16a29de9245d0c712a4 100644 (file)
@@ -28,7 +28,6 @@ import java.util.Map;
 import java.util.Set;
 import java.util.function.Supplier;
 import javax.annotation.Nullable;
-import org.apache.ibatis.session.RowBounds;
 import org.assertj.core.api.ListAssert;
 import org.junit.Rule;
 import org.junit.Test;
@@ -630,56 +629,34 @@ public class ComponentDaoTest {
 
     Set<String> projectQualifiers = newHashSet(Qualifiers.PROJECT);
     Supplier<ComponentQuery.Builder> query = () -> ComponentQuery.builder().setQualifiers(Qualifiers.PROJECT).setOnProvisionedOnly(true);
-    assertThat(underTest.selectProvisioned(dbSession, organization.getUuid(), null, projectQualifiers, new RowBounds(0, 10)))
-      .extracting(ComponentDto::uuid)
-      .containsOnly(provisionedProject.uuid());
     assertThat(underTest.selectByQuery(dbSession, organization.getUuid(), query.get().build(), 0, 10))
       .extracting(ComponentDto::uuid)
       .containsOnly(provisionedProject.uuid());
 
     // pagination
-    assertThat(underTest.selectProvisioned(dbSession, organization.getUuid(), null, projectQualifiers, new RowBounds(2, 10))).isEmpty();
     assertThat(underTest.selectByQuery(dbSession, organization.getUuid(), query.get().build(), 2, 10)).isEmpty();
 
     // filter on qualifiers
-    assertThat(underTest.selectProvisioned(dbSession, organization.getUuid(), null, newHashSet("XXX"), new RowBounds(0, 10))).isEmpty();
     assertThat(underTest.selectByQuery(dbSession, organization.getUuid(), query.get().setQualifiers("XXX").build(), 0, 10)).isEmpty();
-    assertThat(underTest.selectProvisioned(dbSession, organization.getUuid(), null, newHashSet(Qualifiers.PROJECT, "XXX"), new RowBounds(0, 10)))
-      .extracting(ComponentDto::uuid)
-      .containsOnly(provisionedProject.uuid());
     assertThat(underTest.selectByQuery(dbSession, organization.getUuid(), query.get().setQualifiers(Qualifiers.PROJECT, "XXX").build(), 0, 10))
       .extracting(ComponentDto::uuid)
       .containsOnly(provisionedProject.uuid());
-    assertThat(underTest.selectProvisioned(dbSession, organization.getUuid(), null, newHashSet(Qualifiers.PROJECT, Qualifiers.VIEW), new RowBounds(0, 10)))
-      .extracting(ComponentDto::uuid)
-      .containsOnly(provisionedProject.uuid(), provisionedView.uuid());
     assertThat(underTest.selectByQuery(dbSession, organization.getUuid(), query.get().setQualifiers(Qualifiers.PROJECT, Qualifiers.VIEW).build(), 0, 10))
       .extracting(ComponentDto::uuid)
       .containsOnly(provisionedProject.uuid(), provisionedView.uuid());
 
     // match key
-    assertThat(underTest.selectProvisioned(dbSession, organization.getUuid(), provisionedProject.getDbKey(), projectQualifiers, new RowBounds(0, 10)))
-      .extracting(ComponentDto::uuid)
-      .containsExactly(provisionedProject.uuid());
     assertThat(underTest.selectByQuery(dbSession, organization.getUuid(), query.get().setNameOrKeyQuery(provisionedProject.getDbKey()).build(), 0, 10))
       .extracting(ComponentDto::uuid)
       .containsExactly(provisionedProject.uuid());
-    assertThat(underTest.selectProvisioned(dbSession, organization.getUuid(), "pROvisiONed.proJEcT", projectQualifiers, new RowBounds(0, 10)))
-      .extracting(ComponentDto::uuid)
-      .containsExactly(provisionedProject.uuid());
     assertThat(underTest.selectByQuery(dbSession, organization.getUuid(), query.get().setNameOrKeyQuery("pROvisiONed.proJEcT").setPartialMatchOnKey(true).build(), 0, 10))
       .extracting(ComponentDto::uuid)
       .containsExactly(provisionedProject.uuid());
-    assertThat(underTest.selectProvisioned(dbSession, organization.getUuid(), "missing", projectQualifiers, new RowBounds(0, 10))).isEmpty();
     assertThat(underTest.selectByQuery(dbSession, organization.getUuid(), query.get().setNameOrKeyQuery("missing").setPartialMatchOnKey(true).build(), 0, 10)).isEmpty();
-    assertThat(underTest.selectProvisioned(dbSession, organization.getUuid(), "to be escaped '\"\\%", projectQualifiers, new RowBounds(0, 10))).isEmpty();
     assertThat(underTest.selectByQuery(dbSession, organization.getUuid(), query.get().setNameOrKeyQuery("to be escaped '\"\\%").setPartialMatchOnKey(true).build(), 0, 10))
       .isEmpty();
 
     // match name
-    assertThat(underTest.selectProvisioned(dbSession, organization.getUuid(), "ned proj", projectQualifiers, new RowBounds(0, 10)))
-      .extracting(ComponentDto::uuid)
-      .containsExactly(provisionedProject.uuid());
     assertThat(underTest.selectByQuery(dbSession, organization.getUuid(), query.get().setNameOrKeyQuery("ned proj").setPartialMatchOnKey(true).build(), 0, 10))
       .extracting(ComponentDto::uuid)
       .containsExactly(provisionedProject.uuid());
@@ -691,10 +668,11 @@ public class ComponentDaoTest {
     db.components().insertPrivateProject(organization);
     db.components().insertProjectAndSnapshot(newPrivateProjectDto(organization));
     db.components().insertProjectAndSnapshot(ComponentTesting.newView(organization));
+    Supplier<ComponentQuery.Builder> query = () -> ComponentQuery.builder().setOnProvisionedOnly(true);
 
-    assertThat(underTest.countProvisioned(dbSession, organization.getUuid(), null, newHashSet(Qualifiers.PROJECT))).isEqualTo(1);
-    assertThat(underTest.countProvisioned(dbSession, organization.getUuid(), null, newHashSet(Qualifiers.VIEW))).isEqualTo(0);
-    assertThat(underTest.countProvisioned(dbSession, organization.getUuid(), null, newHashSet(Qualifiers.VIEW, Qualifiers.PROJECT))).isEqualTo(1);
+    assertThat(underTest.countByQuery(dbSession, organization.getUuid(), query.get().setQualifiers(Qualifiers.PROJECT).build())).isEqualTo(1);
+    assertThat(underTest.countByQuery(dbSession, organization.getUuid(), query.get().setQualifiers(Qualifiers.VIEW).build())).isEqualTo(0);
+    assertThat(underTest.countByQuery(dbSession, organization.getUuid(), query.get().setQualifiers(Qualifiers.PROJECT, Qualifiers.VIEW).build())).isEqualTo(1);
   }
 
   @Test
index c0e208810c42c11a171c4f653cce634f1704f789..8f37223982bacc8ba102fd729bbf4183d87357e9 100644 (file)
@@ -25,7 +25,6 @@ import java.util.List;
 import java.util.Set;
 import java.util.function.Consumer;
 import javax.annotation.Nullable;
-import org.apache.ibatis.session.RowBounds;
 import org.sonar.api.resources.Qualifiers;
 import org.sonar.api.server.ws.Change;
 import org.sonar.api.server.ws.Request;
@@ -36,8 +35,8 @@ import org.sonar.api.utils.DateUtils;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.component.ComponentDto;
+import org.sonar.db.component.ComponentQuery;
 import org.sonar.db.organization.OrganizationDto;
-import org.sonar.server.es.SearchOptions;
 import org.sonar.server.organization.DefaultOrganizationProvider;
 import org.sonar.server.user.UserSession;
 import org.sonarqube.ws.Common.Paging;
@@ -46,6 +45,8 @@ import org.sonarqube.ws.WsComponents.ProvisionedWsResponse.Component;
 
 import static com.google.common.collect.Sets.newHashSet;
 import static java.util.Optional.ofNullable;
+import static org.sonar.api.utils.Paging.offset;
+import static org.sonar.core.util.Protobuf.setNullable;
 import static org.sonar.core.util.stream.MoreCollectors.toList;
 import static org.sonar.db.permission.OrganizationPermission.PROVISION_PROJECTS;
 import static org.sonar.server.es.SearchOptions.MAX_LIMIT;
@@ -96,9 +97,8 @@ public class ProvisionedAction implements ProjectsWsAction {
   public void handle(Request request, Response response) throws Exception {
     userSession.checkLoggedIn();
 
-    SearchOptions options = new SearchOptions().setPage(
-      request.mandatoryParamAsInt(Param.PAGE),
-      request.mandatoryParamAsInt(Param.PAGE_SIZE));
+    int page = request.mandatoryParamAsInt(Param.PAGE);
+    int pageSize = request.mandatoryParamAsInt(Param.PAGE_SIZE);
     Set<String> desiredFields = desiredFields(request);
     String query = request.param(Param.TEXT_QUERY);
 
@@ -107,20 +107,35 @@ public class ProvisionedAction implements ProjectsWsAction {
         request.getParam(PARAM_ORGANIZATION).or(defaultOrganizationProvider.get()::getKey));
       userSession.checkPermission(PROVISION_PROJECTS, organization);
 
-      RowBounds rowBounds = new RowBounds(options.getOffset(), options.getLimit());
-      List<ComponentDto> projects = dbClient.componentDao().selectProvisioned(dbSession, organization.getUuid(), query, QUALIFIERS_FILTER, rowBounds);
-      int nbOfProjects = dbClient.componentDao().countProvisioned(dbSession, organization.getUuid(), query, QUALIFIERS_FILTER);
+      ComponentQuery dbQuery = buildDbQuery(query);
+      List<ComponentDto> projects = dbClient.componentDao().selectByQuery(dbSession, organization.getUuid(), dbQuery, offset(page, pageSize), pageSize);
+      int nbOfProjects = dbClient.componentDao().countByQuery(dbSession, organization.getUuid(), dbQuery);
+
       ProvisionedWsResponse result = ProvisionedWsResponse.newBuilder()
         .addAllProjects(writeProjects(projects, desiredFields))
         .setPaging(Paging.newBuilder()
           .setTotal(nbOfProjects)
-          .setPageIndex(options.getPage())
-          .setPageSize(options.getLimit()))
+          .setPageIndex(page)
+          .setPageSize(pageSize))
         .build();
       writeProtobuf(result, request, response);
     }
   }
 
+  private static ComponentQuery buildDbQuery(@Nullable String nameOrKeyQuery) {
+    ComponentQuery.Builder dbQuery = ComponentQuery.builder()
+      .setQualifiers(Qualifiers.PROJECT)
+      .setOnProvisionedOnly(true);
+
+    setNullable(nameOrKeyQuery, q -> {
+      dbQuery.setPartialMatchOnKey(true);
+      dbQuery.setNameOrKeyQuery(q);
+      return dbQuery;
+    });
+
+    return dbQuery.build();
+  }
+
   private static List<Component> writeProjects(List<ComponentDto> projects, Set<String> desiredFields) {
     return projects.stream().map(project -> {
       Component.Builder compBuilder = Component.newBuilder().setUuid(project.uuid());