use of org.jumpmind.db.sql.DmlStatement in project symmetric-ds by JumpMind.
the class DefaultDatabaseWriterConflictResolver method isVersionNewer.
protected boolean isVersionNewer(Conflict conflict, AbstractDatabaseWriter writer, CsvData data) {
DefaultDatabaseWriter databaseWriter = (DefaultDatabaseWriter) writer;
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());
String sql = stmt.getColumnsSql(new Column[] { targetTable.getColumnWithName(columnName) });
Long existingVersion = databaseWriter.getTransaction().queryForObject(sql, Long.class, objectValues);
if (existingVersion == null) {
return true;
} else {
Map<String, String> newData = data.toColumnNameValuePairs(sourceTable.getColumnNames(), CsvData.ROW_DATA);
Long loadingVersion = Long.valueOf(newData.get(columnName));
return loadingVersion > existingVersion;
}
}
use of org.jumpmind.db.sql.DmlStatement in project symmetric-ds by JumpMind.
the class StructureDataWriter method buildSql.
protected String buildSql(DmlType dmlType, String[] values, Column[] columns) {
// TODO we should try to reuse statements
// TODO support primary key updates
DmlStatement statement = DmlStatementFactory.createDmlStatement(targetDatabaseName, dmlType, currentTable, useQuotedIdentifiers);
Object[] objects = platform.getObjectValues(binaryEncoding, values, columns, false, false);
Row row = new Row(columns.length);
for (int i = 0; i < columns.length; i++) {
row.put(columns[i].getName(), objects[i]);
}
return statement.buildDynamicSql(binaryEncoding, row, false, useJdbcTimestampFormat);
}
use of org.jumpmind.db.sql.DmlStatement in project symmetric-ds by JumpMind.
the class DbFill method insertRandomRecord.
/**
* Select a random row from the table and update all columns except for primary and foreign keys.
*
* @param sqlTemplate
* @param table
*/
private void insertRandomRecord(Table table) {
DmlStatement insertStatement = createInsertDmlStatement(table);
Row row = createRandomInsertValues(insertStatement, table);
try {
platform.getSqlTemplate().update(insertStatement.getSql(), insertStatement.getValueArray(row.toArray(table.getColumnNames()), row.toArray(table.getPrimaryKeyColumnNames())));
if (verbose) {
log.info("Successful update in " + table.getName());
}
} catch (SqlException ex) {
log.info("Failed to process {} with values of {}", insertStatement.getSql(), ArrayUtils.toString(row.toArray(table.getColumnNames())));
if (continueOnError) {
if (debug) {
log.info("", ex);
}
} else {
throw ex;
}
}
}
use of org.jumpmind.db.sql.DmlStatement in project symmetric-ds by JumpMind.
the class DbFill method deleteRandomRecord.
/**
* Delete a random row in the given table or delete all rows matching selectColumns
* in the given table.
*
* @param table Table to delete from.
* @param selectColumns If provided, the rows that match this criteria are deleted.
*/
private void deleteRandomRecord(Table table) {
DmlStatement deleteStatement = createDeleteDmlStatement(table);
Row row = selectRandomRow(table);
try {
platform.getSqlTemplate().update(deleteStatement.getSql(), row.toArray(table.getColumnNames()));
if (verbose) {
log.info("Successful update in " + table.getName());
}
} catch (SqlException ex) {
log.info("Failed to process {} with values of {}", deleteStatement.getSql(), ArrayUtils.toString(row.toArray(table.getColumnNames())));
if (continueOnError) {
if (debug) {
log.info("", ex);
}
} else {
throw ex;
}
}
}
use of org.jumpmind.db.sql.DmlStatement 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);
auditTables.put(tableName, auditTable);
if (parameterService.is(ParameterConstants.AUTO_CONFIGURE_DATABASE)) {
platform.alterTables(true, 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;
}
Aggregations