@@ -24,6 +24,7 @@ import java.util.LinkedList; | |||
import java.util.List; | |||
import java.util.Map; | |||
import java.util.Optional; | |||
import java.util.Set; | |||
import org.sonar.api.utils.System2; | |||
import org.sonar.db.Dao; | |||
import org.sonar.db.DbSession; | |||
@@ -81,6 +82,13 @@ public class BranchDao implements Dao { | |||
return Optional.ofNullable(mapper(dbSession).selectByKey(projectUuid, key, branchType)); | |||
} | |||
public List<BranchDto> selectByKeys(DbSession dbSession, String projectUuid, Set<String> branchKeys) { | |||
if (branchKeys.isEmpty()) { | |||
return emptyList(); | |||
} | |||
return executeLargeInputs(branchKeys, partition -> mapper(dbSession).selectByKeys(projectUuid, branchKeys)); | |||
} | |||
public Collection<BranchDto> selectByComponent(DbSession dbSession, ComponentDto component) { | |||
String projectUuid = component.getMainBranchProjectUuid(); | |||
if (projectUuid == null) { |
@@ -22,6 +22,7 @@ package org.sonar.db.component; | |||
import java.util.Collection; | |||
import java.util.List; | |||
import java.util.Map; | |||
import java.util.Set; | |||
import javax.annotation.Nullable; | |||
import org.apache.ibatis.annotations.Param; | |||
@@ -40,6 +41,8 @@ public interface BranchMapper { | |||
BranchDto selectByKey(@Param("projectUuid") String projectUuid, @Param("key") String key, @Param("branchType") BranchType branchType); | |||
List<BranchDto> selectByKeys(@Param("projectUuid") String projectUuid, @Param("keys") Set<String> branchKeys); | |||
BranchDto selectByUuid(@Param("uuid") String uuid); | |||
Collection<BranchDto> selectByProjectUuid(@Param("projectUuid") String projectUuid); |
@@ -83,6 +83,17 @@ | |||
pb.branch_type = #{branchType, jdbcType=VARCHAR} | |||
</select> | |||
<select id="selectByKeys" resultType="org.sonar.db.component.BranchDto"> | |||
select <include refid="columns"/> | |||
from project_branches pb | |||
where | |||
pb.project_uuid = #{projectUuid, jdbcType=VARCHAR} and | |||
pb.kee in | |||
<foreach collection="keys" open="(" close=")" item="key" separator=","> | |||
#{key,jdbcType=VARCHAR} | |||
</foreach> | |||
</select> | |||
<select id="selectByBranchKeys" resultType="org.sonar.db.component.BranchDto"> | |||
select | |||
<include refid="columns"/> |
@@ -23,10 +23,12 @@ import com.google.common.collect.Sets; | |||
import com.tngtech.java.junit.dataprovider.DataProvider; | |||
import com.tngtech.java.junit.dataprovider.DataProviderRunner; | |||
import java.util.Collection; | |||
import java.util.Collections; | |||
import java.util.HashMap; | |||
import java.util.List; | |||
import java.util.Map; | |||
import java.util.Optional; | |||
import java.util.Set; | |||
import java.util.stream.Collectors; | |||
import java.util.stream.IntStream; | |||
import org.junit.Rule; | |||
@@ -462,6 +464,40 @@ public class BranchDaoTest { | |||
assertThat(underTest.selectByPullRequestKey(dbSession, "U3", pullRequestId)).isEmpty(); | |||
} | |||
@Test | |||
public void selectByKeys() { | |||
BranchDto mainBranch = new BranchDto() | |||
.setProjectUuid("U1") | |||
.setUuid("U1") | |||
.setBranchType(BranchType.BRANCH) | |||
.setKey("master"); | |||
underTest.insert(dbSession, mainBranch); | |||
BranchDto featureBranch = new BranchDto() | |||
.setProjectUuid("U1") | |||
.setUuid("U2") | |||
.setBranchType(BranchType.BRANCH) | |||
.setKey("feature1"); | |||
underTest.insert(dbSession, featureBranch); | |||
String pullRequestId = "123"; | |||
BranchDto pullRequest = new BranchDto() | |||
.setProjectUuid("U1") | |||
.setUuid("U3") | |||
.setBranchType(BranchType.PULL_REQUEST) | |||
.setKey(pullRequestId) | |||
.setMergeBranchUuid("U4"); | |||
underTest.insert(dbSession, pullRequest); | |||
assertThat(underTest.selectByKeys(dbSession, "U1", Collections.emptySet())) | |||
.isEmpty(); | |||
List<BranchDto> loaded = underTest.selectByKeys(dbSession, "U1", Set.of(mainBranch.getKey(), featureBranch.getKey())); | |||
assertThat(loaded) | |||
.extracting(BranchDto::getUuid) | |||
.containsExactlyInAnyOrder(mainBranch.getUuid(), featureBranch.getUuid()); | |||
} | |||
@Test | |||
public void selectByUuids() { | |||
ComponentDto project = db.components().insertPrivateProject(); |