]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8838 Update tags of a project in DB
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Mon, 27 Feb 2017 15:34:40 +0000 (16:34 +0100)
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Tue, 28 Feb 2017 10:29:30 +0000 (11:29 +0100)
server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentMapper.java
server/sonar-db-dao/src/main/resources/org/sonar/db/component/ComponentMapper.xml
server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentDaoTest.java

index c1f85e478de14536c0591db1dbe63ffef4200d57..87678ede9b3a637969006a6bd2e6a14cf4e94186 100644 (file)
@@ -17,6 +17,7 @@
  * along with this program; if not, write to the Free Software Foundation,
  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  */
+
 package org.sonar.db.component;
 
 import com.google.common.base.Optional;
@@ -49,6 +50,34 @@ import static org.sonar.db.WildcardPosition.BEFORE_AND_AFTER;
 
 public class ComponentDao implements Dao {
 
+  private static List<ComponentDto> selectByQueryImpl(DbSession session, @Nullable String organizationUuid, ComponentQuery query, int offset, int limit) {
+    Set<Long> componentIds = query.getComponentIds();
+    if (componentIds != null && componentIds.isEmpty()) {
+      return emptyList();
+    }
+    return mapper(session).selectByQuery(organizationUuid, query, new RowBounds(offset, limit));
+  }
+
+  private static int countByQueryImpl(DbSession session, @Nullable String organizationUuid, ComponentQuery query) {
+    Set<Long> componentIds = query.getComponentIds();
+    if (componentIds != null && componentIds.isEmpty()) {
+      return 0;
+    }
+    return mapper(session).countByQuery(organizationUuid, query);
+  }
+
+  @CheckForNull
+  private static String buildUpperLikeSql(@Nullable String textQuery) {
+    if (isBlank(textQuery)) {
+      return null;
+    }
+    return buildLikeValue(textQuery.toUpperCase(Locale.ENGLISH), BEFORE_AND_AFTER);
+  }
+
+  private static ComponentMapper mapper(DbSession session) {
+    return session.getMapper(ComponentMapper.class);
+  }
+
   public ComponentDto selectOrFailById(DbSession session, long id) {
     Optional<ComponentDto> componentDto = selectById(session, id);
     if (!componentDto.isPresent()) {
@@ -82,14 +111,6 @@ public class ComponentDao implements Dao {
     return selectByQueryImpl(dbSession, organizationUuid, query, offset, limit);
   }
 
-  private static List<ComponentDto> selectByQueryImpl(DbSession session, @Nullable String organizationUuid, ComponentQuery query, int offset, int limit) {
-    Set<Long> componentIds = query.getComponentIds();
-    if (componentIds != null && componentIds.isEmpty()) {
-      return emptyList();
-    }
-    return mapper(session).selectByQuery(organizationUuid, query, new RowBounds(offset, limit));
-  }
-
   public int countByQuery(DbSession session, ComponentQuery query) {
     return countByQueryImpl(session, null, query);
   }
@@ -99,14 +120,6 @@ public class ComponentDao implements Dao {
     return countByQueryImpl(session, organizationUuid, query);
   }
 
-  private static int countByQueryImpl(DbSession session, @Nullable String organizationUuid, ComponentQuery query) {
-    Set<Long> componentIds = query.getComponentIds();
-    if (componentIds != null && componentIds.isEmpty()) {
-      return 0;
-    }
-    return mapper(session).countByQuery(organizationUuid, query);
-  }
-
   public List<ComponentDto> selectSubProjectsByComponentUuids(DbSession session, Collection<String> keys) {
     if (keys.isEmpty()) {
       return emptyList();
@@ -252,14 +265,6 @@ public class ComponentDao implements Dao {
     return mapper(dbSession).countProvisioned(organizationUuid, buildUpperLikeSql(textQuery), qualifiers);
   }
 
-  @CheckForNull
-  private static String buildUpperLikeSql(@Nullable String textQuery) {
-    if (isBlank(textQuery)) {
-      return null;
-    }
-    return buildLikeValue(textQuery.toUpperCase(Locale.ENGLISH), BEFORE_AND_AFTER);
-  }
-
   public List<ComponentDto> selectGhostProjects(DbSession session, String organizationUuid, @Nullable String query, int offset, int limit) {
     return mapper(session).selectGhostProjects(organizationUuid, buildUpperLikeSql(query), new RowBounds(offset, limit));
   }
@@ -321,6 +326,10 @@ public class ComponentDao implements Dao {
     mapper(session).update(component);
   }
 
+  public void updateTags(DbSession session, ComponentDto component) {
+    mapper(session).updateTags(component);
+  }
+
   public void updateBEnabledToFalse(DbSession session, Collection<String> uuids) {
     executeLargeUpdates(uuids, mapper(session)::updateBEnabledToFalse);
   }
@@ -336,8 +345,4 @@ public class ComponentDao implements Dao {
   public void delete(DbSession session, long componentId) {
     mapper(session).delete(componentId);
   }
-
-  private static ComponentMapper mapper(DbSession session) {
-    return session.getMapper(ComponentMapper.class);
-  }
 }
index 1dbcc9bea8e2ede953aebf0b2f901028b65e7dfd..6a609e37f2687f8d36bca59cead9d692eddddef1 100644 (file)
@@ -17,6 +17,7 @@
  * along with this program; if not, write to the Free Software Foundation,
  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  */
+
 package org.sonar.db.component;
 
 import java.util.Collection;
@@ -142,4 +143,6 @@ public interface ComponentMapper {
   void resetBChangedForRootComponentUuid(@Param("projectUuid") String projectUuid);
 
   void delete(long componentId);
+
+  void updateTags(ComponentDto component);
 }
index e20ed6318bfaeadda2a26d52921e6bed9b5c679c..85229fc27c24847e55a421b90a83bc893f546f08 100644 (file)
@@ -1,4 +1,24 @@
 <?xml version="1.0" encoding="UTF-8" ?>
+<!--
+  ~ SonarQube
+  ~ Copyright (C) 2009-2017 SonarSource SA
+  ~ mailto:info AT sonarsource DOT com
+  ~
+  ~ This program 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.
+  ~
+  ~ This program 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 this program; if not, write to the Free Software Foundation,
+  ~ Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+  -->
+
 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 <mapper namespace="org.sonar.db.component.ComponentMapper">
 
     )
   </insert>
 
+  <update id="updateTags" parameterType="Component" useGeneratedKeys="false">
+    update projects set
+    tags = #{tagsString,jdbcType=VARCHAR}
+    where
+    uuid = #{uuid,jdbcType=VARCHAR}
+  </update>
+
   <update id="update" parameterType="org.sonar.db.component.ComponentUpdateDto" useGeneratedKeys="false">
     update projects set
     b_changed = #{bChanged,jdbcType=BOOLEAN},
     uuid in <foreach collection="uuids" open="(" close=")" item="uuid" separator=",">#{uuid,jdbcType=VARCHAR}</foreach>
   </update>
 
-
   <update id="applyBChangesForRootComponentUuid" parameterType="string" useGeneratedKeys="false">
     update projects set
     copy_component_uuid = b_copy_component_uuid,
index e2fc063c2ea0fad1592a9f30248ac140a8e6fa5f..cc76f0b80e10acd5e2103fd98d351291d9573e68 100644 (file)
@@ -17,6 +17,7 @@
  * along with this program; if not, write to the Free Software Foundation,
  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  */
+
 package org.sonar.db.component;
 
 import com.google.common.base.Optional;
@@ -44,6 +45,7 @@ import org.sonar.db.organization.OrganizationDto;
 import static com.google.common.collect.Lists.newArrayList;
 import static com.google.common.collect.Sets.newHashSet;
 import static java.util.Arrays.asList;
+import static java.util.Collections.emptyList;
 import static java.util.Collections.emptySet;
 import static java.util.Collections.singletonList;
 import static org.assertj.core.api.Assertions.assertThat;
@@ -77,6 +79,12 @@ public class ComponentDaoTest {
   private DbSession dbSession = db.getSession();
   private ComponentDao underTest = new ComponentDao();
 
+  private static ComponentTreeQuery.Builder newTreeQuery(String baseUuid) {
+    return ComponentTreeQuery.builder()
+      .setBaseUuid(baseUuid)
+      .setStrategy(CHILDREN);
+  }
+
   @Test
   public void get_by_uuid() {
     db.prepareDbUnit(getClass(), "shared.xml");
@@ -350,7 +358,7 @@ public class ComponentDaoTest {
     expectedException.expectMessage("Qualifiers cannot be empty");
 
     db.prepareDbUnit(getClass(), "shared.xml");
-    underTest.selectComponentsByQualifiers(dbSession, Collections.<String>emptySet());
+    underTest.selectComponentsByQualifiers(dbSession, Collections.emptySet());
   }
 
   @Test
@@ -385,7 +393,7 @@ public class ComponentDaoTest {
 
     assertThat(underTest.selectSubProjectsByComponentUuids(dbSession, newArrayList("unknown"))).isEmpty();
 
-    assertThat(underTest.selectSubProjectsByComponentUuids(dbSession, Collections.<String>emptyList())).isEmpty();
+    assertThat(underTest.selectSubProjectsByComponentUuids(dbSession, Collections.emptyList())).isEmpty();
   }
 
   @Test
@@ -697,8 +705,7 @@ public class ComponentDaoTest {
       .setEnabled(true)
       .setCreatedAt(DateUtils.parseDate("2014-06-18"))
       .setAuthorizationUpdatedAt(123456789L)
-      .setTags(newArrayList("platform", "analyzers"))
-      ;
+      .setTags(newArrayList("platform", "analyzers"));
 
     underTest.insert(dbSession, componentDto);
     dbSession.commit();
@@ -824,6 +831,16 @@ public class ComponentDaoTest {
         "from projects where uuid='" + uuid + "'");
   }
 
+  @Test
+  public void update_tags() {
+    ComponentDto project = db.components().insertProject(p -> p.setTags(emptyList()));
+
+    underTest.updateTags(dbSession, project.setTags(newArrayList("finance", "toto", "tutu")));
+    dbSession.commit();
+
+    assertThat(underTest.selectOrFailByKey(dbSession, project.key()).getTags()).containsOnly("finance", "toto", "tutu");
+  }
+
   @Test
   public void delete() throws Exception {
     ComponentDto project1 = db.components().insertProject(db.getDefaultOrganization(), (t) -> t.setKey("PROJECT_1"));
@@ -1124,10 +1141,4 @@ public class ComponentDaoTest {
     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)
-      .setStrategy(CHILDREN);
-  }
 }