Search in sources :

Example 6 with JdbcSqlTransaction

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

the class SqliteJdbcSymmetricDialect method setSqliteFunctionResult.

@Override
protected void setSqliteFunctionResult(ISqlTransaction transaction, final String name, final String result) {
    JdbcSqlTransaction trans = (JdbcSqlTransaction) transaction;
    trans.executeCallback(new IConnectionCallback<Object>() {

        @Override
        public Object execute(Connection con) throws SQLException {
            org.sqlite.SQLiteConnection unwrapped = ((org.sqlite.SQLiteConnection) ((org.apache.commons.dbcp.DelegatingConnection) con).getInnermostDelegate());
            org.sqlite.Function.create(unwrapped, name, new org.sqlite.Function() {

                @Override
                protected void xFunc() throws SQLException {
                    this.result(result);
                }
            });
            return null;
        }
    });
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) JdbcSqlTransaction(org.jumpmind.db.sql.JdbcSqlTransaction)

Example 7 with JdbcSqlTransaction

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

the class OracleBulkDatabaseWriter method parseTimestampTZ.

protected Datum parseTimestampTZ(int type, String value) {
    if (value == null || StringUtils.isEmpty(value.trim())) {
        return null;
    }
    try {
        Timestamp timestamp = null;
        TimeZone timezone = null;
        try {
            // Try something like: 2015-11-20 13:37:44.000000000
            timestamp = Timestamp.valueOf(value.trim());
            timezone = TimeZone.getDefault();
        } catch (Exception ex) {
            log.debug("Failed to convert value to timestamp.", ex);
            // Now expecting something like: 2015-11-20 13:37:44.000000000 +08:00
            int split = value.lastIndexOf(" ");
            String timestampString = value.substring(0, split).trim();
            if (timestampString.endsWith(".")) {
                // Cover case where triggers would export format like "2007-01-02 03:20:10."
                timestampString = timestampString.substring(0, timestampString.length() - 1);
            }
            String timezoneString = value.substring(split).trim();
            timestamp = Timestamp.valueOf(timestampString);
            timezone = ((AbstractDatabasePlatform) platform).getTimeZone(timezoneString);
            // the timestamp component needs to actually be in UTC.
            if (type == OracleTypes.TIMESTAMPTZ) {
                timestamp = toUTC(timestamp, timezone);
            }
        }
        Calendar timezoneCalender = Calendar.getInstance();
        timezoneCalender.setTimeZone(timezone);
        timezoneCalender.setTime(timestamp);
        JdbcSqlTransaction jdbcTransaction = (JdbcSqlTransaction) transaction;
        Connection c = jdbcTransaction.getConnection();
        Connection oracleConnection = jdbcExtractor.getNativeConnection(c);
        Datum ts = null;
        if (type == OracleTypes.TIMESTAMPTZ) {
            ts = new TIMESTAMPTZ(oracleConnection, timestamp, timezoneCalender);
        } else {
            ts = new TIMESTAMPLTZ(oracleConnection, timestamp);
        }
        return ts;
    } catch (Exception ex) {
        log.info("Failed to convert '" + value + "' to TIMESTAMPTZ.");
        throw platform.getSqlTemplate().translate(ex);
    }
}
Also used : TimeZone(java.util.TimeZone) Datum(oracle.sql.Datum) Calendar(java.util.Calendar) Connection(java.sql.Connection) TIMESTAMPLTZ(oracle.sql.TIMESTAMPLTZ) JdbcSqlTransaction(org.jumpmind.db.sql.JdbcSqlTransaction) TIMESTAMPTZ(oracle.sql.TIMESTAMPTZ) Timestamp(java.sql.Timestamp) AbstractDatabasePlatform(org.jumpmind.db.platform.AbstractDatabasePlatform) SQLException(java.sql.SQLException) ParseException(java.text.ParseException) BulkSqlException(org.jumpmind.db.sql.BulkSqlException)

Example 8 with JdbcSqlTransaction

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

the class RedshiftBulkDatabaseWriter method flush.

