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.
*
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);
+ }
+ }
+
}
@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);
</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>
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");
--- /dev/null
+<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>