]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8675 don't use resource_index in ProjectQgateAssociationMapper
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Mon, 23 Jan 2017 19:17:09 +0000 (20:17 +0100)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Wed, 25 Jan 2017 10:41:56 +0000 (11:41 +0100)
sonar-db/src/main/java/org/sonar/db/qualitygate/ProjectQgateAssociationQuery.java
sonar-db/src/main/resources/org/sonar/db/qualitygate/ProjectQgateAssociationMapper.xml
sonar-db/src/test/java/org/sonar/db/qualitygate/ProjectQgateAssociationDaoTest.java
sonar-db/src/test/java/org/sonar/db/qualitygate/ProjectQgateAssociationQueryTest.java
sonar-db/src/test/resources/org/sonar/db/qualitygate/ProjectQgateAssociationDaoTest/shared.xml

index 42783799e5715a8246ad6d26164c06ed1ce9292a..d69b4073f8f985d8b637c90022981acab1312e87 100644 (file)
@@ -26,6 +26,8 @@ import java.util.Set;
 import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
 import org.apache.commons.lang.StringUtils;
+import org.sonar.db.DatabaseUtils;
+import org.sonar.db.WildcardPosition;
 
 public class ProjectQgateAssociationQuery {
 
@@ -43,7 +45,7 @@ public class ProjectQgateAssociationQuery {
   private final String projectSearch;
 
   // for internal use in MyBatis
-  private final String projectSearchSql;
+  private final String projectSearchUpperLikeSql;
 
   // max results per page
   private final int pageSize;
@@ -55,23 +57,16 @@ public class ProjectQgateAssociationQuery {
     this.gateId = builder.gateId;
     this.membership = builder.membership;
     this.projectSearch = builder.projectSearch;
-    this.projectSearchSql = projectSearchToSql(projectSearch);
+    if (this.projectSearch == null) {
+      this.projectSearchUpperLikeSql = null;
+    } else {
+      this.projectSearchUpperLikeSql = DatabaseUtils.buildLikeValue(projectSearch.toUpperCase(Locale.ENGLISH), WildcardPosition.BEFORE_AND_AFTER);
+    }
 
     this.pageSize = builder.pageSize;
     this.pageIndex = builder.pageIndex;
   }
 
-  private String projectSearchToSql(@Nullable String value) {
-    if (value == null) {
-      return null;
-    }
-
-    return value
-      .replaceAll("%", "\\\\%")
-      .replaceAll("_", "\\\\_")
-      .toLowerCase(Locale.ENGLISH) + "%";
-  }
-
   public String gateId() {
     return gateId;
   }
@@ -89,10 +84,6 @@ public class ProjectQgateAssociationQuery {
     return projectSearch;
   }
 
-  public String projectSearchSql() {
-    return projectSearchSql;
-  }
-
   public int pageSize() {
     return pageSize;
   }
index 01203faf49ac98fd0d4b10d43ebc322752f09eca..1254dda2303117727f16a2565d3eaf235e9ed736 100644 (file)
@@ -6,30 +6,23 @@
   <select id="selectProjects" parameterType="map" resultType="ProjectQgateAssociation">
     SELECT proj.id as id, proj.name as name, prop.text_value as gateId
     FROM projects proj
+    LEFT JOIN properties prop ON prop.resource_id=proj.id AND prop.prop_key='sonar.qualitygate' AND prop.text_value = #{query.gateId}
+    where
+    proj.qualifier = 'TRK'
+    and proj.enabled = ${_true}
+    and proj.copy_component_uuid is null
+    <choose>
+      <when test="query.membership() == 'selected'">
+        and prop.text_value IS NOT NULL
+      </when>
+      <when test="query.membership() == 'deselected'">
+        and prop.text_value IS NULL
+      </when>
+    </choose>
     <if test="query.projectSearch() != null">
-      JOIN resource_index ind ON ind.root_component_uuid=proj.uuid
+      and (proj.kee = #{query.projectSearch} or upper(proj.name) like #{query.projectSearchUpperLikeSql} escape '/')
     </if>
-    LEFT JOIN properties prop ON prop.resource_id=proj.id AND prop.prop_key='sonar.qualitygate' AND prop.text_value LIKE
-    #{query.gateId}
-    <where>
-      <choose>
-        <when test="query.membership() == 'selected'">
-          AND prop.text_value IS NOT NULL
-        </when>
-        <when test="query.membership() == 'deselected'">
-          AND prop.text_value IS NULL
-        </when>
-      </choose>
-      <if test="query.projectSearch() != null">
-        AND ind.kee LIKE #{query.projectSearchSql}
-      </if>
-      AND proj.qualifier='TRK'
-      AND proj.scope='PRJ'
-      <if test="query.projectSearch() != null">
-        AND ind.qualifier='TRK'
-      </if>
-    </where>
-    ORDER BY proj.name
+    order by proj.name
   </select>
 
   <select id="selectQGateIdByComponentId" parameterType="long" resultType="string">
index f4b5acfed92d826f1ab07059b7fb84d751b42433..d77350c7bb98caef2a7f1c9608551e739063951f 100644 (file)
@@ -38,11 +38,11 @@ public class ProjectQgateAssociationDaoTest {
 
   @Rule
   public DbTester db = DbTester.create(System2.INSTANCE);
-  ComponentDbTester componentDb = new ComponentDbTester(db);
-  DbClient dbClient = db.getDbClient();
-  DbSession dbSession = db.getSession();
 
-  ProjectQgateAssociationDao underTest = db.getDbClient().projectQgateAssociationDao();
+  private ComponentDbTester componentDb = new ComponentDbTester(db);
+  private DbClient dbClient = db.getDbClient();
+  private DbSession dbSession = db.getSession();
+  private ProjectQgateAssociationDao underTest = db.getDbClient().projectQgateAssociationDao();
 
   @Test
   public void select_all_projects_by_query() {
@@ -73,7 +73,7 @@ public class ProjectQgateAssociationDaoTest {
     result = underTest.selectProjects(dbSession, ProjectQgateAssociationQuery.builder().gateId("42").projectSearch("one").build());
     assertThat(result).hasSize(1);
     result = underTest.selectProjects(dbSession, ProjectQgateAssociationQuery.builder().gateId("42").projectSearch("project").build());
-    assertThat(result).hasSize(2);
+    assertThat(result).hasSize(5);
   }
 
   @Test
index 8b86b30cdd30e276df3d823dc6d3eafcb37ba478..93efa0a5c305b3d75752d85419d76ea5b21ae491 100644 (file)
@@ -23,22 +23,11 @@ import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
 
-import static org.assertj.core.api.Assertions.assertThat;
-
 public class ProjectQgateAssociationQueryTest {
 
   @Rule
   public ExpectedException expectedException = ExpectedException.none();
 
-  @Test
-  public void handle_underscore_and_percent() {
-    ProjectQgateAssociationQuery underTest = ProjectQgateAssociationQuery.builder()
-      .projectSearch("project-_%-search")
-      .gateId("1").build();
-
-    assertThat(underTest.projectSearchSql()).isEqualTo("project-\\_\\%-search%");
-  }
-
   @Test
   public void fail_on_null_login() {
     expectedException.expect(NullPointerException.class);
index bcf287703388f3191f07d04276f8f541771b38fd..13ca29d2acc6b600b8e8c23c85bdd4caefd7d86d 100644 (file)
@@ -13,6 +13,8 @@
             name="Project One"
             qualifier="TRK"
             scope="PRJ"
+            enabled="[true]"
+            copy_component_uuid="[null]"
             id="1"/>
   <projects organization_uuid="org1"
             uuid="B"
@@ -22,6 +24,8 @@
             name="Project Two"
             qualifier="TRK"
             scope="PRJ"
+            enabled="[true]"
+            copy_component_uuid="[null]"
             id="2"/>
   <projects organization_uuid="org1"
             uuid="C"
@@ -31,6 +35,8 @@
             name="Project Three"
             qualifier="TRK"
             scope="PRJ"
+            enabled="[true]"
+            copy_component_uuid="[null]"
             id="3"/>
   <projects organization_uuid="org1"
             uuid="D"
@@ -40,6 +46,8 @@
             name="Project Four"
             qualifier="TRK"
             scope="PRJ"
+            enabled="[true]"
+            copy_component_uuid="[null]"
             id="4"/>
   <projects organization_uuid="org1"
             uuid="E"
@@ -49,6 +57,8 @@
             name="Project Five"
             qualifier="TRK"
             scope="PRJ"
+            enabled="[true]"
+            copy_component_uuid="[null]"
             id="5"/>
   <projects organization_uuid="org1"
             uuid="F"
@@ -58,6 +68,8 @@
             name="View Six"
             qualifier="VW"
             scope="PRJ"
+            enabled="[true]"
+            copy_component_uuid="[null]"
             id="6"/>
   <projects organization_uuid="org1"
             uuid="G"
             name="File One"
             qualifier="TRK"
             scope="FIL"
+            enabled="[true]"
+            copy_component_uuid="C"
             id="7"/>
 
-  <resource_index id="1"
-                  kee="project one"
-                  component_uuid="A"
-                  root_component_uuid="A"
-                  position="0"
-                  name_size="11"
-                  qualifier="TRK"/>
-  <resource_index id="2"
-                  kee="roject one"
-                  component_uuid="A"
-                  root_component_uuid="A"
-                  position="1"
-                  name_size="11"
-                  qualifier="TRK"/>
-  <resource_index id="3"
-                  kee="oject one"
-                  component_uuid="A"
-                  root_component_uuid="A"
-                  position="2"
-                  name_size="11"
-                  qualifier="TRK"/>
-  <resource_index id="4"
-                  kee="ject one"
-                  component_uuid="A"
-                  root_component_uuid="A"
-                  position="3"
-                  name_size="11"
-                  qualifier="TRK"/>
-  <resource_index id="5"
-                  kee="ect one"
-                  component_uuid="A"
-                  root_component_uuid="A"
-                  position="4"
-                  name_size="11"
-                  qualifier="TRK"/>
-  <resource_index id="6"
-                  kee="ct one"
-                  component_uuid="A"
-                  root_component_uuid="A"
-                  position="5"
-                  name_size="11"
-                  qualifier="TRK"/>
-  <resource_index id="7"
-                  kee="t one"
-                  component_uuid="A"
-                  root_component_uuid="A"
-                  position="6"
-                  name_size="11"
-                  qualifier="TRK"/>
-  <resource_index id="8"
-                  kee=" one"
-                  component_uuid="A"
-                  root_component_uuid="A"
-                  position="7"
-                  name_size="11"
-                  qualifier="TRK"/>
-  <resource_index id="9"
-                  kee="one"
-                  component_uuid="A"
-                  root_component_uuid="A"
-                  position="8"
-                  name_size="11"
-                  qualifier="TRK"/>
-  <resource_index id="10"
-                  kee="project two"
-                  component_uuid="B"
-                  root_component_uuid="B"
-                  position="0"
-                  name_size="11"
-                  qualifier="TRK"/>
-  <resource_index id="11"
-                  kee="roject two"
-                  component_uuid="B"
-                  root_component_uuid="B"
-                  position="1"
-                  name_size="11"
-                  qualifier="TRK"/>
-  <resource_index id="12"
-                  kee="oject two"
-                  component_uuid="B"
-                  root_component_uuid="B"
-                  position="2"
-                  name_size="11"
-                  qualifier="TRK"/>
-  <resource_index id="13"
-                  kee="ject two"
-                  component_uuid="B"
-                  root_component_uuid="B"
-                  position="3"
-                  name_size="11"
-                  qualifier="TRK"/>
-  <resource_index id="14"
-                  kee="ect two"
-                  component_uuid="B"
-                  root_component_uuid="B"
-                  position="4"
-                  name_size="11"
-                  qualifier="TRK"/>
-  <resource_index id="15"
-                  kee="ct two"
-                  component_uuid="B"
-                  root_component_uuid="B"
-                  position="5"
-                  name_size="11"
-                  qualifier="TRK"/>
-  <resource_index id="16"
-                  kee="t two"
-                  component_uuid="B"
-                  root_component_uuid="B"
-                  position="6"
-                  name_size="11"
-                  qualifier="TRK"/>
-  <resource_index id="17"
-                  kee=" two"
-                  component_uuid="B"
-                  root_component_uuid="B"
-                  position="7"
-                  name_size="11"
-                  qualifier="TRK"/>
-
   <properties id="1"
               prop_key="sonar.qualitygate"
               resource_id="[null]"