import org.sonar.api.utils.SonarException;
import javax.sql.DataSource;
-
-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;
+import java.sql.*;
import java.util.List;
public class DbTemplate implements ServerComponent {
public DbTemplate copyTable(DataSource source, DataSource dest, String table, String... whereClauses) {
LOG.debug("Copy table {}", table);
+ long startup = System.currentTimeMillis();
String selectQuery = selectQuery(table, whereClauses);
truncate(dest, table);
Connection destConnection = null;
ResultSet destResultSet = null;
PreparedStatement destStatement = null;
+ int count = 0;
try {
sourceConnection = source.getConnection();
sourceStatement = sourceConnection.createStatement();
String insertSql = new StringBuilder().append("INSERT INTO ").append(table).append("(").append(Joiner.on(",").join(columnNames))
.append(") VALUES(").append(StringUtils.repeat("?", ",", columnNames.length)).append(")").toString();
destStatement = destConnection.prepareStatement(insertSql);
- int count = 0;
+
do {
copyColumns(sourceResultSet, destStatement, columnNames, columnTypes);
count++;
destStatement.executeBatch();
destConnection.commit();
}
+ if (LOG.isDebugEnabled()) {
+ LOG.debug(" " + count + " rows of " + table + " copied in " + (System.currentTimeMillis() - startup) + " ms");
+ }
} catch (SQLException e) {
LOG.error("Fail to copy table " + table, e);
throw new IllegalStateException("Fail to copy table " + table, e);
import com.google.common.io.Files;
import org.apache.commons.dbcp.BasicDataSource;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import org.sonar.api.ServerComponent;
import org.sonar.api.issue.Issue;
import org.sonar.api.platform.ServerFileSystem;
import java.sql.SQLException;
public class DryRunDatabaseFactory implements ServerComponent {
+ private static final Logger LOG = LoggerFactory.getLogger(DryRunDatabaseFactory.class);
private static final String DIALECT = "h2";
private static final String DRIVER = "org.h2.Driver";
private static final String URL = "jdbc:h2:";
}
public byte[] createDatabaseForDryRun(@Nullable Long projectId) {
+ long startup = System.currentTimeMillis();
String name = serverFileSystem.getTempDir().getAbsolutePath() + "db-" + System.nanoTime();
try {
copy(source, destination, projectId);
close(destination);
+ if (LOG.isDebugEnabled()) {
+ File dbFile = new File(name + ".h2.db");
+ long size = dbFile.length();
+ long duration = System.currentTimeMillis() - startup;
+ if (projectId == null) {
+ LOG.debug("Dry Run Database created in " + duration + " ms, size is " + size + " bytes");
+ } else {
+ LOG.debug("Dry Run Database for project " + projectId + " created in " + duration + " ms, size is " + size + " bytes");
+ }
+ }
+
return dbFileContent(name);
} catch (SQLException e) {
throw new SonarException("Unable to create database for DryRun", e);