use of org.jumpmind.db.platform.IDatabasePlatform 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.IDatabasePlatform in project symmetric-ds by JumpMind.
the class AbstractSymmetricEngine method buildTablesFromDdlUtilXmlIfProvided.
protected boolean buildTablesFromDdlUtilXmlIfProvided() {
boolean loaded = false;
String xml = parameterService.getString(ParameterConstants.AUTO_CONFIGURE_REG_SVR_DDLUTIL_XML);
if (!StringUtils.isBlank(xml)) {
File file = new File(xml);
URL fileUrl = null;
if (file.isFile()) {
try {
fileUrl = file.toURI().toURL();
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
} else {
fileUrl = getClass().getResource(xml);
}
if (fileUrl != null) {
try {
log.info("Building database schema from: {}", xml);
Database database = DatabaseXmlUtil.read(new InputStreamReader(fileUrl.openStream()));
IDatabasePlatform platform = symmetricDialect.getPlatform();
platform.createDatabase(database, true, true);
loaded = true;
} catch (Exception e) {
log.error("", e);
}
}
}
return loaded;
}
use of org.jumpmind.db.platform.IDatabasePlatform in project symmetric-ds by JumpMind.
the class DefaultDatabaseWriterConflictResolver method isTimestampNewer.
protected boolean isTimestampNewer(Conflict conflict, AbstractDatabaseWriter writer, CsvData data) {
DefaultDatabaseWriter databaseWriter = (DefaultDatabaseWriter) writer;
IDatabasePlatform platform = databaseWriter.getPlatform();
String columnName = conflict.getDetectExpression();
Table targetTable = writer.getTargetTable();
Table sourceTable = writer.getSourceTable();
String[] pkData = data.getPkData(targetTable);
Object[] objectValues = databaseWriter.getPlatform().getObjectValues(writer.getBatch().getBinaryEncoding(), pkData, targetTable.getPrimaryKeyColumns());
DmlStatement stmt = databaseWriter.getPlatform().createDmlStatement(DmlType.FROM, targetTable, writer.getWriterSettings().getTextColumnExpression());
Column column = targetTable.getColumnWithName(columnName);
if (column == null) {
throw new RuntimeException(String.format("Could not find a timestamp column with a name of %s on the table %s. " + "Please check your conflict resolution configuration", columnName, targetTable.getQualifiedTableName()));
}
String sql = stmt.getColumnsSql(new Column[] { column });
Map<String, String> newData = data.toColumnNameValuePairs(sourceTable.getColumnNames(), CsvData.ROW_DATA);
String loadingStr = newData.get(columnName);
Date loadingTs = null;
Date existingTs = null;
if (column.isTimestampWithTimezone()) {
// Get the existingTs with timezone
String existingStr = databaseWriter.getTransaction().queryForObject(sql, String.class, objectValues);
// because the row doesn't exist, then existing simply needs to be null
if (existingStr != null) {
int split = existingStr.lastIndexOf(" ");
existingTs = FormatUtils.parseDate(existingStr.substring(0, split).trim(), FormatUtils.TIMESTAMP_PATTERNS, TimeZone.getTimeZone(existingStr.substring(split).trim()));
}
// Get the loadingTs with timezone
int split = loadingStr.lastIndexOf(" ");
loadingTs = FormatUtils.parseDate(loadingStr.substring(0, split).trim(), FormatUtils.TIMESTAMP_PATTERNS, TimeZone.getTimeZone(loadingStr.substring(split).trim()));
} else {
// Get the existingTs
existingTs = databaseWriter.getTransaction().queryForObject(sql, Timestamp.class, objectValues);
// Get the loadingTs
Object[] values = platform.getObjectValues(writer.getBatch().getBinaryEncoding(), new String[] { loadingStr }, new Column[] { column });
if (values[0] instanceof Date) {
loadingTs = (Date) values[0];
} else if (values[0] instanceof String && column.getJdbcTypeName().equalsIgnoreCase(TypeMap.DATETIME2)) {
// SQL Server DateTime2 type is treated as a string internally.
loadingTs = databaseWriter.getPlatform().parseDate(Types.VARCHAR, (String) values[0], false);
} else {
throw new ParseException("Could not parse " + columnName + " with a value of " + loadingStr + " for purposes of conflict detection");
}
}
return existingTs == null || loadingTs.compareTo(existingTs) > 0;
}
use of org.jumpmind.db.platform.IDatabasePlatform in project symmetric-ds by JumpMind.
the class AuditTableDataRouter method toAuditTable.
protected Table toAuditTable(Table table) {
IDatabasePlatform platform = engine.getDatabasePlatform();
Table auditTable = table.copy();
auditTable.setName(String.format("%s_%s", auditTable.getName(), platform.alterCaseToMatchDatabaseDefaultCase("AUDIT")));
Column[] columns = auditTable.getColumns();
auditTable.removeAllColumns();
auditTable.addColumn(new Column(COLUMN_AUDIT_ID, true, Types.BIGINT, 0, 0));
auditTable.addColumn(new Column(COLUMN_AUDIT_TIME, false, Types.TIMESTAMP, 0, 0));
auditTable.addColumn(new Column(COLUMN_AUDIT_EVENT, false, Types.CHAR, 1, 0));
for (Column column : columns) {
column.setRequired(false);
column.setPrimaryKey(false);
column.setAutoIncrement(false);
auditTable.addColumn(column);
}
auditTable.removeAllForeignKeys();
auditTable.removeAllIndices();
platform.alterCaseToMatchDatabaseDefaultCase(auditTable);
return auditTable;
}
use of org.jumpmind.db.platform.IDatabasePlatform in project symmetric-ds by JumpMind.
the class DbExportImportTest method exportNullTimestampToCsv.
@Test
public void exportNullTimestampToCsv() throws Exception {
ISymmetricEngine engine = getSymmetricEngine();
IDatabasePlatform platform = engine.getDatabasePlatform();
Table table = new Table("test_null_timestamp");
table.addColumn(new Column("a", false, Types.TIMESTAMP, -1, -1));
table.addColumn(new Column("b", false, Types.TIMESTAMP, -1, -1));
platform.alterCaseToMatchDatabaseDefaultCase(table);
platform.createTables(true, false, table);
platform.getSqlTemplate().update("insert into test_null_timestamp values(null, null)");
DbExport export = new DbExport(platform);
export.setNoCreateInfo(true);
export.setFormat(Format.CSV);
String csv = export.exportTables(new Table[] { table });
Assert.assertEquals("\"A\",\"B\"" + System.getProperty("line.separator") + ",", csv.trim().toUpperCase());
}
Aggregations