diff options
Diffstat (limited to 'sonar-db')
7 files changed, 125 insertions, 14 deletions
diff --git a/sonar-db/src/main/java/org/sonar/db/component/ComponentDtoFunctions.java b/sonar-db/src/main/java/org/sonar/db/component/ComponentDtoFunctions.java index c75518ebf86..9116cc9eac0 100644 --- a/sonar-db/src/main/java/org/sonar/db/component/ComponentDtoFunctions.java +++ b/sonar-db/src/main/java/org/sonar/db/component/ComponentDtoFunctions.java @@ -34,6 +34,32 @@ public final class ComponentDtoFunctions { return ToKey.INSTANCE; } + public static Function<ComponentDto, Long> toId() { + return ToId.INSTANCE; + } + + public static Function<ComponentDto, String> toProjectUuid() { + return ToProjectUuid.INSTANCE; + } + + public static Function<ComponentDto, String> toUuid() { + return ToUuid.INSTANCE; + } + + public static Function<ComponentDto, Long> toCopyResourceId() { + return ToCopyResourceId.INSTANCE; + } + + private enum ToId implements Function<ComponentDto, Long> { + INSTANCE; + + @Override + public Long apply(@Nonnull ComponentDto input) { + return input.getId(); + } + } + + private enum ToKey implements Function<ComponentDto, String> { INSTANCE; @@ -43,10 +69,6 @@ public final class ComponentDtoFunctions { } } - public static Function<ComponentDto, String> toProjectUuid() { - return ToProjectUuid.INSTANCE; - } - private enum ToProjectUuid implements Function<ComponentDto, String> { INSTANCE; @@ -56,10 +78,6 @@ public final class ComponentDtoFunctions { } } - public static Function<ComponentDto, String> toUuid() { - return ToUuid.INSTANCE; - } - private enum ToUuid implements Function<ComponentDto, String> { INSTANCE; @@ -69,10 +87,6 @@ public final class ComponentDtoFunctions { } } - public static Function<ComponentDto, Long> toCopyResourceId() { - return ToCopyResourceId.INSTANCE; - } - private enum ToCopyResourceId implements Function<ComponentDto, Long> { INSTANCE; diff --git a/sonar-db/src/main/java/org/sonar/db/component/SnapshotDao.java b/sonar-db/src/main/java/org/sonar/db/component/SnapshotDao.java index 4ef0cea7fa4..fc3dead424b 100644 --- a/sonar-db/src/main/java/org/sonar/db/component/SnapshotDao.java +++ b/sonar-db/src/main/java/org/sonar/db/component/SnapshotDao.java @@ -20,11 +20,13 @@ package org.sonar.db.component; +import com.google.common.base.Function; import com.google.common.base.Strings; import com.google.common.collect.Lists; import java.util.Collection; import java.util.List; import javax.annotation.CheckForNull; +import javax.annotation.Nonnull; import javax.annotation.Nullable; import org.apache.ibatis.session.RowBounds; import org.sonar.api.resources.Scopes; @@ -34,6 +36,7 @@ import org.sonar.db.RowNotFoundException; import static com.google.common.base.Preconditions.checkState; import static com.google.common.collect.FluentIterable.from; +import static org.sonar.db.DatabaseUtils.executeLargeInputs; public class SnapshotDao implements Dao { @@ -54,6 +57,16 @@ public class SnapshotDao implements Dao { return value; } + public List<SnapshotDto> selectByIds(final DbSession dbSession, List<Long> snapshotIds) { + return executeLargeInputs(snapshotIds, new Function<List<Long>, List<SnapshotDto>>() { + @Nonnull + @Override + public List<SnapshotDto> apply(@Nonnull List<Long> input) { + return mapper(dbSession).selectByIds(input); + } + }); + } + @CheckForNull public SnapshotDto selectLastSnapshotByComponentId(DbSession session, long componentId) { return mapper(session).selectLastSnapshot(componentId); diff --git a/sonar-db/src/main/java/org/sonar/db/component/SnapshotDtoFunctions.java b/sonar-db/src/main/java/org/sonar/db/component/SnapshotDtoFunctions.java new file mode 100644 index 00000000000..db46706b184 --- /dev/null +++ b/sonar-db/src/main/java/org/sonar/db/component/SnapshotDtoFunctions.java @@ -0,0 +1,54 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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.component; + +import com.google.common.base.Function; +import javax.annotation.Nonnull; + +public class SnapshotDtoFunctions { + public static Function<SnapshotDto, Long> toId() { + return ToId.INSTANCE; + } + + public static Function<SnapshotDto, Long> toComponentId() { + return ToComponentId.INSTANCE; + } + + private enum ToId implements Function<SnapshotDto, Long> { + INSTANCE; + + @Override + public Long apply(@Nonnull SnapshotDto input) { + return input.getId(); + } + } + + private enum ToComponentId implements Function<SnapshotDto, Long> { + INSTANCE; + + @Override + public Long apply(@Nonnull SnapshotDto input) { + return input.getComponentId(); + } + } + + +} diff --git a/sonar-db/src/main/java/org/sonar/db/component/SnapshotMapper.java b/sonar-db/src/main/java/org/sonar/db/component/SnapshotMapper.java index 043390bdf13..b6a43bd6377 100644 --- a/sonar-db/src/main/java/org/sonar/db/component/SnapshotMapper.java +++ b/sonar-db/src/main/java/org/sonar/db/component/SnapshotMapper.java @@ -30,6 +30,8 @@ public interface SnapshotMapper { @CheckForNull SnapshotDto selectByKey(long id); + List<SnapshotDto> selectByIds(@Param("ids") List<Long> ids); + void insert(SnapshotDto snapshot); @CheckForNull @@ -53,7 +55,7 @@ public interface SnapshotMapper { int updateSnapshotAndChildrenLastFlag(@Param(value = "root") Long rootId, @Param(value = "pathRootId") Long pathRootId, @Param(value = "path") String path, @Param(value = "isLast") boolean isLast); - + List<ViewsSnapshotDto> selectSnapshotBefore(@Param("componentId") long componentId, @Param("date") long date); ViewsSnapshotDto selectLatestSnapshot(@Param("componentId") long componentId); diff --git a/sonar-db/src/main/resources/org/sonar/db/component/SnapshotMapper.xml b/sonar-db/src/main/resources/org/sonar/db/component/SnapshotMapper.xml index a8531b0a407..1c408c558e2 100644 --- a/sonar-db/src/main/resources/org/sonar/db/component/SnapshotMapper.xml +++ b/sonar-db/src/main/resources/org/sonar/db/component/SnapshotMapper.xml @@ -49,6 +49,17 @@ </where> </select> + <select id="selectByIds" parameterType="Long" resultType="Snapshot"> + SELECT + <include refid="snapshotColumns"/> + FROM snapshots s + WHERE + s.id in + <foreach collection="ids" item="id" separator="," open="(" close=")"> + #{id} + </foreach> + </select> + <select id="selectLastSnapshot" resultType="Snapshot"> select <include refid="snapshotColumns"/> diff --git a/sonar-db/src/test/java/org/sonar/db/component/ComponentDbTester.java b/sonar-db/src/test/java/org/sonar/db/component/ComponentDbTester.java index 74932cfe65e..523bf5873e7 100644 --- a/sonar-db/src/test/java/org/sonar/db/component/ComponentDbTester.java +++ b/sonar-db/src/test/java/org/sonar/db/component/ComponentDbTester.java @@ -65,6 +65,7 @@ public class ComponentDbTester { public ComponentDto insertComponent(ComponentDto component) { dbClient.componentDao().insert(dbSession, component); + db.commit(); return component; } diff --git a/sonar-db/src/test/java/org/sonar/db/component/SnapshotDaoTest.java b/sonar-db/src/test/java/org/sonar/db/component/SnapshotDaoTest.java index 2f8639db024..6d3f406fa0a 100644 --- a/sonar-db/src/test/java/org/sonar/db/component/SnapshotDaoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/component/SnapshotDaoTest.java @@ -30,11 +30,14 @@ import org.sonar.api.resources.Qualifiers; import org.sonar.api.resources.Scopes; import org.sonar.api.utils.DateUtils; import org.sonar.api.utils.System2; +import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.DbTester; import org.sonar.test.DbTests; +import static com.google.common.collect.Lists.newArrayList; import static org.assertj.core.api.Assertions.assertThat; +import static org.sonar.db.component.ComponentTesting.newProjectDto; import static org.sonar.db.component.SnapshotQuery.SORT_FIELD.BY_DATE; import static org.sonar.db.component.SnapshotQuery.SORT_ORDER.ASC; import static org.sonar.db.component.SnapshotQuery.SORT_ORDER.DESC; @@ -48,7 +51,8 @@ public class SnapshotDaoTest { @Rule public DbTester db = DbTester.create(System2.INSTANCE); - + ComponentDbTester componentDb = new ComponentDbTester(db); + DbClient dbClient = db.getDbClient(); DbSession dbSession = db.getSession(); SnapshotDao underTest = db.getDbClient().snapshotDao(); @@ -96,6 +100,18 @@ public class SnapshotDaoTest { } @Test + public void select_by_ids() { + SnapshotDto snapshot1 = componentDb.insertProjectAndSnapshot(newProjectDto()); + SnapshotDto snapshot2 = componentDb.insertProjectAndSnapshot(newProjectDto()); + SnapshotDto snapshot3 = componentDb.insertProjectAndSnapshot(newProjectDto()); + + List<SnapshotDto> result = underTest.selectByIds(dbSession, newArrayList(snapshot1.getId(), snapshot2.getId(), snapshot3.getId(), 42L)); + + assertThat(result).hasSize(3); + assertThat(result).extracting("id").containsOnly(snapshot1.getId(), snapshot2.getId(), snapshot3.getId()); + } + + @Test public void lastSnapshot_returns_null_when_no_last_snapshot() { SnapshotDto snapshot = underTest.selectLastSnapshotByComponentId(db.getSession(), 123L); |