use of org.postgresql.core.BaseConnection in project symmetric-ds by JumpMind.
the class PostgresBulkDatabaseWriter method open.
@Override
public void open(DataContext context) {
super.open(context);
try {
JdbcSqlTransaction jdbcTransaction = (JdbcSqlTransaction) transaction;
Connection conn = jdbcExtractor.getNativeConnection(jdbcTransaction.getConnection());
copyManager = new CopyManager((BaseConnection) conn);
} catch (Exception ex) {
throw getPlatform().getSqlTemplate().translate(ex);
}
}
use of org.postgresql.core.BaseConnection in project binnavi by google.
the class IntegrationTestSetup method createDatabase.
/**
* Uses the stored data in the test data directory to fill a database with information. The table
* structure is initialized first than the tables are filled. Finally the constraints for the
* database are initialized. This order is essential to avoid foreign key violations.
*
* @param databaseName The name of the database to create.
*/
private void createDatabase(final String databaseName) {
try {
final Connection connection = DriverManager.getConnection(url + databaseName, databaseProperties);
try {
NaviLogger.info("[i] Generating database tables for %s.", databaseName);
connection.prepareStatement(AbstractSQLProvider.parseResourceAsSQLFile(this.getClass().getResourceAsStream(TEST_DATA_DIRECTORY + "database_schema.sql"))).execute();
} catch (final IOException exception) {
CUtilityFunctions.logException(exception);
}
final File testDataDir = new File("./third_party/zynamics/javatests/com/google/security/zynamics/binnavi/testdata/" + databaseName + "/");
final CopyManager manager = new CopyManager((BaseConnection) connection);
for (final File currentFile : testDataDir.listFiles()) {
try (FileReader reader = new FileReader(currentFile)) {
final String tableName = currentFile.getName().split(".sql")[0];
NaviLogger.info("[i] Importing: %s.%s from %s", databaseName, tableName, currentFile.getAbsolutePath());
manager.copyIn("COPY " + tableName + " FROM STDIN", reader);
} catch (final IOException exception) {
CUtilityFunctions.logException(exception);
}
}
try {
NaviLogger.warning("[i] Generating constraints for %s.", databaseName);
connection.prepareStatement(AbstractSQLProvider.parseResourceAsSQLFile(this.getClass().getResourceAsStream(TEST_DATA_DIRECTORY + "database_constraints.sql"))).execute();
} catch (final IOException exception) {
CUtilityFunctions.logException(exception);
}
final String findSequencesQuery = "SELECT 'SELECT SETVAL(' ||quote_literal(S.relname)|| " + "', MAX(' ||quote_ident(C.attname)|| ') ) FROM ' ||quote_ident(T.relname)|| ';' " + " FROM pg_class AS S, pg_depend AS D, pg_class AS T, pg_attribute AS C " + " WHERE S.relkind = 'S' AND S.oid = D.objid AND D.refobjid = T.oid " + " AND D.refobjid = C.attrelid AND D.refobjsubid = C.attnum ORDER BY S.relname; ";
try (PreparedStatement statement = connection.prepareStatement(findSequencesQuery);
ResultSet resultSet = statement.executeQuery()) {
while (resultSet.next()) {
final PreparedStatement fixSequence = connection.prepareStatement(resultSet.getString(1));
fixSequence.execute();
}
} catch (final SQLException exception) {
CUtilityFunctions.logException(exception);
}
} catch (final SQLException exception) {
CUtilityFunctions.logException(exception);
}
}
Aggregations