]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5237 implement RuleDao#keysOfRowsUpdatedAfter()
authorSimon Brandhof <simon.brandhof@gmail.com>
Mon, 28 Apr 2014 16:53:34 +0000 (18:53 +0200)
committerSimon Brandhof <simon.brandhof@gmail.com>
Mon, 28 Apr 2014 16:53:42 +0000 (18:53 +0200)
sonar-core/src/main/java/org/sonar/core/db/Dao.java
sonar-core/src/main/java/org/sonar/core/db/package-info.java [new file with mode: 0644]
sonar-core/src/main/java/org/sonar/core/rule/RuleDao.java
sonar-core/src/main/java/org/sonar/core/rule/RuleMapper.java
sonar-core/src/main/resources/org/sonar/core/rule/RuleMapper.xml
sonar-core/src/test/java/org/sonar/core/rule/RuleDaoTest.java
sonar-server/src/main/java/org/sonar/server/rule/ws/RuleShowWsHandler.java
sonar-server/src/main/java/org/sonar/server/rule2/ws/SearchAction.java
sonar-server/src/main/java/org/sonar/server/rule2/ws/ShowAction.java

index 32ef3e2f611bf36af0ce2e84ed5cf92a98b49ac5..8a6a95e42a25ccf20381044d2b53ff2e28b97807 100644 (file)
@@ -22,7 +22,6 @@ package org.sonar.core.db;
 import org.apache.ibatis.session.SqlSession;
 
 import java.io.Serializable;
-import java.util.Collection;
 
 public interface Dao<E extends Dto<K>, K extends Serializable> {
 
@@ -44,5 +43,5 @@ public interface Dao<E extends Dto<K>, K extends Serializable> {
 
   void deleteByKey(K key, SqlSession session);
 
-  Collection<K> insertsSince(Long timestamp);
+  Iterable<K> keysOfRowsUpdatedAfter(long timestamp);
 }
diff --git a/sonar-core/src/main/java/org/sonar/core/db/package-info.java b/sonar-core/src/main/java/org/sonar/core/db/package-info.java
new file mode 100644 (file)
index 0000000..cb50b64
--- /dev/null
@@ -0,0 +1,23 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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.
+ */
+@ParametersAreNonnullByDefault
+package org.sonar.core.db;
+
+import javax.annotation.ParametersAreNonnullByDefault;
index 8053e3ccd9dae414813c76bb19817ff8e89440f7..9c6b060e8466d65c4745f1788d34cef04cdc582b 100644 (file)
@@ -20,6 +20,8 @@
 package org.sonar.core.rule;
 
 import com.google.common.collect.Lists;
+import org.apache.ibatis.session.ResultContext;
+import org.apache.ibatis.session.ResultHandler;
 import org.apache.ibatis.session.SqlSession;
 import org.sonar.api.BatchComponent;
 import org.sonar.api.ServerComponent;
@@ -29,9 +31,10 @@ import org.sonar.core.db.UnsuportedException;
 import org.sonar.core.persistence.MyBatis;
 
 import javax.annotation.CheckForNull;
-
+import java.sql.Timestamp;
 import java.util.Collection;
 import java.util.List;
+import java.util.Map;
 
 import static com.google.common.collect.Lists.newArrayList;
 
@@ -315,8 +318,20 @@ public class RuleDao extends BaseDao<RuleDto, RuleKey>
   }
 
   @Override
-  public Collection<RuleKey> insertsSince(Long timestamp) {
-    // TODO Auto-generated method stub
-    return null;
+  public Collection<RuleKey> keysOfRowsUpdatedAfter(long timestamp) {
+    SqlSession session = mybatis.openSession();
+    try {
+      final List<RuleKey> keys = Lists.newArrayList();
+      session.select("selectKeysOfRulesUpdatedSince", new Timestamp(timestamp), new ResultHandler() {
+        @Override
+        public void handleResult(ResultContext context) {
+          Map<String, String> map = (Map) context.getResultObject();
+          keys.add(RuleKey.of(map.get("repo"), map.get("rule")));
+        }
+      });
+      return keys;
+    } finally {
+      MyBatis.closeQuietly(session);
+    }
   }
 }
index 75f3d4e62b16a97fa82fd7e7a975e1ed7c07084f..d0d7975a9d00511f1598ea609eba212212f7770e 100644 (file)
@@ -67,4 +67,5 @@ public interface RuleMapper {
   void updateTag(RuleRuleTagDto existingTag);
 
   List<RuleRuleTagDto> selectTagsByRuleIds(@Param("ruleIds") List<Integer> ruleIds);
+
 }
index ce88ab8bdd538ff276997ff04d6e344728874682..ee7b13de0bd21cf054932cb05bcb6c8629cb81bc 100644 (file)
     DELETE FROM rules_rule_tags WHERE id=#{tagId}
   </update>
 
