From ecdcea3dc953aaed896db1a2b9a3a0000831582e Mon Sep 17 00:00:00 2001 From: Pierre Date: Mon, 25 Sep 2023 18:20:40 +0200 Subject: [PATCH] SONAR-20315 update GroupDao queries --- .../it/java/org/sonar/db/user/GroupDaoIT.java | 23 ++++++++----------- .../main/java/org/sonar/db/user/GroupDao.java | 6 ++--- .../java/org/sonar/db/user/GroupMapper.java | 4 ++-- .../org/sonar/db/user/GroupMapper.xml | 7 +++++- .../server/usergroups/ws/SearchAction.java | 4 ++-- 5 files changed, 23 insertions(+), 21 deletions(-) diff --git a/server/sonar-db-dao/src/it/java/org/sonar/db/user/GroupDaoIT.java b/server/sonar-db-dao/src/it/java/org/sonar/db/user/GroupDaoIT.java index a904793fe6a..388ae8f2f0f 100644 --- a/server/sonar-db-dao/src/it/java/org/sonar/db/user/GroupDaoIT.java +++ b/server/sonar-db-dao/src/it/java/org/sonar/db/user/GroupDaoIT.java @@ -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 result = underTest.selectByQuery(dbSession, textSearchQuery("roup%_%/nam"), 0, 10); + List result = underTest.selectByQuery(dbSession, textSearchQuery("roup%_%/nam"), 1, 10); int resultCount = underTest.countByQuery(dbSession, textSearchQuery("roup%_%/nam")); assertThat(result).hasSize(1); diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/user/GroupDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/user/GroupDao.java index 5bd431d723c..96edc7daaba 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/user/GroupDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/user/GroupDao.java @@ -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 selectByQuery(DbSession session, GroupQuery query, int offset, int limit) { - return mapper(session).selectByQuery(query, new RowBounds(offset, limit)); + public List 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) { diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/user/GroupMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/user/GroupMapper.java index ef1879ba8ca..79f62d41645 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/user/GroupMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/user/GroupMapper.java @@ -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 selectByQuery(@Param("query") GroupQuery query, RowBounds rowBounds); + List selectByQuery(@Param("query") GroupQuery query, @Param("pagination") Pagination pagination); int countByQuery(@Param("query") GroupQuery query); diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/user/GroupMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/user/GroupMapper.xml index 77ed6c345c5..dfb733bea4f 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/user/GroupMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/user/GroupMapper.xml @@ -89,14 +89,19 @@ WHERE uuid=#{uuid} - select from groups g order by upper(g.name) + + + offset (#{pagination.startRowNumber,jdbcType=INTEGER}-1) rows fetch next #{pagination.pageSize,jdbcType=INTEGER} rows only + +