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