protected void flush() {
    if (loadedRows > 0) {
        stagedInputFile.close();
        statistics.get(batch).startTimer(DataWriterStatisticConstants.DATABASEMILLIS);
        AmazonS3 s3client = new AmazonS3Client(new BasicAWSCredentials(accessKey, secretKey));
        if (isNotBlank(s3Endpoint)) {
            s3client.setEndpoint(s3Endpoint);
        }
        String objectKey = stagedInputFile.getFile().getName();
        try {
            s3client.putObject(bucket, objectKey, stagedInputFile.getFile());
        } catch (AmazonServiceException ase) {
            log.error("Exception from AWS service: " + ase.getMessage());
        } catch (AmazonClientException ace) {
            log.error("Exception from AWS client: " + ace.getMessage());
        }
        try {
            JdbcSqlTransaction jdbcTransaction = (JdbcSqlTransaction) transaction;
            Connection c = jdbcTransaction.getConnection();
            String sql = "COPY " + getTargetTable().getFullyQualifiedTableName() + " (" + Table.getCommaDeliminatedColumns(table.getColumns()) + ") FROM 's3://" + bucket + "/" + objectKey + "' CREDENTIALS 'aws_access_key_id=" + accessKey + ";aws_secret_access_key=" + secretKey + "' CSV DATEFORMAT 'YYYY-MM-DD HH:MI:SS' " + (needsExplicitIds ? "EXPLICIT_IDS" : "") + (isNotBlank(appendToCopyCommand) ? (" " + appendToCopyCommand) : "");
            Statement stmt = c.createStatement();
            log.debug(sql);
            stmt.execute(sql);
            stmt.close();
            transaction.commit();
        } catch (SQLException ex) {
            throw platform.getSqlTemplate().translate(ex);
        } finally {
            statistics.get(batch).stopTimer(DataWriterStatisticConstants.DATABASEMILLIS);
        }
        stagedInputFile.delete();
        try {
            s3client.deleteObject(bucket, objectKey);
        } catch (AmazonServiceException ase) {
            log.error("Exception from AWS service: " + ase.getMessage());
        } catch (AmazonClientException ace) {
            log.error("Exception from AWS client: " + ace.getMessage());
        }
        createStagingFile();
        loadedRows = 0;
        loadedBytes = 0;
    }
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) AmazonS3Client(com.amazonaws.services.s3.AmazonS3Client) SQLException(java.sql.SQLException) Statement(java.sql.Statement) AmazonClientException(com.amazonaws.AmazonClientException) AmazonServiceException(com.amazonaws.AmazonServiceException) Connection(java.sql.Connection) JdbcSqlTransaction(org.jumpmind.db.sql.JdbcSqlTransaction) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials)

Example 9 with JdbcSqlTransaction

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

the class SqlAnywhereSymmetricDialect 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 10 with JdbcSqlTransaction

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

the class MySqlBulkDatabaseWriter method flush.

protected void flush() {
    if (loadedRows > 0) {
        this.stagedInputFile.close();
        statistics.get(batch).startTimer(DataWriterStatisticConstants.DATABASEMILLIS);
        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 sql = String.format("LOAD DATA " + (isLocal ? "LOCAL " : "") + "INFILE '" + stagedInputFile.getFile().getAbsolutePath()).replace('\\', '/') + "' " + (isReplace ? "REPLACE " : "IGNORE ") + "INTO TABLE " + this.getTargetTable().getQualifiedTableName(quote, catalogSeparator, schemaSeparator) + " FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' ESCAPED BY '\\\\' LINES TERMINATED BY '\\n' STARTING BY ''" + " (" + Table.getCommaDeliminatedColumns(table.getColumns()) + ")";
            Statement stmt = c.createStatement();
            // TODO:  clean this up, deal with errors, etc.?
            log.debug(sql);
            stmt.execute(sql);
            stmt.close();
            transaction.commit();
        } catch (SQLException ex) {
            throw platform.getSqlTemplate().translate(ex);
        } finally {
            statistics.get(batch).stopTimer(DataWriterStatisticConstants.DATABASEMILLIS);
        }
        this.stagedInputFile.delete();
        createStagingFile();
        loadedRows = 0;
        loadedBytes = 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)

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