3 * Copyright (C) 2009-2017 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.server.qualityprofile.index;
22 import java.sql.PreparedStatement;
23 import java.sql.ResultSet;
24 import java.sql.SQLException;
25 import java.util.List;
26 import org.apache.commons.lang.StringUtils;
27 import org.sonar.api.rule.RuleKey;
28 import org.sonar.db.DatabaseUtils;
29 import org.sonar.db.DbClient;
30 import org.sonar.db.DbSession;
31 import org.sonar.db.ResultSetIterator;
32 import org.sonar.db.qualityprofile.RulesProfileDto;
33 import org.sonar.db.rule.SeverityUtil;
34 import org.sonar.server.qualityprofile.ActiveRule;
36 import static org.apache.commons.lang.StringUtils.repeat;
39 * Scrolls over table ISSUES and reads documents to populate
42 class ActiveRuleIteratorForSingleChunk implements ActiveRuleIterator {
44 private static final String[] COLUMNS = {
53 private static final String SQL_ALL = "select " + StringUtils.join(COLUMNS, ",") + " from active_rules ar " +
54 " inner join rules_profiles rp on rp.id = ar.profile_id " +
55 " inner join rules r on r.id = ar.rule_id ";
57 private final PreparedStatement stmt;
58 private final ResultSetIterator<ActiveRuleDoc> iterator;
60 ActiveRuleIteratorForSingleChunk(DbClient dbClient, DbSession dbSession) {
62 stmt = dbClient.getMyBatis().newScrollingSelectStatement(dbSession, SQL_ALL);
63 iterator = new ActiveRuleIteratorInternal(stmt);
64 } catch (Exception e) {
65 throw new IllegalStateException("Fail to prepare SQL request to select all active_rules", e);
69 ActiveRuleIteratorForSingleChunk(DbClient dbClient, DbSession dbSession, RulesProfileDto ruleProfile) {
71 stmt = dbClient.getMyBatis().newScrollingSelectStatement(dbSession, SQL_ALL + " where rp.kee = ?");
72 stmt.setString(1, ruleProfile.getKee());
73 iterator = new ActiveRuleIteratorInternal(stmt);
74 } catch (Exception e) {
75 throw new IllegalStateException("Fail to prepare SQL request to select active_rules of profile " + ruleProfile.getKee(), e);
79 ActiveRuleIteratorForSingleChunk(DbClient dbClient, DbSession dbSession, List<Integer> activeRuleIds) {
81 stmt = dbClient.getMyBatis().newScrollingSelectStatement(dbSession, SQL_ALL + " where ar.id in (" + repeat("?", ",", activeRuleIds.size()) + ")");
82 for (int i = 0; i < activeRuleIds.size(); i++) {
83 stmt.setInt(i + 1, activeRuleIds.get(i));
85 iterator = new ActiveRuleIteratorInternal(stmt);
86 } catch (Exception e) {
87 throw new IllegalStateException("Fail to prepare SQL request to select active_rules", e);
92 public boolean hasNext() {
93 return iterator.hasNext();
97 public ActiveRuleDoc next() {
98 return iterator.next();
102 public void close() {
106 DatabaseUtils.closeQuietly(stmt);
110 private static final class ActiveRuleIteratorInternal extends ResultSetIterator<ActiveRuleDoc> {
112 ActiveRuleIteratorInternal(PreparedStatement stmt) throws SQLException {
117 protected ActiveRuleDoc read(ResultSet rs) throws SQLException {
118 long activeRuleId = rs.getLong(1);
119 int severity = rs.getInt(2);
120 String inheritance = rs.getString(3);
121 RuleKey ruleKey = RuleKey.of(rs.getString(4), rs.getString(5));
122 String ruleProfileUuid = rs.getString(6);
124 return new ActiveRuleDoc(String.valueOf(activeRuleId))
125 .setRuleProfileUuid(ruleProfileUuid)
127 // all the fields must be present, even if value is null
128 .setSeverity(SeverityUtil.getSeverityFromOrdinal(severity))
129 .setInheritance(inheritance == null ? ActiveRule.Inheritance.NONE.name() : inheritance);