diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2014-04-01 19:46:53 +0200 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2014-04-01 19:47:03 +0200 |
commit | 854efb2f842b3021ecb9f1a058bc9297379689b0 (patch) | |
tree | b1c82bbcec4dee400652529bcaff2524078dbccf /sonar-core | |
parent | ca8fc0634e839662d24312c3bbeb49ad64756349 (diff) | |
download | sonarqube-854efb2f842b3021ecb9f1a058bc9297379689b0.tar.gz sonarqube-854efb2f842b3021ecb9f1a058bc9297379689b0.zip |
SONAR-5174 Create a reindex rules method
Diffstat (limited to 'sonar-core')
13 files changed, 175 insertions, 24 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/rule/RuleDao.java b/sonar-core/src/main/java/org/sonar/core/rule/RuleDao.java index f35f189322b..fe04585aa3a 100644 --- a/sonar-core/src/main/java/org/sonar/core/rule/RuleDao.java +++ b/sonar-core/src/main/java/org/sonar/core/rule/RuleDao.java @@ -19,6 +19,7 @@ */ package org.sonar.core.rule; +import com.google.common.collect.Lists; import org.apache.ibatis.session.SqlSession; import org.sonar.api.BatchComponent; import org.sonar.api.ServerComponent; @@ -30,6 +31,8 @@ import javax.annotation.CheckForNull; import java.util.Collection; import java.util.List; +import static com.google.common.collect.Lists.newArrayList; + public class RuleDao implements BatchComponent, ServerComponent { private MyBatis mybatis; @@ -161,6 +164,10 @@ public class RuleDao implements BatchComponent, ServerComponent { } } + //****************************** + // Methods for Rule Parameters + //****************************** + public List<RuleParamDto> selectParameters() { SqlSession session = mybatis.openSession(); try { @@ -174,17 +181,35 @@ public class RuleDao implements BatchComponent, ServerComponent { return getMapper(session).selectAllParams(); } - public List<RuleParamDto> selectParameters(Integer id) { + public List<RuleParamDto> selectParametersByRuleId(Integer ruleId) { SqlSession session = mybatis.openSession(); try { - return selectParameters(id, session); + return selectParametersByRuleId(ruleId, session); } finally { MyBatis.closeQuietly(session); } } - public List<RuleParamDto> selectParameters(Integer ruleId, SqlSession session) { - return getMapper(session).selectParamsForRule(ruleId); + public List<RuleParamDto> selectParametersByRuleId(Integer ruleId, SqlSession session) { + return selectParametersByRuleIds(newArrayList(ruleId)); + } + + public List<RuleParamDto> selectParametersByRuleIds(List<Integer> ruleIds) { + SqlSession session = mybatis.openSession(); + try { + return selectParametersByRuleIds(ruleIds, session); + } finally { + MyBatis.closeQuietly(session); + } + } + + public List<RuleParamDto> selectParametersByRuleIds(List<Integer> ruleIds, SqlSession session) { + List<RuleParamDto> dtos = newArrayList(); + List<List<Integer>> partitionList = Lists.partition(newArrayList(ruleIds), 1000); + for (List<Integer> partition : partitionList) { + dtos.addAll(getMapper(session).selectParamsByRuleIds(partition)); + } + return dtos; } public void insert(RuleParamDto param, SqlSession session) { @@ -224,9 +249,10 @@ public class RuleDao implements BatchComponent, ServerComponent { return session.getMapper(RuleMapper.class); } - public List<RuleRuleTagDto> selectTags(SqlSession session) { - return getMapper(session).selectAllTags(); - } + //*************************** + // Methods for Rule Tags + //*************************** + public void insert(RuleRuleTagDto newTag, SqlSession session) { getMapper(session).insertTag(newTag); @@ -244,16 +270,38 @@ public class RuleDao implements BatchComponent, ServerComponent { getMapper(session).updateTag(existingTag); } - public List<RuleRuleTagDto> selectTags(Integer id) { + public List<RuleRuleTagDto> selectTags(SqlSession session) { + return getMapper(session).selectAllTags(); + } + + public List<RuleRuleTagDto> selectTagsByRuleId(Integer ruleId) { SqlSession session = mybatis.openSession(); try { - return selectTags(id, session); + return selectTagsByRuleIds(ruleId, session); } finally { MyBatis.closeQuietly(session); } } - public List<RuleRuleTagDto> selectTags(Integer id, SqlSession session) { - return getMapper(session).selectTagsForRule(id); + public List<RuleRuleTagDto> selectTagsByRuleIds(Integer ruleId, SqlSession session) { + return selectTagsByRuleIds(newArrayList(ruleId), session); + } + + public List<RuleRuleTagDto> selectTagsByRuleIds(List<Integer> ruleIds) { + SqlSession session = mybatis.openSession(); + try { + return selectTagsByRuleIds(ruleIds, session); + } finally { + MyBatis.closeQuietly(session); + } + } + + public List<RuleRuleTagDto> selectTagsByRuleIds(List<Integer> ruleIds, SqlSession session) { + List<RuleRuleTagDto> dtos = newArrayList(); + List<List<Integer>> partitionList = Lists.partition(newArrayList(ruleIds), 1000); + for (List<Integer> partition : partitionList) { + dtos.addAll(getMapper(session).selectTagsByRuleIds(partition)); + } + return dtos; } } diff --git a/sonar-core/src/main/java/org/sonar/core/rule/RuleDto.java b/sonar-core/src/main/java/org/sonar/core/rule/RuleDto.java index b7965d3bdb1..7ce85241430 100644 --- a/sonar-core/src/main/java/org/sonar/core/rule/RuleDto.java +++ b/sonar-core/src/main/java/org/sonar/core/rule/RuleDto.java @@ -161,11 +161,12 @@ public final class RuleDto { return this; } + @CheckForNull public Integer getParentId() { return parentId; } - public RuleDto setParentId(Integer parentId) { + public RuleDto setParentId(@Nullable Integer parentId) { this.parentId = parentId; return this; } diff --git a/sonar-core/src/main/java/org/sonar/core/rule/RuleMapper.java b/sonar-core/src/main/java/org/sonar/core/rule/RuleMapper.java index 6939314fd5f..e31b14e04aa 100644 --- a/sonar-core/src/main/java/org/sonar/core/rule/RuleMapper.java +++ b/sonar-core/src/main/java/org/sonar/core/rule/RuleMapper.java @@ -47,7 +47,7 @@ public interface RuleMapper { List<RuleParamDto> selectAllParams(); - List<RuleParamDto> selectParamsForRule(Integer id); + List<RuleParamDto> selectParamsByRuleIds(@Param("ruleIds") List<Integer> ruleIds); RuleParamDto selectParamByRuleAndKey(@Param("ruleId") Integer ruleId, @Param("key") String key); @@ -65,5 +65,5 @@ public interface RuleMapper { void updateTag(RuleRuleTagDto existingTag); - List<RuleRuleTagDto> selectTagsForRule(Integer ruleId); + List<RuleRuleTagDto> selectTagsByRuleIds(@Param("ruleIds") List<Integer> ruleIds); } diff --git a/sonar-core/src/main/java/org/sonar/core/technicaldebt/db/CharacteristicDao.java b/sonar-core/src/main/java/org/sonar/core/technicaldebt/db/CharacteristicDao.java index 59a84d38c03..b82c74f4c3a 100644 --- a/sonar-core/src/main/java/org/sonar/core/technicaldebt/db/CharacteristicDao.java +++ b/sonar-core/src/main/java/org/sonar/core/technicaldebt/db/CharacteristicDao.java @@ -20,6 +20,7 @@ package org.sonar.core.technicaldebt.db; +import com.google.common.collect.Lists; import org.apache.ibatis.session.SqlSession; import org.sonar.api.BatchComponent; import org.sonar.api.ServerComponent; @@ -27,8 +28,11 @@ import org.sonar.core.persistence.MyBatis; import javax.annotation.CheckForNull; +import java.util.Collection; import java.util.List; +import static com.google.common.collect.Lists.newArrayList; + public class CharacteristicDao implements BatchComponent, ServerComponent { private final MyBatis mybatis; @@ -101,6 +105,24 @@ public class CharacteristicDao implements BatchComponent, ServerComponent { return session.getMapper(CharacteristicMapper.class).selectCharacteristicsByParentId(parentId); } + public List<CharacteristicDto> selectCharacteristicsByIds(Collection<Integer> ids) { + SqlSession session = mybatis.openSession(); + try { + return selectCharacteristicsByIds(ids, session); + } finally { + MyBatis.closeQuietly(session); + } + } + + public List<CharacteristicDto> selectCharacteristicsByIds(Collection<Integer> ids, SqlSession session) { + List<CharacteristicDto> dtos = newArrayList(); + List<List<Integer>> partitionList = Lists.partition(newArrayList(ids), 1000); + for (List<Integer> partition : partitionList) { + dtos.addAll(session.getMapper(CharacteristicMapper.class).selectCharacteristicsByIds(partition)); + } + return dtos; + } + @CheckForNull public CharacteristicDto selectByKey(String key) { SqlSession session = mybatis.openSession(); diff --git a/sonar-core/src/main/java/org/sonar/core/technicaldebt/db/CharacteristicMapper.java b/sonar-core/src/main/java/org/sonar/core/technicaldebt/db/CharacteristicMapper.java index 910f2df84b0..10b02b8faed 100644 --- a/sonar-core/src/main/java/org/sonar/core/technicaldebt/db/CharacteristicMapper.java +++ b/sonar-core/src/main/java/org/sonar/core/technicaldebt/db/CharacteristicMapper.java @@ -20,6 +20,8 @@ package org.sonar.core.technicaldebt.db; +import org.apache.ibatis.annotations.Param; + import java.util.List; public interface CharacteristicMapper { @@ -32,6 +34,8 @@ public interface CharacteristicMapper { List<CharacteristicDto> selectCharacteristicsByParentId(int parentId); + List<CharacteristicDto> selectCharacteristicsByIds(@Param("ids") List<Integer> ids); + CharacteristicDto selectByKey(String key); CharacteristicDto selectById(int id); diff --git a/sonar-core/src/main/resources/org/sonar/core/rule/RuleMapper.xml b/sonar-core/src/main/resources/org/sonar/core/rule/RuleMapper.xml index 1c10390a436..ce88ab8bdd5 100644 --- a/sonar-core/src/main/resources/org/sonar/core/rule/RuleMapper.xml +++ b/sonar-core/src/main/resources/org/sonar/core/rule/RuleMapper.xml @@ -138,10 +138,12 @@ from rules_parameters </select> - <select id="selectParamsForRule" resultType="RuleParam"> - select <include refid="paramColumns"/> - from rules_parameters - where rule_id=#{id} + <select id="selectParamsByRuleIds" resultType="RuleParam"> + SELECT <include refid="paramColumns"/> + FROM rules_parameters + <where> + AND (<foreach item="id" index="index" collection="ruleIds" open="(" separator=" or " close=")">rule_id=#{id}</foreach>) + </where> </select> <select id="selectParamByRuleAndKey" resultType="RuleParam"> @@ -181,11 +183,13 @@ JOIN rule_tags rt ON rrt.rule_tag_id = rt.id </select> - <select id="selectTagsForRule" resultType="RuleRuleTag"> - select <include refid="tagColumns"/> + <select id="selectTagsByRuleIds" resultType="RuleRuleTag"> + SELECT <include refid="tagColumns"/> FROM rules_rule_tags rrt JOIN rule_tags rt ON rrt.rule_tag_id = rt.id - WHERE rrt.rule_id=#{id} + <where> + AND (<foreach item="id" index="index" collection="ruleIds" open="(" separator=" or " close=")">rrt.rule_id=#{id}</foreach>) + </where> </select> <insert id="insertTag" parameterType="RuleRuleTag" keyColumn="id" useGeneratedKeys="true" keyProperty="id"> diff --git a/sonar-core/src/main/resources/org/sonar/core/technicaldebt/db/CharacteristicMapper.xml b/sonar-core/src/main/resources/org/sonar/core/technicaldebt/db/CharacteristicMapper.xml index 0bae9e151fd..91d7cb70021 100644 --- a/sonar-core/src/main/resources/org/sonar/core/technicaldebt/db/CharacteristicMapper.xml +++ b/sonar-core/src/main/resources/org/sonar/core/technicaldebt/db/CharacteristicMapper.xml @@ -47,6 +47,15 @@ </where> </select> + <select id="selectCharacteristicsByIds" parameterType="map" resultType="Characteristic"> + select <include refid="characteristicColumns"/> + from characteristics c + <where> + and c.enabled=${_true} + AND (<foreach item="id" index="index" collection="ids" open="(" separator=" or " close=")">c.id=#{id}</foreach>) + </where> + </select> + <select id="selectByKey" parameterType="String" resultType="Characteristic"> select <include refid="characteristicColumns"/> from characteristics c diff --git a/sonar-core/src/test/java/org/sonar/core/rule/RuleDaoTest.java b/sonar-core/src/test/java/org/sonar/core/rule/RuleDaoTest.java index cfd6262feca..0a51195d85c 100644 --- a/sonar-core/src/test/java/org/sonar/core/rule/RuleDaoTest.java +++ b/sonar-core/src/test/java/org/sonar/core/rule/RuleDaoTest.java @@ -303,10 +303,10 @@ public class RuleDaoTest extends AbstractDaoTestCase { } @Test - public void select_params_for_rule() throws Exception { - setupData("selectParamsForRule"); + public void select_parameters_by_rule_id() throws Exception { + setupData("select_parameters_by_rule_id"); int ruleId = 1; - List<RuleParamDto> ruleDtos = dao.selectParameters(ruleId); + List<RuleParamDto> ruleDtos = dao.selectParametersByRuleId(ruleId); assertThat(ruleDtos.size()).isEqualTo(1); RuleParamDto ruleDto = ruleDtos.get(0); @@ -318,6 +318,14 @@ public class RuleDaoTest extends AbstractDaoTestCase { } @Test + public void select_parameters_by_rule_ids() throws Exception { + setupData("select_parameters_by_rule_ids"); + + assertThat(dao.selectParametersByRuleIds(newArrayList(1, 2))).hasSize(2); + assertThat(dao.selectParametersByRuleIds(newArrayList(1))).hasSize(1); + } + + @Test public void insert_parameter() { setupData("insert_parameter"); @@ -349,6 +357,20 @@ public class RuleDaoTest extends AbstractDaoTestCase { checkTables("update_parameter", "rules_parameters"); } + @Test + public void select_tags_by_rule_id() throws Exception { + setupData("select_tags_by_rule_id"); + + assertThat(dao.selectTagsByRuleId(3)).hasSize(2); + } + + @Test + public void select_tags_by_rule_ids() throws Exception { + setupData("select_tags_by_rule_ids"); + + assertThat(dao.selectTagsByRuleIds(newArrayList(3, 4))).hasSize(3); + } + private List<Integer> idsFromRuleDtos(List<RuleDto> ruleDtos){ return newArrayList(Iterables.transform(ruleDtos, new Function<RuleDto, Integer>() { @Override diff --git a/sonar-core/src/test/java/org/sonar/core/technicaldebt/db/CharacteristicDaoTest.java b/sonar-core/src/test/java/org/sonar/core/technicaldebt/db/CharacteristicDaoTest.java index d4647c2b403..0379af02c46 100644 --- a/sonar-core/src/test/java/org/sonar/core/technicaldebt/db/CharacteristicDaoTest.java +++ b/sonar-core/src/test/java/org/sonar/core/technicaldebt/db/CharacteristicDaoTest.java @@ -27,6 +27,7 @@ import org.sonar.core.persistence.AbstractDaoTestCase; import java.util.List; +import static com.google.common.collect.Lists.newArrayList; import static org.fest.assertions.Assertions.assertThat; public class CharacteristicDaoTest extends AbstractDaoTestCase { @@ -110,6 +111,17 @@ public class CharacteristicDaoTest extends AbstractDaoTestCase { } @Test + public void select_characteristics_by_ids() { + setupData("shared"); + + assertThat(dao.selectCharacteristicsByIds(newArrayList(1, 2))).hasSize(2); + assertThat(dao.selectCharacteristicsByIds(newArrayList(1))).hasSize(1); + + // Disabled characteristics are not returned + assertThat(dao.selectCharacteristicsByIds(newArrayList(4, 5))).isEmpty(); + } + + @Test public void select_characteristic_by_key() { setupData("shared"); diff --git a/sonar-core/src/test/resources/org/sonar/core/rule/RuleDaoTest/selectParamsForRule.xml b/sonar-core/src/test/resources/org/sonar/core/rule/RuleDaoTest/select_parameters_by_rule_id.xml index 3b21b8a8ae9..3b21b8a8ae9 100644 --- a/sonar-core/src/test/resources/org/sonar/core/rule/RuleDaoTest/selectParamsForRule.xml +++ b/sonar-core/src/test/resources/org/sonar/core/rule/RuleDaoTest/select_parameters_by_rule_id.xml diff --git a/sonar-core/src/test/resources/org/sonar/core/rule/RuleDaoTest/select_parameters_by_rule_ids.xml b/sonar-core/src/test/resources/org/sonar/core/rule/RuleDaoTest/select_parameters_by_rule_ids.xml new file mode 100644 index 00000000000..5d840d5998d --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/rule/RuleDaoTest/select_parameters_by_rule_ids.xml @@ -0,0 +1,7 @@ +<dataset> + + <rules_parameters id="1" rule_id="1" name="myParameter" param_type="plop" default_value="plouf" description="My Parameter"/> + + <rules_parameters id="2" rule_id="2" name="otherParam" param_type="plop" default_value="plouf" description="Other Parameter"/> + +</dataset> diff --git a/sonar-core/src/test/resources/org/sonar/core/rule/RuleDaoTest/select_tags_by_rule_id.xml b/sonar-core/src/test/resources/org/sonar/core/rule/RuleDaoTest/select_tags_by_rule_id.xml new file mode 100644 index 00000000000..8d252e540b4 --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/rule/RuleDaoTest/select_tags_by_rule_id.xml @@ -0,0 +1,11 @@ +<dataset> + + <rule_tags id="3" tag="tag1"/> + <rule_tags id="4" tag="tag3"/> + <rule_tags id="5" tag="tag5"/> + + <rules_rule_tags id="3" rule_id="3" rule_tag_id="3" tag_type="SYSTEM"/> + <rules_rule_tags id="4" rule_id="3" rule_tag_id="4" tag_type="SYSTEM"/> + <rules_rule_tags id="5" rule_id="4" rule_tag_id="5" tag_type="SYSTEM"/> + +</dataset> diff --git a/sonar-core/src/test/resources/org/sonar/core/rule/RuleDaoTest/select_tags_by_rule_ids.xml b/sonar-core/src/test/resources/org/sonar/core/rule/RuleDaoTest/select_tags_by_rule_ids.xml new file mode 100644 index 00000000000..8d252e540b4 --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/rule/RuleDaoTest/select_tags_by_rule_ids.xml @@ -0,0 +1,11 @@ +<dataset> + + <rule_tags id="3" tag="tag1"/> + <rule_tags id="4" tag="tag3"/> + <rule_tags id="5" tag="tag5"/> + + <rules_rule_tags id="3" rule_id="3" rule_tag_id="3" tag_type="SYSTEM"/> + <rules_rule_tags id="4" rule_id="3" rule_tag_id="4" tag_type="SYSTEM"/> + <rules_rule_tags id="5" rule_id="4" rule_tag_id="5" tag_type="SYSTEM"/> + +</dataset> |