From ce3dcb306cc6e0436c8c0490080ef2b1ff623cb9 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Wed, 4 Apr 2012 11:08:35 +0200 Subject: [PATCH] Complete MyBatis mapper for PROPERTIES : add insert and update operations --- .../sonar/core/properties/PropertiesDao.java | 22 +++++++- .../core/properties/PropertiesMapper.java | 7 +-- .../sonar/core/properties/PropertyDto.java | 18 +++---- .../properties/PropertiesMapper-oracle.xml | 54 +++++++++++++++++++ .../core/properties/PropertiesMapper.xml | 37 +++++++++++-- .../core/properties/PropertiesDaoTest.java | 24 ++++++++- .../PropertiesDaoTest/insert-result.xml | 12 +++++ .../properties/PropertiesDaoTest/insert.xml | 1 + .../PropertiesDaoTest/update-result.xml | 12 +++++ .../properties/PropertiesDaoTest/update.xml | 12 +++++ 10 files changed, 180 insertions(+), 19 deletions(-) create mode 100644 sonar-core/src/main/resources/org/sonar/core/properties/PropertiesMapper-oracle.xml create mode 100644 sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/insert-result.xml create mode 100644 sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/insert.xml create mode 100644 sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/update-result.xml create mode 100644 sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/update.xml 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 findUserIdsForFavouriteResource(Integer resourceId) { + public List 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 findUserIdsForFavouriteResource(@Param("resource_id") Integer resourceId); + List findUserIdsForFavouriteResource(Long resourceId); List selectGlobalProperties(); List 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 @@ + + + + + + + + + + + + + + + update properties set text_value = #{value} where id = #{id} + + + + + select properties_seq.NEXTVAL from DUAL + + 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}) + + + 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 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} + + + + + + update properties set text_value = #{value} where id = #{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}) + 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 userIds = dao.findUserIdsForFavouriteResource(2); + List 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 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 @@ + + + + + + + + + + + + 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 @@ + \ 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 @@ + + + + + + + + + + + + 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 @@ + + + + + + + + + + + + -- 2.39.5