aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@gmail.com>2012-03-29 19:47:42 +0200
committerSimon Brandhof <simon.brandhof@gmail.com>2012-03-29 19:49:06 +0200
commit77661fd38dc63b1fae965733fbda8a53ece0a7fa (patch)
treedc6b5f4a34e84bcdc79ef5950389bf63c3ce01f5
parentca69df1a7c175703dbac9d321e93d2fc13847f83 (diff)
downloadsonarqube-77661fd38dc63b1fae965733fbda8a53ece0a7fa.tar.gz
sonarqube-77661fd38dc63b1fae965733fbda8a53ece0a7fa.zip
Add MyBatis mapper to select rows from PROJECTS
-rw-r--r--sonar-core/src/main/java/org/sonar/core/purge/PurgeMapper.java2
-rw-r--r--sonar-core/src/main/java/org/sonar/core/resource/ResourceDao.java18
-rw-r--r--sonar-core/src/main/java/org/sonar/core/resource/ResourceIndexerDao.java16
-rw-r--r--sonar-core/src/main/java/org/sonar/core/resource/ResourceMapper.java12
-rw-r--r--sonar-core/src/main/java/org/sonar/core/resource/ResourceQuery.java43
-rw-r--r--sonar-core/src/main/resources/org/sonar/core/resource/ResourceMapper.xml27
-rw-r--r--sonar-core/src/test/java/org/sonar/core/resource/ResourceDaoTest.java70
-rw-r--r--sonar-core/src/test/resources/org/sonar/core/purge/PurgeCommandsTest/shouldDeleteResource.xml2
8 files changed, 181 insertions, 9 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/purge/PurgeMapper.java b/sonar-core/src/main/java/org/sonar/core/purge/PurgeMapper.java
index 2deacdd4600..3ec22dde831 100644
--- a/sonar-core/src/main/java/org/sonar/core/purge/PurgeMapper.java
+++ b/sonar-core/src/main/java/org/sonar/core/purge/PurgeMapper.java
@@ -75,6 +75,8 @@ public interface PurgeMapper {
void deleteResourceActionPlans(long resourceId);
+ void deleteAuthors(long developerId);
+
void closeResourceReviews(long resourceId);
List<PurgeableSnapshotDto> selectPurgeableSnapshotsWithEvents(long resourceId);
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 e97fb1d7795..480e75ce054 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
@@ -32,6 +32,24 @@ public class ResourceDao {
this.mybatis = mybatis;
}
+ public List<ResourceDto> getResources(ResourceQuery query) {
+ SqlSession session = mybatis.openSession();
+ try {
+ return session.getMapper(ResourceMapper.class).selectResources(query);
+ } finally {
+ MyBatis.closeQuietly(session);
+ }
+ }
+
+ public List<Long> getResourceIds(ResourceQuery query) {
+ SqlSession session = mybatis.openSession();
+ try {
+ return session.getMapper(ResourceMapper.class).selectResourceIds(query);
+ } finally {
+ MyBatis.closeQuietly(session);
+ }
+ }
+
public ResourceDto getResource(long projectId) {
SqlSession session = mybatis.openSession();
try {
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 66148d5fba0..d7e26e8a00e 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
@@ -68,7 +68,7 @@ public class ResourceIndexerDao {
final SqlSession session = mybatis.openBatchSession();
try {
final ResourceIndexerMapper mapper = session.getMapper(ResourceIndexerMapper.class);
- session.select("selectRootProjectIds", /* workaround to get booleans */ResourceIndexerQuery.create(), new ResultHandler() {
+ session.select("org.sonar.core.resource.ResourceIndexerMapper.selectRootProjectIds", /* workaround to get booleans */ResourceIndexerQuery.create(), new ResultHandler() {
public void handleResult(ResultContext context) {
Integer rootProjectId = (Integer) context.getResultObject();
doIndexProject(rootProjectId, session, mapper);
@@ -90,7 +90,7 @@ public class ResourceIndexerDao {
.setScopes(NOT_RENAMABLE_SCOPES)
.setRootProjectId(rootProjectId);
- session.select("selectResources", query, new ResultHandler() {
+ session.select("org.sonar.core.resource.ResourceIndexerMapper.selectResources", query, new ResultHandler() {
public void handleResult(ResultContext context) {
ResourceDto resource = (ResourceDto) context.getResultObject();
doIndex(resource, mapper);
@@ -105,7 +105,7 @@ public class ResourceIndexerDao {
.setScopes(RENAMABLE_SCOPES)
.setRootProjectId(rootProjectId);
- session.select("selectResources", query, new ResultHandler() {
+ session.select("org.sonar.core.resource.ResourceIndexerMapper.selectResources", query, new ResultHandler() {
public void handleResult(ResultContext context) {
ResourceDto resource = (ResourceDto) context.getResultObject();
@@ -133,9 +133,13 @@ public class ResourceIndexerDao {
}
}
- public boolean indexResource(int id, String name, String qualifier, int rootProjectId) {
+ public boolean indexResource(int id, String name, String qualifier, int rootId) {
+ return indexResource(id, name, qualifier, rootId, false);
+ }
+
+ public boolean indexResource(int id, String name, String qualifier, int rootId, boolean force) {
boolean indexed = false;
- if (isIndexableQualifier(qualifier)) {
+ if (force || isIndexableQualifier(qualifier)) {
SqlSession session = mybatis.openSession();
try {
String key = nameToKey(name);
@@ -147,7 +151,7 @@ public class ResourceIndexerDao {
ResourceIndexDto dto = new ResourceIndexDto()
.setResourceId(id)
.setQualifier(qualifier)
- .setRootProjectId(rootProjectId)
+ .setRootProjectId(rootId)
.setNameSize(name.length());
for (int position = 0; position <= key.length() - MINIMUM_KEY_SIZE; position++) {
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 626dfd61f12..f08842207ea 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
@@ -23,6 +23,18 @@ import java.util.List;
public interface ResourceMapper {
SnapshotDto selectSnapshot(Long snapshotId);
+
ResourceDto selectResource(long id);
+
List<ResourceDto> selectDescendantProjects(long rootProjectId);
+
+ /**
+ * @since 2.15
+ */
+ List<ResourceDto> selectResources(ResourceQuery query);
+
+ /**
+ * @since 2.15
+ */
+ List<Long> selectResourceIds(ResourceQuery query);
}
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
new file mode 100644
index 00000000000..8539b21412f
--- /dev/null
+++ b/sonar-core/src/main/java/org/sonar/core/resource/ResourceQuery.java
@@ -0,0 +1,43 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar 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.
+ *
+ * Sonar 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 Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.core.resource;
+
+/**
+ * @since 2.15
+ */
+public final class ResourceQuery {
+ private String[] qualifiers = null;
+
+ private ResourceQuery() {
+ }
+
+ public static ResourceQuery create() {
+ return new ResourceQuery();
+ }
+
+ public String[] getQualifiers() {
+ return qualifiers;
+ }
+
+ public ResourceQuery setQualifiers(String[] qualifiers) {
+ this.qualifiers = qualifiers;
+ return this;
+ }
+}
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 2245bab7513..6df6d82844d 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
@@ -3,7 +3,6 @@
<mapper namespace="org.sonar.core.resource.ResourceMapper">
-
<resultMap id="snapshotResultMap" type="Snapshot">
<id property="id" column="id"/>
<result property="parentId" column="parent_snapshot_id"/>
@@ -31,9 +30,31 @@
<result property="qualifier" column="qualifier"/>
</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>
+ </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>
+ </where>
+ </select>
+
<select id="selectResource" parameterType="long" resultMap="resourceResultMap">
- select * from projects where id=#{id}
- </select>
+ select * from projects where id=#{id}
+ </select>
<select id="selectSnapshot" parameterType="long" resultMap="snapshotResultMap">
select * from snapshots where id=#{id}
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 cb9d765a95b..14a247853ff 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
@@ -19,6 +19,8 @@
*/
package org.sonar.core.resource;
+import org.hamcrest.BaseMatcher;
+import org.hamcrest.Description;
import org.hamcrest.core.Is;
import org.junit.Before;
import org.junit.Test;
@@ -29,6 +31,7 @@ import java.util.List;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
+import static org.junit.matchers.JUnitMatchers.hasItems;
public class ResourceDaoTest extends DaoTestCase {
@@ -72,5 +75,72 @@ public class ResourceDaoTest extends DaoTestCase {
assertNull(dao.getResource(987654321L));
}
+
+ @Test
+ public void getResources_all() {
+ setupData("fixture");
+
+ List<ResourceDto> resources = dao.getResources(ResourceQuery.create());
+
+ assertThat(resources.size(), Is.is(4));
+ }
+
+ @Test
+ public void getResources_filter_by_qualifier() {
+ setupData("fixture");
+
+ List<ResourceDto> resources = dao.getResources(ResourceQuery.create().setQualifiers(new String[]{"TRK", "BRC"}));
+ assertThat(resources.size(), Is.is(2));
+ assertThat(resources, hasItems(new QualifierMatcher("TRK"), new QualifierMatcher("BRC")));
+
+ resources = dao.getResources(ResourceQuery.create().setQualifiers(new String[]{"XXX"}));
+ assertThat(resources.size(), Is.is(0));
+
+ resources = dao.getResources(ResourceQuery.create().setQualifiers(new String[]{}));
+ assertThat(resources.size(), Is.is(4));
+ }
+
+ @Test
+ public void getResourceIds_all() {
+ setupData("fixture");
+
+ List<Long> ids = dao.getResourceIds(ResourceQuery.create());
+
+ assertThat(ids.size(), Is.is(4));
+ }
+
+ @Test
+ public void getResourceIds_filter_by_qualifier() {
+ setupData("fixture");
+
+ List<Long> ids = dao.getResourceIds(ResourceQuery.create().setQualifiers(new String[]{"TRK", "BRC"}));
+ assertThat(ids.size(), Is.is(2));
+ assertThat(ids, hasItems(1l, 2l));
+
+ ids = dao.getResourceIds(ResourceQuery.create().setQualifiers(new String[]{"XXX"}));
+ assertThat(ids.size(), Is.is(0));
+
+ ids = dao.getResourceIds(ResourceQuery.create().setQualifiers(new String[]{}));
+ assertThat(ids.size(), Is.is(4));
+ }
+
+
+ private static class QualifierMatcher extends BaseMatcher<ResourceDto> {
+ private String qualifier;
+
+ private QualifierMatcher(String qualifier) {
+ this.qualifier = qualifier;
+ }
+
+ @Override
+ public boolean matches(Object o) {
+ return ((ResourceDto) o).getQualifier().equals(qualifier);
+ }
+
+ @Override
+ public void describeTo(Description description) {
+ description.appendText("qualifier " + qualifier);
+ }
+ }
}
diff --git a/sonar-core/src/test/resources/org/sonar/core/purge/PurgeCommandsTest/shouldDeleteResource.xml b/sonar-core/src/test/resources/org/sonar/core/purge/PurgeCommandsTest/shouldDeleteResource.xml
index df9741558c4..d9fe0172ed6 100644
--- a/sonar-core/src/test/resources/org/sonar/core/purge/PurgeCommandsTest/shouldDeleteResource.xml
+++ b/sonar-core/src/test/resources/org/sonar/core/purge/PurgeCommandsTest/shouldDeleteResource.xml
@@ -24,4 +24,6 @@
<review_comments id="1" created_at="[null]" updated_at="[null]" review_id="1" user_id="1223" review_text="abc"/>
+ <authors id="1" person_id="1" login="tartanpion" created_at="[null]" updated_at="[null]" />
+ <authors id="2" person_id="1" login="fanfoue" created_at="[null]" updated_at="[null]" />
</dataset> \ No newline at end of file