+
+  <select id="selectKeysOfRulesUpdatedSince" resultType="map">
+    SELECT r.plugin_name as "repo", r.plugin_rule_key as "rule"
+    FROM rules r
+    WHERE r.updated_at IS NULL or r.updated_at &gt;= #{id}
+  </select>
 </mapper>
 
index 6386962c054ab7ce98a067f8e6d3c0ceca4b45ad..3e3bc22abb50e0ab0d3b5af2b91e15279b7ef542 100644 (file)
@@ -32,6 +32,7 @@ import org.sonar.api.utils.DateUtils;
 import org.sonar.check.Cardinality;
 import org.sonar.core.persistence.AbstractDaoTestCase;
 
+import java.util.Arrays;
 import java.util.List;
 
 import static com.google.common.collect.Lists.newArrayList;
@@ -371,6 +372,33 @@ public class RuleDaoTest extends AbstractDaoTestCase {
     assertThat(dao.selectTagsByRuleIds(newArrayList(3, 4))).hasSize(3);
   }
 
+  @Test
+  public void keysOfRowsUpdatedAfter() throws Exception {
+    setupData("empty");
+
+    RuleDto rule1 = new RuleDto()
+      .setId(1)
+      .setRepositoryKey("foo")
+      .setRuleKey("R1")
+      .setName("ROne")
+      .setCreatedAt(DateUtils.parseDate("2013-12-16"))
+      .setUpdatedAt(DateUtils.parseDate("2013-12-16"));
+    RuleDto rule2 = new RuleDto()
+      .setId(2)
+      .setRepositoryKey("foo")
+      .setRuleKey("R2")
+      .setName("RTwo")
+      .setCreatedAt(DateUtils.parseDate("2014-01-28"))
+      .setUpdatedAt(DateUtils.parseDate("2014-05-19"));
+    dao.insert(Arrays.asList(rule1, rule2));
+
+    assertThat(dao.keysOfRowsUpdatedAfter(DateUtils.parseDate("2014-06-01").getTime())).isEmpty();
+    assertThat(dao.keysOfRowsUpdatedAfter(DateUtils.parseDate("2012-01-01").getTime())).hasSize(2);
+    Iterable<RuleKey> keys = dao.keysOfRowsUpdatedAfter(DateUtils.parseDate("2014-05-17").getTime());
+    assertThat(keys).hasSize(1);
+    assertThat(Iterables.getFirst(keys, null).rule()).isEqualTo("R2");
+  }
+
   private List<Integer> idsFromRuleDtos(List<RuleDto> ruleDtos){
     return newArrayList(Iterables.transform(ruleDtos, new Function<RuleDto, Integer>() {
       @Override
index 7625c9244c2e15d95fc0f38a39c082f8fe6bbb7a..6a76e65d384136ff5a8597083cdc031b7d577178 100644 (file)
@@ -103,8 +103,7 @@ public class RuleShowWsHandler implements RequestHandler {
     json
       .prop("key", rule.ruleKey().toString())
       .prop("name", rule.name())
-      .prop("description", rule.description())
-    ;
+      .prop("description", rule.description());
     addLanguage(rule, json);
     addNote(rule, json);
     addDate(rule.createdAt(), "createdAt", json);
@@ -119,7 +118,6 @@ public class RuleShowWsHandler implements RequestHandler {
       Language language = languages.get(languageKey);
       json.prop("language", language == null ? languageKey : language.getName());
     }
-
   }
 
   private void addNote(Rule rule, JsonWriter json) {
index 21f659a63f6d3c688c0a63543c336d6472d899a2..ccd61ea14b4fb104e2a0cb368542c10f37ff1b67 100644 (file)
@@ -55,7 +55,7 @@ public class SearchAction implements RequestHandler {
 
     action
       .createParam("activation")
-      .setDescription("Only if 'qProfile' is set. Possible values are: true | false | all")
+      .setDescription("Used only if 'qProfile' is set. Possible values are: true | false | all")
       .setExampleValue("java:Sonar way");
   }
 
index b844aad181f03d5e3551fa702a7e53f45c9de66a..0c9b75b1a49ccfbcb53c9a78bf63867e8ff738b3 100644 (file)
@@ -43,11 +43,17 @@ public class ShowAction implements RequestHandler {
       .setSince("4.4")
       .setHandler(this);
 
+    action
+      .createParam("repo")
+      .setDescription("Repository key")
+      .setRequired(true)
+      .setExampleValue("javascript");
+
     action
       .createParam("key")
       .setDescription("Rule key")
       .setRequired(true)
-      .setExampleValue("javascript:EmptyBlock");
+      .setExampleValue("EmptyBlock");
   }
 
   @Override