You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AnalysisReportDao.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * SonarQube, open source software quality management tool.
  3. * Copyright (C) 2008-2014 SonarSource
  4. * mailto:contact AT sonarsource DOT com
  5. *
  6. * SonarQube is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * SonarQube is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.computation.db;
  21. import com.google.common.annotations.VisibleForTesting;
  22. import org.sonar.api.utils.System2;
  23. import org.sonar.db.compute.AnalysisReportDto;
  24. import org.sonar.db.compute.AnalysisReportMapper;
  25. import org.sonar.db.Dao;
  26. import org.sonar.db.DbSession;
  27. import javax.annotation.CheckForNull;
  28. import java.util.List;
  29. import static org.sonar.db.compute.AnalysisReportDto.Status.PENDING;
  30. import static org.sonar.db.compute.AnalysisReportDto.Status.WORKING;
  31. public class AnalysisReportDao implements Dao {
  32. private System2 system2;
  33. public AnalysisReportDao(System2 system2) {
  34. this.system2 = system2;
  35. }
  36. /**
  37. * Update all rows with: STATUS='PENDING', STARTED_AT=NULL, UPDATED_AT={now}
  38. */
  39. public void resetAllToPendingStatus(DbSession session) {
  40. mapper(session).resetAllToPendingStatus(system2.now());
  41. }
  42. public void truncate(DbSession session) {
  43. mapper(session).truncate();
  44. }
  45. public List<AnalysisReportDto> selectByProjectKey(DbSession session, String projectKey) {
  46. return mapper(session).selectByProjectKey(projectKey);
  47. }
  48. @VisibleForTesting
  49. AnalysisReportDto selectById(DbSession session, long id) {
  50. return mapper(session).selectById(id);
  51. }
  52. @CheckForNull
  53. public AnalysisReportDto pop(DbSession session) {
  54. List<Long> reportIds = mapper(session).selectAvailables(PENDING, WORKING);
  55. if (reportIds.isEmpty()) {
  56. return null;
  57. }
  58. long reportId = reportIds.get(0);
  59. return tryToPop(session, reportId);
  60. }
  61. @VisibleForTesting
  62. AnalysisReportDto tryToPop(DbSession session, long reportId) {
  63. AnalysisReportMapper mapper = mapper(session);
  64. int nbOfReportBooked = mapper.updateWithBookingReport(reportId, system2.now(), PENDING, WORKING);
  65. if (nbOfReportBooked == 0) {
  66. return null;
  67. }
  68. AnalysisReportDto result = mapper.selectById(reportId);
  69. session.commit();
  70. return result;
  71. }
  72. public List<AnalysisReportDto> selectAll(DbSession session) {
  73. return mapper(session).selectAll();
  74. }
  75. public AnalysisReportDto insert(DbSession session, AnalysisReportDto report) {
  76. report.setCreatedAt(system2.now());
  77. report.setUpdatedAt(system2.now());
  78. mapper(session).insert(report);
  79. return report;
  80. }
  81. public void delete(DbSession session, long id) {
  82. mapper(session).delete(id);
  83. }
  84. private AnalysisReportMapper mapper(DbSession session) {
  85. return session.getMapper(AnalysisReportMapper.class);
  86. }
  87. }