]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9040 fix pagination in CeQueueDao#selectEligibleForPeek
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Tue, 28 Mar 2017 14:06:03 +0000 (16:06 +0200)
committerEric Hartmann <hartmann.eric@gmail.Com>
Thu, 27 Apr 2017 07:23:18 +0000 (09:23 +0200)
server/sonar-db-dao/src/main/java/org/sonar/db/ce/CeQueueDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/ce/CeQueueMapper.java
server/sonar-db-dao/src/main/resources/org/sonar/db/ce/CeQueueMapper.xml

index 73e90cfcfda7679f7b39c680a1e77e85f094d318..dc94239d88282bff3f92802771c075777400257e 100644 (file)
@@ -26,6 +26,7 @@ import org.apache.ibatis.session.RowBounds;
 import org.sonar.api.utils.System2;
 import org.sonar.db.Dao;
 import org.sonar.db.DbSession;
+import org.sonar.db.Pagination;
 
 import static java.util.Collections.emptyList;
 import static org.sonar.db.ce.CeQueueDto.Status.IN_PROGRESS;
@@ -33,7 +34,7 @@ import static org.sonar.db.ce.CeQueueDto.Status.PENDING;
 
 public class CeQueueDao implements Dao {
 
-  private static final RowBounds ONE_ROW_LIMIT = new RowBounds(0, 1);
+  private static final Pagination ONE_RESULT_PAGINATION = Pagination.forPage(1).andSize(1);
 
   private final System2 system2;
 
@@ -110,7 +111,7 @@ public class CeQueueDao implements Dao {
   }
 
   public Optional<CeQueueDto> peek(DbSession session, String workerUuid) {
-    List<EligibleTaskDto> eligibles = mapper(session).selectEligibleForPeek(ONE_ROW_LIMIT);
+    List<EligibleTaskDto> eligibles = mapper(session).selectEligibleForPeek(ONE_RESULT_PAGINATION);
     if (eligibles.isEmpty()) {
       return Optional.absent();
     }
index b89569f01f4093244daf330362b83b994bbab556..d605e2b8be1a7e8b43cd9073c6c64573861790cd 100644 (file)
@@ -24,6 +24,7 @@ import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
 import org.apache.ibatis.annotations.Param;
 import org.apache.ibatis.session.RowBounds;
+import org.sonar.db.Pagination;
 
 public interface CeQueueMapper {
 
@@ -35,7 +36,7 @@ public interface CeQueueMapper {
 
   int countByQuery(@Param("query") CeTaskQuery query);
 
-  List<EligibleTaskDto> selectEligibleForPeek(RowBounds rowBounds);
+  List<EligibleTaskDto> selectEligibleForPeek(@Param("pagination") Pagination pagination);
 
   @CheckForNull
   CeQueueDto selectByUuid(@Param("uuid") String uuid);
index 657c8a9d526cc0101930c193138384951ef30205..2d1f28c90c9202bcfa3fe0567b8e0fc8adf908bd 100644 (file)
 
   <select id="selectEligibleForPeek" resultType="org.sonar.db.ce.EligibleTaskDto">
     select
-      cq.uuid as "uuid",
-      cq.execution_count as "executionCount"
+      <include refid="columnsSelectEligibleForPeek"/>
+    <include refid="sqlSelectEligibleForPeek"/>
+    <include refid="orderBySelectEligibleForPeek"/>
+    limit #{pagination.pageSize,jdbcType=INTEGER} offset #{pagination.offset,jdbcType=INTEGER}
+  </select>
+
+  <select id="selectEligibleForPeek" parameterType="map" resultType="org.sonar.db.ce.EligibleTaskDto" databaseId="mssql">
+    select * from (
+      select
+        row_number() over(<include refid="orderBySelectEligibleForPeek"/>) as number,
+        <include refid="columnsSelectEligibleForPeek"/>
+      <include refid="sqlSelectEligibleForPeek"/>
+    ) as query
+    where
+      query.number between #{pagination.startRowNumber,jdbcType=INTEGER} and #{pagination.endRowNumber,jdbcType=INTEGER}
+    <include refid="orderBySelectEligibleForPeek"/>
+  </select>
+
+  <select id="selectEligibleForPeek" parameterType="map" resultType="org.sonar.db.ce.EligibleTaskDto" databaseId="oracle">
+    select * from (
+      select rownum as rn, t.* from (
+        select
+          <include refid="columnsSelectEligibleForPeek"/>
+        <include refid="sqlSelectEligibleForPeek" />
+        <include refid="orderBySelectEligibleForPeek"/>
+      ) t
+    ) t
+    where
+      t.rn between #{pagination.startRowNumber,jdbcType=INTEGER} and #{pagination.endRowNumber,jdbcType=INTEGER}
+  </select>
+
+  <sql id="columnsSelectEligibleForPeek">
+    cq.uuid as "uuid",
+    cq.execution_count as "executionCount",
+    cq.created_at as "created_at",
+    cq.id as "id"
+  </sql>
+
+  <sql id="sqlSelectEligibleForPeek">
     from
       ce_queue cq
     where
           cq.component_uuid=cq2.component_uuid
           and cq2.status &lt;&gt; 'PENDING'
       )
-    <include refid="orderByDateAndId"/>
-  </select>
+  </sql>
+
+  <sql id="orderBySelectEligibleForPeek">
+    order by
+      created_at asc,
+      id asc
+  </sql>
 
   <insert id="insert" parameterType="org.sonar.db.ce.CeQueueDto" useGeneratedKeys="false">
     insert into ce_queue