diff options
author | Jean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com> | 2014-01-15 16:53:02 +0100 |
---|---|---|
committer | Jean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com> | 2014-01-16 18:53:12 +0100 |
commit | a6df51b599c940bd8f50a9c2cf8031d3fc4c11c5 (patch) | |
tree | ec7d1741651b14dd1fd5b12e6d3e79a6df677a8e /sonar-core | |
parent | 2713ed5e8afdaa59d81a2bc4288fc360008c69b2 (diff) | |
download | sonarqube-a6df51b599c940bd8f50a9c2cf8031d3fc4c11c5.tar.gz sonarqube-a6df51b599c940bd8f50a9c2cf8031d3fc4c11c5.zip |
SONAR-4326 Refactor tag management introducing relation table
Diffstat (limited to 'sonar-core')
12 files changed, 257 insertions, 43 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/DaoUtils.java b/sonar-core/src/main/java/org/sonar/core/persistence/DaoUtils.java index ada0107b857..b230b87f68f 100644 --- a/sonar-core/src/main/java/org/sonar/core/persistence/DaoUtils.java +++ b/sonar-core/src/main/java/org/sonar/core/persistence/DaoUtils.java @@ -19,6 +19,8 @@ */ package org.sonar.core.persistence; +import org.sonar.core.rule.RuleTagDao; + import com.google.common.collect.ImmutableList; import org.sonar.core.dashboard.ActiveDashboardDao; import org.sonar.core.dashboard.DashboardDao; @@ -81,6 +83,7 @@ public final class DaoUtils { ResourceKeyUpdaterDao.class, RoleDao.class, RuleDao.class, + RuleTagDao.class, SemaphoreDao.class, SnapshotDataDao.class, UserDao.class diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/DatabaseVersion.java b/sonar-core/src/main/java/org/sonar/core/persistence/DatabaseVersion.java index 1a518f9f8b9..3c49b2584a8 100644 --- a/sonar-core/src/main/java/org/sonar/core/persistence/DatabaseVersion.java +++ b/sonar-core/src/main/java/org/sonar/core/persistence/DatabaseVersion.java @@ -85,6 +85,7 @@ public class DatabaseVersion implements BatchComponent, ServerComponent { "rules_parameters", "rules_profiles", "rule_tags", + "rules_rule_tags", "semaphores", "schema_migrations", "snapshots", diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/MyBatis.java b/sonar-core/src/main/java/org/sonar/core/persistence/MyBatis.java index 5eb84467609..10fdcb3cb16 100644 --- a/sonar-core/src/main/java/org/sonar/core/persistence/MyBatis.java +++ b/sonar-core/src/main/java/org/sonar/core/persistence/MyBatis.java @@ -58,10 +58,7 @@ import org.sonar.core.purge.PurgeMapper; import org.sonar.core.purge.PurgeableSnapshotDto; import org.sonar.core.qualityprofile.db.*; import org.sonar.core.resource.*; -import org.sonar.core.rule.RuleDto; -import org.sonar.core.rule.RuleMapper; -import org.sonar.core.rule.RuleParamDto; -import org.sonar.core.rule.RuleTagDto; +import org.sonar.core.rule.*; import org.sonar.core.source.jdbc.SnapshotDataDto; import org.sonar.core.source.jdbc.SnapshotDataMapper; import org.sonar.core.source.jdbc.SnapshotSourceMapper; @@ -119,6 +116,7 @@ public class MyBatis implements BatchComponent, ServerComponent { loadAlias(conf, "Rule", RuleDto.class); loadAlias(conf, "RuleParam", RuleParamDto.class); loadAlias(conf, "RuleTag", RuleTagDto.class); + loadAlias(conf, "RuleRuleTag", RuleRuleTagDto.class); loadAlias(conf, "Snapshot", SnapshotDto.class); loadAlias(conf, "Semaphore", SemaphoreDto.class); loadAlias(conf, "SchemaMigration", SchemaMigrationDto.class); @@ -158,7 +156,7 @@ public class MyBatis implements BatchComponent, ServerComponent { ResourceKeyUpdaterMapper.class, ResourceIndexerMapper.class, ResourceSnapshotMapper.class, RoleMapper.class, RuleMapper.class, SchemaMigrationMapper.class, SemaphoreMapper.class, UserMapper.class, WidgetMapper.class, WidgetPropertyMapper.class, MeasureMapper.class, SnapshotDataMapper.class, SnapshotSourceMapper.class, ActionPlanMapper.class, ActionPlanStatsMapper.class, - NotificationQueueMapper.class, CharacteristicMapper.class, + NotificationQueueMapper.class, CharacteristicMapper.class, RuleTagMapper.class, GroupMembershipMapper.class, QualityProfileMapper.class, ActiveRuleMapper.class }; loadMappers(conf, mappers); 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 ee0571450d3..c3f931249b6 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 @@ -176,11 +176,11 @@ public class RuleDao implements BatchComponent, ServerComponent { return session.getMapper(RuleMapper.class); } - public List<RuleTagDto> selectTags(SqlSession session) { + public List<RuleRuleTagDto> selectTags(SqlSession session) { return getMapper(session).selectAllTags(); } - public void insert(RuleTagDto newTag, SqlSession session) { + public void insert(RuleRuleTagDto newTag, SqlSession session) { getMapper(session).insertTag(newTag); } @@ -188,11 +188,15 @@ public class RuleDao implements BatchComponent, ServerComponent { getMapper(sqlSession).deleteParameter(persistedParam.getId()); } - public void deleteTag(RuleTagDto tagToDelete, SqlSession session) { + public void deleteTag(RuleRuleTagDto tagToDelete, SqlSession session) { getMapper(session).deleteTag(tagToDelete.getId().intValue()); } - public List<RuleTagDto> selectTags(Integer id) { + public void update(RuleRuleTagDto existingTag, SqlSession session) { + getMapper(session).updateTag(existingTag); + } + + public List<RuleRuleTagDto> selectTags(Integer id) { SqlSession session = mybatis.openSession(); try { return selectTags(id, session); @@ -201,7 +205,7 @@ public class RuleDao implements BatchComponent, ServerComponent { } } - public List<RuleTagDto> selectTags(Integer id, SqlSession session) { + public List<RuleRuleTagDto> selectTags(Integer id, SqlSession session) { return getMapper(session).selectTagsForRule(id); } } 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 98062661015..c3b62c5af75 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 @@ -50,11 +50,13 @@ public interface RuleMapper { void deleteParameter(Integer paramId); - List<RuleTagDto> selectAllTags(); + List<RuleRuleTagDto> selectAllTags(); - void insertTag(RuleTagDto newTag); + void insertTag(RuleRuleTagDto newTag); void deleteTag(Integer tagId); - List<RuleTagDto> selectTagsForRule(Integer ruleId); + void updateTag(RuleRuleTagDto existingTag); + + List<RuleRuleTagDto> selectTagsForRule(Integer ruleId); } diff --git a/sonar-core/src/main/java/org/sonar/core/rule/RuleRuleTagDto.java b/sonar-core/src/main/java/org/sonar/core/rule/RuleRuleTagDto.java new file mode 100644 index 00000000000..17f518b8c3f --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/core/rule/RuleRuleTagDto.java @@ -0,0 +1,81 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.core.rule; + + +public class RuleRuleTagDto { + + private Long id; + private Integer ruleId; + private Long tagId; + private String tag; + private String type; + + public Long getId() { + return id; + } + + public RuleRuleTagDto setId(Long id) { + this.id = id; + return this; + } + + public Integer getRuleId() { + return ruleId; + } + + public RuleRuleTagDto setRuleId(Integer ruleId) { + this.ruleId = ruleId; + return this; + } + + public Long getTagId() { + return tagId; + } + + public RuleRuleTagDto setTagId(Long tagId) { + this.tagId = tagId; + return this; + } + + public String getTag() { + return tag; + } + + public RuleRuleTagDto setTag(String tag) { + this.tag = tag; + return this; + } + + public RuleTagType getType() { + return RuleTagType.valueOf(type); + } + + public RuleRuleTagDto setType(RuleTagType type) { + this.type = type.name(); + return this; + } + + @Override + public String toString() { + return String.format("RuleRuleTag[id=%d, ruleId=%d, tagId=%d, tag=%s, type=%s]", + id, ruleId, tagId, tag, type); + } +} diff --git a/sonar-core/src/main/java/org/sonar/core/rule/RuleTagDao.java b/sonar-core/src/main/java/org/sonar/core/rule/RuleTagDao.java new file mode 100644 index 00000000000..7e5696ef77f --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/core/rule/RuleTagDao.java @@ -0,0 +1,74 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.core.rule; + +import org.sonar.core.persistence.MyBatis; +import org.sonar.api.ServerExtension; +import org.apache.ibatis.session.SqlSession; + +import java.util.List; + +public class RuleTagDao implements ServerExtension { + + private final MyBatis myBatis; + + public RuleTagDao(MyBatis myBatis) { + this.myBatis = myBatis; + } + + public List<RuleTagDto> selectAll() { + SqlSession session = myBatis.openSession(); + try { + return selectAll(session); + } finally { + MyBatis.closeQuietly(session); + } + } + + public List<RuleTagDto> selectAll(SqlSession session) { + return session.getMapper(RuleTagMapper.class).selectAll(); + } + + public void insert(RuleTagDto newRuleTag) { + SqlSession session = myBatis.openSession(); + try { + insert(newRuleTag, session); + } finally { + MyBatis.closeQuietly(session); + } + } + + public void insert(RuleTagDto newRuleTag, SqlSession session) { + session.getMapper(RuleTagMapper.class).insert(newRuleTag); + } + + public void delete(Long tagId) { + SqlSession session = myBatis.openSession(); + try { + delete(tagId, session); + } finally { + MyBatis.closeQuietly(session); + } + } + + public void delete(long tagId, SqlSession session) { + session.getMapper(RuleTagMapper.class).delete(tagId); + } +} diff --git a/sonar-core/src/main/java/org/sonar/core/rule/RuleTagDto.java b/sonar-core/src/main/java/org/sonar/core/rule/RuleTagDto.java index 9ded402613d..ffb7a927934 100644 --- a/sonar-core/src/main/java/org/sonar/core/rule/RuleTagDto.java +++ b/sonar-core/src/main/java/org/sonar/core/rule/RuleTagDto.java @@ -19,12 +19,11 @@ */ package org.sonar.core.rule; + public class RuleTagDto { private Long id; - private Integer ruleId; private String tag; - private String type; public Long getId() { return id; @@ -35,15 +34,6 @@ public class RuleTagDto { return this; } - public Integer getRuleId() { - return ruleId; - } - - public RuleTagDto setRuleId(Integer ruleId) { - this.ruleId = ruleId; - return this; - } - public String getTag() { return tag; } @@ -53,12 +43,8 @@ public class RuleTagDto { return this; } - public RuleTagType getType() { - return RuleTagType.valueOf(type); - } - - public RuleTagDto setType(RuleTagType type) { - this.type = type.toString(); - return this; + @Override + public String toString() { + return String.format("RuleTag[id=%d, tag=%s]", id, tag); } } diff --git a/sonar-core/src/main/java/org/sonar/core/rule/RuleTagMapper.java b/sonar-core/src/main/java/org/sonar/core/rule/RuleTagMapper.java new file mode 100644 index 00000000000..656313007af --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/core/rule/RuleTagMapper.java @@ -0,0 +1,31 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.core.rule; + +import java.util.List; + +public interface RuleTagMapper { + + List<RuleTagDto> selectAll(); + + void insert(RuleTagDto newTag); + + void delete(Long tagId); +} diff --git a/sonar-core/src/main/resources/org/sonar/core/persistence/schema-h2.ddl b/sonar-core/src/main/resources/org/sonar/core/persistence/schema-h2.ddl index 091220084ca..f250e0019b9 100644 --- a/sonar-core/src/main/resources/org/sonar/core/persistence/schema-h2.ddl +++ b/sonar-core/src/main/resources/org/sonar/core/persistence/schema-h2.ddl @@ -161,8 +161,13 @@ CREATE TABLE "RULES" ( CREATE TABLE "RULE_TAGS" ( "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), + "TAG" VARCHAR(100) NOT NULL +); + +CREATE TABLE "RULES_RULE_TAGS" ( + "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), "RULE_ID" INTEGER NOT NULL, - "TAG" VARCHAR(100) NOT NULL, + "RULE_TAG_ID" INTEGER NOT NULL, "TAG_TYPE" VARCHAR (20) NOT NULL ); @@ -705,6 +710,6 @@ CREATE UNIQUE INDEX "GROUP_ROLES_UNIQUE" ON "GROUP_ROLES" ("GROUP_ID", "RESOURCE CREATE UNIQUE INDEX "RULES_PLUGIN_KEY_AND_NAME" ON "RULES" ("PLUGIN_RULE_KEY", "PLUGIN_NAME"); -CREATE UNIQUE INDEX "RULE_TAGS_UNIQUE" ON "RULE_TAGS" ("RULE_ID", "TAG"); +CREATE UNIQUE INDEX "RULE_TAGS_UNIQUE" ON "RULES_RULE_TAGS" ("RULE_ID", "RULE_TAG_ID"); CREATE INDEX "CHARACTERISTICS_ENABLED" ON "CHARACTERISTICS" ("ENABLED"); 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 c9ce9358e2f..95b931b3bf2 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 @@ -124,27 +124,35 @@ </update> <sql id="tagColumns"> - id, rule_id as "ruleId", tag, tag_type as "type" + rrt.id, rrt.rule_id as "ruleId", rrt.rule_tag_id as "tagId", rrt.tag_type as "type", rt.tag </sql> - <select id="selectAllTags" resultType="RuleTag"> + <select id="selectAllTags" resultType="RuleRuleTag"> SELECT <include refid="tagColumns"/> - FROM rule_tags + FROM rules_rule_tags rrt + JOIN rule_tags rt ON rrt.rule_tag_id = rt.id </select> - <select id="selectTagsForRule" resultType="RuleTag"> + <select id="selectTagsForRule" resultType="RuleRuleTag"> select <include refid="tagColumns"/> - from rule_tags - where rule_id=#{id} + FROM rules_rule_tags rrt + JOIN rule_tags rt ON rrt.rule_tag_id = rt.id + WHERE rrt.rule_id=#{id} </select> - <insert id="insertTag" parameterType="RuleTag" keyColumn="id" useGeneratedKeys="true" keyProperty="id"> - INSERT INTO rule_tags (rule_id, tag, tag_type) - VALUES (#{ruleId}, #{tag}, #{type}) + <insert id="insertTag" parameterType="RuleRuleTag" keyColumn="id" useGeneratedKeys="true" keyProperty="id"> + INSERT INTO rules_rule_tags (rule_id, rule_tag_id, tag_type) + VALUES (#{ruleId}, #{tagId}, #{type}) </insert> + <update id="updateTag" parameterType="RuleRuleTag"> + UPDATE rules_rule_tags + SET tag_type=#{type} + WHERE id=#{id} + </update> + <update id="deleteTag" parameterType="Integer"> - DELETE FROM rule_tags WHERE id=#{tagId} + DELETE FROM rules_rule_tags WHERE id=#{tagId} </update> </mapper> diff --git a/sonar-core/src/main/resources/org/sonar/core/rule/RuleTagMapper.xml b/sonar-core/src/main/resources/org/sonar/core/rule/RuleTagMapper.xml new file mode 100644 index 00000000000..e2c8b16fe52 --- /dev/null +++ b/sonar-core/src/main/resources/org/sonar/core/rule/RuleTagMapper.xml @@ -0,0 +1,21 @@ +<?xml version="1.0" encoding="UTF-8" ?> +<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> + +<mapper namespace="org.sonar.core.rule.RuleTagMapper"> + + <select id="selectAll" resultType="RuleTag"> + SELECT id, tag + FROM rule_tags + </select> + + <insert id="insert" parameterType="RuleTag" keyColumn="id" useGeneratedKeys="true" keyProperty="id"> + INSERT INTO rule_tags (tag) + VALUES (#{tag}) + </insert> + + <update id="delete" parameterType="Integer"> + DELETE FROM rule_tags WHERE id=#{tagId} + </update> + +</mapper> + |