]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-4589 Use optimized query to copy projects table for dryRun
authorJulien HENRY <julien.henry@sonarsource.com>
Tue, 27 Aug 2013 14:09:49 +0000 (16:09 +0200)
committerJulien HENRY <julien.henry@sonarsource.com>
Tue, 27 Aug 2013 15:52:15 +0000 (17:52 +0200)
sonar-core/src/main/java/org/sonar/core/persistence/DbTemplate.java
sonar-core/src/main/java/org/sonar/core/persistence/DryRunDatabaseFactory.java
sonar-core/src/test/java/org/sonar/core/persistence/DbTemplateTest.java [deleted file]
sonar-core/src/test/java/org/sonar/core/persistence/DryRunDatabaseFactoryTest.java
sonar-core/src/test/resources/org/sonar/core/persistence/DryRunDatabaseFactoryTest/multi-modules-with-issues.xml

index 1ed424396b08d45ded0c92f7ecf553568bbe17c1..52d7015e3fb04f2c095723a040ef935d693136a4 100644 (file)
@@ -19,9 +19,7 @@
  */
 package org.sonar.core.persistence;
 
-import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Joiner;
-import com.google.common.collect.Lists;
 import org.apache.commons.dbcp.BasicDataSource;
 import org.apache.commons.dbutils.DbUtils;
 import org.apache.commons.lang.StringUtils;
@@ -31,17 +29,29 @@ import org.sonar.api.ServerComponent;
 import org.sonar.api.utils.SonarException;
 
 import javax.sql.DataSource;
