Search in sources :

Example 11 with IDatabasePlatform

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);
}
Also used : ISymmetricDialect(org.jumpmind.symmetric.db.ISymmetricDialect) IDatabasePlatform(org.jumpmind.db.platform.IDatabasePlatform) DatabaseInfo(org.jumpmind.db.platform.DatabaseInfo) ISymmetricEngine(org.jumpmind.symmetric.ISymmetricEngine) IExtensionService(org.jumpmind.symmetric.service.IExtensionService) IParameterService(org.jumpmind.symmetric.service.IParameterService) Before(org.junit.Before)

Example 12 with IDatabasePlatform

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;
}
Also used : MalformedURLException(java.net.MalformedURLException) IDatabasePlatform(org.jumpmind.db.platform.IDatabasePlatform) InputStreamReader(java.io.InputStreamReader) Database(org.jumpmind.db.model.Database) File(java.io.File) URL(java.net.URL) SqlException(org.jumpmind.db.sql.SqlException) MalformedURLException(java.net.MalformedURLException)

Example 13 with IDatabasePlatform

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;
}
Also used : IDatabasePlatform(org.jumpmind.db.platform.IDatabasePlatform) Table(org.jumpmind.db.model.Table) Timestamp(java.sql.Timestamp) Date(java.util.Date) Column(org.jumpmind.db.model.Column) DmlStatement(org.jumpmind.db.sql.DmlStatement) ParseException(org.jumpmind.exception.ParseException)

Example 14 with IDatabasePlatform

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;
}
Also used : IDatabasePlatform(org.jumpmind.db.platform.IDatabasePlatform) Table(org.jumpmind.db.model.Table) Column(org.jumpmind.db.model.Column)

Example 15 with IDatabasePlatform

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());
}
Also used : IDatabasePlatform(org.jumpmind.db.platform.IDatabasePlatform) DbExport(org.jumpmind.symmetric.io.data.DbExport) Table(org.jumpmind.db.model.Table) Column(org.jumpmind.db.model.Column) AbstractServiceTest(org.jumpmind.symmetric.service.impl.AbstractServiceTest) Test(org.junit.Test)

Aggregations

IDatabasePlatform (org.jumpmind.db.platform.IDatabasePlatform)27 Table (org.jumpmind.db.model.Table)13 Database (org.jumpmind.db.model.Database)9 Test (org.junit.Test)9 AbstractServiceTest (org.jumpmind.symmetric.service.impl.AbstractServiceTest)8 ISqlTemplate (org.jumpmind.db.sql.ISqlTemplate)7 DatabaseInfo (org.jumpmind.db.platform.DatabaseInfo)6 ISqlTransaction (org.jumpmind.db.sql.ISqlTransaction)6 ISymmetricEngine (org.jumpmind.symmetric.ISymmetricEngine)6 DbExport (org.jumpmind.symmetric.io.data.DbExport)6 DbImport (org.jumpmind.symmetric.io.data.DbImport)6 ISymmetricDialect (org.jumpmind.symmetric.db.ISymmetricDialect)5 IExtensionService (org.jumpmind.symmetric.service.IExtensionService)5 IParameterService (org.jumpmind.symmetric.service.IParameterService)5 Column (org.jumpmind.db.model.Column)4 Before (org.junit.Before)4 File (java.io.File)3 DmlStatement (org.jumpmind.db.sql.DmlStatement)3 Row (org.jumpmind.db.sql.Row)3 SqlException (org.jumpmind.db.sql.SqlException)3