diff options
author | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2017-01-13 16:35:16 +0100 |
---|---|---|
committer | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2017-01-16 13:01:40 +0100 |
commit | b063b87fa456cb49cba2afde0e88bff83ecdaedc (patch) | |
tree | ebb11c98dfe5be7e87c1e856debd7686b5b983e6 /sonar-db/src/main/java/org/sonar | |
parent | e09bee5fed18be0ff8730c510ff3d72156957529 (diff) | |
download | sonarqube-b063b87fa456cb49cba2afde0e88bff83ecdaedc.tar.gz sonarqube-b063b87fa456cb49cba2afde0e88bff83ecdaedc.zip |
SONAR-8613 add filter on keys to OrganizationDao.selectByQuery
Diffstat (limited to 'sonar-db/src/main/java/org/sonar')
3 files changed, 76 insertions, 3 deletions
diff --git a/sonar-db/src/main/java/org/sonar/db/organization/OrganizationDao.java b/sonar-db/src/main/java/org/sonar/db/organization/OrganizationDao.java index e39a27d7157..2705c8355e4 100644 --- a/sonar-db/src/main/java/org/sonar/db/organization/OrganizationDao.java +++ b/sonar-db/src/main/java/org/sonar/db/organization/OrganizationDao.java @@ -46,8 +46,9 @@ public class OrganizationDao implements Dao { getMapper(dbSession).insert(organization); } - public List<OrganizationDto> selectByQuery(DbSession dbSession, int offset, int limit) { - return getMapper(dbSession).selectByQuery(offset, limit); + public List<OrganizationDto> selectByQuery(DbSession dbSession, OrganizationQuery organizationQuery, int offset, int limit) { + requireNonNull(organizationQuery, "organizationQuery can't be null"); + return getMapper(dbSession).selectByQuery(organizationQuery, offset, limit); } public Optional<OrganizationDto> selectByUuid(DbSession dbSession, String uuid) { diff --git a/sonar-db/src/main/java/org/sonar/db/organization/OrganizationMapper.java b/sonar-db/src/main/java/org/sonar/db/organization/OrganizationMapper.java index 7da4a027413..8a2b9b3c1fb 100644 --- a/sonar-db/src/main/java/org/sonar/db/organization/OrganizationMapper.java +++ b/sonar-db/src/main/java/org/sonar/db/organization/OrganizationMapper.java @@ -26,7 +26,8 @@ import org.apache.ibatis.annotations.Param; public interface OrganizationMapper { void insert(@Param("organization") OrganizationDto organization); - List<OrganizationDto> selectByQuery(@Param("offset") int offset, @Param("pageSize") int pageSize); + List<OrganizationDto> selectByQuery(@Param("query") OrganizationQuery organizationQuery, + @Param("offset") int offset, @Param("pageSize") int pageSize); @CheckForNull OrganizationDto selectByKey(@Param("key") String key); diff --git a/sonar-db/src/main/java/org/sonar/db/organization/OrganizationQuery.java b/sonar-db/src/main/java/org/sonar/db/organization/OrganizationQuery.java new file mode 100644 index 00000000000..29967eba2b8 --- /dev/null +++ b/sonar-db/src/main/java/org/sonar/db/organization/OrganizationQuery.java @@ -0,0 +1,71 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.db.organization; + +import java.util.Collection; +import java.util.Objects; +import java.util.Set; +import javax.annotation.CheckForNull; +import javax.annotation.Nullable; + +import static org.sonar.core.util.stream.Collectors.toSet; + +public class OrganizationQuery { + private static final OrganizationQuery NO_QUERY = newOrganizationQueryBuilder().build(); + private final Set<String> keys; + + private OrganizationQuery(Builder builder) { + this.keys = builder.keys; + } + + public static OrganizationQuery returnAll() { + return NO_QUERY; + } + + public static Builder newOrganizationQueryBuilder() { + return new Builder(); + } + + @CheckForNull + public Set<String> getKeys() { + return keys; + } + + public static class Builder { + private Set<String> keys; + + private Builder() { + // use static factory method + } + + public Builder setKeys(@Nullable Collection<String> keys) { + if (keys != null && !keys.isEmpty()) { + this.keys = keys.stream() + .filter(Objects::nonNull) + .collect(toSet(keys.size())); + } + return this; + } + + public OrganizationQuery build() { + return new OrganizationQuery(this); + } + } +} |