From: Julien HENRY Date: Tue, 27 Aug 2013 14:09:49 +0000 (+0200) Subject: SONAR-4589 Use optimized query to copy projects table for dryRun X-Git-Tag: 3.7.1-RC1-~44 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=2ede1b3a0a0f69a3475304b85703eb70e1cef1f1;p=sonarqube.git SONAR-4589 Use optimized query to copy projects table for dryRun --- diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/DbTemplate.java b/sonar-core/src/main/java/org/sonar/core/persistence/DbTemplate.java index 1ed424396b0..52d7015e3fb 100644 --- a/sonar-core/src/main/java/org/sonar/core/persistence/DbTemplate.java +++ b/sonar-core/src/main/java/org/sonar/core/persistence/DbTemplate.java @@ -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 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; diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/DryRunDatabaseFactory.java b/sonar-core/src/main/java/org/sonar/core/persistence/DryRunDatabaseFactory.java index e752e618fc8..0125142ce2b 100644 --- a/sonar-core/src/main/java/org/sonar/core/persistence/DryRunDatabaseFactory.java +++ b/sonar-core/src/main/java/org/sonar/core/persistence/DryRunDatabaseFactory.java @@ -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 index 0287d0c6cec..00000000000 --- a/sonar-core/src/test/java/org/sonar/core/persistence/DbTemplateTest.java +++ /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)"); - } -} diff --git a/sonar-core/src/test/java/org/sonar/core/persistence/DryRunDatabaseFactoryTest.java b/sonar-core/src/test/java/org/sonar/core/persistence/DryRunDatabaseFactoryTest.java index 46be722f497..354ab2d0cf9 100644 --- a/sonar-core/src/test/java/org/sonar/core/persistence/DryRunDatabaseFactoryTest.java +++ b/sonar-core/src/test/java/org/sonar/core/persistence/DryRunDatabaseFactoryTest.java @@ -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 diff --git a/sonar-core/src/test/resources/org/sonar/core/persistence/DryRunDatabaseFactoryTest/multi-modules-with-issues.xml b/sonar-core/src/test/resources/org/sonar/core/persistence/DryRunDatabaseFactoryTest/multi-modules-with-issues.xml index 4fbc3f6c720..a9cdf217f0c 100644 --- a/sonar-core/src/test/resources/org/sonar/core/persistence/DryRunDatabaseFactoryTest/multi-modules-with-issues.xml +++ b/sonar-core/src/test/resources/org/sonar/core/persistence/DryRunDatabaseFactoryTest/multi-modules-with-issues.xml @@ -31,10 +31,10 @@ - - - - + + + + @@ -92,4 +92,4 @@ updated_at="[null]" /> - \ No newline at end of file +