Search in sources :

Example 1 with JdbcSqlTransaction

use of org.jumpmind.db.sql.JdbcSqlTransaction in project symmetric-ds by JumpMind.

the class MsSqlBulkDatabaseWriter method flush.

protected void flush() {
    if (loadedRows > 0) {
        this.stagedInputFile.close();
        statistics.get(batch).startTimer(DataWriterStatisticConstants.DATABASEMILLIS);
        String filename;
        if (StringUtils.isEmpty(uncPath)) {
            filename = stagedInputFile.getFile().getAbsolutePath();
        } else {
            filename = uncPath + "\\" + stagedInputFile.getFile().getName();
        }
        try {
            DatabaseInfo dbInfo = platform.getDatabaseInfo();
            String quote = dbInfo.getDelimiterToken();
            String catalogSeparator = dbInfo.getCatalogSeparator();
            String schemaSeparator = dbInfo.getSchemaSeparator();
            JdbcSqlTransaction jdbcTransaction = (JdbcSqlTransaction) transaction;
            Connection c = jdbcTransaction.getConnection();
            String rowTerminatorString = "";
            /*
                 * There seems to be a bug with the SQL server bulk insert when
                 * you have one row with binary data at the end using \n as the
                 * row terminator. It works when you leave the row terminator
                 * out of the bulk insert statement.
                 */
            if (!(rowTerminator.equals("\n") || rowTerminator.equals("\r\n"))) {
                rowTerminatorString = ", ROWTERMINATOR='" + StringEscapeUtils.escapeJava(rowTerminator) + "'";
            }
            String sql = String.format("BULK INSERT " + this.getTargetTable().getQualifiedTableName(quote, catalogSeparator, schemaSeparator) + " FROM '" + filename) + "'" + " WITH ( FIELDTERMINATOR='" + StringEscapeUtils.escapeJava(fieldTerminator) + "', KEEPIDENTITY" + (fireTriggers ? ", FIRE_TRIGGERS" : "") + rowTerminatorString + ");";
            Statement stmt = c.createStatement();
            // TODO:  clean this up, deal with errors, etc.?
            stmt.execute(sql);
            stmt.close();
        } catch (SQLException ex) {
            throw platform.getSqlTemplate().translate(ex);
        } finally {
            statistics.get(batch).stopTimer(DataWriterStatisticConstants.DATABASEMILLIS);
        }
        this.stagedInputFile.delete();
        createStagingFile();
        loadedRows = 0;
    }
}
Also used : DatabaseInfo(org.jumpmind.db.platform.DatabaseInfo) SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) JdbcSqlTransaction(org.jumpmind.db.sql.JdbcSqlTransaction)

Example 2 with JdbcSqlTransaction

use of org.jumpmind.db.sql.JdbcSqlTransaction in project symmetric-ds by JumpMind.

the class OracleBulkDatabaseWriter method flush.

protected void flush() {
    statistics.get(batch).startTimer(DataWriterStatisticConstants.DATABASEMILLIS);
    try {
        if (rowArrays.size() > 0) {
            JdbcSqlTransaction jdbcTransaction = (JdbcSqlTransaction) transaction;
            Connection c = jdbcTransaction.getConnection();
            Connection oracleConnection = jdbcExtractor.getNativeConnection(c);
            Column[] columns = targetTable.getColumns();
            StringBuilder questions = new StringBuilder();
            for (int i = 0; i <= columns.length; i++) {
                questions.append("?, ");
            }
            questions.replace(questions.length() - 2, questions.length(), "");
            String sql = String.format("{ call %s(%s) }", buildProcedureName("i", targetTable), questions);
            OracleCallableStatement stmt = (OracleCallableStatement) oracleConnection.prepareCall(sql);
            for (int i = 0; i < columns.length; i++) {
                Column column = columns[i];
                ArrayDescriptor type = ArrayDescriptor.createDescriptor(getTypeName(column.getMappedTypeCode()), oracleConnection);
                List<Object> columnData = rowArrays.get(i);
                ARRAY array = new ARRAY(type, oracleConnection, columnData.toArray(new Object[columnData.size()]));
                stmt.setObject(i + 1, array);
            }
            int errorIndex = columns.length + 1;
            stmt.registerOutParameter(errorIndex, OracleTypes.ARRAY, getTypeName(Types.INTEGER));
            stmt.execute();
            ARRAY errorsArray = stmt.getARRAY(errorIndex);
            int[] errors;
            if (errorsArray != null) {
                errors = errorsArray.getIntArray();
            } else {
                errors = new int[0];
            }
            if (errors.length > 0) {
                // set the statement count so the failed row number get reported correctly
                statistics.get(batch).set(DataWriterStatisticConstants.STATEMENTCOUNT, errors[0]);
                throw new BulkSqlException(errors, lastEventType.toString(), sql);
            }
        }
    } catch (SQLException ex) {
        throw platform.getSqlTemplate().translate(ex);
    } finally {
        lastEventType = null;
        rowArrays.clear();
        statistics.get(batch).stopTimer(DataWriterStatisticConstants.DATABASEMILLIS);
    }
}
Also used : ARRAY(oracle.sql.ARRAY) SQLException(java.sql.SQLException) Connection(java.sql.Connection) OracleCallableStatement(oracle.jdbc.internal.OracleCallableStatement) BulkSqlException(org.jumpmind.db.sql.BulkSqlException) Column(org.jumpmind.db.model.Column) ArrayDescriptor(oracle.sql.ArrayDescriptor) JdbcSqlTransaction(org.jumpmind.db.sql.JdbcSqlTransaction)

