diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2015-06-05 15:31:20 +0200 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2015-06-10 14:30:39 +0200 |
commit | 5544b41c921fbbb516aafbf0c96aca349c5a2983 (patch) | |
tree | 51b06c9b0bca5b29e90db0eb9c360b5587e49e07 /sonar-core | |
parent | 1de5ba33b22003de4247bd2fd0da393cfae7b2f1 (diff) | |
download | sonarqube-5544b41c921fbbb516aafbf0c96aca349c5a2983.tar.gz sonarqube-5544b41c921fbbb516aafbf0c96aca349c5a2983.zip |
SONAR-6260 Create a PeriodsRepository
The goal is to copy behaviour of PeriodsDefinition and TimeMachineConfiguration from batch in the Compute Engin
Diffstat (limited to 'sonar-core')
4 files changed, 235 insertions, 5 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/component/SnapshotQuery.java b/sonar-core/src/main/java/org/sonar/core/component/SnapshotQuery.java new file mode 100644 index 00000000000..4525a0a1b03 --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/core/component/SnapshotQuery.java @@ -0,0 +1,136 @@ +/* + * 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.core.component; + +import javax.annotation.CheckForNull; +import javax.annotation.Nullable; + +public final class SnapshotQuery { + + public enum SORT_FIELD { + BY_DATE("created_at"); + final String fieldName; + + SORT_FIELD(String fieldName) { + this.fieldName = fieldName; + } + } + + public enum SORT_ORDER { + ASC("asc"), DESC("desc"); + final String order; + + SORT_ORDER(String order) { + this.order = order; + } + } + + private Long componentId; + private Long createdAfter; + private Long createdBefore; + private String status; + private String version; + private Boolean isLast; + private String sortField; + private String sortOrder; + + /** + * filter to return snapshots created at or after a given date + */ + @CheckForNull + public Long getCreatedAfter() { + return createdAfter; + } + + public SnapshotQuery setCreatedAfter(@Nullable Long createdAfter) { + this.createdAfter = createdAfter; + return this; + } + + /** + * filter to return snapshots created before a given date + */ + @CheckForNull + public Long getCreatedBefore() { + return createdBefore; + } + + public SnapshotQuery setCreatedBefore(@Nullable Long createdBefore) { + this.createdBefore = createdBefore; + return this; + } + + @CheckForNull + public Boolean getIsLast() { + return isLast; + } + + public SnapshotQuery setIsLast(@Nullable Boolean isLast) { + this.isLast = isLast; + return this; + } + + @CheckForNull + public Long getComponentId() { + return componentId; + } + + public SnapshotQuery setComponentId(@Nullable Long componentId) { + this.componentId = componentId; + return this; + } + + @CheckForNull + public String getStatus() { + return status; + } + + public SnapshotQuery setStatus(@Nullable String status) { + this.status = status; + return this; + } + + @CheckForNull + public String getVersion() { + return version; + } + + public SnapshotQuery setVersion(@Nullable String version) { + this.version = version; + return this; + } + + public SnapshotQuery setSort(SORT_FIELD sortField, SORT_ORDER sortOrder){ + this.sortField = sortField.fieldName; + this.sortOrder = sortOrder.order; + return this; + } + + @CheckForNull + public String getSortField() { + return sortField; + } + + @CheckForNull + public String getSortOrder() { + return sortOrder; + } +} diff --git a/sonar-core/src/main/java/org/sonar/core/component/db/SnapshotMapper.java b/sonar-core/src/main/java/org/sonar/core/component/db/SnapshotMapper.java index 806128b0d9d..4f047976ab1 100644 --- a/sonar-core/src/main/java/org/sonar/core/component/db/SnapshotMapper.java +++ b/sonar-core/src/main/java/org/sonar/core/component/db/SnapshotMapper.java @@ -24,6 +24,7 @@ import java.util.List; import javax.annotation.CheckForNull; import org.apache.ibatis.annotations.Param; import org.sonar.core.component.SnapshotDto; +import org.sonar.core.component.SnapshotQuery; public interface SnapshotMapper { @@ -35,7 +36,9 @@ public interface SnapshotMapper { @CheckForNull SnapshotDto selectLastSnapshot(Long resourceId); - List<SnapshotDto> selectSnapshotsByComponentId(Long resourceId); + List<SnapshotDto> selectSnapshotsByQuery(@Param("query") SnapshotQuery query); + + List<SnapshotDto> selectPreviousVersionSnapshots(@Param(value = "componentId") Long componentId, @Param(value = "lastVersion") String lastVersion); List<SnapshotDto> selectSnapshotAndChildrenOfScope(@Param(value = "snapshot") Long resourceId, @Param(value = "scope") String scope); diff --git a/sonar-core/src/main/resources/org/sonar/core/component/db/SnapshotMapper.xml b/sonar-core/src/main/resources/org/sonar/core/component/db/SnapshotMapper.xml index fb9c6b44062..8d5b1d0576b 100644 --- a/sonar-core/src/main/resources/org/sonar/core/component/db/SnapshotMapper.xml +++ b/sonar-core/src/main/resources/org/sonar/core/component/db/SnapshotMapper.xml @@ -51,11 +51,51 @@ where s.islast=${_true} and s.project_id = #{resource} </select> - <select id="selectSnapshotsByComponentId" resultType="Snapshot"> - select + <select id="selectSnapshotsByQuery" parameterType="map" resultType="Snapshot"> + SELECT <include refid="snapshotColumns"/> - from snapshots s - where s.project_id = #{resource} + FROM snapshots s + <where> + <if test="query.componentId != null"> + AND s.project_id=#{query.componentId} + </if> + <if test="query.status != null"> + AND status=#{query.status} + </if> + <if test="query.version != null"> + AND version=#{query.version} + </if> + <if test="query.isLast != null"> + AND islast=#{query.isLast} + </if> + <if test="query.createdAfter != null"> + AND created_at>=#{query.createdAfter} + </if> + <if test="query.createdBefore != null"> + AND created_at<#{query.createdBefore} + </if> + </where> + <if test="query.sortField != null"> + ORDER BY + <if test="query.sortField == 'created_at'"> + created_at + </if> + <if test="query.sortOrder == 'asc'"> + asc + </if> + <if test="query.sortOrder == 'desc'"> + desc + </if> + </if> + </select> + + <select id="selectPreviousVersionSnapshots" parameterType="map" resultType="Snapshot"> + SELECT + <include refid="snapshotColumns"/> + FROM snapshots s + INNER JOIN events e ON s.id = e.snapshot_id AND e.name <> #{lastVersion} AND e.category='Version' + INNER JOIN projects p ON p.uuid=e.component_uuid AND p.id=#{componentId} + ORDER BY e.event_date DESC </select> <select id="selectSnapshotAndChildrenOfScope" parameterType="map" resultType="Snapshot"> diff --git a/sonar-core/src/test/java/org/sonar/core/component/SnapshotQueryTest.java b/sonar-core/src/test/java/org/sonar/core/component/SnapshotQueryTest.java new file mode 100644 index 00000000000..6c76f162ec9 --- /dev/null +++ b/sonar-core/src/test/java/org/sonar/core/component/SnapshotQueryTest.java @@ -0,0 +1,51 @@ +/* + * 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.core.component; + +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.sonar.core.component.SnapshotQuery.SORT_FIELD.BY_DATE; +import static org.sonar.core.component.SnapshotQuery.SORT_ORDER.ASC; + +public class SnapshotQueryTest { + + @Test + public void test_setters_and_getters() throws Exception { + SnapshotQuery query = new SnapshotQuery() + .setComponentId(1L) + .setIsLast(true) + .setStatus("P") + .setVersion("1.0") + .setCreatedAfter(10L) + .setCreatedBefore(20L) + .setSort(BY_DATE, ASC); + + assertThat(query.getComponentId()).isEqualTo(1L); + assertThat(query.getIsLast()).isTrue(); + assertThat(query.getStatus()).isEqualTo("P"); + assertThat(query.getVersion()).isEqualTo("1.0"); + assertThat(query.getCreatedAfter()).isEqualTo(10L); + assertThat(query.getCreatedBefore()).isEqualTo(20L); + assertThat(query.getSortField()).isEqualTo("created_at"); + assertThat(query.getSortOrder()).isEqualTo("asc"); + } +} |