From: Pierre Date: Mon, 25 Sep 2023 15:34:38 +0000 (+0200) Subject: SONAR-20315 update GroupMembershipDao queries X-Git-Tag: 10.3.0.82913~327 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=d2c3a5ee01acce7b0a53fd2f106957240f9b0cb1;p=sonarqube.git SONAR-20315 update GroupMembershipDao queries --- diff --git a/server/sonar-db-dao/src/it/java/org/sonar/db/user/GroupMembershipDaoIT.java b/server/sonar-db-dao/src/it/java/org/sonar/db/user/GroupMembershipDaoIT.java index fd8a8f02bf1..f318c3382ec 100644 --- a/server/sonar-db-dao/src/it/java/org/sonar/db/user/GroupMembershipDaoIT.java +++ b/server/sonar-db-dao/src/it/java/org/sonar/db/user/GroupMembershipDaoIT.java @@ -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 result = underTest.selectMembers(db.getSession(), newQuery().groupUuid(group1.getUuid()).memberSearch("admin").build(), 0, 10); + List 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 result = underTest.selectMembers(db.getSession(), newQuery().groupUuid(group1.getUuid()).memberSearch("NaMe").build(), 0, 10); + List 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 result = underTest.selectMembers(db.getSession(), newQuery().groupUuid(group1.getUuid()).build(), 0, 10); + List 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 result = underTest.selectMembers(db.getSession(), newQuery().groupUuid(group1.getUuid()).build(), 0, 2); + List 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(); } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/user/GroupMembershipDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/user/GroupMembershipDao.java index 3ab01e17fdb..c0288482cd2 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/user/GroupMembershipDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/user/GroupMembershipDao.java @@ -20,38 +20,40 @@ 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 selectGroups(DbSession session, GroupMembershipQuery query, String userUuid, int offset, int limit) { - Map params = ImmutableMap.of(QUERY_PARAM_KEY, query, "userUuid", userUuid); - return mapper(session).selectGroups(params, new RowBounds(offset, limit)); + public List selectGroups(DbSession session, GroupMembershipQuery query, String userUuid) { + Map 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 params = ImmutableMap.of(QUERY_PARAM_KEY, query, "userUuid", userUuid); + Map params = Map.of(QUERY_PARAM_KEY, query, "userUuid", userUuid); return mapper(session).countGroups(params); } - public List selectMembers(DbSession session, UserMembershipQuery query, int offset, int limit) { - Map params = ImmutableMap.of(QUERY_PARAM_KEY, query, "groupUuid", query.groupUuid()); - return mapper(session).selectMembers(params, new RowBounds(offset, limit)); + public List selectMembers(DbSession session, UserMembershipQuery query) { + Map 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 params = ImmutableMap.of(QUERY_PARAM_KEY, query, "groupUuid", query.groupUuid()); + Map params = Map.of(QUERY_PARAM_KEY, query, "groupUuid", query.groupUuid()); return mapper(session).countMembers(params); } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/user/GroupMembershipMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/user/GroupMembershipMapper.java index 2fa1230cc36..4cf86050b5f 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/user/GroupMembershipMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/user/GroupMembershipMapper.java @@ -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 selectGroups(Map params, RowBounds rowBounds); + List selectGroups(Map params); int countGroups(Map params); - List selectMembers(Map params, RowBounds rowBounds); + List selectMembers(Map params); int countMembers(Map params); diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/user/GroupMembershipMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/user/GroupMembershipMapper.xml index 5234dec8aca..4812a9dfdca 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/user/GroupMembershipMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/user/GroupMembershipMapper.xml @@ -21,10 +21,11 @@ - SELECT g.uuid as uuid, g.name as name, g.description as description, gu.user_uuid as userUuid ORDER BY g.name + offset (#{pagination.startRowNumber,jdbcType=INTEGER}-1) rows fetch next #{pagination.pageSize,jdbcType=INTEGER} rows only +