]> source.dussan.org Git - sonarqube.git/commitdiff
Add UserDao.selectByIds
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Fri, 15 Apr 2016 07:12:13 +0000 (09:12 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Fri, 15 Apr 2016 07:32:14 +0000 (09:32 +0200)
sonar-db/src/main/java/org/sonar/db/user/UserDao.java
sonar-db/src/main/java/org/sonar/db/user/UserMapper.java
sonar-db/src/main/resources/org/sonar/db/user/UserMapper.xml
sonar-db/src/test/java/org/sonar/db/user/UserDaoTest.java
sonar-db/src/test/resources/org/sonar/db/user/UserDaoTest/selectUsersByIds.xml [new file with mode: 0644]

index 5d0eada9cc7f1af3232df65b9ed6137685f5e12b..7d746d50bdf05679feeebf04b72a436568213268 100644 (file)
@@ -61,6 +61,16 @@ public class UserDao implements Dao {
     return session.getMapper(UserMapper.class).selectUser(userId);
   }
 
+  /**
+   * Select users by ids, including disabled users. An empty list is returned
+   * if list of ids is empty, without any db round trips.
+   *
+   * Used by the Governance plugin
+   */
+  public List<UserDto> selectByIds(DbSession session, Collection<Long> ids) {
+    return DatabaseUtils.executeLargeInputs(ids, new SelectByIds(mapper(session)));
+  }
+
   /**
    * Search for user by login. Disabled users are ignored.
    *
@@ -219,4 +229,18 @@ public class UserDao implements Dao {
       return map.get(login);
     }
   }
+
+  private static class SelectByIds implements Function<List<Long>, List<UserDto>> {
+    private final UserMapper mapper;
+
+    private SelectByIds(UserMapper mapper) {
+      this.mapper = mapper;
+    }
+
+    @Override
+    public List<UserDto> apply(@Nonnull List<Long> partitionOfLogins) {
+      return mapper.selectByIds(partitionOfLogins);
+    }
+  }
+
 }
index e3c32da3ad664f31388b0cae3e5d6ea794856e36..e577c57e2419533cbcb94d714a70f232e05afa5e 100644 (file)
@@ -45,14 +45,11 @@ public interface UserMapper {
   @CheckForNull
   UserDto selectUserByLogin(String login);
 
-  List<UserDto> selectUsersByLogins(@Param("logins") List<String> logins);
-
   List<UserDto> selectUsers(UserQuery query);
 
   List<UserDto> selectByLogins(List<String> logins);
 
-  @CheckForNull
-  GroupDto selectGroupByName(String name);
+  List<UserDto> selectByIds(@Param("ids") List<Long> ids);
 
   long countByEmail(String email);
 
index 7d9e4d692307576af4048b2edb7bccc165b820a6..36f618867728c21f497a6f4a394836bdb0fcaf4a 100644 (file)
     </foreach>
   </select>
 
-  <select id="selectUsersByLogins" parameterType="map" resultType="User">
+  <select id="selectByIds" parameterType="string" resultType="User">
     SELECT
     <include refid="userColumns"/>
-    FROM users u WHERE
-    (<foreach item="login" index="index" collection="logins" open="(" separator=" or " close=")">
-    u.login=#{login}
-  </foreach>)
+    FROM users u
+    WHERE u.id in
+    <foreach collection="ids" open="(" close=")" item="id" separator=",">
+      #{id}
+    </foreach>
   </select>
 
   <select id="selectUsers" parameterType="map" resultType="User">
     where lower(u.email)=#{email} AND u.active=${_true}
   </select>
 
-  <select id="selectGroupByName" parameterType="string" resultType="Group">
-    SELECT id, name, description, created_at AS "createdAt", updated_at AS "updatedAt"
-    FROM groups WHERE name=#{id}
-  </select>
-
   <delete id="removeUserFromGroups" parameterType="long">
     DELETE FROM groups_users WHERE user_id=#{id}
   </delete>
index 6afef4c64b3e751284ad830c5dee7a9d4bf67c11..2e5f9ecd41bee5a770c574cf1aed5cff97b0e893 100644 (file)
@@ -76,6 +76,17 @@ public class UserDaoTest {
     when(system2.now()).thenReturn(NOW);
   }
 
+  @Test
+  public void selectUsersIds() {
+    db.prepareDbUnit(getClass(), "selectUsersByIds.xml");
+
+    Collection<UserDto> users = underTest.selectByIds(session, asList(100L, 101L, 987L));
+    assertThat(users).hasSize(2);
+    assertThat(users).extracting("login").containsOnly("marius", "inactive_user");
+
+    assertThat(underTest.selectByIds(session, Collections.<Long>emptyList())).isEmpty();
+  }
+
   @Test
   public void selectUserByLogin_ignore_inactive() {
     db.prepareDbUnit(getClass(), "selectActiveUserByLogin.xml");
diff --git a/sonar-db/src/test/resources/org/sonar/db/user/UserDaoTest/selectUsersByIds.xml b/sonar-db/src/test/resources/org/sonar/db/user/UserDaoTest/selectUsersByIds.xml
new file mode 100644 (file)
index 0000000..3be84a2
--- /dev/null
@@ -0,0 +1,9 @@
+<dataset>
+  <users id="100" login="inactive_user" name="Disabled" email="inactive@lesbronzes.fr" created_at="1418215735482"
+         updated_at="1418215735485" active="[false]"/>
+  <users id="101" login="marius" name="Marius" email="marius@lesbronzes.fr" created_at="1418215735482"
+         updated_at="1418215735485" active="[true]"/>
+  <users id="102" login="jcdus" name="Jean-Claude Dus" email="jcdus@lesbronzes.fr" created_at="1418215735482"
+         updated_at="1418215735485" active="[true]"/>
+
+</dataset>