From 831860a0885b5d93da6e4af02200910defe60404 Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Thu, 29 Aug 2013 15:14:26 +0200 Subject: [PATCH] SONAR-4603 Improve SQL query to copy issues in H2 DB in dryRun --- .../persistence/DryRunDatabaseFactory.java | 33 ++++++++++++------- 1 file changed, 21 insertions(+), 12 deletions(-) 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 baa3539bfe7..9d37ae9101c 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 @@ -96,25 +96,34 @@ public class DryRunDatabaseFactory implements ServerComponent { .copyTable(source, dest, "rules_profiles") .copyTable(source, dest, "alerts"); if (projectId != null) { - 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()); + template.copyTable(source, dest, "projects", projectQuery(projectId, false)); template.copyTable(source, dest, "snapshots", "SELECT * FROM snapshots WHERE project_id=" + projectId); template.copyTable(source, dest, "project_measures", "SELECT m.* FROM project_measures m INNER JOIN snapshots s on m.snapshot_id=s.id WHERE s.project_id=" + projectId); - 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 + "'"); + StringBuilder issueQuery = new StringBuilder() + .append("SELECT issues.* FROM issues") + .append(" INNER JOIN (") + .append(projectQuery(projectId, true)) + .append(") resources") + .append(" ON issues.component_id=resources.id") + .append(" AND status <> '").append(Issue.STATUS_CLOSED).append("'"); + template.copyTable(source, dest, "issues", issueQuery.toString()); } } + private String projectQuery(Long projectId, boolean returnOnlyIds) { + return new StringBuilder() + .append("SELECT p.").append(returnOnlyIds ? "id" : "*") + .append(" FROM projects p INNER JOIN snapshots s ON p.id = s.project_id") + .append(" WHERE s.islast=").append(database.getDialect().getTrueSqlValue()) + .append(" AND (") + .append(" s.root_project_id=").append(projectId) + .append(" OR p.id=").append(projectId) + .append(" OR p.root_id=").append(projectId) + .append(" )").toString(); + } + private BasicDataSource create(String dialect, String driver, String user, String password, String url) { BasicDataSource dataSource = new DbTemplate().dataSource(driver, user, password, url); new DbTemplate().createSchema(dataSource, dialect); -- 2.39.5