]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-20315 update GroupMembershipDao queries
authorPierre <pierre.guillot@sonarsource.com>
Mon, 25 Sep 2023 15:34:38 +0000 (17:34 +0200)
committersonartech <sonartech@sonarsource.com>
Thu, 28 Sep 2023 20:03:12 +0000 (20:03 +0000)
server/sonar-db-dao/src/it/java/org/sonar/db/user/GroupMembershipDaoIT.java
server/sonar-db-dao/src/main/java/org/sonar/db/user/GroupMembershipDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/user/GroupMembershipMapper.java
server/sonar-db-dao/src/main/resources/org/sonar/db/user/GroupMembershipMapper.xml
server/sonar-webserver-webapi/src/main/java/org/sonar/server/user/ws/GroupsAction.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/usergroups/ws/UsersAction.java

index fd8a8f02bf1f94b531c4ae43e4cb1f9c7620529d..f318c3382ec07055296c4aaf4d989c286660e962 100644 (file)
@@ -87,17 +87,17 @@ public class GroupMembershipDaoIT {
     db.users().insertMember(group2, user2);
 
     // user1 is member of 3 groups
-    assertThat(underTest.selectGroups(db.getSession(), builder().membership(IN).build(), user1.getUuid(), 0, 10)).hasSize(3);
-    assertThat(underTest.selectGroups(db.getSession(), builder().membership(OUT).build(), user1.getUuid(), 0, 10)).isEmpty();
+    assertThat(underTest.selectGroups(db.getSession(), builder().membership(IN).build(), user1.getUuid())).hasSize(3);
+    assertThat(underTest.selectGroups(db.getSession(), builder().membership(OUT).build(), user1.getUuid())).isEmpty();
     // user2 is member of 1 group on 3
-    assertThat(underTest.selectGroups(db.getSession(), builder().membership(IN).build(), user2.getUuid(), 0, 10)).hasSize(1);
-    assertThat(underTest.selectGroups(db.getSession(), builder().membership(OUT).build(), user2.getUuid(), 0, 10)).hasSize(2);
+    assertThat(underTest.selectGroups(db.getSession(), builder().membership(IN).build(), user2.getUuid())).hasSize(1);
+    assertThat(underTest.selectGroups(db.getSession(), builder().membership(OUT).build(), user2.getUuid())).hasSize(2);
     // user3 is member of 0 group
-    assertThat(underTest.selectGroups(db.getSession(), builder().membership(IN).build(), user3.getUuid(), 0, 10)).isEmpty();
-    assertThat(underTest.selectGroups(db.getSession(), builder().membership(OUT).build(), user3.getUuid(), 0, 10)).hasSize(3);
+    assertThat(underTest.selectGroups(db.getSession(), builder().membership(IN).build(), user3.getUuid())).isEmpty();
+    assertThat(underTest.selectGroups(db.getSession(), builder().membership(OUT).build(), user3.getUuid())).hasSize(3);
     // unknown user is member of 0 group
-    assertThat(underTest.selectGroups(db.getSession(), builder().membership(IN).build(), "999", 0, 10)).isEmpty();
-    assertThat(underTest.selectGroups(db.getSession(), builder().membership(OUT).build(), "999", 0, 10)).hasSize(3);
+    assertThat(underTest.selectGroups(db.getSession(), builder().membership(IN).build(), "999")).isEmpty();
+    assertThat(underTest.selectGroups(db.getSession(), builder().membership(OUT).build(), "999")).hasSize(3);
   }
 
   @Test
@@ -159,13 +159,13 @@ public class GroupMembershipDaoIT {
     db.users().insertMember(group2, user2);
 
     // 100 has 1 member
-    assertThat(underTest.selectMembers(db.getSession(), newQuery().groupUuid(group1.getUuid()).membership(UserMembershipQuery.IN).build(), 0, 10)).hasSize(1);
+    assertThat(underTest.selectMembers(db.getSession(), newQuery().groupUuid(group1.getUuid()).membership(UserMembershipQuery.IN).build())).hasSize(1);
     // 101 has 2 members
-    assertThat(underTest.selectMembers(db.getSession(), newQuery().groupUuid(group2.getUuid()).membership(UserMembershipQuery.IN).build(), 0, 10)).hasSize(2);
+    assertThat(underTest.selectMembers(db.getSession(), newQuery().groupUuid(group2.getUuid()).membership(UserMembershipQuery.IN).build())).hasSize(2);
     // 102 has 1 member
-    assertThat(underTest.selectMembers(db.getSession(), newQuery().groupUuid(group3.getUuid()).membership(UserMembershipQuery.IN).build(), 0, 10)).hasSize(1);
+    assertThat(underTest.selectMembers(db.getSession(), newQuery().groupUuid(group3.getUuid()).membership(UserMembershipQuery.IN).build())).hasSize(1);
     // 103 has no member
-    assertThat(underTest.selectMembers(db.getSession(), newQuery().groupUuid(emptyGroup.getUuid()).membership(UserMembershipQuery.IN).build(), 0, 10)).isEmpty();
+    assertThat(underTest.selectMembers(db.getSession(), newQuery().groupUuid(emptyGroup.getUuid()).membership(UserMembershipQuery.IN).build())).isEmpty();
   }
 
   @Test
