]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6749 Add query to get custom measures from a metric key and a text value 496/head
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 7 Sep 2015 14:12:51 +0000 (16:12 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 7 Sep 2015 14:16:09 +0000 (16:16 +0200)
sonar-db/src/main/java/org/sonar/db/component/ComponentDao.java
sonar-db/src/main/java/org/sonar/db/component/ComponentMapper.java
sonar-db/src/main/java/org/sonar/db/measure/custom/CustomMeasureDao.java
sonar-db/src/main/java/org/sonar/db/measure/custom/CustomMeasureDto.java
sonar-db/src/main/java/org/sonar/db/measure/custom/CustomMeasureMapper.java
sonar-db/src/main/resources/org/sonar/db/component/ComponentMapper.xml
sonar-db/src/main/resources/org/sonar/db/measure/custom/CustomMeasureMapper.xml
sonar-db/src/test/java/org/sonar/db/measure/custom/CustomMeasureDaoTest.java
sonar-db/src/test/resources/org/sonar/db/measure/custom/CustomMeasureDaoTest/select_by_metric_key_and_text_value.xml [new file with mode: 0644]

index 4b753923e859ba07527c0be35d08ebef41f73712..2973460b1b3067ba4858ed8d0a66a84290e734dc 100644 (file)
@@ -239,15 +239,6 @@ public class ComponentDao implements Dao {
     return mapper(dbSession).selectByProjectUuid(projectUuid);
   }
 
-  /**
-   * Retrieves all components with a specific customer measure, no other filtering is done by this method.
-   *
-   * Used by Views plugin
-   */
-  public List<ComponentDto> selectByCustomMeasure(String metricKey, String metricValue, DbSession dbSession) {
-    return mapper(dbSession).selectByCustomMeasure(metricKey, metricValue);
-  }
-
   private static void addPartialQueryParameterIfNotNull(Map<String, Object> parameters, @Nullable String keyOrNameFilter) {
     if (keyOrNameFilter != null) {
       parameters.put("query", "%" + keyOrNameFilter.toUpperCase() + "%");
index 1f405472d4fd4d7b56722ef6fb9bdca2229e3af9..4204f8ad370a616a3d0f78118df0038256b88788 100644 (file)
@@ -55,8 +55,6 @@ public interface ComponentMapper {
 
   List<ComponentDto> selectByProjectUuid(@Param("projectUuid") String projectUuid);
 
-  List<ComponentDto> selectByCustomMeasure(@Param("metricKey") String metricKey, @Param("metricValue") String metricValue);
-
   List<String> selectExistingUuids(@Param("uuids") Collection<String> uuids);
 
   /**
index 3689413a78e4a72d5d0d119c014dbe5a6f1251a7..90a5cd2e3f8d3ef7f95a538010ba0c9735161af8 100644 (file)
@@ -84,6 +84,13 @@ public class CustomMeasureDao implements Dao {
     return mapper(session).selectByComponentUuid(componentUuid);
   }
 
+  /**
+   * Used by Views plugin
+   */
+  public List<CustomMeasureDto> selectByMetricKeyAndTextValue(DbSession session, String metricKey, String textValue) {
+    return mapper(session).selectByMetricKeyAndTextValue(metricKey, textValue);
+  }
+
   private CustomMeasureMapper mapper(DbSession session) {
     return session.getMapper(CustomMeasureMapper.class);
   }
index c17e0228fd9d5554db0b789cff8421c8d701049d..8de62f8b7217aaf4ab8971b053511fee3a3bc866 100644 (file)
 
 package org.sonar.db.measure.custom;
 
+import javax.annotation.CheckForNull;
+import javax.annotation.Nullable;
+
 public class CustomMeasureDto {
+
   private long id;
   private int metricId;
   private String componentUuid;
@@ -49,11 +53,12 @@ public class CustomMeasureDto {
     return this;
   }
 
+  @CheckForNull
   public String getTextValue() {
     return textValue;
   }
 
-  public CustomMeasureDto setTextValue(String textValue) {
+  public CustomMeasureDto setTextValue(@Nullable String textValue) {
     this.textValue = textValue;
     return this;
   }
index cb029d6f7c81f935335ac9f94fda8740e5d4f993..d69733bcfe53b96e171c1e776922a256e155f713 100644 (file)
@@ -41,6 +41,8 @@ public interface CustomMeasureMapper {
 
   List<CustomMeasureDto> selectByComponentUuid(String s, RowBounds rowBounds);
 
+  List<CustomMeasureDto> selectByMetricKeyAndTextValue(@Param("metricKey") String metricKey, @Param("textValue") String textValue);
+
   int countByComponentUuid(String componentUuid);
 
   int countByComponentIdAndMetricId(@Param("componentUuid") String componentUuid, @Param("metricId") int metricId);
index 86861586b7d5efdb2403b5611fd59c6b5153c777..a3c2a787c1356da5ea0d7dde1933eeabc75c6626 100644 (file)
     </where>
   </select>
 
-  <select id="selectResourcesByCustomerMeasure" parameterType="String" resultType="Component">
-    SELECT
-    <include refid="componentColumns"/>
-    FROM projects p, metric m, manual_measures mm
-    <where>
-      m.name=#{metricKey}
-      and mm.metric_id = m.id
-      and mm.text_value=#{metricValue}
-      and p.id=mm.resourceId
-    </where>
-  </select>
-
   <select id="countById" parameterType="long" resultType="long">
     SELECT count(p.id)
     FROM projects p
index 8c264791389abb764e01e0e0c9a5310c51ad45f7..722724332f95309e6b4ffc5764b6c59f1dc5253d 100644 (file)
     where m.component_uuid=#{componentUuid}
   </select>
 
+  <select id="selectByMetricKeyAndTextValue" resultType="CustomMeasure">
+    SELECT
+    <include refid="selectColumns"/>
+    FROM manual_measures m
+    INNER JOIN metrics metric ON metric.id=m.metric_id AND metric.name=#{metricKey}
+    <where>
+      m.text_value=#{textValue}
+    </where>
+  </select>
+
   <insert id="insert" parameterType="CustomMeasure" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
     INSERT INTO manual_measures (
     metric_id, component_uuid, value, text_value, user_login, description, created_at, updated_at
index 6090ddcf783b7824f232b3d8dafd3f3f1d7b60ed..aaa68ff7347a26dd29827287de9f1a2a9f68951e 100644 (file)
@@ -157,4 +157,15 @@ public class CustomMeasureDaoTest {
     underTest.selectOrFail(session, 42L);
   }
 
+  @Test
+  public void select_by_metric_key_and_text_value() throws Exception {
+    db.prepareDbUnit(getClass(), "select_by_metric_key_and_text_value.xml");
+
+    List<CustomMeasureDto> result = underTest.selectByMetricKeyAndTextValue(session, "customKey", "value1");
+
+    assertThat(result).extracting("id").containsOnly(20L, 21L);
+
+    assertThat(underTest.selectByMetricKeyAndTextValue(session, "customKey", "unknown")).isEmpty();
+    assertThat(underTest.selectByMetricKeyAndTextValue(session, "unknown", "value1")).isEmpty();
+  }
 }
diff --git a/sonar-db/src/test/resources/org/sonar/db/measure/custom/CustomMeasureDaoTest/select_by_metric_key_and_text_value.xml b/sonar-db/src/test/resources/org/sonar/db/measure/custom/CustomMeasureDaoTest/select_by_metric_key_and_text_value.xml
new file mode 100644 (file)
index 0000000..ff00f75
--- /dev/null
@@ -0,0 +1,14 @@
+<dataset>
+
+  <metrics id="10" name="ncloc"/>
+  <metrics id="11" name="customKey"/>
+
+  <manual_measures id="20" metric_id="11" component_uuid="ABCD" value="[null]" text_value="value1"
+                  user_login="seb" description="" created_at="123456789" updated_at="123456789" />
+  <manual_measures id="21" metric_id="11" component_uuid="BCDE" value="[null]" text_value="value1"
+                  user_login="seb" description="" created_at="123456789" updated_at="123456789" />
+  <manual_measures id="22" metric_id="11" component_uuid="CDEF" value="[null]" text_value="value2"
+                  user_login="seb" description="" created_at="123456789" updated_at="123456789" />
+
+
+</dataset>