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.

RuleDao.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 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.rule;
  21. import java.util.Collection;
  22. import java.util.List;
  23. import java.util.Optional;
  24. import java.util.Set;
  25. import java.util.function.Consumer;
  26. import javax.annotation.Nullable;
  27. import org.apache.ibatis.session.ResultHandler;
  28. import org.sonar.api.rule.RuleKey;
  29. import org.sonar.api.rules.RuleQuery;
  30. import org.sonar.core.util.UuidFactory;
  31. import org.sonar.db.Dao;
  32. import org.sonar.db.DbSession;
  33. import org.sonar.db.RowNotFoundException;
  34. import org.sonar.db.es.RuleExtensionId;
  35. import org.sonar.db.organization.OrganizationDto;
  36. import static com.google.common.base.Preconditions.checkNotNull;
  37. import static java.util.Collections.emptyList;
  38. import static java.util.Optional.ofNullable;
  39. import static org.sonar.db.DatabaseUtils.executeLargeInputs;
  40. import static org.sonar.db.DatabaseUtils.executeLargeInputsWithoutOutput;
  41. import static org.sonar.db.DatabaseUtils.executeLargeUpdates;
  42. public class RuleDao implements Dao {
  43. private final UuidFactory uuidFactory;
  44. public RuleDao(UuidFactory uuidFactory) {
  45. this.uuidFactory = uuidFactory;
  46. }
  47. public Optional<RuleDto> selectByKey(DbSession session, String organizationUuid, RuleKey key) {
  48. RuleDto res = mapper(session).selectByKey(organizationUuid, key);
  49. ensureOrganizationIsSet(organizationUuid, res);
  50. return ofNullable(res);
  51. }
  52. public RuleDto selectOrFailByKey(DbSession session, RuleKey key) {
  53. RuleDefinitionDto ruleDefinitionDto = selectOrFailDefinitionByKey(session, key);
  54. return new RuleDto(ruleDefinitionDto, new RuleMetadataDto());
  55. }
  56. public Optional<RuleDefinitionDto> selectDefinitionByKey(DbSession session, RuleKey key) {
  57. return ofNullable(mapper(session).selectDefinitionByKey(key));
  58. }
  59. public Optional<RuleMetadataDto> selectMetadataByKey(DbSession session, RuleKey key, String organizationUuid) {
  60. return ofNullable(mapper(session).selectMetadataByKey(key, organizationUuid));
  61. }
  62. public RuleDto selectOrFailByKey(DbSession session, OrganizationDto organization, RuleKey key) {
  63. RuleDto rule = mapper(session).selectByKey(organization.getUuid(), key);
  64. if (rule == null) {
  65. throw new RowNotFoundException(String.format("Rule with key '%s' does not exist", key));
  66. }
  67. ensureOrganizationIsSet(organization.getUuid(), rule);
  68. return rule;
  69. }
  70. public RuleDefinitionDto selectOrFailDefinitionByKey(DbSession session, RuleKey key) {
  71. RuleDefinitionDto rule = mapper(session).selectDefinitionByKey(key);
  72. if (rule == null) {
  73. throw new RowNotFoundException(String.format("Rule with key '%s' does not exist", key));
  74. }
  75. return rule;
  76. }
  77. public Optional<RuleDto> selectByUuid(String uuid, String organizationUuid, DbSession session) {
  78. RuleDto res = mapper(session).selectByUuid(organizationUuid, uuid);
  79. ensureOrganizationIsSet(organizationUuid, res);
  80. return ofNullable(res);
  81. }
  82. public Optional<RuleDefinitionDto> selectDefinitionByUuid(String uuid, DbSession session) {
  83. return ofNullable(mapper(session).selectDefinitionByUuid(uuid));
  84. }
  85. public List<RuleDto> selectByUuids(DbSession session, String organizationUuid, List<String> uuids) {
  86. if (uuids.isEmpty()) {
  87. return emptyList();
  88. }
  89. return ensureOrganizationIsSet(
  90. organizationUuid,
  91. executeLargeInputs(uuids, chunk -> mapper(session).selectByUuids(organizationUuid, chunk)));
  92. }
  93. public List<RuleDefinitionDto> selectDefinitionByUuids(DbSession session, Collection<String> uuids) {
  94. if (uuids.isEmpty()) {
  95. return emptyList();
  96. }
  97. return executeLargeInputs(uuids, mapper(session)::selectDefinitionByUuids);
  98. }
  99. public List<RuleDto> selectByKeys(DbSession session, OrganizationDto organization, Collection<RuleKey> keys) {
  100. if (keys.isEmpty()) {
  101. return emptyList();
  102. }
  103. return ensureOrganizationIsSet(organization.getUuid(),
  104. executeLargeInputs(keys, chunk -> mapper(session).selectByKeys(organization.getUuid(), chunk)));
  105. }
  106. public List<RuleDto> selectByKeys(DbSession session, String organizationUuid, Collection<RuleKey> keys) {
  107. if (keys.isEmpty()) {
  108. return emptyList();
  109. }
  110. return ensureOrganizationIsSet(organizationUuid,
  111. executeLargeInputs(keys, chunk -> mapper(session).selectByKeys(organizationUuid, chunk)));
  112. }
  113. public List<RuleDefinitionDto> selectDefinitionByKeys(DbSession session, Collection<RuleKey> keys) {
  114. if (keys.isEmpty()) {
  115. return emptyList();
  116. }
  117. return executeLargeInputs(keys, mapper(session)::selectDefinitionByKeys);
  118. }
  119. public void selectEnabled(DbSession session, ResultHandler<RuleDefinitionDto> resultHandler) {
  120. mapper(session).selectEnabled(resultHandler);
  121. }
  122. public List<RuleDto> selectAll(DbSession session, String organizationUuid) {
  123. return ensureOrganizationIsSet(organizationUuid, mapper(session).selectAll(organizationUuid));
  124. }
  125. public List<RuleDefinitionDto> selectAllDefinitions(DbSession session) {
  126. return mapper(session).selectAllDefinitions();
  127. }
  128. public List<RuleDto> selectByTypeAndLanguages(DbSession session, String organizationUuid, List<Integer> types, List<String> languages) {
  129. return ensureOrganizationIsSet(organizationUuid,
  130. executeLargeInputs(languages, chunk -> mapper(session).selectByTypeAndLanguages(organizationUuid, types, chunk)));
  131. }
  132. public List<RuleDto> selectByQuery(DbSession session, String organizationUuid, RuleQuery ruleQuery) {
  133. return ensureOrganizationIsSet(organizationUuid, mapper(session).selectByQuery(organizationUuid, ruleQuery));
  134. }
  135. private static void ensureOrganizationIsSet(String organizationUuid, @Nullable RuleDto res) {
  136. if (res != null) {
  137. res.setOrganizationUuid(organizationUuid);
  138. }
  139. }
  140. private static List<RuleDto> ensureOrganizationIsSet(String organizationUuid, List<RuleDto> res) {
  141. res.forEach(dto -> ensureOrganizationIsSet(organizationUuid, dto));
  142. return res;
  143. }
  144. public void insert(DbSession session, RuleDefinitionDto dto) {
  145. checkNotNull(dto.getUuid(), "RuleDefinitionDto has no 'uuid'.");
  146. mapper(session).insertDefinition(dto);
  147. }
  148. public void insert(DbSession session, RuleMetadataDto dto) {
  149. checkNotNull(dto.getRuleUuid(), "RuleMetadataDto has no 'ruleUuid'.");
  150. mapper(session).insertMetadata(dto);
  151. }
  152. public void update(DbSession session, RuleDefinitionDto dto) {
  153. mapper(session).updateDefinition(dto);
  154. }
  155. public void insertOrUpdate(DbSession session, RuleMetadataDto dto) {
  156. if (mapper(session).countMetadata(dto) > 0) {
  157. mapper(session).updateMetadata(dto);
  158. } else {
  159. mapper(session).insertMetadata(dto);
  160. }
  161. }
  162. public void scrollIndexingRuleExtensionsByIds(DbSession dbSession, Collection<RuleExtensionId> ruleExtensionIds, Consumer<RuleExtensionForIndexingDto> consumer) {
  163. RuleMapper mapper = mapper(dbSession);
  164. executeLargeInputsWithoutOutput(ruleExtensionIds,
  165. pageOfRuleExtensionIds -> mapper
  166. .selectIndexingRuleExtensionsByIds(pageOfRuleExtensionIds)
  167. .forEach(consumer));
  168. }
  169. public void scrollIndexingRuleExtensions(DbSession dbSession, Consumer<RuleExtensionForIndexingDto> consumer) {
  170. mapper(dbSession).scrollIndexingRuleExtensions(context -> {
  171. RuleExtensionForIndexingDto dto = context.getResultObject();
  172. consumer.accept(dto);
  173. });
  174. }
  175. public void scrollIndexingRulesByKeys(DbSession dbSession, Collection<String> ruleUuids, Consumer<RuleForIndexingDto> consumer) {
  176. RuleMapper mapper = mapper(dbSession);
  177. executeLargeInputsWithoutOutput(ruleUuids,
  178. pageOfRuleUuids -> mapper
  179. .selectIndexingRulesByUuids(pageOfRuleUuids)
  180. .forEach(consumer));
  181. }
  182. public void scrollIndexingRules(DbSession dbSession, Consumer<RuleForIndexingDto> consumer) {
  183. mapper(dbSession).scrollIndexingRules(context -> {
  184. RuleForIndexingDto dto = context.getResultObject();
  185. consumer.accept(dto);
  186. });
  187. }
  188. private static RuleMapper mapper(DbSession session) {
  189. return session.getMapper(RuleMapper.class);
  190. }
  191. /**
  192. * RuleParams
  193. */
  194. public List<RuleParamDto> selectRuleParamsByRuleKey(DbSession session, RuleKey key) {
  195. return mapper(session).selectParamsByRuleKey(key);
  196. }
  197. public List<RuleParamDto> selectRuleParamsByRuleKeys(DbSession session, Collection<RuleKey> ruleKeys) {
  198. return executeLargeInputs(ruleKeys, mapper(session)::selectParamsByRuleKeys);
  199. }
  200. public List<RuleParamDto> selectRuleParamsByRuleUuids(DbSession dbSession, Collection<String> ruleUuids) {
  201. return executeLargeInputs(ruleUuids, mapper(dbSession)::selectParamsByRuleUuids);
  202. }
  203. public void insertRuleParam(DbSession session, RuleDefinitionDto rule, RuleParamDto param) {
  204. checkNotNull(rule.getUuid(), "Rule uuid must be set");
  205. param.setRuleUuid(rule.getUuid());
  206. param.setUuid(uuidFactory.create());
  207. mapper(session).insertParameter(param);
  208. }
  209. public RuleParamDto updateRuleParam(DbSession session, RuleDefinitionDto rule, RuleParamDto param) {
  210. checkNotNull(rule.getUuid(), "Rule uuid must be set");
  211. checkNotNull(param.getUuid(), "Rule parameter is not yet persisted must be set");
  212. param.setRuleUuid(rule.getUuid());
  213. mapper(session).updateParameter(param);
  214. return param;
  215. }
  216. public void deleteRuleParam(DbSession session, String ruleParameterUuid) {
  217. mapper(session).deleteParameter(ruleParameterUuid);
  218. }
  219. public Set<DeprecatedRuleKeyDto> selectAllDeprecatedRuleKeys(DbSession session) {
  220. return mapper(session).selectAllDeprecatedRuleKeys();
  221. }
  222. public void deleteDeprecatedRuleKeys(DbSession dbSession, Collection<String> uuids) {
  223. if (uuids.isEmpty()) {
  224. return;
  225. }
  226. executeLargeUpdates(uuids, mapper(dbSession)::deleteDeprecatedRuleKeys);
  227. }
  228. public void insert(DbSession dbSession, DeprecatedRuleKeyDto deprecatedRuleKey) {
  229. mapper(dbSession).insertDeprecatedRuleKey(deprecatedRuleKey);
  230. }
  231. }