use of org.jumpmind.db.model.Column in project symmetric-ds by JumpMind.
the class SqlPersistenceManager 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.model.Column in project symmetric-ds by JumpMind.
the class SqlPersistenceManager method mapObjectToTable.
protected LinkedHashMap<String, Column> mapObjectToTable(Object object, Table table) {
LinkedHashMap<String, Column> columnNames = new LinkedHashMap<String, Column>();
PropertyDescriptor[] pds = PropertyUtils.getPropertyDescriptors(object);
for (int i = 0; i < pds.length; i++) {
String propName = pds[i].getName();
Column column = table.getColumnWithName(camelCaseToUnderScores(propName));
if (column != null) {
columnNames.put(propName, column);
}
}
return columnNames;
}
use of org.jumpmind.db.model.Column in project symmetric-ds by JumpMind.
the class SqlPersistenceManager method map.
@Override
public <T> T map(Map<String, Object> row, Class<T> clazz, String catalogName, String schemaName, String tableName) {
try {
T object = clazz.newInstance();
Table table = findTable(catalogName, schemaName, tableName);
LinkedHashMap<String, Column> objectToTableMapping = mapObjectToTable(object, table);
Set<String> propertyNames = objectToTableMapping.keySet();
for (String propertyName : propertyNames) {
Object value = row.get(objectToTableMapping.get(propertyName).getName());
BeanUtils.copyProperty(object, propertyName, value);
}
return object;
} catch (Exception e) {
throw toRuntimeException(e);
}
}
use of org.jumpmind.db.model.Column in project symmetric-ds by JumpMind.
the class SqlPersistenceManager 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.model.Column in project symmetric-ds by JumpMind.
the class DbFill method getDependentColumnValues.
private Map<Column, Object> getDependentColumnValues(Table table) {
Map<Column, Object> columnValues = new HashMap<Column, Object>();
for (Column column : table.getColumns()) {
String key = table.getName() + "." + column.getName();
List<Object> commonValue = commonDependencyValues.get(key);
if (commonValue != null && commonValue.size() != 0) {
columnValues.put(column, commonValue.get(0));
}
}
return columnValues;
}
Aggregations