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.

FileSourceDao.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 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.source;
  21. import com.google.common.base.Splitter;
  22. import java.sql.Connection;
  23. import java.sql.PreparedStatement;
  24. import java.sql.ResultSet;
  25. import java.sql.SQLException;
  26. import java.util.Collection;
  27. import java.util.Collections;
  28. import java.util.List;
  29. import javax.annotation.CheckForNull;
  30. import org.apache.ibatis.session.ResultHandler;
  31. import org.sonar.db.Dao;
  32. import org.sonar.db.DatabaseUtils;
  33. import org.sonar.db.DbSession;
  34. import static org.sonar.db.DatabaseUtils.toUniqueAndSortedPartitions;
  35. public class FileSourceDao implements Dao {
  36. private static final Splitter END_OF_LINE_SPLITTER = Splitter.on('\n');
  37. @CheckForNull
  38. public FileSourceDto selectByFileUuid(DbSession session, String fileUuid) {
  39. return mapper(session).selectByFileUuid(fileUuid);
  40. }
  41. @CheckForNull
  42. public LineHashVersion selectLineHashesVersion(DbSession dbSession, String fileUuid) {
  43. Integer version = mapper(dbSession).selectLineHashesVersion(fileUuid);
  44. return version == null ? null : LineHashVersion.valueOf(version);
  45. }
  46. @CheckForNull
  47. public List<String> selectLineHashes(DbSession dbSession, String fileUuid) {
  48. Connection connection = dbSession.getConnection();
  49. PreparedStatement pstmt = null;
  50. ResultSet rs = null;
  51. try {
  52. pstmt = connection.prepareStatement("SELECT line_hashes FROM file_sources WHERE file_uuid=?");
  53. pstmt.setString(1, fileUuid);
  54. rs = pstmt.executeQuery();
  55. if (rs.next()) {
  56. String string = rs.getString(1);
  57. if (string == null) {
  58. return Collections.emptyList();
  59. }
  60. return END_OF_LINE_SPLITTER.splitToList(string);
  61. }
  62. return null;
  63. } catch (SQLException e) {
  64. throw new IllegalStateException("Fail to read FILE_SOURCES.LINE_HASHES of file " + fileUuid, e);
  65. } finally {
  66. DatabaseUtils.closeQuietly(rs);
  67. DatabaseUtils.closeQuietly(pstmt);
  68. DatabaseUtils.closeQuietly(connection);
  69. }
  70. }
  71. /**
  72. * Scroll line hashes of all <strong>enabled</strong> components (should be files, but not enforced) with specified
  73. * uuids in no specific order with 'SOURCE' source and a non null path.
  74. */
  75. public void scrollLineHashes(DbSession dbSession, Collection<String> fileUUids, ResultHandler<LineHashesWithUuidDto> rowHandler) {
  76. for (List<String> fileUuidsPartition : toUniqueAndSortedPartitions(fileUUids)) {
  77. mapper(dbSession).scrollLineHashes(fileUuidsPartition, rowHandler);
  78. }
  79. }
  80. public void insert(DbSession session, FileSourceDto dto) {
  81. mapper(session).insert(dto);
  82. }
  83. public void update(DbSession session, FileSourceDto dto) {
  84. mapper(session).update(dto);
  85. }
  86. private static FileSourceMapper mapper(DbSession session) {
  87. return session.getMapper(FileSourceMapper.class);
  88. }
  89. }