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