3 * Copyright (C) 2009-2020 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.ce.task.projectanalysis.dbmigration;
22 import com.google.common.collect.Iterables;
23 import java.sql.SQLException;
24 import org.sonar.ce.task.CeTask;
25 import org.sonar.db.Database;
26 import org.sonar.db.source.FileSourceDto;
27 import org.sonar.server.platform.db.migration.step.DataChange;
28 import org.sonar.server.platform.db.migration.step.MassUpdate;
29 import org.sonar.server.platform.db.migration.step.Select;
30 import org.sonar.server.platform.db.migration.step.SqlStatement;
32 import static org.sonar.db.source.FileSourceDto.LINE_COUNT_NOT_POPULATED;
34 public class PopulateFileSourceLineCount extends DataChange implements ProjectAnalysisDataChange {
35 private final CeTask ceTask;
37 public PopulateFileSourceLineCount(Database database, CeTask ceTask) {
43 protected void execute(Context context) throws SQLException {
44 String componentUuid = ceTask.getComponent().get().getUuid();
45 Long unInitializedFileSources = context.prepareSelect("select count(1) from file_sources where line_count = ? and project_uuid = ?")
46 .setInt(1, LINE_COUNT_NOT_POPULATED)
47 .setString(2, componentUuid)
48 .get(row -> row.getLong(1));
50 if (unInitializedFileSources != null && unInitializedFileSources > 0) {
51 MassUpdate massUpdate = context.prepareMassUpdate();
52 massUpdate.select("select uuid,line_hashes from file_sources where line_count = ? and project_uuid = ?")
53 .setInt(1, LINE_COUNT_NOT_POPULATED)
54 .setString(2, componentUuid);
55 massUpdate.update("update file_sources set line_count = ? where uuid = ?");
56 massUpdate.rowPluralName("line counts of sources of project " + componentUuid);
57 massUpdate.execute(PopulateFileSourceLineCount::handle);
61 private static boolean handle(Select.Row row, SqlStatement update) throws SQLException {
62 String rowUuid = row.getString(1);
63 String rawData = row.getNullableString(2);
65 int lineCount = rawData == null ? 0 : Iterables.size(FileSourceDto.LINES_HASHES_SPLITTER.split(rawData));
66 update.setInt(1, lineCount);
67 update.setString(2, rowUuid);