3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact 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 org.apache.commons.lang.StringUtils;
26 import org.sonar.api.rule.RuleKey;
27 import org.sonar.db.DbClient;
28 import org.sonar.db.DbSession;
29 import org.sonar.db.ResultSetIterator;
30 import org.sonar.db.qualityprofile.ActiveRuleKey;
31 import org.sonar.db.rule.SeverityUtil;
32 import org.sonar.server.qualityprofile.ActiveRule;
35 * Scrolls over table ACTIVE_RULES and reads documents to populate the active rules index
37 public class ActiveRuleResultSetIterator extends ResultSetIterator<ActiveRuleDoc> {
39 private static final String[] FIELDS = {
51 private static final String SQL_ALL = "SELECT " + StringUtils.join(FIELDS, ",") + " FROM active_rules a " +
52 "INNER JOIN rules_profiles qp ON qp.id=a.profile_id " +
53 "INNER JOIN rules r ON r.id = a.rule_id " +
54 "LEFT JOIN rules_profiles profile_parent ON profile_parent.kee=qp.parent_kee ";
56 private static final String SQL_AFTER_DATE = SQL_ALL + " WHERE a.updated_at_ms>?";
58 private ActiveRuleResultSetIterator(PreparedStatement stmt) throws SQLException {
62 static ActiveRuleResultSetIterator create(DbClient dbClient, DbSession session, long afterDate) {
64 String sql = afterDate > 0L ? SQL_AFTER_DATE : SQL_ALL;
65 PreparedStatement stmt = dbClient.getMyBatis().newScrollingSelectStatement(session, sql);
67 stmt.setLong(1, afterDate);
69 return new ActiveRuleResultSetIterator(stmt);
70 } catch (SQLException e) {
71 throw new IllegalStateException("Fail to prepare SQL request to select all active rules", e);
76 protected ActiveRuleDoc read(ResultSet rs) throws SQLException {
77 RuleKey ruleKey = RuleKey.of(rs.getString(4), rs.getString(3));
78 ActiveRuleKey activeRuleKey = ActiveRuleKey.of(rs.getString(5), ruleKey);
80 ActiveRuleDoc doc = new ActiveRuleDoc(activeRuleKey);
82 // all the fields must be present, even if value is null
83 doc.setSeverity(SeverityUtil.getSeverityFromOrdinal(rs.getInt(1)));
85 String inheritance = rs.getString(2);
86 if (inheritance != null) {
87 doc.setInheritance(inheritance);
89 doc.setInheritance(ActiveRule.Inheritance.NONE.name());
92 String parentProfileKey = rs.getString(6);
93 if (parentProfileKey != null) {
94 doc.setParentKey(ActiveRuleKey.of(parentProfileKey, ruleKey).toString());
96 doc.setParentKey(null);
99 doc.setCreatedAt(rs.getLong(7));
100 doc.setUpdatedAt(rs.getLong(8));