aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-db/src
diff options
context:
space:
mode:
authorJulien Lancelot <julien.lancelot@sonarsource.com>2017-01-23 18:12:30 +0100
committerJulien Lancelot <julien.lancelot@sonarsource.com>2017-01-24 18:36:49 +0100
commit17cbda842774f2331d65a53cd645ba2dcd2d154a (patch)
treeda8494229f2834ccb9b920b7822dc63ef45959f3 /sonar-db/src
parent4fc7e70ddb24c863a2686b954676790adf071aa7 (diff)
downloadsonarqube-17cbda842774f2331d65a53cd645ba2dcd2d154a.tar.gz
sonarqube-17cbda842774f2331d65a53cd645ba2dcd2d154a.zip
SONAR-7298 Deprecate and rewrite WS api/projects/index in Java
Diffstat (limited to 'sonar-db/src')
-rw-r--r--sonar-db/src/main/java/org/sonar/db/component/ComponentDao.java10
-rw-r--r--sonar-db/src/main/java/org/sonar/db/component/ComponentMapper.java2
-rw-r--r--sonar-db/src/main/resources/org/sonar/db/component/ComponentMapper.xml20
-rw-r--r--sonar-db/src/test/java/org/sonar/db/component/ComponentDaoTest.java28
4 files changed, 55 insertions, 5 deletions
diff --git a/sonar-db/src/main/java/org/sonar/db/component/ComponentDao.java b/sonar-db/src/main/java/org/sonar/db/component/ComponentDao.java
index e4d2527af9b..2d892055a79 100644
--- a/sonar-db/src/main/java/org/sonar/db/component/ComponentDao.java
+++ b/sonar-db/src/main/java/org/sonar/db/component/ComponentDao.java
@@ -39,15 +39,16 @@ import org.sonar.db.Dao;
import org.sonar.db.DatabaseUtils;
import org.sonar.db.DbSession;
import org.sonar.db.RowNotFoundException;
-import org.sonar.db.WildcardPosition;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.collect.Maps.newHashMapWithExpectedSize;
import static java.util.Collections.emptyList;
import static java.util.Objects.requireNonNull;
import static org.apache.commons.lang.StringUtils.isBlank;
+import static org.sonar.db.DatabaseUtils.buildLikeValue;
import static org.sonar.db.DatabaseUtils.executeLargeInputs;
import static org.sonar.db.DatabaseUtils.executeLargeUpdates;
+import static org.sonar.db.WildcardPosition.BEFORE_AND_AFTER;
public class ComponentDao implements Dao {
@@ -239,7 +240,7 @@ public class ComponentDao implements Dao {
if (isBlank(textQuery)) {
return null;
}
- return DatabaseUtils.buildLikeValue(textQuery.toUpperCase(Locale.ENGLISH), WildcardPosition.BEFORE_AND_AFTER);
+ return DatabaseUtils.buildLikeValue(textQuery.toUpperCase(Locale.ENGLISH), BEFORE_AND_AFTER);
}
public List<ComponentDto> selectGhostProjects(DbSession session, int offset, int limit, @Nullable String query) {
@@ -288,6 +289,11 @@ public class ComponentDao implements Dao {
return new HashSet<>(mapper(dbSession).selectComponentsByQualifiers(qualifiers));
}
+ public List<ComponentDto> selectProjectsByNameQuery(DbSession dbSession, @Nullable String nameQuery, boolean includeModules) {
+ String nameQueryForSql = nameQuery == null ? null : buildLikeValue(nameQuery, BEFORE_AND_AFTER).toUpperCase(Locale.ENGLISH);
+ return mapper(dbSession).selectProjectsByNameQuery(nameQueryForSql, includeModules);
+ }
+
private static void addPartialQueryParameterIfNotNull(Map<String, Object> parameters, @Nullable String keyOrNameFilter) {
// TODO rely on resource_index table and match exactly the key
if (keyOrNameFilter != null) {
diff --git a/sonar-db/src/main/java/org/sonar/db/component/ComponentMapper.java b/sonar-db/src/main/java/org/sonar/db/component/ComponentMapper.java
index 101ac0f26c6..7cf6f163b49 100644
--- a/sonar-db/src/main/java/org/sonar/db/component/ComponentMapper.java
+++ b/sonar-db/src/main/java/org/sonar/db/component/ComponentMapper.java
@@ -117,6 +117,8 @@ public interface ComponentMapper {
List<ComponentDto> selectComponentsHavingSameKeyOrderedById(String key);
+ List<ComponentDto> selectProjectsByNameQuery(@Param("nameQuery") @Nullable String nameQuery, @Param("includeModules") boolean includeModules);
+
long countGhostProjects(Map<String, Object> parameters);
void selectForIndexing(@Param("projectUuid") @Nullable String projectUuid, ResultHandler handler);
diff --git a/sonar-db/src/main/resources/org/sonar/db/component/ComponentMapper.xml b/sonar-db/src/main/resources/org/sonar/db/component/ComponentMapper.xml
index 5813d2aca3b..3ff8987b017 100644
--- a/sonar-db/src/main/resources/org/sonar/db/component/ComponentMapper.xml
+++ b/sonar-db/src/main/resources/org/sonar/db/component/ComponentMapper.xml
@@ -425,6 +425,26 @@
</if>
</select>
+ <select id="selectProjectsByNameQuery" resultType="Component">
+ select
+ <include refid="componentColumns"/>
+ from projects p
+ <where>
+ p.enabled=${_true}
+ AND p.copy_component_uuid is null
+ <if test="includeModules == false">
+ AND p.qualifier = 'TRK'
+ </if>
+ <if test="includeModules == true">
+ AND (p.qualifier = 'TRK' OR p.qualifier = 'BRC')
+ </if>
+ <if test="nameQuery != null">
+ AND UPPER(p.name) like #{nameQuery,jdbcType=VARCHAR}
+ </if>
+ </where>
+ ORDER BY p.name
+ </select>
+
<insert id="insert" parameterType="Component" keyColumn="id" useGeneratedKeys="true" keyProperty="id">
INSERT INTO projects (
organization_uuid,
diff --git a/sonar-db/src/test/java/org/sonar/db/component/ComponentDaoTest.java b/sonar-db/src/test/java/org/sonar/db/component/ComponentDaoTest.java
index 9bbaaf20aa7..e0b62a5dc03 100644
--- a/sonar-db/src/test/java/org/sonar/db/component/ComponentDaoTest.java
+++ b/sonar-db/src/test/java/org/sonar/db/component/ComponentDaoTest.java
@@ -720,9 +720,9 @@ public class ComponentDaoTest {
@Test
public void updateBEnabledToFalse() {
- ComponentDto dto1 = ComponentTesting.newProjectDto(db.getDefaultOrganization(), "U1");
- ComponentDto dto2 = ComponentTesting.newProjectDto(db.getDefaultOrganization(), "U2");
- ComponentDto dto3 = ComponentTesting.newProjectDto(db.getDefaultOrganization(), "U3");
+ ComponentDto dto1 = newProjectDto(db.getDefaultOrganization(), "U1");
+ ComponentDto dto2 = newProjectDto(db.getDefaultOrganization(), "U2");
+ ComponentDto dto3 = newProjectDto(db.getDefaultOrganization(), "U3");
underTest.insert(dbSession, dto1, dto2, dto3);
underTest.updateBEnabledToFalse(dbSession, asList("U1", "U2"));
@@ -1001,6 +1001,28 @@ public class ComponentDaoTest {
assertThat(components).extracting("organizationUuid").containsOnly(organizationDto.getUuid());
}
+ @Test
+ public void select_projects_by_name_query() {
+ OrganizationDto organizationDto = db.organizations().insert();
+ ComponentDto project1 = db.components().insertComponent(newProjectDto(organizationDto).setName("project1"));
+ ComponentDto module1 = db.components().insertComponent(newModuleDto(project1).setName("module1"));
+ ComponentDto subModule1 = db.components().insertComponent(newModuleDto(module1).setName("subModule1"));
+ ComponentDto file = db.components().insertComponent(newFileDto(subModule1).setName("file"));
+ ComponentDto project2 = db.components().insertComponent(newProjectDto(organizationDto).setName("project2"));
+ ComponentDto project3 = db.components().insertComponent(newProjectDto(organizationDto).setName("project3"));
+
+ assertThat(underTest.selectProjectsByNameQuery(dbSession, null, false)).extracting(ComponentDto::uuid)
+ .containsOnly(project1.uuid(), project2.uuid(), project3.uuid());
+ assertThat(underTest.selectProjectsByNameQuery(dbSession, null, true)).extracting(ComponentDto::uuid)
+ .containsOnly(project1.uuid(), project2.uuid(), project3.uuid(), module1.uuid(), subModule1.uuid());
+ assertThat(underTest.selectProjectsByNameQuery(dbSession, "project1", false)).extracting(ComponentDto::uuid).containsOnly(project1.uuid());
+ assertThat(underTest.selectProjectsByNameQuery(dbSession, "ct1", false)).extracting(ComponentDto::uuid).containsOnly(project1.uuid());
+ assertThat(underTest.selectProjectsByNameQuery(dbSession, "pro", false)).extracting(ComponentDto::uuid).containsOnly(project1.uuid(), project2.uuid(), project3.uuid());
+ assertThat(underTest.selectProjectsByNameQuery(dbSession, "jec", false)).extracting(ComponentDto::uuid).containsOnly(project1.uuid(), project2.uuid(), project3.uuid());
+ assertThat(underTest.selectProjectsByNameQuery(dbSession, "1", true)).extracting(ComponentDto::uuid).containsOnly(project1.uuid(), module1.uuid(), subModule1.uuid());
+ assertThat(underTest.selectProjectsByNameQuery(dbSession, "unknown", true)).extracting(ComponentDto::uuid).isEmpty();
+ }
+
private static ComponentTreeQuery.Builder newTreeQuery(String baseUuid) {
return ComponentTreeQuery.builder()
.setBaseUuid(baseUuid)