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.

BranchDao.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2018 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program 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. * This program 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.db.component;
  21. import java.util.Collection;
  22. import java.util.List;
  23. import java.util.Optional;
  24. import org.sonar.api.utils.System2;
  25. import org.sonar.db.Dao;
  26. import org.sonar.db.DbSession;
  27. import static org.sonar.db.DatabaseUtils.executeLargeInputs;
  28. public class BranchDao implements Dao {
  29. private final System2 system2;
  30. public BranchDao(System2 system2) {
  31. this.system2 = system2;
  32. }
  33. public void insert(DbSession dbSession, BranchDto dto) {
  34. setKeyType(dto);
  35. mapper(dbSession).insert(dto, system2.now());
  36. }
  37. public void upsert(DbSession dbSession, BranchDto dto) {
  38. BranchMapper mapper = mapper(dbSession);
  39. long now = system2.now();
  40. setKeyType(dto);
  41. if (mapper.update(dto, now) == 0) {
  42. mapper.insert(dto, now);
  43. }
  44. }
  45. private static void setKeyType(BranchDto dto) {
  46. if (dto.getBranchType() == BranchType.PULL_REQUEST) {
  47. dto.setKeyType(KeyType.PULL_REQUEST);
  48. } else {
  49. dto.setKeyType(KeyType.BRANCH);
  50. }
  51. }
  52. public int updateMainBranchName(DbSession dbSession, String projectUuid, String newBranchKey) {
  53. long now = system2.now();
  54. return mapper(dbSession).updateMainBranchName(projectUuid, newBranchKey, now);
  55. }
  56. public Optional<BranchDto> selectByBranchKey(DbSession dbSession, String projectUuid, String key) {
  57. return selectByKey(dbSession, projectUuid, key, KeyType.BRANCH);
  58. }
  59. public Optional<BranchDto> selectByPullRequestKey(DbSession dbSession, String projectUuid, String key) {
  60. return selectByKey(dbSession, projectUuid, key, KeyType.PULL_REQUEST);
  61. }
  62. private static Optional<BranchDto> selectByKey(DbSession dbSession, String projectUuid, String key, KeyType keyType) {
  63. return Optional.ofNullable(mapper(dbSession).selectByKey(projectUuid, key, keyType));
  64. }
  65. public Collection<BranchDto> selectByComponent(DbSession dbSession, ComponentDto component) {
  66. String projectUuid = component.getMainBranchProjectUuid();
  67. if (projectUuid == null) {
  68. projectUuid = component.projectUuid();
  69. }
  70. return mapper(dbSession).selectByProjectUuid(projectUuid);
  71. }
  72. public List<BranchDto> selectByUuids(DbSession session, Collection<String> uuids) {
  73. return executeLargeInputs(uuids, mapper(session)::selectByUuids);
  74. }
  75. public Optional<BranchDto> selectByUuid(DbSession session, String uuid) {
  76. return Optional.ofNullable(mapper(session).selectByUuid(uuid));
  77. }
  78. public boolean hasNonMainBranches(DbSession dbSession) {
  79. return mapper(dbSession).countNonMainBranches() > 0L;
  80. }
  81. private static BranchMapper mapper(DbSession dbSession) {
  82. return dbSession.getMapper(BranchMapper.class);
  83. }
  84. }