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.

FileSourcesUpdaterHelper.java 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 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.server.source.index;
  21. import com.google.common.base.Joiner;
  22. import java.sql.PreparedStatement;
  23. import java.sql.SQLException;
  24. import java.util.ArrayList;
  25. import java.util.List;
  26. import javax.annotation.Nullable;
  27. import org.elasticsearch.action.update.UpdateRequest;
  28. import org.sonar.db.DbClient;
  29. import org.sonar.db.DbSession;
  30. public class FileSourcesUpdaterHelper {
  31. private static final String SQL_ALL = "SELECT %s FROM file_sources WHERE data_type='%s' ";
  32. private static final String PROJECT_FILTER = " AND project_uuid=?";
  33. private static final String[] FIELDS = {
  34. "project_uuid",
  35. "file_uuid",
  36. "updated_at",
  37. "binary_data"
  38. };
  39. private static final String FIELDS_ONE_LINE = Joiner.on(",").join(FIELDS);
  40. private FileSourcesUpdaterHelper() {
  41. // only static stuff
  42. }
  43. public static PreparedStatement preparedStatementToSelectFileSources(DbClient dbClient, DbSession session, String dataType, @Nullable String projectUuid)
  44. throws SQLException {
  45. String sql = createSQL(dataType, projectUuid);
  46. // rows are big, so they are scrolled once at a time (one row in memory at a time)
  47. PreparedStatement stmt = dbClient.getMyBatis().newScrollingSingleRowSelectStatement(session, sql);
  48. if (projectUuid != null) {
  49. stmt.setString(1, projectUuid);
  50. }
  51. return stmt;
  52. }
  53. private static String createSQL(String dataType, @Nullable String projectUuid) {
  54. StringBuilder sql = new StringBuilder(String.format(SQL_ALL, FIELDS_ONE_LINE, dataType));
  55. if (projectUuid != null) {
  56. sql.append(PROJECT_FILTER);
  57. }
  58. return sql.toString();
  59. }
  60. public static class Row {
  61. private final String fileUuid;
  62. private final String projectUuid;
  63. private final long updatedAt;
  64. private final List<UpdateRequest> updateRequests = new ArrayList<>();
  65. public Row(String projectUuid, String fileUuid, long updatedAt) {
  66. this.projectUuid = projectUuid;
  67. this.fileUuid = fileUuid;
  68. this.updatedAt = updatedAt;
  69. }
  70. public String getProjectUuid() {
  71. return projectUuid;
  72. }
  73. public String getFileUuid() {
  74. return fileUuid;
  75. }
  76. public long getUpdatedAt() {
  77. return updatedAt;
  78. }
  79. public List<UpdateRequest> getUpdateRequests() {
  80. return updateRequests;
  81. }
  82. }
  83. }