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.

MetricDao.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 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.metric;
  21. import com.google.common.base.Predicate;
  22. import com.google.common.collect.Collections2;
  23. import com.google.common.collect.Lists;
  24. import java.util.ArrayList;
  25. import java.util.Collection;
  26. import java.util.Collections;
  27. import java.util.List;
  28. import java.util.Map;
  29. import java.util.Set;
  30. import javax.annotation.CheckForNull;
  31. import javax.annotation.Nonnull;
  32. import javax.annotation.Nullable;
  33. import org.apache.ibatis.session.RowBounds;
  34. import org.sonar.db.Dao;
  35. import org.sonar.db.DbSession;
  36. import org.sonar.db.RowNotFoundException;
  37. import static com.google.common.collect.Lists.newArrayList;
  38. import static org.sonar.db.DatabaseUtils.executeLargeInputs;
  39. import static org.sonar.db.DatabaseUtils.executeLargeInputsWithoutOutput;
  40. public class MetricDao implements Dao {
  41. @CheckForNull
  42. public MetricDto selectByKey(DbSession session, String key) {
  43. return mapper(session).selectByKey(key);
  44. }
  45. public List<MetricDto> selectByKeys(final DbSession session, Collection<String> keys) {
  46. return executeLargeInputs(keys, mapper(session)::selectByKeys);
  47. }
  48. public MetricDto selectOrFailByKey(DbSession session, String key) {
  49. MetricDto metric = selectByKey(session, key);
  50. if (metric == null) {
  51. throw new RowNotFoundException(String.format("Metric key '%s' not found", key));
  52. }
  53. return metric;
  54. }
  55. public List<MetricDto> selectAll(DbSession session) {
  56. return mapper(session).selectAll();
  57. }
  58. public List<MetricDto> selectEnabled(DbSession session) {
  59. return mapper(session).selectAllEnabled();
  60. }
  61. public List<MetricDto> selectEnabled(DbSession session, @Nullable Boolean isCustom, int offset, int limit) {
  62. Map<String, Object> properties;
  63. if (isCustom != null) {
  64. properties = Collections.singletonMap("isCustom", isCustom);
  65. } else {
  66. properties = Collections.emptyMap();
  67. }
  68. return mapper(session).selectAllEnabled(properties, new RowBounds(offset, limit));
  69. }
  70. public int countEnabled(DbSession session, @Nullable Boolean isCustom) {
  71. return mapper(session).countEnabled(isCustom);
  72. }
  73. public MetricDto insert(DbSession session, MetricDto dto) {
  74. mapper(session).insert(dto);
  75. return dto;
  76. }
  77. public void insert(DbSession session, Collection<MetricDto> items) {
  78. for (MetricDto item : items) {
  79. insert(session, item);
  80. }
  81. }
  82. public void insert(DbSession session, MetricDto item, MetricDto... others) {
  83. insert(session, Lists.asList(item, others));
  84. }
  85. public List<String> selectEnabledDomains(DbSession session) {
  86. return newArrayList(Collections2.filter(mapper(session).selectDomains(), new NotEmptyPredicate()));
  87. }
  88. public List<MetricDto> selectAvailableCustomMetricsByComponentUuid(DbSession session, String projectUuid) {
  89. return mapper(session).selectAvailableCustomMetricsByComponentUuid(projectUuid);
  90. }
  91. public List<MetricDto> selectByIds(DbSession session, Set<Integer> idsSet) {
  92. return executeLargeInputs(new ArrayList<>(idsSet), mapper(session)::selectByIds);
  93. }
  94. private static class NotEmptyPredicate implements Predicate<String> {
  95. @Override
  96. public boolean apply(@Nonnull String input) {
  97. return !input.isEmpty();
  98. }
  99. }
  100. private static MetricMapper mapper(DbSession session) {
  101. return session.getMapper(MetricMapper.class);
  102. }
  103. public void disableCustomByIds(final DbSession session, List<Integer> ids) {
  104. executeLargeInputsWithoutOutput(ids, input -> mapper(session).disableByIds(input));
  105. }
  106. /**
  107. * Disable a metric and return {@code false} if the metric does not exist
  108. * or is already disabled.
  109. */
  110. public boolean disableCustomByKey(DbSession session, String key) {
  111. return mapper(session).disableByKey(key) == 1;
  112. }
  113. public void update(DbSession session, MetricDto metric) {
  114. mapper(session).update(metric);
  115. }
  116. @CheckForNull
  117. public MetricDto selectById(DbSession session, long id) {
  118. return mapper(session).selectById(id);
  119. }
  120. }