use of org.jumpmind.db.sql.ISqlTemplate in project symmetric-ds by JumpMind.
the class AuditTableDataRouter method routeToNodes.
public Set<String> routeToNodes(SimpleRouterContext context, DataMetaData dataMetaData, Set<Node> nodes, boolean initialLoad, boolean initialLoadSelectUsed, TriggerRouter triggerRouter) {
DataEventType eventType = dataMetaData.getData().getDataEventType();
if (eventType == DataEventType.INSERT || eventType == DataEventType.UPDATE || eventType == DataEventType.DELETE) {
IParameterService parameterService = engine.getParameterService();
IDatabasePlatform platform = engine.getDatabasePlatform();
TriggerHistory triggerHistory = dataMetaData.getTriggerHistory();
Table table = dataMetaData.getTable().copyAndFilterColumns(triggerHistory.getParsedColumnNames(), triggerHistory.getParsedPkColumnNames(), true);
String tableName = table.getFullyQualifiedTableName();
Table auditTable = auditTables.get(tableName);
if (auditTable == null) {
auditTable = toAuditTable(table);
if (parameterService.is(ParameterConstants.AUTO_CONFIGURE_DATABASE)) {
platform.alterTables(true, auditTable);
}
auditTable = platform.getTableFromCache(auditTable.getCatalog(), auditTable.getSchema(), auditTable.getName(), false);
auditTables.put(tableName, auditTable);
}
DatabaseInfo dbInfo = platform.getDatabaseInfo();
String auditTableName = auditTable.getQualifiedTableName(dbInfo.getDelimiterToken(), dbInfo.getCatalogSeparator(), dbInfo.getSchemaSeparator());
ISqlTemplate template = platform.getSqlTemplate();
Map<String, Object> values = null;
if (eventType != DataEventType.DELETE) {
values = new HashMap<String, Object>(getNewDataAsObject(null, dataMetaData, engine.getSymmetricDialect(), false));
} else {
values = new HashMap<String, Object>(getOldDataAsObject(null, dataMetaData, engine.getSymmetricDialect(), false));
}
Long sequence = (Long) context.get(auditTableName);
if (sequence == null) {
sequence = 1l + template.queryForLong(String.format("select max(%s) from %s", auditTable.getColumnWithName(COLUMN_AUDIT_ID).getName(), auditTableName));
} else {
sequence = 1l + sequence;
}
context.put(auditTable.getName(), sequence);
values.put(auditTable.getColumnWithName(COLUMN_AUDIT_ID).getName(), sequence);
values.put(auditTable.getColumnWithName(COLUMN_AUDIT_TIME).getName(), new Date());
values.put(auditTable.getColumnWithName(COLUMN_AUDIT_EVENT).getName(), eventType.getCode());
DmlStatement statement = platform.createDmlStatement(DmlType.INSERT, auditTable, null);
int[] types = statement.getTypes();
Object[] args = statement.getValueArray(values);
String sql = statement.getSql();
template.update(sql, args, types);
}
return null;
}
use of org.jumpmind.db.sql.ISqlTemplate in project symmetric-ds by JumpMind.
the class LookupTableDataRouter method getLookupTable.
@SuppressWarnings("unchecked")
protected Map<String, Set<String>> getLookupTable(final Map<String, String> params, Router router, SimpleRouterContext routingContext) {
final String CTX_CACHE_KEY = LOOKUP_TABLE_KEY + "." + params.get(PARAM_TABLE);
Map<String, Set<String>> lookupMap = (Map<String, Set<String>>) routingContext.getContextCache().get(CTX_CACHE_KEY);
if (lookupMap == null) {
ISqlTemplate template = symmetricDialect.getPlatform().getSqlTemplate();
final Map<String, Set<String>> fillMap = new HashMap<String, Set<String>>();
template.query(String.format("select %s, %s from %s", params.get(PARAM_MAPPED_KEY_COLUMN), params.get(PARAM_EXTERNAL_ID_COLUMN), params.get(PARAM_TABLE)), new ISqlRowMapper<Object>() {
public Object mapRow(Row rs) {
String key = rs.getString(params.get(PARAM_MAPPED_KEY_COLUMN));
String value = rs.getString(params.get(PARAM_EXTERNAL_ID_COLUMN));
Set<String> ids = fillMap.get(key);
if (ids == null) {
ids = new HashSet<String>();
fillMap.put(key, ids);
}
ids.add(value);
return value;
}
});
lookupMap = fillMap;
routingContext.getContextCache().put(CTX_CACHE_KEY, lookupMap);
}
return lookupMap;
}
use of org.jumpmind.db.sql.ISqlTemplate 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;
}
use of org.jumpmind.db.sql.ISqlTemplate in project symmetric-ds by JumpMind.
the class GroupletService method deleteGrouplet.
public void deleteGrouplet(Grouplet grouplet) {
List<GroupletLink> links = grouplet.getGroupletLinks();
for (GroupletLink link : links) {
deleteGroupletLink(grouplet, link);
}
List<TriggerRouterGrouplet> triggerRouters = grouplet.getTriggerRouterGrouplets();
for (TriggerRouterGrouplet triggerRouterGrouplet : triggerRouters) {
deleteTriggerRouterGrouplet(grouplet, triggerRouterGrouplet);
}
ISqlTemplate sqlTemplate = platform.getSqlTemplate();
sqlTemplate.update(getSql("deleteGroupletSql"), new Object[] { grouplet.getGroupletId() }, new int[] { Types.VARCHAR });
}
use of org.jumpmind.db.sql.ISqlTemplate in project symmetric-ds by JumpMind.
the class GroupletService method deleteGroupletLink.
public void deleteGroupletLink(Grouplet grouplet, GroupletLink link) {
ISqlTemplate sqlTemplate = platform.getSqlTemplate();
sqlTemplate.update(getSql("deleteGroupletLinkSql"), new Object[] { grouplet.getGroupletId(), link.getExternalId() }, new int[] { Types.VARCHAR, Types.VARCHAR });
}
Aggregations