*/
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;
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;
DbUtils.closeQuietly(sourceStatement);
DbUtils.closeQuietly(sourceConnection);
}
-
return this;
}
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;
import javax.annotation.Nullable;
import javax.sql.DataSource;
+
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
.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 + "'");
}
}
+++ /dev/null
-/*
- * 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)");
- }
-}
byte[] database = localDatabaseFactory.createDatabaseForDryRun(300L);
dataSource = createDatabase(database);
assertThat(rowCount("issues")).isEqualTo(1);
+ assertThat(rowCount("projects")).isEqualTo(4);
}
@Test
byte[] database = localDatabaseFactory.createDatabaseForDryRun(301L);
dataSource = createDatabase(database);
assertThat(rowCount("issues")).isEqualTo(1);
+ assertThat(rowCount("projects")).isEqualTo(2);
}
@Test
<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"/>
updated_at="[null]"
/>
-</dataset>
\ No newline at end of file
+</dataset>