You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IssueDao.java 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.db.issue;
  21. import java.util.Collection;
  22. import java.util.List;
  23. import java.util.Optional;
  24. import java.util.Set;
  25. import javax.annotation.Nullable;
  26. import org.apache.ibatis.annotations.Param;
  27. import org.apache.ibatis.cursor.Cursor;
  28. import org.sonar.db.Dao;
  29. import org.sonar.db.DbSession;
  30. import org.sonar.db.Pagination;
  31. import org.sonar.db.RowNotFoundException;
  32. import org.sonar.db.component.ComponentDto;
  33. import static org.sonar.db.DatabaseUtils.executeLargeInputs;
  34. public class IssueDao implements Dao {
  35. public static final int DEFAULT_PAGE_SIZE = 1000;
  36. public static final int BIG_PAGE_SIZE = 1000000;
  37. public Optional<IssueDto> selectByKey(DbSession session, String key) {
  38. return Optional.ofNullable(mapper(session).selectByKey(key));
  39. }
  40. public IssueDto selectOrFailByKey(DbSession session, String key) {
  41. Optional<IssueDto> issue = selectByKey(session, key);
  42. if (issue.isEmpty()) {
  43. throw new RowNotFoundException(String.format("Issue with key '%s' does not exist", key));
  44. }
  45. return issue.get();
  46. }
  47. /**
  48. * Gets a list issues by their keys. The result does NOT contain {@code null} values for issues not found, so
  49. * the size of result may be less than the number of keys. A single issue is returned
  50. * if input keys contain multiple occurrences of a key.
  51. * <p>Results may be in a different order as input keys.</p>
  52. */
  53. public List<IssueDto> selectByKeys(DbSession session, Collection<String> keys) {
  54. return executeLargeInputs(keys, mapper(session)::selectByKeys);
  55. }
  56. public Set<String> selectIssueKeysByComponentUuid(DbSession session, String componentUuid) {
  57. return mapper(session).selectIssueKeysByComponentUuid(componentUuid);
  58. }
  59. public Set<String> selectIssueKeysByComponentUuid(DbSession session, String componentUuid,
  60. List<String> includingRepositories, List<String> excludingRepositories,
  61. List<String> languages, int page) {
  62. return mapper(session).selectIssueKeysByComponentUuidWithFilters(componentUuid, includingRepositories, excludingRepositories,
  63. languages, Pagination.forPage(page).andSize(BIG_PAGE_SIZE));
  64. }
  65. public Set<String> selectIssueKeysByComponentUuidAndChangedSinceDate(DbSession session, String componentUuid, long changedSince,
  66. List<String> includingRepositories, List<String> excludingRepositories,
  67. List<String> languages, int page) {
  68. return mapper(session).selectIssueKeysByComponentUuidAndChangedSinceDate(componentUuid, changedSince,
  69. includingRepositories, excludingRepositories, languages, Pagination.forPage(page).andSize(BIG_PAGE_SIZE));
  70. }
  71. public Set<String> selectComponentUuidsOfOpenIssuesForProjectUuid(DbSession session, String projectUuid) {
  72. return mapper(session).selectComponentUuidsOfOpenIssuesForProjectUuid(projectUuid);
  73. }
  74. public List<PrIssueDto> selectOpenByComponentUuids(DbSession dbSession, Collection<String> componentUuids) {
  75. return executeLargeInputs(componentUuids, mapper(dbSession)::selectOpenByComponentUuids);
  76. }
  77. public Collection<IssueGroupDto> selectIssueGroupsByComponent(DbSession dbSession, ComponentDto component, long leakPeriodBeginningDate) {
  78. return mapper(dbSession).selectIssueGroupsByComponent(component, leakPeriodBeginningDate);
  79. }
  80. public Collection<IssueImpactGroupDto> selectIssueImpactGroupsByComponent(DbSession dbSession, ComponentDto component, long leakPeriodBeginningDate) {
  81. return mapper(dbSession).selectIssueImpactGroupsByComponent(component, leakPeriodBeginningDate);
  82. }
  83. public Cursor<IndexedIssueDto> scrollIssuesForIndexation(DbSession dbSession, @Nullable @Param("branchUuid") String branchUuid,
  84. @Nullable @Param("issueKeys") Collection<String> issueKeys) {
  85. return mapper(dbSession).scrollIssuesForIndexation(branchUuid, issueKeys);
  86. }
  87. public void insert(DbSession session, IssueDto dto) {
  88. mapper(session).insert(dto);
  89. insertIssueImpacts(session, dto);
  90. }
  91. /**
  92. * In certain circumstances, most notably persisting issues in the CE, importing issues and web issue storage, we wish to avoid persisting
  93. * both issues and their issue impacts in the same batch transactions, as this introduces a significant performance regression. In those
  94. * situations, issues will be inserted first, and then subsequently their issue impacts using
  95. * {@link IssueDao#insertIssueImpacts(DbSession, IssueDto) ()}.
  96. */
  97. public void insertWithoutImpacts(DbSession session, IssueDto dto) {
  98. mapper(session).insert(dto);
  99. }
  100. public void insertIssueImpacts(DbSession session, IssueDto issueDto) {
  101. IssueMapper mapper = mapper(session);
  102. issueDto.getImpacts()
  103. .forEach(impact -> mapper.insertIssueImpact(issueDto.getKey(), impact));
  104. }
  105. private void updateIssueImpacts(DbSession session, IssueDto issueDto) {
  106. deleteIssueImpacts(session, issueDto);
  107. insertIssueImpacts(session, issueDto);
  108. }
  109. public void deleteIssueImpacts(DbSession session, IssueDto issueDto) {
  110. mapper(session).deleteIssueImpacts(issueDto.getKey());
  111. }
  112. public void insert(DbSession session, IssueDto dto, IssueDto... others) {
  113. insert(session, dto);
  114. for (IssueDto other : others) {
  115. insert(session, other);
  116. }
  117. }
  118. public void update(DbSession session, IssueDto dto) {
  119. mapper(session).update(dto);
  120. updateIssueImpacts(session, dto);
  121. }
  122. /**
  123. * Update issue without updating issue impacts. Used only in batch update.
  124. * Issue impacts should be updated separately.
  125. */
  126. public void updateWithoutIssueImpacts(DbSession session, IssueDto dto) {
  127. mapper(session).update(dto);
  128. }
  129. public boolean updateIfBeforeSelectedDate(DbSession session, IssueDto dto) {
  130. return mapper(session).updateIfBeforeSelectedDate(dto) != 0;
  131. }
  132. public List<IssueDto> selectByKeysIfNotUpdatedAt(DbSession session, List<String> keys, long updatedAt) {
  133. return mapper(session).selectByKeysIfNotUpdatedAt(keys, updatedAt);
  134. }
  135. public void insertAsNewCodeOnReferenceBranch(DbSession session, NewCodeReferenceIssueDto dto) {
  136. mapper(session).insertAsNewCodeOnReferenceBranch(dto);
  137. }
  138. public void deleteAsNewCodeOnReferenceBranch(DbSession session, String issueKey) {
  139. mapper(session).deleteAsNewCodeOnReferenceBranch(issueKey);
  140. }
  141. private static IssueMapper mapper(DbSession session) {
  142. return session.getMapper(IssueMapper.class);
  143. }
  144. public List<IssueDto> selectByBranch(DbSession dbSession, Set<String> issueKeysSnapshot, IssueQueryParams issueQueryParams) {
  145. return mapper(dbSession).selectByBranch(issueKeysSnapshot, issueQueryParams.getChangedSince());
  146. }
  147. public List<String> selectRecentlyClosedIssues(DbSession dbSession, IssueQueryParams issueQueryParams) {
  148. return mapper(dbSession).selectRecentlyClosedIssues(issueQueryParams);
  149. }
  150. /**
  151. * Returned results are unordered.
  152. */
  153. public List<String> selectIssueKeysByQuery(DbSession dbSession, IssueListQuery issueListQuery, Pagination pagination) {
  154. return mapper(dbSession).selectIssueKeysByQuery(issueListQuery, pagination);
  155. }
  156. }