-import java.sql.*;
-import java.util.List;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.sql.Timestamp;
+import java.sql.Types;
 
 public class DbTemplate implements ServerComponent {
   private static final Logger LOG = LoggerFactory.getLogger(DbTemplate.class);
 
-  public DbTemplate copyTable(DataSource source, DataSource dest, String table, String... whereClauses) {
+  public DbTemplate copyTable(DataSource source, DataSource dest, String table) {
+    String selectQuery = "SELECT * FROM " + table;
+    copyTable(source, dest, table, selectQuery);
+
+    return this;
+  }
+
+  public DbTemplate copyTable(DataSource source, DataSource dest, String table, String selectQuery) {
     LOG.debug("Copy table {}", table);
     long startup = System.currentTimeMillis();
 
-    String selectQuery = selectQuery(table, whereClauses);
     truncate(dest, table);
 
     Connection sourceConnection = null;
@@ -95,7 +105,6 @@ public class DbTemplate implements ServerComponent {
       DbUtils.closeQuietly(sourceStatement);
       DbUtils.closeQuietly(sourceConnection);
     }
-
     return this;
   }
 
@@ -129,20 +138,6 @@ public class DbTemplate implements ServerComponent {
     return columnTypes;
   }
 
-  @VisibleForTesting
-  static String selectQuery(String table, String... whereClauses) {
-    String selectQuery = "SELECT * FROM " + table;
-    if (whereClauses.length > 0) {
-      List<String> clauses = Lists.newArrayList();
-      for (String whereClause : whereClauses) {
-        clauses.add('(' + whereClause + ')');
-      }
-
-      selectQuery += " WHERE " + Joiner.on(" AND ").join(clauses);
-    }
-    return selectQuery;
-  }
-
   public int getRowCount(DataSource dataSource, String table) {
     Connection connection = null;
     Statement statement = null;
index e752e618fc80db9b7bfa25b90c6e9a43b96d5b51..0125142ce2b3ad03df56fc842f075768262e3077 100644 (file)
@@ -30,6 +30,7 @@ import org.sonar.api.utils.SonarException;
 
 import javax.annotation.Nullable;
 import javax.sql.DataSource;
+
 import java.io.File;
 import java.io.IOException;
 import java.sql.SQLException;
@@ -95,14 +96,19 @@ public class DryRunDatabaseFactory implements ServerComponent {
       .copyTable(source, dest, "rules_profiles")
       .copyTable(source, dest, "alerts");
     if (projectId != null) {
-      String snapshotCondition = "islast=" + database.getDialect().getTrueSqlValue() + " and (project_id=" + projectId + " or root_project_id=" + projectId + ")";
-      template.copyTable(source, dest, "projects",
-        "id in (select project_id from snapshots where " + snapshotCondition + ") or (id=" + projectId + " or root_id=" + projectId + ")");
-      template.copyTable(source, dest, "snapshots", snapshotCondition);
+      StringBuilder projectQuery = new StringBuilder();
+      projectQuery.append("SELECT p.* FROM projects p INNER JOIN snapshots s ON p.id = s.project_id");
+      projectQuery.append(" WHERE s.islast=").append(database.getDialect().getTrueSqlValue());
+      projectQuery.append(" AND (");
+      projectQuery.append("   s.root_project_id=").append(projectId);
+      projectQuery.append("   OR p.id=").append(projectId);
+      projectQuery.append("   OR p.root_id=").append(projectId);
+      projectQuery.append(" )");
+      template.copyTable(source, dest, "projects", projectQuery.toString());
 
-      String forRootModule = "(root_component_id in (select id from projects where id=" + projectId + " and qualifier='TRK'))";
-      String forSubModule = "(component_id in (select id from projects where id=" + projectId + " or root_id=" + projectId + "))";
-      template.copyTable(source, dest, "issues", "(" + forRootModule + ") or( " + forSubModule + ")", "status<>'" + Issue.STATUS_CLOSED + "'");
+      String forRootModule = "root_component_id in (select id from projects where id=" + projectId + " and qualifier='TRK')";
+      String forSubModule = "component_id in (select id from projects where id=" + projectId + " or root_id=" + projectId + ")";
+      template.copyTable(source, dest, "issues", "SELECT * FROM issues WHERE ((" + forRootModule + ") OR ( " + forSubModule + ")) AND status <> '" + Issue.STATUS_CLOSED + "'");
     }
   }
 
diff --git a/sonar-core/src/test/java/org/sonar/core/persistence/DbTemplateTest.java b/sonar-core/src/test/java/org/sonar/core/persistence/DbTemplateTest.java
deleted file mode 100644 (file)
index 0287d0c..0000000
+++ /dev/null
@@ -1,32 +0,0 @@
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2013 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.
- */
-package org.sonar.core.persistence;
-
-import org.junit.Test;
-
-import static org.fest.assertions.Assertions.assertThat;
-
-public class DbTemplateTest {
-  @Test
-  public void selectQuery() {
-    String select = DbTemplate.selectQuery("measures", "metric_id=2", "enabled=true");
-    assertThat(select).isEqualTo("SELECT * FROM measures WHERE (metric_id=2) AND (enabled=true)");
-  }
-}
index 46be722f497946cd82545bb8209463e7d2b07a49..354ab2d0cf9b67885cd95c4142ede9793e5f7462 100644 (file)
@@ -105,6 +105,7 @@ public class DryRunDatabaseFactoryTest extends AbstractDaoTestCase {
     byte[] database = localDatabaseFactory.createDatabaseForDryRun(300L);
     dataSource = createDatabase(database);
     assertThat(rowCount("issues")).isEqualTo(1);
+    assertThat(rowCount("projects")).isEqualTo(4);
   }
 
   @Test
@@ -117,6 +118,7 @@ public class DryRunDatabaseFactoryTest extends AbstractDaoTestCase {
     byte[] database = localDatabaseFactory.createDatabaseForDryRun(301L);
     dataSource = createDatabase(database);
     assertThat(rowCount("issues")).isEqualTo(1);
+    assertThat(rowCount("projects")).isEqualTo(2);
   }
 
   @Test
index 4fbc3f6c72021ad88889b794e75cac7347f7ce34..a9cdf217f0cb19d81f58d15f12ce95ad5b5a3dfe 100644 (file)
   <projects id="302" kee="struts-el" root_id="300" qualifier="BRC" scope="PRJ" />
   <projects id="303" kee="Action.java" root_id="301" qualifier="CLA" scope="FIL" />
 
-  <snapshots id="3000" project_id="300" root_snapshot_id="[null]" path="" islast="[true]"/>
-  <snapshots id="3001" project_id="301" root_snapshot_id="3000" path="3000." islast="[true]"/>
-  <snapshots id="3002" project_id="302" root_snapshot_id="3000" path="3000." islast="[true]"/>
-  <snapshots id="3003" project_id="303" root_snapshot_id="3000" path="3000.3001." islast="[true]"/>
+  <snapshots id="3000" project_id="300" root_project_id="300" root_snapshot_id="[null]" path="" islast="[true]"/>
+  <snapshots id="3001" project_id="301" root_project_id="300" root_snapshot_id="3000" path="3000." islast="[true]"/>
+  <snapshots id="3002" project_id="302" root_project_id="300" root_snapshot_id="3000" path="3000." islast="[true]"/>
+  <snapshots id="3003" project_id="303" root_project_id="300" root_snapshot_id="3000" path="3000.3001." islast="[true]"/>
 
   <rules id="500" plugin_rule_key="AvoidCycle" plugin_name="squid"/>
   <rules id="501" plugin_rule_key="NullRef" plugin_name="squid"/>
@@ -92,4 +92,4 @@
       updated_at="[null]"
       />
 
-</dataset>
\ No newline at end of file
+</dataset>