@@ -177,13 +177,13 @@ public class GroupMembershipDaoIT {
     db.users().insertMember(group2, user2);
 
     // 100 has 1 member
-    assertThat(underTest.selectMembers(db.getSession(), newQuery().groupUuid(group1.getUuid()).membership(UserMembershipQuery.OUT).build(), 0, 10)).hasSize(1);
+    assertThat(underTest.selectMembers(db.getSession(), newQuery().groupUuid(group1.getUuid()).membership(UserMembershipQuery.OUT).build())).hasSize(1);
     // 101 has 2 members
-    assertThat(underTest.selectMembers(db.getSession(), newQuery().groupUuid(group2.getUuid()).membership(UserMembershipQuery.OUT).build(), 0, 10)).isEmpty();
+    assertThat(underTest.selectMembers(db.getSession(), newQuery().groupUuid(group2.getUuid()).membership(UserMembershipQuery.OUT).build())).isEmpty();
     // 102 has 1 member
-    assertThat(underTest.selectMembers(db.getSession(), newQuery().groupUuid(group3.getUuid()).membership(UserMembershipQuery.OUT).build(), 0, 10)).hasSize(1);
+    assertThat(underTest.selectMembers(db.getSession(), newQuery().groupUuid(group3.getUuid()).membership(UserMembershipQuery.OUT).build())).hasSize(1);
     // 103 has no member
-    assertThat(underTest.selectMembers(db.getSession(), newQuery().groupUuid(emptyGroup.getUuid()).membership(UserMembershipQuery.OUT).build(), 0, 10)).hasSize(2);
+    assertThat(underTest.selectMembers(db.getSession(), newQuery().groupUuid(emptyGroup.getUuid()).membership(UserMembershipQuery.OUT).build())).hasSize(2);
   }
 
   @Test
@@ -193,13 +193,13 @@ public class GroupMembershipDaoIT {
     db.users().insertMember(group3, user1);
     db.users().insertMember(group2, user2);
 
-    List<UserMembershipDto> result = underTest.selectMembers(db.getSession(), newQuery().groupUuid(group1.getUuid()).memberSearch("admin").build(), 0, 10);
+    List<UserMembershipDto> result = underTest.selectMembers(db.getSession(), newQuery().groupUuid(group1.getUuid()).memberSearch("admin").build());
     assertThat(result).hasSize(2);
 
     assertThat(result.get(0).getName()).isEqualTo("Admin name");
     assertThat(result.get(1).getName()).isEqualTo("Not Admin");
 
-    result = underTest.selectMembers(db.getSession(), newQuery().groupUuid(group1.getUuid()).memberSearch("not").build(), 0, 10);
+    result = underTest.selectMembers(db.getSession(), newQuery().groupUuid(group1.getUuid()).memberSearch("not").build());
     assertThat(result).hasSize(1);
   }
 
@@ -211,13 +211,13 @@ public class GroupMembershipDaoIT {
     db.users().insertMember(group2, user2);
 
     // search is case insensitive only on name
-    List<UserMembershipDto> result = underTest.selectMembers(db.getSession(), newQuery().groupUuid(group1.getUuid()).memberSearch("NaMe").build(), 0, 10);
+    List<UserMembershipDto> result = underTest.selectMembers(db.getSession(), newQuery().groupUuid(group1.getUuid()).memberSearch("NaMe").build());
     assertThat(result).hasSize(1);
 
-    result = underTest.selectMembers(db.getSession(), newQuery().groupUuid(group1.getUuid()).memberSearch("login").build(), 0, 10);
+    result = underTest.selectMembers(db.getSession(), newQuery().groupUuid(group1.getUuid()).memberSearch("login").build());
     assertThat(result).hasSize(1);
 
-    result = underTest.selectMembers(db.getSession(), newQuery().groupUuid(group1.getUuid()).memberSearch("email").build(), 0, 10);
+    result = underTest.selectMembers(db.getSession(), newQuery().groupUuid(group1.getUuid()).memberSearch("email").build());
     assertThat(result).hasSize(1);
   }
 
