--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 SonarSource SA
+ * mailto:info 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;
+
+public class OrganizationCount {
+ private String organizationUuid;
+ private int memberCount;
+
+ public String getOrganizationUuid() {
+ return organizationUuid;
+ }
+
+ public OrganizationCount setOrganizationUuid(String organizationUuid) {
+ this.organizationUuid = organizationUuid;
+ return this;
+ }
+
+ public int getMemberCount() {
+ return memberCount;
+ }
+
+ public OrganizationCount setMemberCount(int memberCount) {
+ this.memberCount = memberCount;
+ return this;
+ }
+}
package org.sonar.db.organization;
+import com.google.common.collect.ImmutableMultiset;
+import com.google.common.collect.Multiset;
import java.util.List;
import java.util.Optional;
import java.util.Set;
return mapper(dbSession).selectOrganizationUuidsByUser(userId);
}
+ public Multiset<String> countByOrganizationUuids(DbSession dbSession, List<String> organizationUuids) {
+ ImmutableMultiset.Builder<String> counts = ImmutableMultiset.builder();
+ executeLargeInputsWithoutOutput(organizationUuids, list -> mapper(dbSession).countByOrganizationUuids(list, result -> {
+ OrganizationCount organizationUuidCount = (OrganizationCount) result.getResultObject();
+ counts.setCount(organizationUuidCount.getOrganizationUuid(), organizationUuidCount.getMemberCount());
+ }));
+
+ return counts.build();
+ }
+
/**
*
* @param loginOrganizationConsumer {@link BiConsumer}<String,String> (login, organization uuid)
import java.util.Map;
import java.util.Set;
import org.apache.ibatis.annotations.Param;
+import org.apache.ibatis.session.ResultHandler;
public interface OrganizationMemberMapper {
OrganizationMemberDto select(@Param("organizationUuid") String organizationUuid, @Param("userId") int userId);
void deleteByOrganization(@Param("organizationUuid") String organizationUuid);
void deleteByUserId(@Param("userId") int userId);
+
+ void countByOrganizationUuids(@Param("organizationUuids") List<String> organizationUuids, ResultHandler resultHandler);
}
inner join users u on om.user_id=u.id
</select>
+ <select id="countByOrganizationUuids" resultType="org.sonar.db.organization.OrganizationCount">
+ select om.organization_uuid as "organizationUuid", count(om.user_id) as "memberCount"
+ from organization_members om
+ where
+ om.organization_uuid in
+ <foreach collection="organizationUuids" open="(" close=")" item="organizationUuid" separator=",">
+ #{organizationUuid, jdbcType=VARCHAR}
+ </foreach>
+ group by om.organization_uuid
+ </select>
+
<insert id="insert" parameterType="OrganizationMember" useGeneratedKeys="false">
insert into organization_members
(
package org.sonar.db.organization;
+import com.google.common.collect.Multiset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.sonar.db.DbTester;
import org.sonar.db.user.UserDto;
+import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
import static org.assertj.core.api.Assertions.tuple;
assertThat(result).containsOnly(tuple("L_1", "ORG_1"), tuple("L_1", "ORG_2"), tuple("L_2", "ORG_1"));
}
+ @Test
+ public void count_by_organization_uuid() {
+ UserDto georgeOrwell = db.users().insertUser();
+ UserDto rayBradbury = db.users().insertUser();
+ UserDto frankHerbert = db.users().insertUser();
+ OrganizationDto org1 = db.organizations().insert();
+ db.organizations().addMember(org1, georgeOrwell);
+ db.organizations().addMember(org1, rayBradbury);
+ db.organizations().addMember(org1, frankHerbert);
+ OrganizationDto org2 = db.organizations().insert();
+ db.organizations().addMember(org2, georgeOrwell);
+ OrganizationDto org3 = db.organizations().insert();
+
+ Multiset<String> result = underTest.countByOrganizationUuids(dbSession, asList(org1.getUuid(), org2.getUuid(), org3.getUuid()));
+
+ assertThat(result.count(org1.getUuid())).isEqualTo(3);
+ assertThat(result.count(org2.getUuid())).isEqualTo(1);
+ assertThat(result.count(org3.getUuid())).isEqualTo(0);
+ assertThat(result.count("ORG_42")).isEqualTo(0);
+ }
+
@Test
public void insert() {
underTest.insert(dbSession, create("O_1", 256));