aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-core
diff options
context:
space:
mode:
authorJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>2015-03-23 18:18:18 +0100
committerJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>2015-03-26 14:51:08 +0100
commit2bc5c0c7689068f5403d474901d264458afa036a (patch)
tree331d259a2d890dbbd49ed35232737bbdd937812b /sonar-core
parenta61164dda70bcfa11054c726d6b6ef160841aaae (diff)
downloadsonarqube-2bc5c0c7689068f5403d474901d264458afa036a.tar.gz
sonarqube-2bc5c0c7689068f5403d474901d264458afa036a.zip
SONAR-6329 Update handling of project/profile association in service layer and DAOs
Diffstat (limited to 'sonar-core')
-rw-r--r--sonar-core/src/main/java/org/sonar/core/qualityprofile/db/QualityProfileDao.java34
-rw-r--r--sonar-core/src/main/java/org/sonar/core/qualityprofile/db/QualityProfileDto.java11
-rw-r--r--sonar-core/src/main/java/org/sonar/core/qualityprofile/db/QualityProfileMapper.java16
-rw-r--r--sonar-core/src/main/resources/org/sonar/core/qualityprofile/db/QualityProfileMapper.xml57
-rw-r--r--sonar-core/src/test/java/org/sonar/core/qualityprofile/db/QualityProfileDaoTest.java15
-rw-r--r--sonar-core/src/test/resources/org/sonar/core/qualityprofile/db/QualityProfileDaoTest/insert-result.xml2
-rw-r--r--sonar-core/src/test/resources/org/sonar/core/qualityprofile/db/QualityProfileDaoTest/projects.xml17
-rw-r--r--sonar-core/src/test/resources/org/sonar/core/qualityprofile/db/QualityProfileDaoTest/shared.xml5
8 files changed, 94 insertions, 63 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/QualityProfileDao.java b/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/QualityProfileDao.java
index 01244ef1ae2..219b2147990 100644
--- a/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/QualityProfileDao.java
+++ b/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/QualityProfileDao.java
@@ -170,7 +170,7 @@ public class QualityProfileDao implements ServerComponent, DaoComponent {
@CheckForNull
public QualityProfileDto getDefaultProfile(String language, DbSession session) {
- return session.getMapper(QualityProfileMapper.class).selectDefaultProfile(language, String.format("sonar.profile.%s", language));
+ return session.getMapper(QualityProfileMapper.class).selectDefaultProfile(language);
}
@CheckForNull
@@ -184,18 +184,18 @@ public class QualityProfileDao implements ServerComponent, DaoComponent {
}
@CheckForNull
- public QualityProfileDto getByProjectAndLanguage(long projectId, String language, String key) {
+ public QualityProfileDto getByProjectAndLanguage(long projectId, String language) {
DbSession session = mybatis.openSession(false);
try {
- return session.getMapper(QualityProfileMapper.class).selectByProjectIdAndLanguage(projectId, language, key);
+ return session.getMapper(QualityProfileMapper.class).selectByProjectIdAndLanguage(projectId, language);
} finally {
MyBatis.closeQuietly(session);
}
}
@CheckForNull
- public QualityProfileDto getByProjectAndLanguage(String projectKey, String language, String propertyKeyPrefix, DbSession session) {
- return session.getMapper(QualityProfileMapper.class).selectByProjectAndLanguage(projectKey, language, propertyKeyPrefix);
+ public QualityProfileDto getByProjectAndLanguage(String projectKey, String language, DbSession session) {
+ return session.getMapper(QualityProfileMapper.class).selectByProjectAndLanguage(projectKey, language);
}
public List<QualityProfileDto> findByLanguage(String language) {
@@ -297,25 +297,37 @@ public class QualityProfileDao implements ServerComponent, DaoComponent {
}
}
- public List<ComponentDto> selectProjects(String propertyKey, String propertyValue) {
+ public List<ComponentDto> selectProjects(String profileName, String language) {
DbSession session = mybatis.openSession(false);
try {
- return selectProjects(propertyKey, propertyValue, session);
+ return selectProjects(profileName, language, session);
} finally {
MyBatis.closeQuietly(session);
}
}
- public List<ComponentDto> selectProjects(String propertyKey, String propertyValue, DbSession session) {
- return session.getMapper(QualityProfileMapper.class).selectProjects(propertyKey, propertyValue);
+ public List<ComponentDto> selectProjects(String profileName, String language, DbSession session) {
+ return session.getMapper(QualityProfileMapper.class).selectProjects(profileName, language);
}
- public int countProjects(String propertyKey, String propertyValue) {
+ public int countProjects(String profileName, String language) {
DbSession session = mybatis.openSession(false);
try {
- return session.getMapper(QualityProfileMapper.class).countProjects(propertyKey, propertyValue);
+ return session.getMapper(QualityProfileMapper.class).countProjects(profileName, language);
} finally {
MyBatis.closeQuietly(session);
}
}
+
+ public void insertProjectProfileAssociation(String projectUuid, String profileKey, DbSession session) {
+ session.getMapper(QualityProfileMapper.class).insertProjectProfileAssociation(projectUuid, profileKey);
+ }
+
+ public void deleteProjectProfileAssociation(String projectUuid, String profileKey, DbSession session) {
+ session.getMapper(QualityProfileMapper.class).deleteProjectProfileAssociation(projectUuid, profileKey);
+ }
+
+ public void deleteAllProjectProfileAssociation(String profileKey, DbSession session) {
+ session.getMapper(QualityProfileMapper.class).deleteAllProjectProfileAssociation(profileKey);
+ }
}
diff --git a/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/QualityProfileDto.java b/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/QualityProfileDto.java
index b11695a519a..ae43582e405 100644
--- a/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/QualityProfileDto.java
+++ b/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/QualityProfileDto.java
@@ -25,6 +25,7 @@ import org.sonar.core.persistence.Dto;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
+
import java.util.Date;
public class QualityProfileDto extends Dto<String> {
@@ -35,6 +36,7 @@ public class QualityProfileDto extends Dto<String> {
private String language;
private String parentKee;
private String rulesUpdatedAt;
+ private boolean isDefault;
/**
* @deprecated use {@link #createFor(String)}
@@ -113,6 +115,15 @@ public class QualityProfileDto extends Dto<String> {
return this;
}
+ public boolean isDefault() {
+ return isDefault;
+ }
+
+ public QualityProfileDto setDefault(boolean isDefault) {
+ this.isDefault = isDefault;
+ return this;
+ }
+
public static QualityProfileDto createFor(String key) {
return new QualityProfileDto().setKee(key);
}
diff --git a/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/QualityProfileMapper.java b/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/QualityProfileMapper.java
index 3ba36880451..2dfc8aa9542 100644
--- a/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/QualityProfileMapper.java
+++ b/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/QualityProfileMapper.java
@@ -38,7 +38,7 @@ public interface QualityProfileMapper {
List<QualityProfileDto> selectAll();
@CheckForNull
- QualityProfileDto selectDefaultProfile(@Param("language") String language, @Param("propKey") String propKey);
+ QualityProfileDto selectDefaultProfile(@Param("language") String language);
@CheckForNull
QualityProfileDto selectByNameAndLanguage(@Param("name") String name, @Param("language") String language);
@@ -63,11 +63,17 @@ public interface QualityProfileMapper {
// PROJECTS
- List<ComponentDto> selectProjects(@Param("value") String propertyValue, @Param("key") String propertyKey);
+ List<ComponentDto> selectProjects(@Param("profileName") String profileName, @Param("language") String language);
- int countProjects(@Param("value") String propertyValue, @Param("key") String propertyKey);
+ int countProjects(@Param("profileName") String profileName, @Param("language") String language);
- QualityProfileDto selectByProjectIdAndLanguage(@Param("projectId") Long projectId, @Param("language") String language, @Param("key") String propertyKeyPrefix);
+ QualityProfileDto selectByProjectIdAndLanguage(@Param("projectId") Long projectId, @Param("language") String language);
- QualityProfileDto selectByProjectAndLanguage(@Param("projectKey") String projectKey, @Param("language") String language, @Param("propertyKey") String propertyKeyPrefix);
+ QualityProfileDto selectByProjectAndLanguage(@Param("projectKey") String projectKey, @Param("language") String language);
+
+ void insertProjectProfileAssociation(@Param("projectUuid") String projectUuid, @Param("profileKey") String profileKey);
+
+ void deleteProjectProfileAssociation(@Param("projectUuid") String projectUuid, @Param("profileKey") String profileKey);
+
+ void deleteAllProjectProfileAssociation(@Param("profileKey") String profileKey);
}
diff --git a/sonar-core/src/main/resources/org/sonar/core/qualityprofile/db/QualityProfileMapper.xml b/sonar-core/src/main/resources/org/sonar/core/qualityprofile/db/QualityProfileMapper.xml
index b8a6ea50889..8d76a86144f 100644
--- a/sonar-core/src/main/resources/org/sonar/core/qualityprofile/db/QualityProfileMapper.xml
+++ b/sonar-core/src/main/resources/org/sonar/core/qualityprofile/db/QualityProfileMapper.xml
@@ -9,20 +9,22 @@
p.name as name,
p.language as language,
p.parent_kee as parentKee,
+ p.is_default as isDefault,
p.created_at as createdAt,
p.updated_at as updatedAt,
p.rules_updated_at as rulesUpdatedAt
</sql>
<insert id="insert" parameterType="QualityProfile" keyColumn="id" useGeneratedKeys="true" keyProperty="id" >
- INSERT INTO rules_profiles (kee, parent_kee, name, language, created_at, updated_at, rules_updated_at)
- VALUES (#{kee}, #{parentKee}, #{name}, #{language}, #{createdAt}, #{updatedAt}, #{rulesUpdatedAt,})
+ INSERT INTO rules_profiles (kee, parent_kee, name, language, is_default, created_at, updated_at, rules_updated_at)
+ VALUES (#{kee}, #{parentKee}, #{name}, #{language}, #{isDefault}, #{createdAt}, #{updatedAt}, #{rulesUpdatedAt,})
</insert>
<update id="update" parameterType="QualityProfile" >
UPDATE rules_profiles SET
name=#{name},
language=#{language},
+ is_default=#{isDefault},
parent_kee=#{parentKee},
updated_at=#{updatedAt},
rules_updated_at=#{rulesUpdatedAt}
@@ -86,54 +88,61 @@
<select id="selectDefaultProfile" parameterType="map" resultType="QualityProfile">
SELECT <include refid="profilesColumns"/>
FROM rules_profiles p
- INNER JOIN properties prop ON prop.prop_key=#{propKey}
- AND prop.resource_id IS NULL
- AND prop.text_value LIKE p.name
+ WHERE p.is_default=${_true}
AND p.language=#{language}
</select>
<select id="selectProjects" parameterType="Integer" resultType="Component">
- SELECT projects.id as id, projects.name as name, projects.kee as kee
+ SELECT projects.id as id, projects.name as name, projects.kee as kee, projects.uuid as uuid
FROM projects projects
- LEFT JOIN properties ON properties.resource_id = projects.id
+ JOIN project_profiles pp ON pp.project_uuid = projects.uuid
+ JOIN rules_profiles prof ON pp.profile_key = prof.kee
<where>
- AND properties.resource_id IS NOT NULL
- AND properties.prop_key=#{key}
- AND properties.text_value LIKE #{value}
+ AND prof.name = #{profileName}
+ AND prof.language = #{language}
</where>
</select>
<select id="countProjects" parameterType="Integer" resultType="Integer">
SELECT count(projects.id)
FROM projects projects
- LEFT JOIN properties ON properties.resource_id = projects.id
+ JOIN project_profiles pp ON pp.project_uuid=projects.uuid
+ JOIN rules_profiles prof ON pp.profile_key=prof.kee
<where>
- AND properties.resource_id IS NOT NULL
- AND properties.prop_key=#{key}
- AND properties.text_value LIKE #{value}
+ AND prof.language=#{language}
+ AND prof.name=#{profileName}
</where>
</select>
<select id="selectByProjectIdAndLanguage" parameterType="map" resultType="QualityProfile">
SELECT <include refid="profilesColumns"/>
FROM rules_profiles p
- INNER JOIN properties prop ON prop.resource_id=#{projectId}
- AND prop.prop_key LIKE #{key}
- AND prop.text_value LIKE p.name
+ JOIN project_profiles pp ON pp.profile_key=p.kee
+ JOIN projects project ON pp.project_uuid=project.uuid
+ AND project.id=#{projectId}
WHERE p.language=#{language}
</select>
<select id="selectByProjectAndLanguage" parameterType="map" resultType="QualityProfile">
SELECT <include refid="profilesColumns"/>
- FROM rules_profiles p, projects project
- INNER JOIN properties prop ON prop.resource_id=project.id
- <where>
- AND p.language=#{language}
- AND prop.prop_key LIKE #{propertyKey}
- AND prop.text_value LIKE p.name
+ FROM rules_profiles p
+ JOIN project_profiles pp ON pp.profile_key=p.kee
+ JOIN projects project ON pp.project_uuid=project.uuid
AND project.kee=#{projectKey}
- </where>
+ WHERE p.language=#{language}
</select>
+ <insert id="insertProjectProfileAssociation" keyColumn="id" useGeneratedKeys="true">
+ INSERT INTO project_profiles (project_uuid, profile_key) VALUES (#{projectUuid}, #{profileKey})
+ </insert>
+
+ <update id="deleteProjectProfileAssociation">
+ DELETE FROM project_profiles WHERE project_uuid=#{projectUuid} AND profile_key=#{profileKey}
+ </update>
+
+ <update id="deleteAllProjectProfileAssociation">
+ DELETE FROM project_profiles WHERE profile_key=#{profileKey}
+ </update>
+
</mapper>
diff --git a/sonar-core/src/test/java/org/sonar/core/qualityprofile/db/QualityProfileDaoTest.java b/sonar-core/src/test/java/org/sonar/core/qualityprofile/db/QualityProfileDaoTest.java
index ea836070470..7518834fdd7 100644
--- a/sonar-core/src/test/java/org/sonar/core/qualityprofile/db/QualityProfileDaoTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/qualityprofile/db/QualityProfileDaoTest.java
@@ -73,7 +73,8 @@ public class QualityProfileDaoTest extends AbstractDaoTestCase {
.setId(1)
.setName("New Name")
.setLanguage("js")
- .setParentKee("fghij");
+ .setParentKee("fghij")
+ .setDefault(false);
dao.update(dto);
@@ -214,21 +215,21 @@ public class QualityProfileDaoTest extends AbstractDaoTestCase {
public void select_projects() {
setupData("projects");
- assertThat(dao.selectProjects("Sonar Way", "sonar.profile.java")).hasSize(2);
+ assertThat(dao.selectProjects("Sonar Way", "java")).hasSize(2);
}
@Test
public void count_projects() {
setupData("projects");
- assertThat(dao.countProjects("Sonar Way", "sonar.profile.java")).isEqualTo(2);
+ assertThat(dao.countProjects("Sonar Way", "java")).isEqualTo(2);
}
@Test
public void select_by_project_id_and_language() {
setupData("projects");
- QualityProfileDto dto = dao.getByProjectAndLanguage(1L, "java", "sonar.profile.java");
+ QualityProfileDto dto = dao.getByProjectAndLanguage(1L, "java");
assertThat(dto.getId()).isEqualTo(1);
}
@@ -236,10 +237,10 @@ public class QualityProfileDaoTest extends AbstractDaoTestCase {
public void select_by_project_key_and_language() {
setupData("projects");
- QualityProfileDto dto = dao.getByProjectAndLanguage("org.codehaus.sonar:sonar", "java", "sonar.profile.java", session);
+ QualityProfileDto dto = dao.getByProjectAndLanguage("org.codehaus.sonar:sonar", "java", session);
assertThat(dto.getId()).isEqualTo(1);
- assertThat(dao.getByProjectAndLanguage("org.codehaus.sonar:sonar", "unkown", "sonar.profile.java", session)).isNull();
- assertThat(dao.getByProjectAndLanguage("unknown", "java", "sonar.profile.java", session)).isNull();
+ assertThat(dao.getByProjectAndLanguage("org.codehaus.sonar:sonar", "unkown", session)).isNull();
+ assertThat(dao.getByProjectAndLanguage("unknown", "java", session)).isNull();
}
}
diff --git a/sonar-core/src/test/resources/org/sonar/core/qualityprofile/db/QualityProfileDaoTest/insert-result.xml b/sonar-core/src/test/resources/org/sonar/core/qualityprofile/db/QualityProfileDaoTest/insert-result.xml
index 90f86af8a4a..8a9e9ce1ada 100644
--- a/sonar-core/src/test/resources/org/sonar/core/qualityprofile/db/QualityProfileDaoTest/insert-result.xml
+++ b/sonar-core/src/test/resources/org/sonar/core/qualityprofile/db/QualityProfileDaoTest/insert-result.xml
@@ -1,6 +1,6 @@
<dataset>
- <rules_profiles id="1" name="Sonar Way" language="java" parent_kee="[null]" kee="java_sonar_way" is_default="[false]"
+ <rules_profiles id="1" name="Sonar Way" language="java" parent_kee="[null]" kee="java_sonar_way" is_default="[true]"
rules_updated_at="[null]" created_at="[null]" updated_at="[null]"/>
<rules_profiles id="2" name="Sonar Way" language="js" parent_kee="[null]" kee="js_sonar_way" is_default="[false]"
diff --git a/sonar-core/src/test/resources/org/sonar/core/qualityprofile/db/QualityProfileDaoTest/projects.xml b/sonar-core/src/test/resources/org/sonar/core/qualityprofile/db/QualityProfileDaoTest/projects.xml
index 5fe60e17f66..5cc32bfefad 100644
--- a/sonar-core/src/test/resources/org/sonar/core/qualityprofile/db/QualityProfileDaoTest/projects.xml
+++ b/sonar-core/src/test/resources/org/sonar/core/qualityprofile/db/QualityProfileDaoTest/projects.xml
@@ -1,21 +1,16 @@
<dataset>
- <rules_profiles id="1" name="Sonar Way" language="java" parent_kee="[null]" kee="java_sonar_way" is_default="[false]"
+ <rules_profiles id="1" name="Sonar Way" language="java" parent_kee="[null]" kee="java_sonar_way" is_default="[true]"
rules_updated_at="[null]" created_at="[null]" updated_at="[null]"/>
- <rules_profiles id="2" name="Sonar Way" language="js" parent_kee="[null]" kee="js_sonar_way" is_default="[false]"
+ <rules_profiles id="2" name="Sonar Way" language="js" parent_kee="[null]" kee="js_sonar_way" is_default="[true]"
rules_updated_at="[null]" created_at="[null]" updated_at="[null]"/>
<projects id="1" uuid="A" kee="org.codehaus.sonar:sonar" name="SonarQube"/>
<projects id="2" uuid="B" kee="org.codehaus.sonar-plugins.java:java" name="SonarQube Java"/>
- <properties id="1" prop_key="sonar.profile.java" text_value="Sonar Way" resource_id="1"/>
- <properties id="2" prop_key="sonar.profile.java" text_value="Sonar Way" resource_id="2"/>
-
- <!-- Property used to know the default profile, should not be returned when searching for projects -->
- <properties id="3" prop_key="sonar.profile.java" text_value="Sonar Way" resource_id="[null]"/>
-
- <properties id="4" prop_key="sonar.profile.js" text_value="Sonar Way" resource_id="1"/>
- <properties id="5" prop_key="sonar.profile.js" text_value="Sonar Way" resource_id="2"/>
- <properties id="6" prop_key="sonar.profile.js" text_value="Sonar Way" resource_id="[null]"/>
+ <project_profiles id="1" project_uuid="A" profile_key="java_sonar_way"/>
+ <project_profiles id="2" project_uuid="B" profile_key="java_sonar_way"/>
+ <project_profiles id="3" project_uuid="A" profile_key="js_sonar_way"/>
+ <project_profiles id="4" project_uuid="B" profile_key="js_sonar_way"/>
</dataset>
diff --git a/sonar-core/src/test/resources/org/sonar/core/qualityprofile/db/QualityProfileDaoTest/shared.xml b/sonar-core/src/test/resources/org/sonar/core/qualityprofile/db/QualityProfileDaoTest/shared.xml
index ec98099a8a6..bf8e0113b24 100644
--- a/sonar-core/src/test/resources/org/sonar/core/qualityprofile/db/QualityProfileDaoTest/shared.xml
+++ b/sonar-core/src/test/resources/org/sonar/core/qualityprofile/db/QualityProfileDaoTest/shared.xml
@@ -1,12 +1,9 @@
<dataset>
- <rules_profiles id="1" name="Sonar Way" language="java" parent_kee="[null]" kee="java_sonar_way" is_default="[false]"
+ <rules_profiles id="1" name="Sonar Way" language="java" parent_kee="[null]" kee="java_sonar_way" is_default="[true]"
rules_updated_at="[null]" created_at="[null]" updated_at="[null]"/>
<rules_profiles id="2" name="Sonar Way" language="js" parent_kee="[null]" kee="js_sonar_way" is_default="[false]"
rules_updated_at="[null]" created_at="[null]" updated_at="[null]"/>
- <properties id="1" prop_key="sonar.profile.java" text_value="Sonar Way" resource_id="[null]"/>
- <properties id="2" prop_key="sonar.profile.java" text_value="Sonar Way" resource_id="1"/>
-
</dataset>