123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378 |
- /*
- * SonarQube
- * Copyright (C) 2009-2021 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
- package org.sonar.db.component;
-
- import com.google.common.collect.Ordering;
- import java.util.Arrays;
- import java.util.Collection;
- import java.util.Collections;
- import java.util.HashSet;
- import java.util.LinkedList;
- import java.util.List;
- import java.util.Map;
- import java.util.Optional;
- import java.util.Set;
- import java.util.stream.Stream;
- 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.DbSession;
- import org.sonar.db.RowNotFoundException;
-
- import static com.google.common.base.Preconditions.checkArgument;
- import static java.util.Collections.emptyList;
- import static org.sonar.core.util.stream.MoreCollectors.toList;
- import static org.sonar.core.util.stream.MoreCollectors.toSet;
- import static org.sonar.db.DatabaseUtils.checkThatNotTooManyConditions;
- import static org.sonar.db.DatabaseUtils.executeLargeInputs;
- import static org.sonar.db.DatabaseUtils.executeLargeInputsIntoSet;
- import static org.sonar.db.DatabaseUtils.executeLargeUpdates;
- import static org.sonar.db.component.ComponentDto.generateBranchKey;
- import static org.sonar.db.component.ComponentDto.generatePullRequestKey;
-
- public class ComponentDao implements Dao {
- private static List<ComponentDto> selectByQueryImpl(DbSession session, ComponentQuery query, int offset, int limit) {
- if (query.hasEmptySetOfComponents()) {
- return emptyList();
- }
- checkThatNotTooManyComponents(query);
- return mapper(session).selectByQuery(query, new RowBounds(offset, limit));
- }
-
- private static int countByQueryImpl(DbSession session, ComponentQuery query) {
- if (query.hasEmptySetOfComponents()) {
- return 0;
- }
- checkThatNotTooManyComponents(query);
- return mapper(session).countByQuery(query);
- }
-
- private static ComponentMapper mapper(DbSession session) {
- return session.getMapper(ComponentMapper.class);
- }
-
- public Optional<ComponentDto> selectByUuid(DbSession session, String uuid) {
- return Optional.ofNullable(mapper(session).selectByUuid(uuid));
- }
-
- public ComponentDto selectOrFailByUuid(DbSession session, String uuid) {
- Optional<ComponentDto> componentDto = selectByUuid(session, uuid);
- if (!componentDto.isPresent()) {
- throw new RowNotFoundException(String.format("Component with uuid '%s' not found", uuid));
- }
- return componentDto.get();
- }
-
- /**
- * @throws IllegalArgumentException if parameter query#getComponentIds() has more than {@link org.sonar.db.DatabaseUtils#PARTITION_SIZE_FOR_ORACLE} values
- * @throws IllegalArgumentException if parameter query#getComponentKeys() has more than {@link org.sonar.db.DatabaseUtils#PARTITION_SIZE_FOR_ORACLE} values
- * @throws IllegalArgumentException if parameter query#getMainComponentUuids() has more than {@link org.sonar.db.DatabaseUtils#PARTITION_SIZE_FOR_ORACLE} values
- */
- public List<ComponentDto> selectByQuery(DbSession dbSession, ComponentQuery query, int offset, int limit) {
- return selectByQueryImpl(dbSession, query, offset, limit);
- }
-
- /**
- * @throws IllegalArgumentException if parameter query#getComponentIds() has more than {@link org.sonar.db.DatabaseUtils#PARTITION_SIZE_FOR_ORACLE} values
- * @throws IllegalArgumentException if parameter query#getComponentKeys() has more than {@link org.sonar.db.DatabaseUtils#PARTITION_SIZE_FOR_ORACLE} values
- * @throws IllegalArgumentException if parameter query#getMainComponentUuids() has more than {@link org.sonar.db.DatabaseUtils#PARTITION_SIZE_FOR_ORACLE} values
- */
- public int countByQuery(DbSession session, ComponentQuery query) {
- return countByQueryImpl(session, query);
- }
-
- public List<ComponentDto> selectSubProjectsByComponentUuids(DbSession session, Collection<String> uuids) {
- if (uuids.isEmpty()) {
- return emptyList();
- }
- return mapper(session).selectSubProjectsByComponentUuids(uuids);
- }
-
- public List<ComponentDto> selectDescendantModules(DbSession session, String rootComponentUuid) {
- return mapper(session).selectDescendantModules(rootComponentUuid, Scopes.PROJECT, false);
- }
-
- public List<ComponentDto> selectEnabledDescendantModules(DbSession session, String rootComponentUuid) {
- return mapper(session).selectDescendantModules(rootComponentUuid, Scopes.PROJECT, true);
- }
-
- public List<FilePathWithHashDto> selectEnabledDescendantFiles(DbSession session, String rootComponentUuid) {
- return mapper(session).selectDescendantFiles(rootComponentUuid, Scopes.FILE, true);
- }
-
- public List<FilePathWithHashDto> selectEnabledFilesFromProject(DbSession session, String rootComponentUuid) {
- return mapper(session).selectEnabledFilesFromProject(rootComponentUuid);
- }
-
- public List<ComponentDto> selectByUuids(DbSession session, Collection<String> uuids) {
- return executeLargeInputs(uuids, mapper(session)::selectByUuids);
- }
-
- public List<String> selectExistingUuids(DbSession session, Collection<String> uuids) {
- return executeLargeInputs(uuids, mapper(session)::selectExistingUuids);
- }
-
- /**
- * Return all components of a project (including disable ones)
- */
- public List<ComponentDto> selectAllComponentsFromProjectKey(DbSession session, String projectKey) {
- return mapper(session).selectComponentsFromProjectKeyAndScope(projectKey, null, false);
- }
-
- public List<KeyWithUuidDto> selectUuidsByKeyFromProjectKey(DbSession session, String projectKey) {
- return mapper(session).selectUuidsByKeyFromProjectKey(projectKey);
- }
-
- public List<ComponentDto> selectProjectAndModulesFromProjectKey(DbSession session, String projectKey, boolean excludeDisabled) {
- return mapper(session).selectComponentsFromProjectKeyAndScope(projectKey, Scopes.PROJECT, excludeDisabled);
- }
-
- public int countEnabledModulesByProjectUuid(DbSession session, String projectUuid) {
- return mapper(session).countEnabledModulesByProjectUuid(projectUuid);
- }
-
- public List<ComponentDto> selectEnabledModulesFromProjectKey(DbSession session, String projectKey) {
- return selectProjectAndModulesFromProjectKey(session, projectKey, true);
- }
-
- public List<ComponentDto> selectByKeys(DbSession session, Collection<String> keys) {
- return executeLargeInputs(keys, mapper(session)::selectByKeys);
- }
-
- public List<ComponentDto> selectByKeysAndBranch(DbSession session, Collection<String> keys, String branch) {
- List<String> dbKeys = keys.stream().map(k -> generateBranchKey(k, branch)).collect(toList());
- List<String> allKeys = Stream.of(keys, dbKeys).flatMap(Collection::stream).collect(toList());
- return executeLargeInputs(allKeys, subKeys -> mapper(session).selectByKeysAndBranch(subKeys, branch));
- }
-
- /**
- * Return list of components that will will mix main and branch components.
- * Please note that a project can only appear once in the list, it's not possible to ask for many branches on same project with this method.
- */
- public List<ComponentDto> selectByKeysAndBranches(DbSession session, Map<String, String> branchesByKey) {
- Set<String> dbKeys = branchesByKey.entrySet().stream()
- .map(entry -> generateBranchKey(entry.getKey(), entry.getValue()))
- .collect(toSet());
- return selectByDbKeys(session, dbKeys);
- }
-
- public List<ComponentDto> selectByDbKeys(DbSession session, Set<String> dbKeys) {
- return executeLargeInputs(dbKeys, subKeys -> mapper(session).selectByDbKeys(subKeys));
- }
-
- public List<ComponentDto> selectByKeysAndPullRequest(DbSession session, Collection<String> keys, String pullRequestId) {
- List<String> dbKeys = keys.stream().map(k -> generatePullRequestKey(k, pullRequestId)).collect(toList());
- List<String> allKeys = Stream.of(keys, dbKeys).flatMap(Collection::stream).collect(toList());
- return executeLargeInputs(allKeys, subKeys -> mapper(session).selectByKeysAndBranch(subKeys, pullRequestId));
- }
-
- /**
- * List of ancestors, ordered from root to parent. The list is empty
- * if the component is a tree root. Disabled components are excluded by design
- * as tree represents the more recent analysis.
- */
- public List<ComponentDto> selectAncestors(DbSession dbSession, ComponentDto component) {
- if (component.isRoot()) {
- return Collections.emptyList();
- }
- List<String> ancestorUuids = component.getUuidPathAsList();
- List<ComponentDto> ancestors = selectByUuids(dbSession, ancestorUuids);
- return Ordering.explicit(ancestorUuids).onResultOf(ComponentDto::uuid).immutableSortedCopy(ancestors);
- }
-
- /**
- * Select the children or the leaves of a base component, given by its UUID. The components that are not present in last
- * analysis are ignored.
- * <p>
- * An empty list is returned if the base component does not exist or if the base component is a leaf.
- */
- public List<ComponentDto> selectDescendants(DbSession dbSession, ComponentTreeQuery query) {
- Optional<ComponentDto> componentOpt = selectByUuid(dbSession, query.getBaseUuid());
- if (!componentOpt.isPresent()) {
- return emptyList();
- }
- ComponentDto component = componentOpt.get();
- return mapper(dbSession).selectDescendants(query, componentOpt.get().uuid(), query.getUuidPath(component));
- }
-
- public ComponentDto selectOrFailByKey(DbSession session, String key) {
- Optional<ComponentDto> component = selectByKey(session, key);
- if (!component.isPresent()) {
- throw new RowNotFoundException(String.format("Component key '%s' not found", key));
- }
- return component.get();
- }
-
- public Optional<ComponentDto> selectByKey(DbSession session, String key) {
- return Optional.ofNullable(mapper(session).selectByKey(key));
- }
-
- public Optional<ComponentDto> selectByKeyAndBranch(DbSession session, String key, String branch) {
- return Optional.ofNullable(mapper(session).selectBranchByKeyAndBranchKey(key, generateBranchKey(key, branch), branch));
- }
-
- public Optional<ComponentDto> selectByKeyAndPullRequest(DbSession session, String key, String pullRequestId) {
- return Optional.ofNullable(mapper(session).selectPrByKeyAndBranchKey(key, generatePullRequestKey(key, pullRequestId), pullRequestId));
- }
-
- public List<UuidWithProjectUuidDto> selectAllViewsAndSubViews(DbSession session) {
- return mapper(session).selectUuidsForQualifiers(Qualifiers.APP, Qualifiers.VIEW, Qualifiers.SUBVIEW);
- }
-
- /**
- * Used by Governance
- */
- public Set<String> selectViewKeysWithEnabledCopyOfProject(DbSession session, Set<String> projectUuids) {
- return executeLargeInputsIntoSet(projectUuids,
- partition -> mapper(session).selectViewKeysWithEnabledCopyOfProject(partition),
- i -> i);
- }
-
- public List<String> selectProjectsFromView(DbSession session, String viewUuid, String projectViewUuid) {
- return mapper(session).selectProjectsFromView("%." + viewUuid + ".%", projectViewUuid);
- }
-
- /**
- * Returns all projects (Scope {@link Scopes#PROJECT} and qualifier
- * {@link Qualifiers#PROJECT}) which are enabled.
- * <p>
- * Branches are not returned.
- * <p>
- * Used by Views.
- */
- public List<ComponentDto> selectProjects(DbSession session) {
- return mapper(session).selectProjects();
- }
-
- /**
- * Selects all components that are relevant for indexing. The result is not returned (since it is usually too big), but handed over to the <code>handler</code>
- *
- * @param session the database session
- * @param projectUuid the project uuid, which is selected with all of its children
- * @param handler the action to be applied to every result
- */
- public void scrollForIndexing(DbSession session, @Nullable String projectUuid, ResultHandler<ComponentDto> handler) {
- mapper(session).scrollForIndexing(projectUuid, handler);
- }
-
- /**
- * Retrieves all components with a specific root project Uuid, no other filtering is done by this method.
- * <p>
- * Used by Views plugin
- */
- public List<ComponentDto> selectByProjectUuid(String projectUuid, DbSession dbSession) {
- return mapper(dbSession).selectByProjectUuid(projectUuid);
- }
-
- /**
- * Retrieve enabled components keys with given qualifiers
- * <p>
- * 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));
- }
-
- public List<ComponentWithModuleUuidDto> selectEnabledComponentsWithModuleUuidFromProjectKey(DbSession dbSession, String projectKey) {
- return mapper(dbSession).selectEnabledComponentsWithModuleUuidFromProjectKey(projectKey);
- }
-
- /**
- * Returns components with open issues from P/Rs that use a certain branch as reference (reference branch).
- * Excludes components from the current branch.
- */
- public List<KeyWithUuidDto> selectAllSiblingComponentKeysHavingOpenIssues(DbSession dbSession, String referenceBranchUuid, String currentBranchUuid) {
- return mapper(dbSession).selectAllSiblingComponentKeysHavingOpenIssues(referenceBranchUuid, currentBranchUuid);
- }
-
- /**
- * Scroll all <strong>enabled</strong> files of the specified project (same project_uuid) in no specific order with
- * 'SOURCE' source and a non null path.
- */
- public void scrollAllFilesForFileMove(DbSession session, String projectUuid, ResultHandler<FileMoveRowDto> handler) {
- mapper(session).scrollAllFilesForFileMove(projectUuid, handler);
- }
-
- public void insert(DbSession session, ComponentDto item) {
- mapper(session).insert(item);
- }
-
- public void insert(DbSession session, Collection<ComponentDto> items) {
- insert(session, items.stream());
- }
-
- private void insert(DbSession session, Stream<ComponentDto> items) {
- items.forEach(item -> insert(session, item));
- }
-
- public void insert(DbSession session, ComponentDto item, ComponentDto... others) {
- insert(session, Stream.concat(Stream.of(item), Arrays.stream(others)));
- }
-
- public void update(DbSession session, ComponentUpdateDto component) {
- mapper(session).update(component);
- }
-
- public void updateBEnabledToFalse(DbSession session, Collection<String> uuids) {
- executeLargeUpdates(uuids, mapper(session)::updateBEnabledToFalse);
- }
-
- public void applyBChangesForRootComponentUuid(DbSession session, String projectUuid) {
- mapper(session).applyBChangesForRootComponentUuid(projectUuid);
- }
-
- public void resetBChangedForRootComponentUuid(DbSession session, String projectUuid) {
- mapper(session).resetBChangedForRootComponentUuid(projectUuid);
- }
-
- public void setPrivateForRootComponentUuid(DbSession session, String projectUuid, boolean isPrivate) {
- mapper(session).setPrivateForRootComponentUuid(projectUuid, isPrivate);
- }
-
- public void delete(DbSession session, String componentUuid) {
- mapper(session).delete(componentUuid);
- }
-
- private static void checkThatNotTooManyComponents(ComponentQuery query) {
- checkThatNotTooManyConditions(query.getComponentKeys(), "Too many component keys in query");
- checkThatNotTooManyConditions(query.getComponentUuids(), "Too many component UUIDs in query");
- }
-
- public List<ProjectNclocDistributionDto> selectPrivateProjectsWithNcloc(DbSession dbSession) {
- return mapper(dbSession).selectPrivateProjectsWithNcloc();
- }
-
- public boolean existAnyOfComponentsWithQualifiers(DbSession session, Collection<String> componentKeys, Set<String> qualifiers) {
- if (!componentKeys.isEmpty()) {
- List<Boolean> result = new LinkedList<>();
- return executeLargeInputs(componentKeys, input -> {
- boolean groupNeedIssueSync = mapper(session).checkIfAnyOfComponentsWithQualifiers(input, qualifiers) > 0;
- result.add(groupNeedIssueSync);
- return result;
- }).stream().anyMatch(b -> b);
- }
- return false;
- }
-
- }
|