Example 3 with JdbcSqlTransaction

use of org.jumpmind.db.sql.JdbcSqlTransaction 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 JdbcSqlTransaction

use of org.jumpmind.db.sql.JdbcSqlTransaction in project symmetric-ds by JumpMind.

the class MySqlSymmetricDialect method switchCatalogForTriggerInstall.

@Override
protected String switchCatalogForTriggerInstall(String catalog, ISqlTransaction transaction) {
    if (catalog != null) {
        Connection c = ((JdbcSqlTransaction) transaction).getConnection();
        String previousCatalog;
        try {
            previousCatalog = c.getCatalog();
            c.setCatalog(catalog);
            return previousCatalog;
        } catch (SQLException e) {
            throw new SqlException(e);
        }
    } else {
        return null;
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) SqlException(org.jumpmind.db.sql.SqlException) JdbcSqlTransaction(org.jumpmind.db.sql.JdbcSqlTransaction)

Example 5 with JdbcSqlTransaction

use of org.jumpmind.db.sql.JdbcSqlTransaction in project symmetric-ds by JumpMind.

the class MsSqlSymmetricDialect method switchCatalogForTriggerInstall.

@Override
protected String switchCatalogForTriggerInstall(String catalog, ISqlTransaction transaction) {
    if (catalog != null) {
        Connection c = ((JdbcSqlTransaction) transaction).getConnection();
        String previousCatalog = null;
        try {
            previousCatalog = c.getCatalog();
            c.setCatalog(catalog);
            return previousCatalog;
        } catch (SQLException e) {
            if (catalog != null) {
                try {
                    c.setCatalog(previousCatalog);
                } catch (SQLException ex) {
                }
            }
            throw new SqlException(e);
        }
    } else {
        return null;
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) SqlException(org.jumpmind.db.sql.SqlException) JdbcSqlTransaction(org.jumpmind.db.sql.JdbcSqlTransaction)

Aggregations

Connection (java.sql.Connection)11 SQLException (java.sql.SQLException)11 JdbcSqlTransaction (org.jumpmind.db.sql.JdbcSqlTransaction)11 SqlException (org.jumpmind.db.sql.SqlException)4 Statement (java.sql.Statement)3 DatabaseInfo (org.jumpmind.db.platform.DatabaseInfo)2 BulkSqlException (org.jumpmind.db.sql.BulkSqlException)2 AmazonClientException (com.amazonaws.AmazonClientException)1 AmazonServiceException (com.amazonaws.AmazonServiceException)1 BasicAWSCredentials (com.amazonaws.auth.BasicAWSCredentials)1 AmazonS3 (com.amazonaws.services.s3.AmazonS3)1 AmazonS3Client (com.amazonaws.services.s3.AmazonS3Client)1 Timestamp (java.sql.Timestamp)1 ParseException (java.text.ParseException)1 Calendar (java.util.Calendar)1 TimeZone (java.util.TimeZone)1 OracleCallableStatement (oracle.jdbc.internal.OracleCallableStatement)1 ARRAY (oracle.sql.ARRAY)1 ArrayDescriptor (oracle.sql.ArrayDescriptor)1 Datum (oracle.sql.Datum)1