Search in sources :

Example 1 with CopyManager

use of org.postgresql.copy.CopyManager in project flyway by flyway.

the class EnterpriseDBDbSupport method executePgCopy.

@Override
public void executePgCopy(Connection connection, String sql) throws SQLException {
    int split = sql.indexOf(";");
    String statement = sql.substring(0, split);
    String data = sql.substring(split + 1).trim();
    CopyManager copyManager = new CopyManager(connection.unwrap(BaseConnection.class));
    try {
        copyManager.copyIn(statement, new StringReader(data));
    } catch (IOException e) {
        throw new SQLException("Unable to execute COPY operation", e);
    }
}
Also used : SQLException(java.sql.SQLException) StringReader(java.io.StringReader) CopyManager(org.postgresql.copy.CopyManager) IOException(java.io.IOException) BaseConnection(org.postgresql.core.BaseConnection)

Example 2 with CopyManager

use of org.postgresql.copy.CopyManager in project flyway by flyway.

the class PostgreSQLDbSupport method executePgCopy.

@Override
public void executePgCopy(Connection connection, String sql) throws SQLException {
    int split = sql.indexOf(";");
    String statement = sql.substring(0, split);
    String data = sql.substring(split + 1).trim();
    CopyManager copyManager = new CopyManager(connection.unwrap(BaseConnection.class));
    try {
        copyManager.copyIn(statement, new StringReader(data));
    } catch (IOException e) {
        throw new SQLException("Unable to execute COPY operation", e);
    }
}
Also used : SQLException(java.sql.SQLException) StringReader(java.io.StringReader) CopyManager(org.postgresql.copy.CopyManager) IOException(java.io.IOException) BaseConnection(org.postgresql.core.BaseConnection)

Example 3 with CopyManager

use of org.postgresql.copy.CopyManager 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 4 with CopyManager

use of org.postgresql.copy.CopyManager 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)

Example 5 with CopyManager

use of org.postgresql.copy.CopyManager in project torodb by torodb.

the class PostgreSqlWriteInterface method copyInsertDocPartData.

private void copyInsertDocPartData(PGConnection connection, String schemaName, DocPartData docPartData) throws SQLException, IOException {
    final int maxBatchSize = 1024;
    final CopyManager copyManager = connection.getCopyAPI();
    final MetaDocPart metaDocPart = docPartData.getMetaDocPart();
    Collection<InternalField<?>> internalFields = postgreSqlMetaDataReadInterface.getInternalFields(metaDocPart);
    final StringBuilder sb = new StringBuilder(65536);
    final String copyStatement = getCopyInsertDocPartDataStatement(schemaName, docPartData, metaDocPart, internalFields);
    Iterator<DocPartRow> docPartRowIterator = docPartData.iterator();
    int docCounter = 0;
    while (docPartRowIterator.hasNext()) {
        DocPartRow tableRow = docPartRowIterator.next();
        docCounter++;
        addValuesToCopy(sb, tableRow, internalFields);
        assert sb.length() != 0;
        if (docCounter % maxBatchSize == 0 || !docPartRowIterator.hasNext()) {
            executeCopy(copyManager, copyStatement, sb);
            sb.setLength(0);
        }
    }
}
Also used : MetaDocPart(com.torodb.core.transaction.metainf.MetaDocPart) InternalField(com.torodb.backend.InternalField) DocPartRow(com.torodb.core.d2r.DocPartRow) CopyManager(org.postgresql.copy.CopyManager)

Aggregations

CopyManager (org.postgresql.copy.CopyManager)5 SQLException (java.sql.SQLException)4 BaseConnection (org.postgresql.core.BaseConnection)4 IOException (java.io.IOException)3 StringReader (java.io.StringReader)2 Connection (java.sql.Connection)2 InternalField (com.torodb.backend.InternalField)1 DocPartRow (com.torodb.core.d2r.DocPartRow)1 MetaDocPart (com.torodb.core.transaction.metainf.MetaDocPart)1 File (java.io.File)1 FileReader (java.io.FileReader)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 JdbcSqlTransaction (org.jumpmind.db.sql.JdbcSqlTransaction)1