]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-20315 update GroupDao queries
authorPierre <pierre.guillot@sonarsource.com>
Mon, 25 Sep 2023 16:20:40 +0000 (18:20 +0200)
committersonartech <sonartech@sonarsource.com>
Thu, 28 Sep 2023 20:03:13 +0000 (20:03 +0000)
server/sonar-db-dao/src/it/java/org/sonar/db/user/GroupDaoIT.java
server/sonar-db-dao/src/main/java/org/sonar/db/user/GroupDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/user/GroupMapper.java
server/sonar-db-dao/src/main/resources/org/sonar/db/user/GroupMapper.xml
server/sonar-webserver-webapi/src/main/java/org/sonar/server/usergroups/ws/SearchAction.java

index a904793fe6a528995e8980159732485f2e79b0c1..388ae8f2f0f45b0a8b9ccf70a098ea68297e325e 100644 (file)
@@ -155,34 +155,31 @@ public class GroupDaoIT {
      */
 
     // Null query
-    assertThat(underTest.selectByQuery(dbSession, EMPTY_QUERY, 0, 10))
+    assertThat(underTest.selectByQuery(dbSession, EMPTY_QUERY, 1, 10))
       .hasSize(5)
       .extracting("name").containsOnly("customers-group1", "customers-group2", "customers-group3", "SONAR-ADMINS", "sonar-users");
 
     // Empty query
-    assertThat(underTest.selectByQuery(dbSession, textSearchQuery(""), 0, 10))
+    assertThat(underTest.selectByQuery(dbSession, textSearchQuery(""), 1, 10))
       .hasSize(5)
       .extracting("name").containsOnly("customers-group1", "customers-group2", "customers-group3", "SONAR-ADMINS", "sonar-users");
 
     // Filter on name
-    assertThat(underTest.selectByQuery(dbSession, textSearchQuery("sonar"), 0, 10))
+    assertThat(underTest.selectByQuery(dbSession, textSearchQuery("sonar"), 1, 10))
       .hasSize(2)
       .extracting("name").containsOnly("SONAR-ADMINS", "sonar-users");
 
     //Filter on name and additionalClause
-    assertThat(underTest.selectByQuery(dbSession, textSearchAndManagedClauseQuery("sonar", " name = 'SONAR-ADMINS'"), 0, 10))
+    assertThat(underTest.selectByQuery(dbSession, textSearchAndManagedClauseQuery("sonar", " name = 'SONAR-ADMINS'"), 1, 10))
       .hasSize(1)
       .extracting("name").containsOnly("SONAR-ADMINS");
 
     // Pagination
-    assertThat(underTest.selectByQuery(dbSession, EMPTY_QUERY, 0, 3))
-      .hasSize(3);
-    assertThat(underTest.selectByQuery(dbSession, EMPTY_QUERY, 3, 3))
-      .hasSize(2);
-    assertThat(underTest.selectByQuery(dbSession, EMPTY_QUERY, 6, 3)).isEmpty();
-    assertThat(underTest.selectByQuery(dbSession, EMPTY_QUERY, 0, 5))
-      .hasSize(5);
-    assertThat(underTest.selectByQuery(dbSession, EMPTY_QUERY, 5, 5)).isEmpty();
+    assertThat(underTest.selectByQuery(dbSession, EMPTY_QUERY, 1, 3)).hasSize(3);
+    assertThat(underTest.selectByQuery(dbSession, EMPTY_QUERY, 2, 3)).hasSize(2);
+    assertThat(underTest.selectByQuery(dbSession, EMPTY_QUERY, 3, 3)).isEmpty();
+    assertThat(underTest.selectByQuery(dbSession, EMPTY_QUERY, 1, 5)).hasSize(5);
+    assertThat(underTest.selectByQuery(dbSession, EMPTY_QUERY, 4, 5)).isEmpty();
   }
 
   private static GroupQuery textSearchQuery(String query) {
@@ -199,7 +196,7 @@ public class GroupDaoIT {
     underTest.insert(dbSession, newGroupDto().setName(groupNameWithSpecialCharacters));
     db.commit();
 
-    List<GroupDto> result = underTest.selectByQuery(dbSession, textSearchQuery("roup%_%/nam"), 0, 10);
+    List<GroupDto> result = underTest.selectByQuery(dbSession, textSearchQuery("roup%_%/nam"), 1, 10);
     int resultCount = underTest.countByQuery(dbSession, textSearchQuery("roup%_%/nam"));
 
     assertThat(result).hasSize(1);
index 5bd431d723c8bd5d1968ae453ad2489e1cfcdded..96edc7daabaa4be7b8376c33359950ba65f93d12 100644 (file)
@@ -24,10 +24,10 @@ import java.util.Date;
 import java.util.List;
 import java.util.Optional;
 import javax.annotation.CheckForNull;
-import org.apache.ibatis.session.RowBounds;
 import org.sonar.api.utils.System2;
 import org.sonar.db.Dao;
 import org.sonar.db.DbSession;
+import org.sonar.db.Pagination;
 import org.sonar.db.audit.AuditPersister;
 import org.sonar.db.audit.model.UserGroupNewValue;
 
@@ -77,8 +77,8 @@ public class GroupDao implements Dao {
     return mapper(session).countByQuery(query);
   }
 
-  public List<GroupDto> selectByQuery(DbSession session, GroupQuery query, int offset, int limit) {
-    return mapper(session).selectByQuery(query, new RowBounds(offset, limit));
+  public List<GroupDto> selectByQuery(DbSession session, GroupQuery query, int page, int pageSize) {
+    return mapper(session).selectByQuery(query, Pagination.forPage(page).andSize(pageSize));
   }
 
   public GroupDto insert(DbSession session, GroupDto item) {
index ef1879ba8ca7660e43ed8acb662f27ffbc17bb92..79f62d41645d2233db67cea277653727fd8485e9 100644 (file)
@@ -22,7 +22,7 @@ package org.sonar.db.user;
 import java.util.List;
 import javax.annotation.CheckForNull;
 import org.apache.ibatis.annotations.Param;
-import org.apache.ibatis.session.RowBounds;
+import org.sonar.db.Pagination;
 
 public interface GroupMapper {
 
@@ -37,7 +37,7 @@ public interface GroupMapper {
 
   void update(GroupDto item);
 
-  List<GroupDto> selectByQuery(@Param("query") GroupQuery query, RowBounds rowBounds);
+  List<GroupDto> selectByQuery(@Param("query") GroupQuery query, @Param("pagination") Pagination pagination);
 
   int countByQuery(@Param("query") GroupQuery query);
 
index 77ed6c345c529764d5d8b4fdcb656e153c3a1bf7..dfb733bea4f1a3f931cf7b6ac7227bed84075235 100644 (file)
     WHERE uuid=#{uuid}
   </update>
 
-  <select id="selectByQuery" parameterType="map" resultType="Group">
+  <select id="selectByQuery" resultType="Group">
     select
     <include refid="groupColumns"/>
     from groups g
     <include refid="searchByQueryWhereClause"/>
     order by upper(g.name)
+    <include refid="pagination"/>
   </select>
 
+  <sql id="pagination">
+    offset (#{pagination.startRowNumber,jdbcType=INTEGER}-1) rows fetch next #{pagination.pageSize,jdbcType=INTEGER} rows only
+  </sql>
+
   <select id="countByQuery" parameterType="map" resultType="int">
     select count(1)
     from groups g
index fc91e750c0a68386aa56cc5a5dc9c35d3a3c0af1..853e20538f712ae68e8e738d21f9a034b976f553 100644 (file)
@@ -114,11 +114,11 @@ public class SearchAction implements UserGroupsWsAction {
       GroupDto defaultGroup = defaultGroupFinder.findDefaultGroup(dbSession);
 
       int limit = dbClient.groupDao().countByQuery(dbSession, query);
-      Paging paging = forPageIndex(page).withPageSize(pageSize).andTotal(limit);
-      List<GroupDto> groups = dbClient.groupDao().selectByQuery(dbSession, query, options.getOffset(), pageSize);
+      List<GroupDto> groups = dbClient.groupDao().selectByQuery(dbSession, query, page, pageSize);
       List<String> groupUuids = extractGroupUuids(groups);
       Map<String, Boolean> groupUuidToIsManaged = managedInstanceService.getGroupUuidToManaged(dbSession, new HashSet<>(groupUuids));
       Map<String, Integer> userCountByGroup = dbClient.groupMembershipDao().countUsersByGroups(dbSession, groupUuids);
+      Paging paging = forPageIndex(page).withPageSize(pageSize).andTotal(limit);
       writeProtobuf(buildResponse(groups, userCountByGroup, groupUuidToIsManaged, fields, paging, defaultGroup), request, response);
     }
   }