Search in sources :

Example 1 with BaseConnection

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);
    }
}
Also used : Connection(java.sql.Connection) BaseConnection(org.postgresql.core.BaseConnection) JdbcSqlTransaction(org.jumpmind.db.sql.JdbcSqlTransaction) CopyManager(org.postgresql.copy.CopyManager) BaseConnection(org.postgresql.core.BaseConnection) SQLException(java.sql.SQLException)

Example 2 with BaseConnection

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);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) BaseConnection(org.postgresql.core.BaseConnection) ResultSet(java.sql.ResultSet) FileReader(java.io.FileReader) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException) CopyManager(org.postgresql.copy.CopyManager) File(java.io.File)

Aggregations

Connection (java.sql.Connection)2 SQLException (java.sql.SQLException)2 CopyManager (org.postgresql.copy.CopyManager)2 BaseConnection (org.postgresql.core.BaseConnection)2 File (java.io.File)1 FileReader (java.io.FileReader)1 IOException (java.io.IOException)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 JdbcSqlTransaction (org.jumpmind.db.sql.JdbcSqlTransaction)1