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));
}
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;
*/
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);
</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"/>
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;
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());
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
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;
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;
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;
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);
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());