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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
}
Aggregations