@@ -228,7 +228,7 @@ public class GroupMembershipDaoIT {
     db.users().insertMember(group3, user1);
     db.users().insertMember(group2, user2);
 
-    List<UserMembershipDto> result = underTest.selectMembers(db.getSession(), newQuery().groupUuid(group1.getUuid()).build(), 0, 10);
+    List<UserMembershipDto> result = underTest.selectMembers(db.getSession(), newQuery().groupUuid(group1.getUuid()).build());
     assertThat(result).hasSize(2);
     assertThat(result.get(0).getName()).isEqualTo("Admin name");
     assertThat(result.get(1).getName()).isEqualTo("Not Admin");
@@ -241,16 +241,16 @@ public class GroupMembershipDaoIT {
     db.users().insertMember(group3, user1);
     db.users().insertMember(group2, user2);
 
-    List<UserMembershipDto> result = underTest.selectMembers(db.getSession(), newQuery().groupUuid(group1.getUuid()).build(), 0, 2);
+    List<UserMembershipDto> result = underTest.selectMembers(db.getSession(), newQuery().groupUuid(group1.getUuid()).pageSize(2).pageIndex(1).build());
     assertThat(result).hasSize(2);
     assertThat(result.get(0).getName()).isEqualTo("Admin name");
     assertThat(result.get(1).getName()).isEqualTo("Not Admin");
 
-    result = underTest.selectMembers(db.getSession(), newQuery().groupUuid("100").build(), 1, 2);
+    result = underTest.selectMembers(db.getSession(), newQuery().groupUuid("100").pageSize(1).pageIndex(2).build());
     assertThat(result).hasSize(1);
     assertThat(result.get(0).getName()).isEqualTo("Not Admin");
 
-    result = underTest.selectMembers(db.getSession(), newQuery().groupUuid("100").build(), 2, 1);
+    result = underTest.selectMembers(db.getSession(), newQuery().groupUuid("100").pageSize(1).pageIndex(3).build());
     assertThat(result).isEmpty();
   }
 
index 3ab01e17fdb2fb4dabdaa6bf3b68d7361babe6e1..c0288482cd2b3a1fc82c8796c5fc7c157985d8a8 100644 (file)
 package org.sonar.db.user;
 
 import com.google.common.collect.ArrayListMultimap;
-import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.Multimap;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
-import org.apache.ibatis.session.RowBounds;
 import org.sonar.db.Dao;
 import org.sonar.db.DbSession;
+import org.sonar.db.Pagination;
 
 import static org.sonar.db.DatabaseUtils.executeLargeInputs;
 
 public class GroupMembershipDao implements Dao {
   private static final String QUERY_PARAM_KEY = "query";
 
-  public List<GroupMembershipDto> selectGroups(DbSession session, GroupMembershipQuery query, String userUuid, int offset, int limit) {
-    Map<String, Object> params = ImmutableMap.of(QUERY_PARAM_KEY, query, "userUuid", userUuid);
-    return mapper(session).selectGroups(params, new RowBounds(offset, limit));
+  public List<GroupMembershipDto> selectGroups(DbSession session, GroupMembershipQuery query, String userUuid) {
+    Map<String, Object> params = Map.of(QUERY_PARAM_KEY, query,
+      "userUuid", userUuid,
+      "pagination", Pagination.forPage(query.pageIndex()).andSize(query.pageSize()));
+    return mapper(session).selectGroups(params);
   }
 
   public int countGroups(DbSession session, GroupMembershipQuery query, String userUuid) {
-    Map<String, Object> params = ImmutableMap.of(QUERY_PARAM_KEY, query, "userUuid", userUuid);
+    Map<String, Object> params = Map.of(QUERY_PARAM_KEY, query, "userUuid", userUuid);
     return mapper(session).countGroups(params);
   }
 
-  public List<UserMembershipDto> selectMembers(DbSession session, UserMembershipQuery query, int offset, int limit) {
-    Map<String, Object> params = ImmutableMap.of(QUERY_PARAM_KEY, query, "groupUuid", query.groupUuid());
-    return mapper(session).selectMembers(params, new RowBounds(offset, limit));
+  public List<UserMembershipDto> selectMembers(DbSession session, UserMembershipQuery query) {
+    Map<String, Object> params = Map.of(QUERY_PARAM_KEY, query, "groupUuid", query.groupUuid(),
+      "pagination", Pagination.forPage(query.pageIndex()).andSize(query.pageSize()));
+    return mapper(session).selectMembers(params);
   }
 
   public int countMembers(DbSession session, UserMembershipQuery query) {
-    Map<String, Object> params = ImmutableMap.of(QUERY_PARAM_KEY, query, "groupUuid", query.groupUuid());
+    Map<String, Object> params = Map.of(QUERY_PARAM_KEY, query, "groupUuid", query.groupUuid());
     return mapper(session).countMembers(params);
   }
 
index 2fa1230cc362fda69919669c40eae40604d52dc9..4cf86050b5f0b8921e14a87e2931b5a83b0b855d 100644 (file)
@@ -22,15 +22,14 @@ package org.sonar.db.user;
 import java.util.List;
 import java.util.Map;
 import org.apache.ibatis.annotations.Param;
-import org.apache.ibatis.session.RowBounds;
 
 public interface GroupMembershipMapper {
 
-  List<GroupMembershipDto> selectGroups(Map<String, Object> params, RowBounds rowBounds);
+  List<GroupMembershipDto> selectGroups(Map<String, Object> params);
 
   int countGroups(Map<String, Object> params);
 
-  List<UserMembershipDto> selectMembers(Map<String, Object> params, RowBounds rowBounds);
+  List<UserMembershipDto> selectMembers(Map<String, Object> params);
 
   int countMembers(Map<String, Object> params);
 
index 5234dec8aca463cda56baa0027b348b4bb65a146..4812a9dfdcafdcf896229d3e584a3427395e49f8 100644 (file)
     </where>
   </sql>
 
-  <select id="selectGroups" parameterType="map" resultType="GroupMembership">
+  <select id="selectGroups" resultType="GroupMembership">
     SELECT g.uuid as uuid, g.name as name, g.description as description, gu.user_uuid as userUuid
     <include refid="commonClauses"/>
     ORDER BY g.name
+    offset (#{pagination.startRowNumber,jdbcType=INTEGER}-1) rows fetch next #{pagination.pageSize,jdbcType=INTEGER} rows only
   </select>
 
   <select id="countGroups" parameterType="map" resultType="int">
     </where>
   </sql>
 
-  <select id="selectMembers" parameterType="map" resultType="org.sonar.db.user.UserMembershipDto">
+  <select id="selectMembers" resultType="org.sonar.db.user.UserMembershipDto">
     SELECT u.uuid as uuid, u.login as login, u.name as name, gu.group_uuid as groupUuid
     <include refid="userCommonClauses"/>
     ORDER BY u.name ASC
+    offset (#{pagination.startRowNumber,jdbcType=INTEGER}-1) rows fetch next #{pagination.pageSize,jdbcType=INTEGER} rows only
   </select>
 
   <select id="countMembers" parameterType="map" resultType="int">
index 31a2753dae4ea26f5323051cfe86b81486f2bf06..619409322bbaafeca2660766934a386ef281a35a 100644 (file)
@@ -108,7 +108,7 @@ public class GroupsAction implements UsersWsAction {
       UserDto user = checkFound(dbClient.userDao().selectActiveUserByLogin(dbSession, login), "Unknown user: %s", login);
       int total = dbClient.groupMembershipDao().countGroups(dbSession, query, user.getUuid());
       Paging paging = forPageIndex(query.pageIndex()).withPageSize(query.pageSize()).andTotal(total);
-      List<GroupMembershipDto> groups = dbClient.groupMembershipDao().selectGroups(dbSession, query, user.getUuid(), paging.offset(), query.pageSize());
+      List<GroupMembershipDto> groups = dbClient.groupMembershipDao().selectGroups(dbSession, query, user.getUuid());
       return buildResponse(groups, defaultGroupFinder.findDefaultGroup(dbSession), paging);
     }
   }
index 01fc9477cfd907dd08fa3af468abce734004096f..20bfcb93b451a9a76605200084b901fee55d3f4e 100644 (file)
@@ -104,12 +104,12 @@ public class UsersAction implements UserGroupsWsAction {
         .pageSize(pageSize)
         .build();
       int total = dbClient.groupMembershipDao().countMembers(dbSession, query);
-      Paging paging = forPageIndex(page).withPageSize(pageSize).andTotal(total);
-      List<UserMembershipDto> users = dbClient.groupMembershipDao().selectMembers(dbSession, query, paging.offset(), paging.pageSize());
+      List<UserMembershipDto> users = dbClient.groupMembershipDao().selectMembers(dbSession, query);
       Map<String, Boolean> userUuidToIsManaged = managedInstanceService.getUserUuidToManaged(dbSession, getUserUuids(users));
       try (JsonWriter json = response.newJsonWriter()) {
         json.beginObject();
         writeMembers(json, users, userUuidToIsManaged);
+        Paging paging = forPageIndex(page).withPageSize(pageSize).andTotal(total);
         writePaging(json, paging);
         json.name("paging").beginObject()
           .prop("pageIndex", page)