import org.sonar.api.server.ServerSide;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
+import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.qualityprofile.ActiveRuleDto;
import org.sonar.db.qualityprofile.ActiveRuleKey;
import org.sonar.db.qualityprofile.ActiveRuleParamDto;
import org.sonar.db.qualityprofile.QualityProfileDto;
import org.sonar.db.rule.RuleDto;
-import org.sonar.server.db.DbClient;
import org.sonar.server.qualityprofile.ActiveRule;
import org.sonar.server.qualityprofile.QProfileLoader;
import org.sonar.server.rule.index.RuleQuery;
for (RuleDto rule : rules) {
ActiveRule activeRule = loader.getActiveRule(ActiveRuleKey.of(profileKey, rule.getKey()));
if (activeRule != null) {
- Optional<ActiveRuleDto> activeRuleDto = dbClient.activeRuleDao().selectByActiveRuleKey(dbSession, activeRule.key());
+ Optional<ActiveRuleDto> activeRuleDto = dbClient.activeRuleDao().selectByKey(dbSession, activeRule.key());
checkFoundWithOptional(activeRuleDto, "Active rule with key '%s' not found", activeRule.key().toString());
List<ActiveRuleParamDto> activeRuleParamDtos = dbClient.activeRuleDao().selectParamsByActiveRuleId(dbSession, activeRuleDto.get().getId());
ListMultimap<ActiveRuleKey, ActiveRuleParamDto> activeRuleParamByActiveRuleKey = ArrayListMultimap.create(1, activeRuleParamDtos.size());
// Load details of all active rules
for (RuleDto rule : rules) {
List<ActiveRule> activeRules = loader.findActiveRulesByRule(rule.getKey());
- List<ActiveRuleDto> activeRuleDtos = dbClient.activeRuleDao().selectByActiveRuleKeys(dbSession, Lists.transform(activeRules, ActiveRuleToKey.INSTANCE));
+ List<ActiveRuleDto> activeRuleDtos = dbClient.activeRuleDao().selectByKeys(dbSession, Lists.transform(activeRules, ActiveRuleToKey.INSTANCE));
Map<Integer, ActiveRuleKey> activeRuleIdsByKey = new HashMap<>();
for (ActiveRuleDto activeRuleDto : activeRuleDtos) {
activeRuleIdsByKey.put(activeRuleDto.getId(), activeRuleDto.getKey());
void completeShow(DbSession dbSession, RuleDto rule, ShowResponse.Builder response) {
List<ActiveRule> activeRules = loader.findActiveRulesByRule(rule.getKey());
- List<ActiveRuleDto> activeRuleDtos = dbClient.activeRuleDao().selectByActiveRuleKeys(dbSession, Lists.transform(activeRules, ActiveRuleToKey.INSTANCE));
+ List<ActiveRuleDto> activeRuleDtos = dbClient.activeRuleDao().selectByKeys(dbSession, Lists.transform(activeRules, ActiveRuleToKey.INSTANCE));
Map<Integer, ActiveRuleKey> activeRuleIdsByKey = new HashMap<>();
for (ActiveRuleDto activeRuleDto : activeRuleDtos) {
activeRuleIdsByKey.put(activeRuleDto.getId(), activeRuleDto.getKey());
*/
package org.sonar.db.qualityprofile;
+import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
+import java.util.ArrayList;
import java.util.List;
import javax.annotation.CheckForNull;
+import javax.annotation.Nonnull;
import org.sonar.db.Dao;
+import org.sonar.db.DatabaseUtils;
import org.sonar.db.DbSession;
import org.sonar.db.RowNotFoundException;
import org.sonar.db.rule.RuleDto;
throw new RowNotFoundException(String.format("Active rule with key '%s' does not exist", key));
}
+ public List<ActiveRuleDto> selectByKeys(DbSession dbSession, List<ActiveRuleKey> keys) {
+ List<SqlActiveRuleKey> sqlKeys = new ArrayList<>();
+ for (ActiveRuleKey key : keys) {
+ sqlKeys.add(new SqlActiveRuleKey(key));
+ }
+ return DatabaseUtils.executeLargeInputs(sqlKeys, new KeyToDto(mapper(dbSession)));
+ }
+
public List<ActiveRuleDto> selectByRule(DbSession dbSession, RuleDto rule) {
Preconditions.checkNotNull(rule.getId(), RULE_IS_NOT_PERSISTED);
return mapper(dbSession).selectByRuleId(rule.getId());
return mapper(dbSession).selectAll();
}
- public List<ActiveRuleParamDto> selectAllParams(DbSession dbSession) {
- return mapper(dbSession).selectAllParams();
+ public List<ActiveRuleDto> selectByProfileKey(DbSession session, String profileKey) {
+ return mapper(session).selectByProfileKey(profileKey);
}
public ActiveRuleDto insert(DbSession session, ActiveRuleDto item) {
* Nested DTO ActiveRuleParams
*/
+ public List<ActiveRuleParamDto> selectParamsByActiveRuleId(DbSession dbSession, Integer activeRuleId) {
+ return mapper(dbSession).selectParamsByActiveRuleId(activeRuleId);
+ }
+
+ public List<ActiveRuleParamDto> selectParamsByActiveRuleIds(final DbSession dbSession, List<Integer> activeRuleIds) {
+ return DatabaseUtils.executeLargeInputs(activeRuleIds, new ParamIdToDto(mapper(dbSession)));
+ }
+
+ public List<ActiveRuleParamDto> selectParamsByActiveRuleKey(DbSession session, ActiveRuleKey key) {
+ Preconditions.checkNotNull(key, ACTIVE_RULE_KEY_CANNOT_BE_NULL);
+ ActiveRuleDto activeRule = selectOrFailByKey(session, key);
+ return mapper(session).selectParamsByActiveRuleId(activeRule.getId());
+ }
+
+ @CheckForNull
+ public ActiveRuleParamDto selectParamByKeyAndName(ActiveRuleKey key, String name, DbSession session) {
+ Preconditions.checkNotNull(key, ACTIVE_RULE_KEY_CANNOT_BE_NULL);
+ Preconditions.checkNotNull(name, PARAMETER_NAME_CANNOT_BE_NULL);
+ Optional<ActiveRuleDto> activeRule = selectByKey(session, key);
+ if (activeRule.isPresent()) {
+ return mapper(session).selectParamByActiveRuleAndKey(activeRule.get().getId(), name);
+ }
+ return null;
+ }
+
+ public List<ActiveRuleParamDto> selectAllParams(DbSession dbSession) {
+ return mapper(dbSession).selectAllParams();
+ }
+
public ActiveRuleParamDto insertParam(DbSession session, ActiveRuleDto activeRule, ActiveRuleParamDto activeRuleParam) {
Preconditions.checkArgument(activeRule.getId() != null, ACTIVE_RULE_IS_NOT_PERSISTED);
Preconditions.checkArgument(activeRuleParam.getId() == null, ACTIVE_RULE_PARAM_IS_ALREADY_PERSISTED);
mapper(session).deleteParameter(activeRuleParam.getId());
}
- public List<ActiveRuleDto> selectByProfileKey(DbSession session, String profileKey) {
- return mapper(session).selectByProfileKey(profileKey);
- }
-
- /**
- * Finder methods for ActiveRuleParams
- */
-
- public List<ActiveRuleParamDto> selectParamsByActiveRuleKey(DbSession session, ActiveRuleKey key) {
- Preconditions.checkNotNull(key, ACTIVE_RULE_KEY_CANNOT_BE_NULL);
- ActiveRuleDto activeRule = selectOrFailByKey(session, key);
- return mapper(session).selectParamsByActiveRuleId(activeRule.getId());
- }
-
- @CheckForNull
- public ActiveRuleParamDto selectParamByKeyAndName(ActiveRuleKey key, String name, DbSession session) {
- Preconditions.checkNotNull(key, ACTIVE_RULE_KEY_CANNOT_BE_NULL);
- Preconditions.checkNotNull(name, PARAMETER_NAME_CANNOT_BE_NULL);
- Optional<ActiveRuleDto> activeRule = selectByKey(session, key);
- if (activeRule.isPresent()) {
- return mapper(session).selectParamByActiveRuleAndKey(activeRule.get().getId(), name);
- }
- return null;
- }
-
public void deleteParamsByRuleParam(DbSession dbSession, RuleDto rule, String paramKey) {
List<ActiveRuleDto> activeRules = selectByRule(dbSession, rule);
for (ActiveRuleDto activeRule : activeRules) {
private ActiveRuleMapper mapper(DbSession session) {
return session.getMapper(ActiveRuleMapper.class);
}
+
+ private static class KeyToDto implements Function<List<SqlActiveRuleKey>, List<ActiveRuleDto>> {
+ private final ActiveRuleMapper mapper;
+
+ private KeyToDto(ActiveRuleMapper mapper) {
+ this.mapper = mapper;
+ }
+
+ @Override
+ public List<ActiveRuleDto> apply(@Nonnull List<SqlActiveRuleKey> partitionOfIds) {
+ return mapper.selectByKeys(partitionOfIds);
+ }
+ }
+
+ private static class ParamIdToDto implements Function<List<Integer>, List<ActiveRuleParamDto>> {
+ private final ActiveRuleMapper mapper;
+
+ private ParamIdToDto(ActiveRuleMapper mapper) {
+ this.mapper = mapper;
+ }
+
+ @Override
+ public List<ActiveRuleParamDto> apply(@Nonnull List<Integer> partitionOfIds) {
+ return mapper.selectParamsByActiveRuleIds(partitionOfIds);
+ }
+ }
}