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.

LiveMeasureDao.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 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.measure;
  21. import java.util.Collection;
  22. import java.util.Collections;
  23. import java.util.List;
  24. import java.util.Optional;
  25. import org.apache.ibatis.session.ResultHandler;
  26. import org.sonar.api.utils.System2;
  27. import org.sonar.core.util.Uuids;
  28. import org.sonar.db.Dao;
  29. import org.sonar.db.DbSession;
  30. import org.sonar.db.component.ComponentDto;
  31. import org.sonar.db.dialect.Dialect;
  32. import static org.sonar.api.measures.CoreMetrics.NCLOC_KEY;
  33. import static org.sonar.db.DatabaseUtils.executeLargeInputs;
  34. public class LiveMeasureDao implements Dao {
  35. private final System2 system2;
  36. public LiveMeasureDao(System2 system2) {
  37. this.system2 = system2;
  38. }
  39. public List<LiveMeasureDto> selectByComponentUuidsAndMetricUuids(DbSession dbSession, Collection<String> largeComponentUuids, Collection<String> metricUuids) {
  40. if (largeComponentUuids.isEmpty() || metricUuids.isEmpty()) {
  41. return Collections.emptyList();
  42. }
  43. return executeLargeInputs(
  44. largeComponentUuids,
  45. componentUuids -> mapper(dbSession).selectByComponentUuidsAndMetricUuids(componentUuids, metricUuids));
  46. }
  47. public List<LiveMeasureDto> selectForProjectsByMetricUuids(DbSession dbSession, Collection<String> metricUuids) {
  48. return mapper(dbSession).selectForProjectsByMetricUuids(metricUuids);
  49. }
  50. public void scrollSelectByComponentUuidAndMetricKeys(DbSession dbSession, String componentUuid, Collection<String> metricKeys, ResultHandler<LiveMeasureDto> handler) {
  51. if (metricKeys.isEmpty()) {
  52. return;
  53. }
  54. mapper(dbSession).scrollSelectByComponentUuidAndMetricKeys(componentUuid, metricKeys, handler);
  55. }
  56. public List<LiveMeasureDto> selectByComponentUuidsAndMetricKeys(DbSession dbSession, Collection<String> largeComponentUuids, Collection<String> metricKeys) {
  57. if (largeComponentUuids.isEmpty() || metricKeys.isEmpty()) {
  58. return Collections.emptyList();
  59. }
  60. return executeLargeInputs(
  61. largeComponentUuids,
  62. componentUuids -> mapper(dbSession).selectByComponentUuidsAndMetricKeys(componentUuids, metricKeys));
  63. }
  64. public List<LiveMeasureDto> selectByComponentUuidAndMetricKeys(DbSession dbSession, String componentUuid, Collection<String> metricKeys) {
  65. if (metricKeys.isEmpty()) {
  66. return Collections.emptyList();
  67. }
  68. return mapper(dbSession).selectByComponentUuidAndMetricKeys(componentUuid, metricKeys);
  69. }
  70. public Optional<LiveMeasureDto> selectMeasure(DbSession dbSession, String componentUuid, String metricKey) {
  71. LiveMeasureDto liveMeasureDto = mapper(dbSession).selectByComponentUuidAndMetricKey(componentUuid, metricKey);
  72. return Optional.ofNullable(liveMeasureDto);
  73. }
  74. public void selectTreeByQuery(DbSession dbSession, ComponentDto baseComponent, MeasureTreeQuery query, ResultHandler<LiveMeasureDto> resultHandler) {
  75. if (query.returnsEmpty()) {
  76. return;
  77. }
  78. mapper(dbSession).selectTreeByQuery(query, baseComponent.uuid(), query.getUuidPath(baseComponent), resultHandler);
  79. }
  80. public long sumNclocOfBiggestBranchForProject(DbSession dbSession, String projectUuid){
  81. Long ncloc = mapper(dbSession).sumNclocOfBiggestBranchForProject(projectUuid, NCLOC_KEY);
  82. return ncloc == null ? 0L : ncloc;
  83. }
  84. public List<LargestBranchNclocDto> getLargestBranchNclocPerProject(DbSession dbSession) {
  85. return mapper(dbSession).getLargestBranchNclocPerProject();
  86. }
  87. public List<ProjectLocDistributionDto> selectLargestBranchesLocDistribution(DbSession session, String nclocUuid, String nclocDistributionUuid) {
  88. return mapper(session).selectLargestBranchesLocDistribution(nclocUuid, nclocDistributionUuid);
  89. }
  90. public long countProjectsHavingMeasure(DbSession dbSession, String metric) {
  91. return mapper(dbSession).countProjectsHavingMeasure(metric);
  92. }
  93. public void insert(DbSession dbSession, LiveMeasureDto dto) {
  94. mapper(dbSession).insert(dto, Uuids.create(), system2.now());
  95. }
  96. public void insertOrUpdate(DbSession dbSession, LiveMeasureDto dto) {
  97. LiveMeasureMapper mapper = mapper(dbSession);
  98. long now = system2.now();
  99. if (mapper.update(dto, now) == 0) {
  100. mapper.insert(dto, Uuids.create(), now);
  101. }
  102. }
  103. public void deleteByComponent(DbSession dbSession, String componentUuid) {
  104. mapper(dbSession).deleteByComponent(componentUuid);
  105. }
  106. /**
  107. * Similar to {@link #insertOrUpdate(DbSession, LiveMeasureDto)}, except that it triggers a single SQL request
  108. * <strong>This method should not be called unless {@link Dialect#supportsUpsert()} is true</strong>
  109. */
  110. public int upsert(DbSession dbSession, LiveMeasureDto dto) {
  111. dto.setUuidForUpsert(Uuids.create());
  112. return mapper(dbSession).upsert(dto, system2.now());
  113. }
  114. public void deleteByComponentUuidExcludingMetricUuids(DbSession dbSession, String componentUuid, List<String> excludedMetricUuids) {
  115. mapper(dbSession).deleteByComponentUuidExcludingMetricUuids(componentUuid, excludedMetricUuids);
  116. }
  117. private static LiveMeasureMapper mapper(DbSession dbSession) {
  118. return dbSession.getMapper(LiveMeasureMapper.class);
  119. }
  120. }