Search in sources :

Example 6 with StringMapper

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

the class SubSelectDataRouter method routeToNodes.

public Set<String> routeToNodes(SimpleRouterContext routingContext, DataMetaData dataMetaData, Set<Node> nodes, boolean initialLoad, boolean initialLoadSelectUsed, TriggerRouter triggerRouter) {
    String sql = FormatUtils.replaceToken(SQL, "prefixName", symmetricDialect.getTablePrefix(), true);
    String subSelect = dataMetaData.getRouter().getRouterExpression();
    Set<String> nodeIds = null;
    if (!StringUtils.isBlank(subSelect) && !initialLoadSelectUsed) {
        try {
            Map<String, Object> sqlParams = getDataObjectMap(dataMetaData, symmetricDialect, true);
            sqlParams.put("NODE_GROUP_ID", dataMetaData.getRouter().getNodeGroupLink().getTargetNodeGroupId());
            sqlParams.put("EXTERNAL_DATA", dataMetaData.getData().getExternalData());
            ISqlTemplate template = symmetricDialect.getPlatform().getSqlTemplate();
            List<String> ids = template.query(String.format("%s%s", sql, subSelect), new StringMapper(), sqlParams);
            if (ids != null) {
                nodeIds = new HashSet<String>(ids);
            }
        } catch (InvalidSqlException ex) {
            log.error("The subselect expression was invalid for the {} subselect router for the '{}' event for table '{}'", new Object[] { dataMetaData.getRouter().getRouterId(), dataMetaData.getData().getDataEventType().name(), dataMetaData.getData().getTableName() });
            throw ex;
        }
    } else if (initialLoadSelectUsed) {
        nodeIds = toNodeIds(nodes, null);
    } else {
        throw new InvalidSqlException("The subselect expression is missing for the %s router", dataMetaData.getRouter().getRouterId());
    }
    return nodeIds;
}
Also used : StringMapper(org.jumpmind.db.sql.mapper.StringMapper) ISqlTemplate(org.jumpmind.db.sql.ISqlTemplate) InvalidSqlException(org.jumpmind.db.sql.InvalidSqlException)

Example 7 with StringMapper

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

the class AbstractTriggerRouterServiceTest method test06InitialLoadSql.

@Test
public void test06InitialLoadSql() throws Exception {
    ITriggerRouterService triggerRouterService = getTriggerRouterService();
    IParameterService parameterService = getParameterService();
    parameterService.saveParameter(ParameterConstants.INITIAL_LOAD_CONCAT_CSV_IN_SQL_ENABLED, true, "unittest");
    TriggerRouter triggerRouter = triggerRouterService.getTriggerRouterForTableForCurrentNode(true, null, null, TEST_TRIGGERS_TABLE, true).iterator().next();
    Table table = getDbDialect().getPlatform().getTableFromCache(triggerRouter.getTrigger().getSourceTableName(), true);
    Trigger trigger = triggerRouter.getTrigger();
    String sql = getDbDialect().createInitialLoadSqlFor(new Node("1", null, "1.0"), triggerRouter, table, triggerRouterService.getNewestTriggerHistoryForTrigger(trigger.getTriggerId(), trigger.getSourceCatalogName(), trigger.getSourceSchemaName(), trigger.getSourceTableName()), getConfigurationService().getChannel(triggerRouter.getTrigger().getChannelId()), null);
    List<String> csvStrings = getSqlTemplate().query(sql, new StringMapper());
    assertTrue(csvStrings.size() > 0);
    String csvString = csvStrings.get(0);
    // DB2 captures decimal differently
    csvString = csvString.replaceFirst("\"00001\\.\"", "\"1\"");
    // Informix captures decimal differently
    csvString = csvString.replaceFirst("\"1.0000000000000000\"", "\"1\"");
    // ASA captures decimal differently
    csvString = csvString.replaceFirst("\"1.000000\"", "\"1\"");
    assertTrue(csvString.endsWith(EXPECTED_INSERT1_CSV_ENDSWITH), "Received " + csvString + ", Expected the string to end with " + EXPECTED_INSERT1_CSV_ENDSWITH);
}
Also used : StringMapper(org.jumpmind.db.sql.mapper.StringMapper) Table(org.jumpmind.db.model.Table) Trigger(org.jumpmind.symmetric.model.Trigger) ITriggerRouterService(org.jumpmind.symmetric.service.ITriggerRouterService) Node(org.jumpmind.symmetric.model.Node) TriggerRouter(org.jumpmind.symmetric.model.TriggerRouter) IParameterService(org.jumpmind.symmetric.service.IParameterService) Test(org.junit.Test)

Aggregations

StringMapper (org.jumpmind.db.sql.mapper.StringMapper)7 ArrayList (java.util.ArrayList)2 List (java.util.List)1 Table (org.jumpmind.db.model.Table)1 ChangeCatalogConnectionHandler (org.jumpmind.db.sql.ChangeCatalogConnectionHandler)1 ISqlTemplate (org.jumpmind.db.sql.ISqlTemplate)1 InvalidSqlException (org.jumpmind.db.sql.InvalidSqlException)1 Node (org.jumpmind.symmetric.model.Node)1 NodeSecurity (org.jumpmind.symmetric.model.NodeSecurity)1 Trigger (org.jumpmind.symmetric.model.Trigger)1 TriggerRouter (org.jumpmind.symmetric.model.TriggerRouter)1 IParameterService (org.jumpmind.symmetric.service.IParameterService)1 ITriggerRouterService (org.jumpmind.symmetric.service.ITriggerRouterService)1 Test (org.junit.Test)1