use of org.jumpmind.db.sql.DmlStatement in project symmetric-ds by JumpMind.
the class DbFill method updateRandomRecord.
private void updateRandomRecord(ISqlTransaction tran, Table table) {
DmlStatement updStatement = createUpdateDmlStatement(table);
Row row = createRandomUpdateValues(tran, updStatement, table);
Object[] values = new Object[table.getColumnCount()];
int i = 0;
for (Column column : table.getColumns()) {
if (!column.isPrimaryKey()) {
values[i++] = row.get(column.getName());
}
}
for (Column column : table.getPrimaryKeyColumns()) {
values[i++] = row.get(column.getName());
}
try {
tran.prepareAndExecute(updStatement.getSql(), values);
} catch (SqlException ex) {
log.info("Failed to update {}: {}", table.getName(), ex.getMessage());
if (continueOnError) {
if (debug) {
logRow(row);
log.info("", ex);
}
} else {
throw ex;
}
}
}
use of org.jumpmind.db.sql.DmlStatement in project symmetric-ds by JumpMind.
the class JdbcPersistenceManager method find.
@Override
public <T> List<T> find(Class<T> clazz, String catalogName, String schemaName, String tableName) {
try {
Table table = findTable(catalogName, schemaName, tableName);
T object = clazz.newInstance();
LinkedHashMap<String, Column> objectToTableMapping = mapObjectToTable(object, table);
Column[] columns = objectToTableMapping.values().toArray(new Column[objectToTableMapping.size()]);
DmlStatement statement = databasePlatform.createDmlStatement(DmlType.SELECT_ALL, table.getCatalog(), table.getSchema(), table.getName(), null, columns, null, null);
String sql = statement.getSql();
List<Row> rows = databasePlatform.getSqlTemplate().query(sql);
List<T> objects = new ArrayList<T>();
for (Row row : rows) {
object = clazz.newInstance();
Set<String> propertyNames = objectToTableMapping.keySet();
for (String propertyName : propertyNames) {
Object value = row.get(objectToTableMapping.get(propertyName).getName());
BeanUtils.copyProperty(object, propertyName, value);
}
objects.add(object);
}
return objects;
} catch (Exception e) {
throw toRuntimeException(e);
}
}
use of org.jumpmind.db.sql.DmlStatement in project symmetric-ds by JumpMind.
the class JdbcPersistenceManager method refresh.
@Override
public void refresh(Object object, String catalogName, String schemaName, String tableName) {
try {
Table table = findTable(catalogName, schemaName, tableName);
LinkedHashMap<String, Column> objectToTableMapping = mapObjectToTable(object, table);
LinkedHashMap<String, Object> objectValuesByColumnName = getObjectValuesByColumnName(object, objectToTableMapping);
Column[] columns = objectToTableMapping.values().toArray(new Column[objectToTableMapping.size()]);
List<Column> keys = new ArrayList<Column>(1);
for (Column column : columns) {
if (column.isPrimaryKey()) {
keys.add(column);
}
}
DmlStatement statement = databasePlatform.createDmlStatement(DmlType.SELECT, table.getCatalog(), table.getSchema(), table.getName(), keys.toArray(new Column[keys.size()]), columns, null, null);
String sql = statement.getSql();
Object[] values = statement.getValueArray(objectValuesByColumnName);
Row row = databasePlatform.getSqlTemplate().queryForRow(sql, values);
if (row != null) {
Set<String> propertyNames = objectToTableMapping.keySet();
for (String propertyName : propertyNames) {
Object value = row.get(objectToTableMapping.get(propertyName).getName());
BeanUtils.copyProperty(object, propertyName, value);
}
}
} catch (Exception e) {
throw toRuntimeException(e);
}
}
use of org.jumpmind.db.sql.DmlStatement in project symmetric-ds by JumpMind.
the class JdbcPersistenceManager method excecuteDml.
protected int excecuteDml(DmlType type, Object object, String catalogName, String schemaName, String tableName) {
Table table = findTable(catalogName, schemaName, tableName);
LinkedHashMap<String, Column> objectToTableMapping = mapObjectToTable(object, table);
LinkedHashMap<String, Object> objectValuesByColumnName = getObjectValuesByColumnName(object, objectToTableMapping);
Column[] columns = objectToTableMapping.values().toArray(new Column[objectToTableMapping.size()]);
List<Column> keys = new ArrayList<Column>(1);
for (Column column : columns) {
if (column.isPrimaryKey()) {
keys.add(column);
}
}
boolean[] nullKeyValues = new boolean[keys.size()];
int i = 0;
for (Column column : keys) {
nullKeyValues[i++] = objectValuesByColumnName.get(column.getName()) == null;
}
DmlStatement statement = databasePlatform.createDmlStatement(type, table.getCatalog(), table.getSchema(), table.getName(), keys.toArray(new Column[keys.size()]), columns, nullKeyValues, null);
String sql = statement.getSql();
Object[] values = statement.getValueArray(objectValuesByColumnName);
int[] types = statement.getTypes();
return databasePlatform.getSqlTemplate().update(sql, values, types);
}
use of org.jumpmind.db.sql.DmlStatement in project symmetric-ds by JumpMind.
the class DefaultDatabaseWriter method getCurData.
protected String getCurData(ISqlTransaction transaction) {
String curVal = null;
if (writerSettings.isSaveCurrentValueOnError()) {
String[] keyNames = Table.getArrayColumns(context.getTable().getPrimaryKeyColumns());
String[] columnNames = Table.getArrayColumns(context.getTable().getColumns());
org.jumpmind.db.model.Table targetTable = platform.getTableFromCache(context.getTable().getCatalog(), context.getTable().getSchema(), context.getTable().getName(), false);
targetTable = targetTable.copyAndFilterColumns(columnNames, keyNames, true);
String[] data = context.getData().getParsedData(CsvData.OLD_DATA);
if (data == null) {
data = context.getData().getParsedData(CsvData.ROW_DATA);
}
Column[] columns = targetTable.getColumns();
Object[] objectValues = platform.getObjectValues(context.getBatch().getBinaryEncoding(), data, columns);
Map<String, Object> columnDataMap = CollectionUtils.toMap(columnNames, objectValues);
Column[] pkColumns = targetTable.getPrimaryKeyColumns();
Object[] args = new Object[pkColumns.length];
for (int i = 0; i < pkColumns.length; i++) {
args[i] = columnDataMap.get(pkColumns[i].getName());
}
DmlStatement sqlStatement = platform.createDmlStatement(DmlType.SELECT, targetTable, writerSettings.getTextColumnExpression());
Row row = null;
List<Row> list = transaction.query(sqlStatement.getSql(), new ISqlRowMapper<Row>() {
public Row mapRow(Row row) {
return row;
}
}, args, null);
if (list != null && list.size() > 0) {
row = list.get(0);
}
if (row != null) {
String[] existData = platform.getStringValues(context.getBatch().getBinaryEncoding(), columns, row, false, false);
if (existData != null) {
curVal = CsvUtils.escapeCsvData(existData);
}
}
}
return curVal;
}
Aggregations