<mapper namespace="org.sonar.db.ce.CeActivityMapper">
<sql id="columns">
+ ca.id,
ca.uuid,
ca.task_type as taskType,
ca.component_uuid as componentUuid,
</select>
<select id="selectByQuery" parameterType="map" resultType="org.sonar.db.ce.CeActivityDto">
- select
+ SELECT
<include refid="columns"/>
<include refid="sqlSelectByQuery" />
- order by ca.submitted_at desc, ca.id desc
+ ORDER BY ca.id desc
+ LIMIT #{pageSize} OFFSET #{offset}
+ </select>
+
+ <select id="selectByQuery" parameterType="map" resultType="org.sonar.db.ce.CeActivityDto" databaseId="mssql">
+ SELECT * FROM (
+ SELECT ROW_NUMBER() OVER(ORDER BY id desc) AS NUMBER,
+ <include refid="columns"/>
+ <include refid="sqlSelectByQuery" />
+ ) AS QUERY
+ WHERE NUMBER BETWEEN (#{offset} * #{pageSize} + 1) AND ((#{offset} + 1) * #{pageSize})
+ ORDER BY id desc
+ </select>
+
+ <select id="selectByQuery" parameterType="map" resultType="org.sonar.db.ce.CeActivityDto" databaseId="oracle">
+ SELECT * FROM (
+ SELECT ROWNUM AS rn, t.* FROM (
+ SELECT
+ <include refid="columns"/>
+ <include refid="sqlSelectByQuery" />
+ ORDER BY ca.id desc
+ ) t
+ ) t
+ WHERE rn BETWEEN (#{offset} * #{pageSize} + 1) AND ((#{offset} + 1) * #{pageSize})
</select>
<select id="countByQuery" parameterType="map" resultType="int">
*/
package org.sonar.db.ce;
+import com.google.common.base.Function;
import com.google.common.base.Optional;
+import com.google.common.collect.FluentIterable;
import java.util.Collections;
import java.util.List;
+import javax.annotation.Nonnull;
import org.junit.Rule;
import org.junit.Test;
import org.sonar.api.utils.internal.TestSystem2;
import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.db.ce.CeTaskTypes.REPORT;
-
public class CeActivityDaoTest {
TestSystem2 system2 = new TestSystem2().setNow(1_450_000_000_000L);
assertThat(dtos).extracting("uuid").containsExactly("TASK_2");
}
+ @Test
+ public void selectByQuery_is_paginated_and_return_results_sorted_from_last_to_first() {
+ insert("TASK_1", REPORT, "PROJECT_1", CeActivityDto.Status.SUCCESS);
+ insert("TASK_2", REPORT, "PROJECT_1", CeActivityDto.Status.FAILED);
+ insert("TASK_3", REPORT, "PROJECT_2", CeActivityDto.Status.SUCCESS);
+ insert("TASK_4", "views", null, CeActivityDto.Status.SUCCESS);
+
+ assertThat(selectPageOfUuids(0, 1)).containsExactly("TASK_4");
+ assertThat(selectPageOfUuids(1, 1)).containsExactly("TASK_3");
+ assertThat(selectPageOfUuids(0, 3)).containsExactly("TASK_4", "TASK_3", "TASK_2");
+ assertThat(selectPageOfUuids(0, 4)).containsExactly("TASK_4", "TASK_3", "TASK_2", "TASK_1");
+ assertThat(selectPageOfUuids(0, 100)).containsExactly("TASK_4", "TASK_3", "TASK_2", "TASK_1");
+ assertThat(selectPageOfUuids(0, 0)).isEmpty();
+ assertThat(selectPageOfUuids(10, 2)).isEmpty();
+ }
+
@Test
public void test_countByQuery() {
insert("TASK_1", REPORT, "PROJECT_1", CeActivityDto.Status.SUCCESS);
system2.setNow(date);
underTest.insert(db.getSession(), dto);
}
+
+ private List<String> selectPageOfUuids(int offset, int pageSize) {
+ return FluentIterable.from(underTest.selectByQuery(db.getSession(), new CeTaskQuery(), offset, pageSize)).transform(CeActivityToUuid.INSTANCE).toList();
+ }
+
+ private enum CeActivityToUuid implements Function<CeActivityDto, String> {
+ INSTANCE;
+
+ @Override
+ public String apply(@Nonnull CeActivityDto input) {
+ return input.getUuid();
+ }
+ }
}