diff options
author | David Gageot <david@gageot.net> | 2012-10-26 16:36:13 +0200 |
---|---|---|
committer | David Gageot <david@gageot.net> | 2012-10-26 17:46:10 +0200 |
commit | 7342e90d136081c7b4a8780f0ef0fb4401301ac3 (patch) | |
tree | cff9d24c2f1f4b185bbc63cb4f7aec1aad1db285 /sonar-core/src | |
parent | 610539f58636a85d1576c15f71af0ac9e57fe0ee (diff) | |
download | sonarqube-7342e90d136081c7b4a8780f0ef0fb4401301ac3.tar.gz sonarqube-7342e90d136081c7b4a8780f0ef0fb4401301ac3.zip |
SONAR-3895 Remove Local Mode and fix Dry Run on postgresql
Diffstat (limited to 'sonar-core/src')
-rw-r--r-- | sonar-core/src/main/java/org/sonar/core/persistence/DbTemplate.java | 60 | ||||
-rw-r--r-- | sonar-core/src/main/java/org/sonar/core/persistence/DryRunDatabaseFactory.java (renamed from sonar-core/src/main/java/org/sonar/core/persistence/LocalDatabaseFactory.java) | 11 | ||||
-rw-r--r-- | sonar-core/src/test/java/org/sonar/core/persistence/DryRunDatabaseFactoryTest.java (renamed from sonar-core/src/test/java/org/sonar/core/persistence/LocalDatabaseFactoryTest.java) | 10 | ||||
-rw-r--r-- | sonar-core/src/test/resources/org/sonar/core/persistence/DryRunDatabaseFactoryTest/should_create_database.xml (renamed from sonar-core/src/test/resources/org/sonar/core/persistence/LocalDatabaseFactoryTest/should_create_database.xml) | 4 |
4 files changed, 38 insertions, 47 deletions
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 9dec7ff067f..bca98d80f8e 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 @@ -40,8 +40,6 @@ public class DbTemplate implements ServerComponent { public DbTemplate copyTable(DataSource source, DataSource dest, String table, String query) { LOG.info("Copy table " + table); - int colCount = getColumnCount(source, table); - truncate(dest, table); Connection sourceConnection = null; @@ -49,29 +47,35 @@ public class DbTemplate implements ServerComponent { ResultSet sourceResultSet = null; Connection destConnection = null; ResultSet destResultSet = null; + PreparedStatement destStatement = null; try { sourceConnection = source.getConnection(); sourceStatement = sourceConnection.createStatement(); sourceResultSet = sourceStatement.executeQuery(query); - destConnection = dest.getConnection(); - destConnection.setAutoCommit(false); + if (sourceResultSet.next()) { + int colCount = sourceResultSet.getMetaData().getColumnCount(); - PreparedStatement destStatement = destConnection.prepareStatement("INSERT INTO " + table + " VALUES(" + StringUtils.repeat("?", ",", colCount) + ")"); - while (sourceResultSet.next()) { - for (int col = 1; col <= colCount; col++) { - Object value = sourceResultSet.getObject(col); - destStatement.setObject(col, value); - } - destStatement.addBatch(); - } + destConnection = dest.getConnection(); + destConnection.setAutoCommit(false); - destStatement.executeBatch(); - destConnection.commit(); - destStatement.close(); + destStatement = destConnection.prepareStatement("INSERT INTO " + table + " VALUES(" + StringUtils.repeat("?", ",", colCount) + ")"); + do { + for (int col = 1; col <= colCount; col++) { + Object value = sourceResultSet.getObject(col); + destStatement.setObject(col, value); + } + destStatement.addBatch(); + } while (sourceResultSet.next()); + + destStatement.executeBatch(); + destConnection.commit(); + } } catch (SQLException e) { + LOG.error("Fail to copy table " + table, e); throw new SonarException("Fail to copy table " + table, e); } finally { + DatabaseUtils.closeQuietly(destStatement); DatabaseUtils.closeQuietly(destResultSet); DatabaseUtils.closeQuietly(destConnection); DatabaseUtils.closeQuietly(sourceResultSet); @@ -82,28 +86,7 @@ public class DbTemplate implements ServerComponent { return this; } - public int getColumnCount(DataSource dataSource, String table) { - Connection connection = null; - ResultSet metaData = null; - try { - connection = dataSource.getConnection(); - metaData = connection.getMetaData().getColumns(null, null, table, null); - - int nbColumns = 0; - while (metaData.next()) { - nbColumns++; - } - - return nbColumns; - } catch (SQLException e) { - throw new SonarException("Fail to get column count for table " + table, e); - } finally { - DatabaseUtils.closeQuietly(metaData); - DatabaseUtils.closeQuietly(connection); - } - } - - public int getRowCount(BasicDataSource dataSource, String table) { + public int getRowCount(DataSource dataSource, String table) { Connection connection = null; Statement statement = null; ResultSet resultSet = null; @@ -114,6 +97,7 @@ public class DbTemplate implements ServerComponent { return resultSet.next() ? resultSet.getInt(1) : 0; } catch (SQLException e) { + LOG.error("Fail to get row count for table " + table, e); throw new SonarException("Fail to get row count for table " + table, e); } finally { DatabaseUtils.closeQuietly(resultSet); @@ -130,6 +114,7 @@ public class DbTemplate implements ServerComponent { statement = connection.createStatement(); statement.executeUpdate("TRUNCATE TABLE " + table); } catch (SQLException e) { + LOG.error("Fail to truncate table " + table, e); throw new SonarException("Fail to truncate table " + table, e); } finally { DatabaseUtils.closeQuietly(statement); @@ -154,6 +139,7 @@ public class DbTemplate implements ServerComponent { connection = dataSource.getConnection(); DdlUtils.createSchema(connection, dialect); } catch (SQLException e) { + LOG.error("Fail to createSchema local database schema", e); throw new SonarException("Fail to createSchema local database schema", e); } finally { DatabaseUtils.closeQuietly(connection); diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/LocalDatabaseFactory.java b/sonar-core/src/main/java/org/sonar/core/persistence/DryRunDatabaseFactory.java index edebb896eef..156db8ab830 100644 --- a/sonar-core/src/main/java/org/sonar/core/persistence/LocalDatabaseFactory.java +++ b/sonar-core/src/main/java/org/sonar/core/persistence/DryRunDatabaseFactory.java @@ -31,7 +31,7 @@ import java.io.File; import java.io.IOException; import java.sql.SQLException; -public class LocalDatabaseFactory implements ServerComponent { +public class DryRunDatabaseFactory implements ServerComponent { private static final String DIALECT = "h2"; private static final String DRIVER = "org.h2.Driver"; private static final String URL = "jdbc:h2:"; @@ -41,12 +41,12 @@ public class LocalDatabaseFactory implements ServerComponent { private final Database database; private final ServerFileSystem serverFileSystem; - public LocalDatabaseFactory(Database database, ServerFileSystem serverFileSystem) { + public DryRunDatabaseFactory(Database database, ServerFileSystem serverFileSystem) { this.database = database; this.serverFileSystem = serverFileSystem; } - public byte[] createDatabaseForLocalMode(int resourceId) { + public byte[] createDatabaseForDryRun(int resourceId) { String name = serverFileSystem.getTempDir().getAbsolutePath() + "db-" + System.nanoTime(); try { @@ -56,14 +56,15 @@ public class LocalDatabaseFactory implements ServerComponent { return dbFileContent(name); } catch (SQLException e) { - throw new SonarException("Unable to create database for local mode", e); + throw new SonarException("Unable to create database for dry run", e); } } private void copy(DataSource source, DataSource dest, int resourceId) { new DbTemplate() .copyTable(source, dest, "PROPERTIES", - "SELECT * FROM PROPERTIES WHERE (((USER_ID IS NULL) AND (RESOURCE_ID IS NULL)) OR (RESOURCE_ID='" + resourceId + "')) AND NOT (PROP_KEY LIKE '%.secured')") + "SELECT * FROM PROPERTIES WHERE (((USER_ID IS NULL) AND (RESOURCE_ID IS NULL)) OR (RESOURCE_ID='" + resourceId + + "')) AND NOT (PROP_KEY LIKE '%.secured')") .copyTable(source, dest, "RULES_PROFILES", "SELECT * FROM RULES_PROFILES") .copyTable(source, dest, "RULES", "SELECT * FROM RULES") .copyTable(source, dest, "RULES_PARAMETERS", "SELECT * FROM RULES_PARAMETERS") diff --git a/sonar-core/src/test/java/org/sonar/core/persistence/LocalDatabaseFactoryTest.java b/sonar-core/src/test/java/org/sonar/core/persistence/DryRunDatabaseFactoryTest.java index 3eb0d75c300..b44340a1fc5 100644 --- a/sonar-core/src/test/java/org/sonar/core/persistence/LocalDatabaseFactoryTest.java +++ b/sonar-core/src/test/java/org/sonar/core/persistence/DryRunDatabaseFactoryTest.java @@ -36,8 +36,8 @@ import static org.fest.assertions.Assertions.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -public class LocalDatabaseFactoryTest extends AbstractDaoTestCase { - private LocalDatabaseFactory localDatabaseFactory; +public class DryRunDatabaseFactoryTest extends AbstractDaoTestCase { + private DryRunDatabaseFactory localDatabaseFactory; private ServerFileSystem serverFileSystem = mock(ServerFileSystem.class); private BasicDataSource dataSource; @@ -47,7 +47,7 @@ public class LocalDatabaseFactoryTest extends AbstractDaoTestCase { @Before public void setUp() { - localDatabaseFactory = new LocalDatabaseFactory(getDatabase(), serverFileSystem); + localDatabaseFactory = new DryRunDatabaseFactory(getDatabase(), serverFileSystem); } @After @@ -58,12 +58,12 @@ public class LocalDatabaseFactoryTest extends AbstractDaoTestCase { } @Test - public void should_create_database() throws IOException { + public void should_create_database() throws IOException, SQLException { setupData("should_create_database"); when(serverFileSystem.getTempDir()).thenReturn(temporaryFolder.getRoot()); - byte[] database = localDatabaseFactory.createDatabaseForLocalMode(1); + byte[] database = localDatabaseFactory.createDatabaseForDryRun(1); dataSource = createDatabase(database); assertThat(rowCount("PROPERTIES")).isEqualTo(2); diff --git a/sonar-core/src/test/resources/org/sonar/core/persistence/LocalDatabaseFactoryTest/should_create_database.xml b/sonar-core/src/test/resources/org/sonar/core/persistence/DryRunDatabaseFactoryTest/should_create_database.xml index 30343973116..0e266518906 100644 --- a/sonar-core/src/test/resources/org/sonar/core/persistence/LocalDatabaseFactoryTest/should_create_database.xml +++ b/sonar-core/src/test/resources/org/sonar/core/persistence/DryRunDatabaseFactoryTest/should_create_database.xml @@ -4,4 +4,8 @@ <properties id="3" prop_key="globalProperty" text_value="value3" resource_id="[null]" user_id="[null]"/> <properties id="4" prop_key="userProperty" text_value="value4" resource_id="[null]" user_id="1"/> <properties id="5" prop_key="property.secured" text_value="value5" resource_id="[null]" user_id="[null]"/> + + <rules_profiles id="1" name="Sonar way with Findbugs" language="java" parent_name="" version="1" + used_profile="false"/> + </dataset>
\ No newline at end of file |