use of org.jumpmind.db.platform.DatabaseInfo in project symmetric-ds by JumpMind.
the class TableExtractDataReaderSource method startNewCursor.
protected void startNewCursor() {
DatabaseInfo dbInfo = platform.getDatabaseInfo();
String sql = String.format("select * from %s %s", table.getQualifiedTableName(dbInfo.getDelimiterToken(), dbInfo.getCatalogSeparator(), dbInfo.getSchemaSeparator()), StringUtils.isNotBlank(whereClause) ? " where " + whereClause : "");
this.cursor = platform.getSqlTemplate().queryForCursor(sql, new ISqlRowMapper<CsvData>() {
public CsvData mapRow(Row row) {
return new CsvData(DataEventType.INSERT, toStringData(row, table.getPrimaryKeyColumns()), toStringData(row, table.getColumns()));
}
});
}
use of org.jumpmind.db.platform.DatabaseInfo in project symmetric-ds by JumpMind.
the class ExtractDataReader method buildSelect.
protected String buildSelect(Table table, Column lobColumn, Column[] pkColumns) {
StringBuilder sql = new StringBuilder("select ");
DatabaseInfo dbInfo = platform.getDatabaseInfo();
String quote = platform.getDdlBuilder().isDelimitedIdentifierModeOn() ? dbInfo.getDelimiterToken() : "";
sql.append(quote);
sql.append(lobColumn.getName());
sql.append(quote);
sql.append(",");
sql.delete(sql.length() - 1, sql.length());
sql.append(" from ");
sql.append(table.getQualifiedTableName(quote, dbInfo.getCatalogSeparator(), dbInfo.getSchemaSeparator()));
sql.append(" where ");
for (Column col : pkColumns) {
sql.append(quote);
sql.append(col.getName());
sql.append(quote);
sql.append("=? and ");
}
sql.delete(sql.length() - 5, sql.length());
return sql.toString();
}
use of org.jumpmind.db.platform.DatabaseInfo in project symmetric-ds by JumpMind.
the class RouterServiceTest method setup.
@Before
public void setup() {
ISymmetricEngine engine = mock(ISymmetricEngine.class);
IParameterService parameterService = mock(IParameterService.class);
ISymmetricDialect symmetricDialect = mock(ISymmetricDialect.class);
IDatabasePlatform databasePlatform = mock(IDatabasePlatform.class);
IExtensionService extensionService = mock(IExtensionService.class);
when(databasePlatform.getDatabaseInfo()).thenReturn(new DatabaseInfo());
when(symmetricDialect.getPlatform()).thenReturn(databasePlatform);
when(engine.getDatabasePlatform()).thenReturn(databasePlatform);
when(engine.getParameterService()).thenReturn(parameterService);
when(engine.getSymmetricDialect()).thenReturn(symmetricDialect);
when(engine.getExtensionService()).thenReturn(extensionService);
routerService = new RouterService(engine);
}
use of org.jumpmind.db.platform.DatabaseInfo in project symmetric-ds by JumpMind.
the class MySqlBulkDatabaseWriter method getCommaDeliminatedColumns.
protected String getCommaDeliminatedColumns(Column[] cols) {
DatabaseInfo dbInfo = platform.getDatabaseInfo();
String quote = dbInfo.getDelimiterToken();
StringBuilder columns = new StringBuilder();
columns.append("(");
// The target syntax is (where BLOB is involved):
// ('column1', 'column2', @hexColumn3) SET 'column3'=UNHEX(@hexColumn3);
Map<String, String> blobColumns = new LinkedHashMap<String, String>();
if (cols != null && cols.length > 0) {
for (Column column : cols) {
boolean blob = column.isOfBinaryType();
if (blob) {
String hexVariable = String.format("@%s_hex", column.getName());
blobColumns.put(column.getName(), hexVariable);
columns.append(hexVariable);
} else {
columns.append(quote);
columns.append(column.getName());
columns.append(quote);
}
columns.append(",");
}
columns.replace(columns.length() - 1, columns.length(), "");
columns.append(")");
// Build this (optional) clause:
// SET 'column3'=UNHEX(@hexColumn3);
StringBuilder setClause = new StringBuilder();
for (String columnName : blobColumns.keySet()) {
if (setClause.length() == 0) {
setClause.append(" SET ");
}
setClause.append(quote);
setClause.append(columnName);
setClause.append(quote);
setClause.append("=UNHEX(");
setClause.append(blobColumns.get(columnName));
setClause.append("),");
}
if (setClause.length() > 0) {
setClause.setLength(setClause.length() - 1);
columns.append(setClause);
}
return columns.toString();
} else {
return " ";
}
}
use of org.jumpmind.db.platform.DatabaseInfo 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 (DATAFILETYPE='widechar', 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;
}
}
Aggregations