diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2012-07-09 18:02:05 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2012-07-09 18:21:46 +0200 |
commit | f07417cd41173aa0f543dbe6f6bbbcdd0948a005 (patch) | |
tree | 496c715cb4ecac833eb33bf0eae43db70bce79ec /sonar-core | |
parent | 61ef36129b56cf7154446fbeb0b4231be189d275 (diff) | |
download | sonarqube-f07417cd41173aa0f543dbe6f6bbbcdd0948a005.tar.gz sonarqube-f07417cd41173aa0f543dbe6f6bbbcdd0948a005.zip |
Complete MyBatis ResourceDao
* new method insertOrUpdate()
* new criteria in ResourceQuery
Diffstat (limited to 'sonar-core')
12 files changed, 333 insertions, 37 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/resource/ResourceDao.java b/sonar-core/src/main/java/org/sonar/core/resource/ResourceDao.java index 480e75ce054..260b4abcb7e 100644 --- a/sonar-core/src/main/java/org/sonar/core/resource/ResourceDao.java +++ b/sonar-core/src/main/java/org/sonar/core/resource/ResourceDao.java @@ -86,4 +86,22 @@ public class ResourceDao { appendChildProjects(subProject.getId(), mapper, resources); } } + + public ResourceDao insertOrUpdate(ResourceDto... resources) { + SqlSession session = mybatis.openSession(); + ResourceMapper mapper = session.getMapper(ResourceMapper.class); + try { + for (ResourceDto resource : resources) { + if (resource.getId()==null) { + mapper.insert(resource); + } else { + mapper.update(resource); + } + } + session.commit(); + } finally { + MyBatis.closeQuietly(session); + } + return this; + } } diff --git a/sonar-core/src/main/java/org/sonar/core/resource/ResourceDto.java b/sonar-core/src/main/java/org/sonar/core/resource/ResourceDto.java index 6eeeab179e1..4049df31883 100644 --- a/sonar-core/src/main/java/org/sonar/core/resource/ResourceDto.java +++ b/sonar-core/src/main/java/org/sonar/core/resource/ResourceDto.java @@ -25,9 +25,15 @@ public final class ResourceDto { private String key; private String name; private String longName; - private Integer rootId; + private Long rootId; private String scope; private String qualifier; + private boolean enabled = true; + private String description; + private String language; + private Long copyResourceId; + private Long personId; + private Long profileId; public Long getId() { return id; @@ -56,11 +62,11 @@ public final class ResourceDto { return this; } - public Integer getRootId() { + public Long getRootId() { return rootId; } - public ResourceDto setRootId(Integer rootId) { + public ResourceDto setRootId(Long rootId) { this.rootId = rootId; return this; } @@ -92,34 +98,57 @@ public final class ResourceDto { return this; } - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((id == null) ? 0 : id.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - ResourceDto other = (ResourceDto) obj; - if (id == null) { - if (other.id != null) { - return false; - } - } else if (!id.equals(other.id)) { - return false; - } - return true; + public boolean isEnabled() { + return enabled; } + public ResourceDto setEnabled(boolean b) { + this.enabled = b; + return this; + } + + public String getDescription() { + return description; + } + + public ResourceDto setDescription(String description) { + this.description = description; + return this; + } + + public String getLanguage() { + return language; + } + + public ResourceDto setLanguage(String language) { + this.language = language; + return this; + } + + public Long getCopyResourceId() { + return copyResourceId; + } + + public ResourceDto setCopyResourceId(Long copyResourceId) { + this.copyResourceId = copyResourceId; + return this; + } + + public Long getPersonId() { + return personId; + } + + public ResourceDto setPersonId(Long personId) { + this.personId = personId; + return this; + } + + public Long getProfileId() { + return profileId; + } + + public ResourceDto setProfileId(Long profileId) { + this.profileId = profileId; + return this; + } } diff --git a/sonar-core/src/main/java/org/sonar/core/resource/ResourceIndexerDao.java b/sonar-core/src/main/java/org/sonar/core/resource/ResourceIndexerDao.java index 77e2831efee..b5f14b2c044 100644 --- a/sonar-core/src/main/java/org/sonar/core/resource/ResourceIndexerDao.java +++ b/sonar-core/src/main/java/org/sonar/core/resource/ResourceIndexerDao.java @@ -139,11 +139,11 @@ public class ResourceIndexerDao { ResourceIndexerMapper mapper = session.getMapper(ResourceIndexerMapper.class); ResourceDto resource = mapper.selectResourceToIndex(id); if (resource != null) { - Integer rootId = resource.getRootId(); + Long rootId = resource.getRootId(); if (rootId == null) { - rootId = resource.getId().intValue(); + rootId = resource.getId(); } - indexed = indexResource(resource.getId().intValue(), resource.getName(), resource.getQualifier(), rootId, session, mapper); + indexed = indexResource(resource.getId(), resource.getName(), resource.getQualifier(), rootId, session, mapper); } return indexed; } finally { @@ -163,7 +163,7 @@ public class ResourceIndexerDao { return indexed; } - private boolean indexResource(int id, String name, String qualifier, int rootId, SqlSession session, ResourceIndexerMapper mapper) { + private boolean indexResource(long id, String name, String qualifier, long rootId, SqlSession session, ResourceIndexerMapper mapper) { boolean indexed = false; String key = nameToKey(name); if (key.length() >= MINIMUM_KEY_SIZE) { @@ -193,7 +193,7 @@ public class ResourceIndexerDao { * If the resource is indexed with a different key, then this index is dropped and the * resource must be indexed again. */ - private boolean sanitizeIndex(int resourceId, String key, ResourceIndexerMapper mapper) { + private boolean sanitizeIndex(long resourceId, String key, ResourceIndexerMapper mapper) { ResourceIndexDto masterIndex = mapper.selectMasterIndexByResourceId(resourceId); if (masterIndex != null && !StringUtils.equals(key, masterIndex.getKey())) { // resource has been renamed -> drop existing indexes diff --git a/sonar-core/src/main/java/org/sonar/core/resource/ResourceMapper.java b/sonar-core/src/main/java/org/sonar/core/resource/ResourceMapper.java index bd54734db46..cba769cf2c0 100644 --- a/sonar-core/src/main/java/org/sonar/core/resource/ResourceMapper.java +++ b/sonar-core/src/main/java/org/sonar/core/resource/ResourceMapper.java @@ -44,4 +44,8 @@ public interface ResourceMapper { * @since 3.2 */ void selectResources(ResourceQuery query, ResultHandler resultHandler); + + void insert(ResourceDto resource); + + void update(ResourceDto resource); } diff --git a/sonar-core/src/main/java/org/sonar/core/resource/ResourceQuery.java b/sonar-core/src/main/java/org/sonar/core/resource/ResourceQuery.java index a9aa2e9a229..6ae769715cd 100644 --- a/sonar-core/src/main/java/org/sonar/core/resource/ResourceQuery.java +++ b/sonar-core/src/main/java/org/sonar/core/resource/ResourceQuery.java @@ -24,6 +24,8 @@ package org.sonar.core.resource; */ public final class ResourceQuery { private String[] qualifiers = null; + private String key = null; + private boolean excludeDisabled = false; private ResourceQuery() { } @@ -40,4 +42,22 @@ public final class ResourceQuery { this.qualifiers = qualifiers; return this; } + + public String getKey() { + return key; + } + + public ResourceQuery setKey(String key) { + this.key = key; + return this; + } + + public boolean isExcludeDisabled() { + return excludeDisabled; + } + + public ResourceQuery setExcludeDisabled(boolean b) { + this.excludeDisabled = b; + return this; + } } diff --git a/sonar-core/src/main/resources/org/sonar/core/resource/ResourceMapper-oracle.xml b/sonar-core/src/main/resources/org/sonar/core/resource/ResourceMapper-oracle.xml new file mode 100644 index 00000000000..558e4a3577a --- /dev/null +++ b/sonar-core/src/main/resources/org/sonar/core/resource/ResourceMapper-oracle.xml @@ -0,0 +1,107 @@ +<?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.resource.ResourceMapper"> + + <resultMap id="snapshotResultMap" type="Snapshot"> + <id property="id" column="id"/> + <result property="parentId" column="parent_snapshot_id"/> + <result property="rootId" column="root_snapshot_id"/> + <result property="date" column="created_at"/> + <result property="buildDate" column="build_date"/> + <result property="resourceId" column="project_id"/> + <result property="status" column="status"/> + <result property="purgeStatus" column="purge_status"/> + <result property="last" column="islast"/> + <result property="scope" column="scope"/> + <result property="qualifier" column="qualifier"/> + <result property="version" column="version"/> + <result property="path" column="path"/> + <result property="depth" column="depth"/> + <result property="rootProjectId" column="root_project_id"/> + </resultMap> + + <resultMap id="resourceResultMap" type="Resource"> + <id property="id" column="id"/> + <result property="key" column="kee"/> + <result property="name" column="name"/> + <result property="longName" column="long_name"/> + <result property="rootId" column="root_id"/> + <result property="scope" column="scope"/> + <result property="qualifier" column="qualifier"/> + <result property="enabled" column="enabled"/> + <result property="description" column="description"/> + <result property="language" column="language"/> + <result property="copyResourceId" column="copy_resource_id"/> + <result property="personId" column="person_id"/> + <result property="profileId" column="profile_id"/> + </resultMap> + + <select id="selectResources" parameterType="map" resultMap="resourceResultMap"> + select * + from projects p + <where> + <if test="qualifiers != null and qualifiers.length!=0"> + and p.qualifier in + <foreach item="qualifier" index="index" collection="qualifiers" open="(" separator="," close=")">#{qualifier}</foreach> + </if> + <if test="key != null"> + and p.kee=#{key} + </if> + <if test="excludeDisabled"> + and p.enabled=${_true} + </if> + </where> + </select> + + <select id="selectResourceIds" parameterType="map" resultType="long"> + select p.id + from projects p + <where> + <if test="qualifiers != null and qualifiers.length!=0"> + and p.qualifier in + <foreach item="qualifier" index="index" collection="qualifiers" open="(" separator="," close=")">#{qualifier}</foreach> + </if> + <if test="key != null"> + and p.kee=#{key} + </if> + <if test="excludeDisabled"> + and p.enabled=${_true} + </if> + </where> + </select> + + <select id="selectResource" parameterType="long" resultMap="resourceResultMap"> + select * from projects where id=#{id} + </select> + + <select id="selectSnapshot" parameterType="long" resultMap="snapshotResultMap"> + select * from snapshots where id=#{id} + </select> + + <select id="selectDescendantProjects" parameterType="long" resultMap="resourceResultMap"> + select * from projects where scope='PRJ' and root_id=#{id} + </select> + + <insert id="insert" parameterType="Resource" useGeneratedKeys="true" keyProperty="id"> + <selectKey order="BEFORE" resultType="Long" keyProperty="id"> + select projects_seq.NEXTVAL from DUAL + </selectKey> + + insert into projects + (id, name, long_name, description, scope, qualifier, kee, language, root_id, copy_resource_id, person_id, profile_id, enabled) + values ( + #{id}, #{name, jdbcType=VARCHAR}, #{longName, jdbcType=VARCHAR}, #{description, jdbcType=VARCHAR}, #{scope, jdbcType=VARCHAR}, #{qualifier, jdbcType=VARCHAR}, + #{key, jdbcType=VARCHAR}, #{language, jdbcType=VARCHAR}, #{rootId, jdbcType=INTEGER}, #{copyResourceId, jdbcType=INTEGER}, + #{personId, jdbcType=INTEGER}, #{profileId, jdbcType=INTEGER}, #{enabled, jdbcType=BOOLEAN} + ) + </insert> + + <update id="update" parameterType="Resource"> + update projects set name=#{name, jdbcType=VARCHAR}, long_name=#{longName, jdbcType=VARCHAR}, description=#{description, jdbcType=VARCHAR}, + scope=#{scope, jdbcType=VARCHAR}, qualifier=#{qualifier, jdbcType=VARCHAR}, kee=#{key, jdbcType=VARCHAR}, + language=#{language, jdbcType=VARCHAR}, root_id=#{rootId, jdbcType=INTEGER}, copy_resource_id=#{copyResourceId, jdbcType=INTEGER}, + person_id=#{personId, jdbcType=INTEGER}, profile_id=#{profileId, jdbcType=INTEGER}, enabled=#{enabled, jdbcType=BOOLEAN} where id=#{id,jdbcType=INTEGER} + </update> +</mapper> + diff --git a/sonar-core/src/main/resources/org/sonar/core/resource/ResourceMapper.xml b/sonar-core/src/main/resources/org/sonar/core/resource/ResourceMapper.xml index c604f28d154..728cb9dda0f 100644 --- a/sonar-core/src/main/resources/org/sonar/core/resource/ResourceMapper.xml +++ b/sonar-core/src/main/resources/org/sonar/core/resource/ResourceMapper.xml @@ -29,6 +29,12 @@ <result property="rootId" column="root_id"/> <result property="scope" column="scope"/> <result property="qualifier" column="qualifier"/> + <result property="enabled" column="enabled"/> + <result property="description" column="description"/> + <result property="language" column="language"/> + <result property="copyResourceId" column="copy_resource_id"/> + <result property="personId" column="person_id"/> + <result property="profileId" column="profile_id"/> </resultMap> <select id="selectResources" parameterType="map" resultMap="resourceResultMap"> @@ -39,6 +45,12 @@ and p.qualifier in <foreach item="qualifier" index="index" collection="qualifiers" open="(" separator="," close=")">#{qualifier}</foreach> </if> + <if test="key != null"> + and p.kee=#{key} + </if> + <if test="excludeDisabled"> + and p.enabled=${_true} + </if> </where> </select> @@ -50,6 +62,12 @@ and p.qualifier in <foreach item="qualifier" index="index" collection="qualifiers" open="(" separator="," close=")">#{qualifier}</foreach> </if> + <if test="key != null"> + and p.kee=#{key} + </if> + <if test="excludeDisabled"> + and p.enabled=${_true} + </if> </where> </select> @@ -64,5 +82,22 @@ <select id="selectDescendantProjects" parameterType="long" resultMap="resourceResultMap"> select * from projects where scope='PRJ' and root_id=#{id} </select> + + <insert id="insert" parameterType="Resource" useGeneratedKeys="true" keyProperty="id"> + insert into projects + (name, long_name, description, scope, qualifier, kee, language, root_id, copy_resource_id, person_id, profile_id, enabled) + values ( + #{name, jdbcType=VARCHAR}, #{longName, jdbcType=VARCHAR}, #{description, jdbcType=VARCHAR}, #{scope, jdbcType=VARCHAR}, #{qualifier, jdbcType=VARCHAR}, + #{key, jdbcType=VARCHAR}, #{language, jdbcType=VARCHAR}, #{rootId, jdbcType=INTEGER}, #{copyResourceId, jdbcType=INTEGER}, + #{personId, jdbcType=INTEGER}, #{profileId, jdbcType=INTEGER}, #{enabled, jdbcType=BOOLEAN} + ) + </insert> + + <update id="update" parameterType="Resource"> + update projects set name=#{name, jdbcType=VARCHAR}, long_name=#{longName, jdbcType=VARCHAR}, description=#{description, jdbcType=VARCHAR}, + scope=#{scope, jdbcType=VARCHAR}, qualifier=#{qualifier, jdbcType=VARCHAR}, kee=#{key, jdbcType=VARCHAR}, + language=#{language, jdbcType=VARCHAR}, root_id=#{rootId, jdbcType=INTEGER}, copy_resource_id=#{copyResourceId, jdbcType=INTEGER}, + person_id=#{personId, jdbcType=INTEGER}, profile_id=#{profileId, jdbcType=INTEGER}, enabled=#{enabled, jdbcType=BOOLEAN} where id=#{id,jdbcType=INTEGER} + </update> </mapper> diff --git a/sonar-core/src/test/java/org/sonar/core/resource/ResourceDaoTest.java b/sonar-core/src/test/java/org/sonar/core/resource/ResourceDaoTest.java index a16eec194c8..26f8513367a 100644 --- a/sonar-core/src/test/java/org/sonar/core/resource/ResourceDaoTest.java +++ b/sonar-core/src/test/java/org/sonar/core/resource/ResourceDaoTest.java @@ -21,6 +21,8 @@ package org.sonar.core.resource; import org.junit.Before; import org.junit.Test; +import org.sonar.api.resources.Qualifiers; +import org.sonar.api.resources.Scopes; import org.sonar.core.persistence.AbstractDaoTestCase; import java.util.List; @@ -63,6 +65,9 @@ public class ResourceDaoTest extends AbstractDaoTestCase { assertThat(resource.getName()).isEqualTo("Struts"); assertThat(resource.getLongName()).isEqualTo("Apache Struts"); assertThat(resource.getScope()).isEqualTo("PRJ"); + assertThat(resource.getDescription()).isEqualTo("the description"); + assertThat(resource.getLanguage()).isEqualTo("java"); + assertThat(resource.isEnabled()).isTrue(); } @Test @@ -96,6 +101,15 @@ public class ResourceDaoTest extends AbstractDaoTestCase { } @Test + public void getResources_filter_by_key() { + setupData("fixture"); + + List<ResourceDto> resources = dao.getResources(ResourceQuery.create().setKey("org.struts:struts-core")); + assertThat(resources).hasSize(1); + assertThat(resources.get(0).getKey()).isEqualTo("org.struts:struts-core"); + } + + @Test public void getResourceIds_all() { setupData("fixture"); @@ -117,5 +131,40 @@ public class ResourceDaoTest extends AbstractDaoTestCase { ids = dao.getResourceIds(ResourceQuery.create().setQualifiers(new String[]{})); assertThat(ids).hasSize(4); } + + @Test + public void getResources_exclude_disabled() { + setupData("getResources_exclude_disabled"); + + assertThat(dao.getResourceIds(ResourceQuery.create().setExcludeDisabled(false))).containsOnly(1L, 2L); + assertThat(dao.getResourceIds(ResourceQuery.create().setExcludeDisabled(true))).containsOnly(2L); + } + + @Test + public void insertOrUpdate() { + setupData("insertOrUpdate"); + + // update because already persisted + ResourceDto project = new ResourceDto() + .setKey("org.struts:struts").setScope(Scopes.PROJECT).setQualifier(Qualifiers.PROJECT) + .setName("Struts").setLongName("Apache Struts").setLanguage("java").setDescription("MVC Framework") + .setId(1L); + + // insert + ResourceDto file1 = new ResourceDto() + .setKey("org.struts:struts:org.struts.Action").setScope(Scopes.FILE).setQualifier(Qualifiers.FILE) + .setLanguage("java").setName("Action").setLongName("org.struts.Action"); + ResourceDto file2 = new ResourceDto() + .setKey("org.struts:struts:org.struts.Filter").setScope(Scopes.FILE).setQualifier(Qualifiers.FILE) + .setLanguage("java").setName("Filter").setLongName("org.struts.Filter"); + + dao.insertOrUpdate(project, file1, file2); + + assertThat(project.getId()).isNotNull(); + assertThat(file1.getId()).isNotNull(); + assertThat(file2.getId()).isNotNull(); + checkTables("insertOrUpdate", "projects"); + } + } diff --git a/sonar-core/src/test/resources/org/sonar/core/resource/ResourceDaoTest/fixture.xml b/sonar-core/src/test/resources/org/sonar/core/resource/ResourceDaoTest/fixture.xml index e92a256f553..6c362ca7963 100644 --- a/sonar-core/src/test/resources/org/sonar/core/resource/ResourceDaoTest/fixture.xml +++ b/sonar-core/src/test/resources/org/sonar/core/resource/ResourceDaoTest/fixture.xml @@ -2,7 +2,7 @@ <!-- root project --> <projects id="1" root_id="[null]" scope="PRJ" qualifier="TRK" kee="org.struts:struts" name="Struts" - description="[null]" long_name="Apache Struts" + description="the description" long_name="Apache Struts" enabled="[true]" language="java" copy_resource_id="[null]" person_id="[null]"/> <snapshots id="1" project_id="1" parent_snapshot_id="[null]" root_project_id="1" root_snapshot_id="[null]" status="P" islast="[false]" purge_status="[null]" diff --git a/sonar-core/src/test/resources/org/sonar/core/resource/ResourceDaoTest/getResources_exclude_disabled.xml b/sonar-core/src/test/resources/org/sonar/core/resource/ResourceDaoTest/getResources_exclude_disabled.xml new file mode 100644 index 00000000000..f210035c786 --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/resource/ResourceDaoTest/getResources_exclude_disabled.xml @@ -0,0 +1,12 @@ +<dataset> + + <!-- disabled --> + <projects id="1" root_id="[null]" scope="PRJ" qualifier="TRK" kee="org.struts:struts" name="Struts" + description="the description" long_name="Apache Struts" + enabled="[false]" language="java" copy_resource_id="[null]" person_id="[null]"/> + + <!-- enabled --> + <projects id="2" root_id="[null]" scope="PRJ" qualifier="TRK" kee="org.struts:struts" name="Struts" + description="the description" long_name="Apache Struts" + enabled="[true]" language="java" copy_resource_id="[null]" person_id="[null]"/> +</dataset> diff --git a/sonar-core/src/test/resources/org/sonar/core/resource/ResourceDaoTest/insertOrUpdate-result.xml b/sonar-core/src/test/resources/org/sonar/core/resource/ResourceDaoTest/insertOrUpdate-result.xml new file mode 100644 index 00000000000..668f78eed16 --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/resource/ResourceDaoTest/insertOrUpdate-result.xml @@ -0,0 +1,15 @@ +<dataset> + + <projects id="1" root_id="[null]" scope="PRJ" qualifier="TRK" kee="org.struts:struts" name="Struts" + description="MVC Framework" long_name="Apache Struts" + enabled="[true]" language="java" copy_resource_id="[null]" person_id="[null]" profile_id="[null]"/> + + <projects id="2" root_id="[null]" scope="FIL" qualifier="FIL" kee="org.struts:struts:org.struts.Action" name="Action" + description="[null]" long_name="org.struts.Action" + enabled="[true]" language="java" copy_resource_id="[null]" person_id="[null]" profile_id="[null]"/> + + <projects id="3" root_id="[null]" scope="FIL" qualifier="FIL" kee="org.struts:struts:org.struts.Filter" name="Filter" + description="[null]" long_name="org.struts.Filter" + enabled="[true]" language="java" copy_resource_id="[null]" person_id="[null]" profile_id="[null]"/> + +</dataset> diff --git a/sonar-core/src/test/resources/org/sonar/core/resource/ResourceDaoTest/insertOrUpdate.xml b/sonar-core/src/test/resources/org/sonar/core/resource/ResourceDaoTest/insertOrUpdate.xml new file mode 100644 index 00000000000..10fdddc66ff --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/resource/ResourceDaoTest/insertOrUpdate.xml @@ -0,0 +1,7 @@ +<dataset> + + <projects id="1" root_id="200" scope="PRJ" qualifier="TRK" kee="old key" name="old name" + description="old name" long_name="old long name" + enabled="[false]" language="old" copy_resource_id="2" person_id="3" profile_id="4"/> + +</dataset> |