diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2012-04-04 11:08:35 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2012-04-04 11:09:06 +0200 |
commit | ce3dcb306cc6e0436c8c0490080ef2b1ff623cb9 (patch) | |
tree | 622ca859113002ff37d7380ee53724c3ae61f577 /sonar-core/src | |
parent | 2e24fb395108f50e96db34398e1f9458b3f1c4c1 (diff) | |
download | sonarqube-ce3dcb306cc6e0436c8c0490080ef2b1ff623cb9.tar.gz sonarqube-ce3dcb306cc6e0436c8c0490080ef2b1ff623cb9.zip |
Complete MyBatis mapper for PROPERTIES : add insert and update operations
Diffstat (limited to 'sonar-core/src')
10 files changed, 180 insertions, 19 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/properties/PropertiesDao.java b/sonar-core/src/main/java/org/sonar/core/properties/PropertiesDao.java index 78392eeb473..c21cd08f48e 100644 --- a/sonar-core/src/main/java/org/sonar/core/properties/PropertiesDao.java +++ b/sonar-core/src/main/java/org/sonar/core/properties/PropertiesDao.java @@ -19,6 +19,7 @@ */ package org.sonar.core.properties; +import org.apache.commons.lang.StringUtils; import org.apache.ibatis.session.SqlSession; import org.sonar.api.BatchComponent; import org.sonar.api.ServerComponent; @@ -40,7 +41,7 @@ public class PropertiesDao implements BatchComponent, ServerComponent { * @param resourceId the resource id * @return the list of logins (maybe be empty - obviously) */ - public List<String> findUserIdsForFavouriteResource(Integer resourceId) { + public List<String> findUserIdsForFavouriteResource(Long resourceId) { SqlSession session = mybatis.openSession(); PropertiesMapper mapper = session.getMapper(PropertiesMapper.class); try { @@ -69,4 +70,23 @@ public class PropertiesDao implements BatchComponent, ServerComponent { MyBatis.closeQuietly(session); } } + + public void setProperty(PropertyDto property) { + SqlSession session = mybatis.openSession(); + PropertiesMapper mapper = session.getMapper(PropertiesMapper.class); + try { + PropertyDto persistedProperty = mapper.selectByKey(property); + if (persistedProperty != null && !StringUtils.equals(persistedProperty.getValue(), property.getValue())) { + persistedProperty.setValue(property.getValue()); + mapper.update(persistedProperty); + session.commit(); + + } else if (persistedProperty == null) { + mapper.insert(property); + session.commit(); + } + } finally { + MyBatis.closeQuietly(session); + } + } } diff --git a/sonar-core/src/main/java/org/sonar/core/properties/PropertiesMapper.java b/sonar-core/src/main/java/org/sonar/core/properties/PropertiesMapper.java index be3e79db7b0..90cf9ac8612 100644 --- a/sonar-core/src/main/java/org/sonar/core/properties/PropertiesMapper.java +++ b/sonar-core/src/main/java/org/sonar/core/properties/PropertiesMapper.java @@ -21,12 +21,13 @@ package org.sonar.core.properties; import java.util.List; -import org.apache.ibatis.annotations.Param; - public interface PropertiesMapper { - List<String> findUserIdsForFavouriteResource(@Param("resource_id") Integer resourceId); + List<String> findUserIdsForFavouriteResource(Long resourceId); List<PropertyDto> selectGlobalProperties(); List<PropertyDto> selectProjectProperties(String resourceKey); + PropertyDto selectByKey(PropertyDto key); + void update(PropertyDto property); + void insert(PropertyDto property); } diff --git a/sonar-core/src/main/java/org/sonar/core/properties/PropertyDto.java b/sonar-core/src/main/java/org/sonar/core/properties/PropertyDto.java index 9623f5f686e..afc42fbe6f6 100644 --- a/sonar-core/src/main/java/org/sonar/core/properties/PropertyDto.java +++ b/sonar-core/src/main/java/org/sonar/core/properties/PropertyDto.java @@ -20,17 +20,17 @@ package org.sonar.core.properties; public final class PropertyDto { - private Integer id; + private Long id; private String key; private String value; - private Integer resourceId; - private Integer userId; + private Long resourceId; + private Long userId; - public Integer getId() { + public Long getId() { return id; } - public PropertyDto setId(Integer id) { + public PropertyDto setId(Long id) { this.id = id; return this; } @@ -53,20 +53,20 @@ public final class PropertyDto { return this; } - public Integer getResourceId() { + public Long getResourceId() { return resourceId; } - public PropertyDto setResourceId(Integer resourceId) { + public PropertyDto setResourceId(Long resourceId) { this.resourceId = resourceId; return this; } - public Integer getUserId() { + public Long getUserId() { return userId; } - public PropertyDto setUserId(Integer userId) { + public PropertyDto setUserId(Long userId) { this.userId = userId; return this; } diff --git a/sonar-core/src/main/resources/org/sonar/core/properties/PropertiesMapper-oracle.xml b/sonar-core/src/main/resources/org/sonar/core/properties/PropertiesMapper-oracle.xml new file mode 100644 index 00000000000..907983b8ed9 --- /dev/null +++ b/sonar-core/src/main/resources/org/sonar/core/properties/PropertiesMapper-oracle.xml @@ -0,0 +1,54 @@ +<?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.properties.PropertiesMapper"> + + <select id="findUserIdsForFavouriteResource" parameterType="map" resultType="String"> + SELECT U.login + FROM properties P, users U + WHERE P.prop_key = 'favourite' AND P.resource_id = #{id} AND P.user_id = U.id + </select> + + <select id="selectGlobalProperties" resultType="Property"> + select p.id as id, p.prop_key as "key", p.text_value as value, p.resource_id as resourceId, p.user_id as userId + from properties p + where p.resource_id is null and p.user_id is null + </select> + + <select id="selectProjectProperties" parameterType="String" resultType="Property"> + select p.id as id, p.prop_key as "key", p.text_value as value, p.resource_id as resourceId, p.user_id as userId + from properties p, projects r + where p.resource_id=r.id and p.user_id is null and r.kee=#{id} + </select> + + <select id="selectByKey" parameterType="map" resultType="Property"> + select p.id as id, p.prop_key as "key", p.text_value as value, p.resource_id as resourceId, p.user_id as userId + from properties p + where p.prop_key=#{key} + <if test="resourceId == null"> + AND p.resource_id is null + </if> + <if test="resourceId != null"> + AND p.resource_id=#{resourceId} + </if> + <if test="userId == null"> + AND p.user_id is null + </if> + <if test="userId != null"> + AND p.user_id=#{userId} + </if> + </select> + + <update id="update" parameterType="Property"> + update properties set text_value = #{value} where id = #{id} + </update> + + <insert id="insert" parameterType="Property" useGeneratedKeys="true" keyProperty="id"> + <selectKey order="BEFORE" resultType="Long" keyProperty="id"> + select properties_seq.NEXTVAL from DUAL + </selectKey> + INSERT INTO properties (id, prop_key, resource_id, user_id, text_value) + VALUES (#{id}, #{key, jdbcType=VARCHAR}, #{resourceId, jdbcType=INTEGER}, #{userId, jdbcType=INTEGER}, #{value, jdbcType=VARCHAR}) + </insert> + +</mapper> diff --git a/sonar-core/src/main/resources/org/sonar/core/properties/PropertiesMapper.xml b/sonar-core/src/main/resources/org/sonar/core/properties/PropertiesMapper.xml index 1878b5f31af..f00498c4aba 100644 --- a/sonar-core/src/main/resources/org/sonar/core/properties/PropertiesMapper.xml +++ b/sonar-core/src/main/resources/org/sonar/core/properties/PropertiesMapper.xml @@ -6,7 +6,7 @@ <select id="findUserIdsForFavouriteResource" parameterType="map" resultType="String"> SELECT U.login FROM properties P, users U - WHERE P.prop_key = 'favourite' AND P.resource_id = #{resource_id} AND P.user_id = U.id + WHERE P.prop_key = 'favourite' AND P.resource_id = #{id} AND P.user_id = U.id </select> <select id="selectGlobalProperties" resultType="Property"> @@ -16,9 +16,36 @@ </select> <select id="selectProjectProperties" parameterType="String" resultType="Property"> - select p.id as id, p.prop_key as "key", p.text_value as value, p.resource_id as resourceId, p.user_id as userId - from properties p, projects r - where p.resource_id=r.id and p.user_id is null and r.kee=#{id} - </select> + select p.id as id, p.prop_key as "key", p.text_value as value, p.resource_id as resourceId, p.user_id as userId + from properties p, projects r + where p.resource_id=r.id and p.user_id is null and r.kee=#{id} + </select> + + <select id="selectByKey" parameterType="map" resultType="Property"> + select p.id as id, p.prop_key as "key", p.text_value as value, p.resource_id as resourceId, p.user_id as userId + from properties p + where p.prop_key=#{key} + <if test="resourceId == null"> + AND p.resource_id is null + </if> + <if test="resourceId != null"> + AND p.resource_id=#{resourceId} + </if> + <if test="userId == null"> + AND p.user_id is null + </if> + <if test="userId != null"> + AND p.user_id=#{userId} + </if> + </select> + + <update id="update" parameterType="Property"> + update properties set text_value = #{value} where id = #{id} + </update> + + <insert id="insert" parameterType="Property" useGeneratedKeys="true" keyProperty="id"> + INSERT INTO properties (prop_key, resource_id, user_id, text_value) + VALUES (#{key, jdbcType=VARCHAR}, #{resourceId, jdbcType=INTEGER}, #{userId, jdbcType=INTEGER}, #{value, jdbcType=VARCHAR}) + </insert> </mapper> diff --git a/sonar-core/src/test/java/org/sonar/core/properties/PropertiesDaoTest.java b/sonar-core/src/test/java/org/sonar/core/properties/PropertiesDaoTest.java index 074e4f36a96..092dd43cf5a 100644 --- a/sonar-core/src/test/java/org/sonar/core/properties/PropertiesDaoTest.java +++ b/sonar-core/src/test/java/org/sonar/core/properties/PropertiesDaoTest.java @@ -41,7 +41,7 @@ public class PropertiesDaoTest extends DaoTestCase { @Test public void shouldFindUserIdsForFavouriteResource() throws Exception { setupData("shouldFindUserIdsForFavouriteResource"); - List<String> userIds = dao.findUserIdsForFavouriteResource(2); + List<String> userIds = dao.findUserIdsForFavouriteResource(2L); assertThat(userIds.size(), is(2)); assertThat(userIds, hasItems("user3", "user4")); } @@ -72,6 +72,28 @@ public class PropertiesDaoTest extends DaoTestCase { assertThat(first.getValue(), is("one")); } + @Test + public void setProperty_update() throws Exception { + setupData("update"); + + dao.setProperty(new PropertyDto().setKey("global.key").setValue("new_global")); + dao.setProperty(new PropertyDto().setKey("project.key").setResourceId(10L).setValue("new_project")); + dao.setProperty(new PropertyDto().setKey("user.key").setUserId(100L).setValue("new_user")); + + checkTables("update", "properties"); + } + + @Test + public void setProperty_insert() throws Exception { + setupData("insert"); + + dao.setProperty(new PropertyDto().setKey("global.key").setValue("new_global")); + dao.setProperty(new PropertyDto().setKey("project.key").setResourceId(10L).setValue("new_project")); + dao.setProperty(new PropertyDto().setKey("user.key").setUserId(100L).setValue("new_user")); + + checkTables("insert", new String[]{"id"}, "properties"); + } + private PropertyDto findById(List<PropertyDto> properties, int id) { for (PropertyDto property : properties) { if (property.getId() == id) { diff --git a/sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/insert-result.xml b/sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/insert-result.xml new file mode 100644 index 00000000000..3d4da8c80bc --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/insert-result.xml @@ -0,0 +1,12 @@ +<dataset> + + <!-- global --> + <properties prop_key="global.key" text_value="new_global" resource_id="[null]" user_id="[null]"/> + + <!-- project --> + <properties prop_key="project.key" text_value="new_project" resource_id="10" user_id="[null]"/> + + <!-- user --> + <properties prop_key="user.key" text_value="new_user" resource_id="[null]" user_id="100"/> + +</dataset> diff --git a/sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/insert.xml b/sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/insert.xml new file mode 100644 index 00000000000..5ed00ba028b --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/insert.xml @@ -0,0 +1 @@ +<dataset></dataset>
\ No newline at end of file diff --git a/sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/update-result.xml b/sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/update-result.xml new file mode 100644 index 00000000000..3e5eb87705c --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/update-result.xml @@ -0,0 +1,12 @@ +<dataset> + + <!-- global --> + <properties id="1" prop_key="global.key" text_value="new_global" resource_id="[null]" user_id="[null]"/> + + <!-- project --> + <properties id="2" prop_key="project.key" text_value="new_project" resource_id="10" user_id="[null]"/> + + <!-- user --> + <properties id="3" prop_key="user.key" text_value="new_user" resource_id="[null]" user_id="100"/> + +</dataset> diff --git a/sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/update.xml b/sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/update.xml new file mode 100644 index 00000000000..69387405cd5 --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/update.xml @@ -0,0 +1,12 @@ +<dataset> + + <!-- global --> + <properties id="1" prop_key="global.key" text_value="global" resource_id="[null]" user_id="[null]"/> + + <!-- project --> + <properties id="2" prop_key="project.key" text_value="project" resource_id="10" user_id="[null]"/> + + <!-- user --> + <properties id="3" prop_key="user.key" text_value="user" resource_id="[null]" user_id="100"/> + +</dataset> |