aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-db
diff options
context:
space:
mode:
authorJulien Lancelot <julien.lancelot@sonarsource.com>2016-02-23 16:01:56 +0100
committerJulien Lancelot <julien.lancelot@sonarsource.com>2016-02-29 13:26:54 +0100
commit5d148ea07dc451cb3ebc39ee1fc334830048a4a7 (patch)
tree498c483099d51d10eacd651477404eec3b6fc7b2 /sonar-db
parenta530cb94df60f4293ee8524edc65eaa63c58e0a4 (diff)
downloadsonarqube-5d148ea07dc451cb3ebc39ee1fc334830048a4a7.tar.gz
sonarqube-5d148ea07dc451cb3ebc39ee1fc334830048a4a7.zip
SONAR-7330 Add active rule index definition
Diffstat (limited to 'sonar-db')
-rw-r--r--sonar-db/src/main/java/org/sonar/db/qualityprofile/ActiveRuleDao.java171
-rw-r--r--sonar-db/src/main/java/org/sonar/db/qualityprofile/ActiveRuleDto.java35
-rw-r--r--sonar-db/src/main/java/org/sonar/db/qualityprofile/QualityProfileDao.java12
-rw-r--r--sonar-db/src/main/resources/org/sonar/db/qualityprofile/ActiveRuleMapper.xml17
-rw-r--r--sonar-db/src/test/java/org/sonar/db/qualityprofile/ActiveRuleDaoTest.java25
5 files changed, 237 insertions, 23 deletions
diff --git a/sonar-db/src/main/java/org/sonar/db/qualityprofile/ActiveRuleDao.java b/sonar-db/src/main/java/org/sonar/db/qualityprofile/ActiveRuleDao.java
new file mode 100644
index 00000000000..808b85f593e
--- /dev/null
+++ b/sonar-db/src/main/java/org/sonar/db/qualityprofile/ActiveRuleDao.java
@@ -0,0 +1,171 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.db.qualityprofile;
+
+import com.google.common.base.Optional;
+import com.google.common.base.Preconditions;
+import java.util.List;
+import javax.annotation.CheckForNull;
+import org.sonar.db.Dao;
+import org.sonar.db.DbSession;
+import org.sonar.db.RowNotFoundException;
+import org.sonar.db.rule.RuleDto;
+
+public class ActiveRuleDao implements Dao {
+
+ private static final String QUALITY_PROFILE_IS_NOT_PERSISTED = "Quality profile is not persisted (missing id)";
+ private static final String RULE_IS_NOT_PERSISTED = "Rule is not persisted";
+ private static final String RULE_PARAM_IS_NOT_PERSISTED = "Rule param is not persisted";
+ private static final String ACTIVE_RULE_KEY_CANNOT_BE_NULL = "ActiveRuleKey cannot be null";
+ private static final String ACTIVE_RULE_IS_NOT_PERSISTED = "ActiveRule is not persisted";
+ private static final String ACTIVE_RULE_IS_ALREADY_PERSISTED = "ActiveRule is already persisted";
+ private static final String ACTIVE_RULE_PARAM_IS_NOT_PERSISTED = "ActiveRuleParam is not persisted";
+ private static final String ACTIVE_RULE_PARAM_IS_ALREADY_PERSISTED = "ActiveRuleParam is already persisted";
+ private static final String PARAMETER_NAME_CANNOT_BE_NULL = "ParameterName cannot be null";
+
+ public Optional<ActiveRuleDto> selectByKey(DbSession session, ActiveRuleKey key) {
+ return Optional.fromNullable(mapper(session).selectByKey(key.qProfile(), key.ruleKey().repository(), key.ruleKey().rule()));
+ }
+
+ public ActiveRuleDto selectOrFailByKey(DbSession session, ActiveRuleKey key) {
+ Optional<ActiveRuleDto> activeRule = selectByKey(session, key);
+ if (activeRule.isPresent()) {
+ return activeRule.get();
+ }
+ throw new RowNotFoundException(String.format("Active rule with key '%s' does not exist", key));
+ }
+
+ public List<ActiveRuleDto> selectByRule(DbSession dbSession, RuleDto rule) {
+ Preconditions.checkNotNull(rule.getId(), RULE_IS_NOT_PERSISTED);
+ return mapper(dbSession).selectByRuleId(rule.getId());
+ }
+
+ public List<ActiveRuleParamDto> selectAllParams(DbSession dbSession) {
+ return mapper(dbSession).selectAllParams();
+ }
+
+ public ActiveRuleDto insert(DbSession session, ActiveRuleDto item) {
+ Preconditions.checkArgument(item.getProfileId() != null, QUALITY_PROFILE_IS_NOT_PERSISTED);
+ Preconditions.checkArgument(item.getRuleId() != null, RULE_IS_NOT_PERSISTED);
+ Preconditions.checkArgument(item.getId() == null, ACTIVE_RULE_IS_ALREADY_PERSISTED);
+ mapper(session).insert(item);
+ return item;
+ }
+
+ public ActiveRuleDto update(DbSession session, ActiveRuleDto item) {
+ Preconditions.checkArgument(item.getProfileId() != null, QUALITY_PROFILE_IS_NOT_PERSISTED);
+ Preconditions.checkArgument(item.getRuleId() != null, ActiveRuleDao.RULE_IS_NOT_PERSISTED);
+ Preconditions.checkArgument(item.getId() != null, ACTIVE_RULE_IS_NOT_PERSISTED);
+ mapper(session).update(item);
+ return item;
+ }
+
+ public void delete(DbSession session, ActiveRuleKey key) {
+ Optional<ActiveRuleDto> activeRule = selectByKey(session, key);
+ if (activeRule.isPresent()) {
+ mapper(session).deleteParameters(activeRule.get().getId());
+ mapper(session).delete(activeRule.get().getId());
+ }
+ }
+
+ /**
+ * Nested DTO ActiveRuleParams
+ */
+
+ public ActiveRuleParamDto insertParam(DbSession session, ActiveRuleDto activeRule, ActiveRuleParamDto activeRuleParam) {
+ Preconditions.checkArgument(activeRule.getId() != null, ACTIVE_RULE_IS_NOT_PERSISTED);
+ Preconditions.checkArgument(activeRuleParam.getId() == null, ACTIVE_RULE_PARAM_IS_ALREADY_PERSISTED);
+ Preconditions.checkNotNull(activeRuleParam.getRulesParameterId(), RULE_PARAM_IS_NOT_PERSISTED);
+
+ activeRuleParam.setActiveRuleId(activeRule.getId());
+ mapper(session).insertParameter(activeRuleParam);
+ return activeRuleParam;
+ }
+
+ public void deleteParamByKeyAndName(DbSession session, ActiveRuleKey key, String param) {
+ // TODO SQL rewrite to delete by key
+ Optional<ActiveRuleDto> activeRule = selectByKey(session, key);
+ if (activeRule.isPresent()) {
+ ActiveRuleParamDto activeRuleParam = mapper(session).selectParamByActiveRuleAndKey(activeRule.get().getId(), param);
+ if (activeRuleParam != null) {
+ mapper(session).deleteParameter(activeRuleParam.getId());
+ }
+ }
+ }
+
+ public void updateParam(DbSession session, ActiveRuleDto activeRule, ActiveRuleParamDto activeRuleParam) {
+ Preconditions.checkNotNull(activeRule.getId(), ACTIVE_RULE_IS_NOT_PERSISTED);
+ Preconditions.checkNotNull(activeRuleParam.getId(), ACTIVE_RULE_PARAM_IS_NOT_PERSISTED);
+ mapper(session).updateParameter(activeRuleParam);
+ }
+
+ public void deleteParam(DbSession session, ActiveRuleDto activeRule, ActiveRuleParamDto activeRuleParam) {
+ Preconditions.checkNotNull(activeRule.getId(), ACTIVE_RULE_IS_NOT_PERSISTED);
+ Preconditions.checkNotNull(activeRuleParam.getId(), ACTIVE_RULE_PARAM_IS_NOT_PERSISTED);
+ mapper(session).deleteParameter(activeRuleParam.getId());
+ }
+
+ public void deleteByProfileKey(DbSession session, String profileKey) {
+ /** Functional cascade for params */
+ for (ActiveRuleDto activeRule : selectByProfileKey(session, profileKey)) {
+ delete(session, activeRule.getKey());
+ }
+ }
+
+ public List<ActiveRuleDto> selectByProfileKey(DbSession session, String profileKey) {
+ return mapper(session).selectByProfileKey(profileKey);
+ }
+
+ /**
+ * Finder methods for ActiveRuleParams
+ */
+
+ public List<ActiveRuleParamDto> selectParamsByActiveRuleKey(DbSession session, ActiveRuleKey key) {
+ Preconditions.checkNotNull(key, ACTIVE_RULE_KEY_CANNOT_BE_NULL);
+ ActiveRuleDto activeRule = selectOrFailByKey(session, key);
+ return mapper(session).selectParamsByActiveRuleId(activeRule.getId());
+ }
+
+ @CheckForNull
+ public ActiveRuleParamDto selectParamByKeyAndName(ActiveRuleKey key, String name, DbSession session) {
+ Preconditions.checkNotNull(key, ACTIVE_RULE_KEY_CANNOT_BE_NULL);
+ Preconditions.checkNotNull(name, PARAMETER_NAME_CANNOT_BE_NULL);
+ Optional<ActiveRuleDto> activeRule = selectByKey(session, key);
+ if (activeRule.isPresent()) {
+ return mapper(session).selectParamByActiveRuleAndKey(activeRule.get().getId(), name);
+ }
+ return null;
+ }
+
+ public void deleteParamsByRuleParam(DbSession dbSession, RuleDto rule, String paramKey) {
+ List<ActiveRuleDto> activeRules = selectByRule(dbSession, rule);
+ for (ActiveRuleDto activeRule : activeRules) {
+ for (ActiveRuleParamDto activeParam : selectParamsByActiveRuleKey(dbSession, activeRule.getKey())) {
+ if (activeParam.getKey().equals(paramKey)) {
+ deleteParam(dbSession, activeRule, activeParam);
+ }
+ }
+ }
+ }
+
+ private ActiveRuleMapper mapper(DbSession session) {
+ return session.getMapper(ActiveRuleMapper.class);
+ }
+}
diff --git a/sonar-db/src/main/java/org/sonar/db/qualityprofile/ActiveRuleDto.java b/sonar-db/src/main/java/org/sonar/db/qualityprofile/ActiveRuleDto.java
index 17aa911ad9d..2f6328e413d 100644
--- a/sonar-db/src/main/java/org/sonar/db/qualityprofile/ActiveRuleDto.java
+++ b/sonar-db/src/main/java/org/sonar/db/qualityprofile/ActiveRuleDto.java
@@ -36,17 +36,20 @@ public class ActiveRuleDto extends Dto<ActiveRuleKey> {
public static final String INHERITED = ActiveRule.INHERITED;
public static final String OVERRIDES = ActiveRule.OVERRIDES;
- private String repository;
- private String ruleField;
- private String profileKey;
-
private Integer id;
private Integer profileId;
private Integer ruleId;
private Integer severity;
private String inheritance;
- //This field do not exists in db, it's only retrieve by joins
+
+ private long createdAtInMs;
+ private long updatedAtInMs;
+
+ //These fields do not exists in db, it's only retrieve by joins
private Integer parentId;
+ private String repository;
+ private String ruleField;
+ private String profileKey;
/**
* @deprecated for internal use, should be private
@@ -77,7 +80,6 @@ public class ActiveRuleDto extends Dto<ActiveRuleKey> {
return profileId;
}
- // TODO mark as private
public ActiveRuleDto setProfileId(Integer profileId) {
this.profileId = profileId;
return this;
@@ -87,7 +89,6 @@ public class ActiveRuleDto extends Dto<ActiveRuleKey> {
return ruleId;
}
- // TODO mark as private
public ActiveRuleDto setRuleId(Integer ruleId) {
this.ruleId = ruleId;
return this;
@@ -122,10 +123,12 @@ public class ActiveRuleDto extends Dto<ActiveRuleKey> {
}
@CheckForNull
+ @Deprecated
public Integer getParentId() {
return parentId;
}
+ @Deprecated
public ActiveRuleDto setParentId(@Nullable Integer parentId) {
this.parentId = parentId;
return this;
@@ -139,6 +142,24 @@ public class ActiveRuleDto extends Dto<ActiveRuleKey> {
return StringUtils.equals(OVERRIDES, inheritance);
}
+ public long getUpdatedAtInMs() {
+ return updatedAtInMs;
+ }
+
+ public ActiveRuleDto setUpdatedAtInMs(long updatedAtInMs) {
+ this.updatedAtInMs = updatedAtInMs;
+ return this;
+ }
+
+ public long getCreatedAtInMs() {
+ return createdAtInMs;
+ }
+
+ public ActiveRuleDto setCreatedAtInMs(long createdAtInMs) {
+ this.createdAtInMs = createdAtInMs;
+ return this;
+ }
+
public static ActiveRuleDto createFor(QualityProfileDto profileDto, RuleDto ruleDto) {
Preconditions.checkNotNull(profileDto.getId(), "Profile is not persisted");
Preconditions.checkNotNull(ruleDto.getId(), "Rule is not persisted");
diff --git a/sonar-db/src/main/java/org/sonar/db/qualityprofile/QualityProfileDao.java b/sonar-db/src/main/java/org/sonar/db/qualityprofile/QualityProfileDao.java
index 70f610d12d2..089adea159e 100644
--- a/sonar-db/src/main/java/org/sonar/db/qualityprofile/QualityProfileDao.java
+++ b/sonar-db/src/main/java/org/sonar/db/qualityprofile/QualityProfileDao.java
@@ -32,8 +32,8 @@ import org.sonar.api.utils.System2;
import org.sonar.db.Dao;
import org.sonar.db.DbSession;
import org.sonar.db.MyBatis;
-import org.sonar.db.component.ComponentDto;
import org.sonar.db.RowNotFoundException;
+import org.sonar.db.component.ComponentDto;
@ServerSide
public class QualityProfileDao implements Dao {
@@ -209,21 +209,11 @@ public class QualityProfileDao implements Dao {
}
}
- /**
- * @deprecated Replaced by
- * {@link #selectByKey(DbSession, String)}
- */
- @Deprecated
@CheckForNull
public QualityProfileDto selectById(DbSession session, int id) {
return mapper(session).selectById(id);
}
- /**
- * @deprecated Replaced by
- * {@link #selectByKey(DbSession, String)}
- */
- @Deprecated
@CheckForNull
public QualityProfileDto selectById(int id) {
DbSession session = mybatis.openSession(false);
diff --git a/sonar-db/src/main/resources/org/sonar/db/qualityprofile/ActiveRuleMapper.xml b/sonar-db/src/main/resources/org/sonar/db/qualityprofile/ActiveRuleMapper.xml
index 00f008aca51..ebeecc1030d 100644
--- a/sonar-db/src/main/resources/org/sonar/db/qualityprofile/ActiveRuleMapper.xml
+++ b/sonar-db/src/main/resources/org/sonar/db/qualityprofile/ActiveRuleMapper.xml
@@ -13,7 +13,9 @@
r.plugin_name as "repository",
qp.kee as "profileKey",
a.created_at as "createdAt",
- a.updated_at as "updatedAt"
+ a.updated_at as "updatedAt",
+ a.created_at_ms as "createdAtInMs",
+ a.updated_at_ms as "updatedAtInMs"
</sql>
<sql id="activeRuleKeyJoin">
@@ -21,6 +23,7 @@
INNER JOIN rules r ON r.id = a.rule_id
</sql>
+ <!-- Should be removed when ActiveRuleDao v2 will be removed -->
<sql id="activeRuleColumns">
a.id,
a.profile_id as profileId,
@@ -29,9 +32,12 @@
a.inheritance as inheritance,
active_rule_parent.id as parentId,
a.created_at as "createdAt",
- a.updated_at as "updatedAt"
+ a.updated_at as "updatedAt",
+ a.created_at_ms as "createdAtInMs",
+ a.updated_at_ms as "updatedAtInMs"
</sql>
+ <!-- Should be removed when ActiveRuleDao v2 will be removed -->
<sql id="activeRuleJoin">
INNER JOIN rules_profiles qp ON qp.id=a.profile_id
LEFT JOIN rules_profiles profile_parent ON profile_parent.kee=qp.parent_kee
@@ -52,8 +58,8 @@
</select>
<insert id="insert" parameterType="ActiveRule" keyColumn="id" useGeneratedKeys="true" keyProperty="id">
- INSERT INTO active_rules (profile_id, rule_id, failure_level, inheritance, created_at, updated_at)
- VALUES (#{profileId}, #{ruleId}, #{severity}, #{inheritance}, #{createdAt}, #{updatedAt})
+ INSERT INTO active_rules (profile_id, rule_id, failure_level, inheritance, created_at, updated_at, created_at_ms, updated_at_ms)
+ VALUES (#{profileId}, #{ruleId}, #{severity}, #{inheritance}, #{createdAt}, #{updatedAt}, #{createdAtInMs}, #{updatedAtInMs})
</insert>
<update id="update" parameterType="ActiveRule">
@@ -62,7 +68,8 @@
rule_id=#{ruleId},
failure_level=#{severity},
inheritance=#{inheritance},
- updated_at=#{updatedAt}
+ updated_at=#{updatedAt},
+ updated_at_ms=#{updatedAtInMs}
WHERE id=#{id}
</update>
diff --git a/sonar-db/src/test/java/org/sonar/db/qualityprofile/ActiveRuleDaoTest.java b/sonar-db/src/test/java/org/sonar/db/qualityprofile/ActiveRuleDaoTest.java
new file mode 100644
index 00000000000..1db32fe9e92
--- /dev/null
+++ b/sonar-db/src/test/java/org/sonar/db/qualityprofile/ActiveRuleDaoTest.java
@@ -0,0 +1,25 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.db.qualityprofile;
+
+// TODO
+public class ActiveRuleDaoTest {
+
+}