use of org.jumpmind.db.model.Column in project symmetric-ds by JumpMind.
the class DbFill method selectRandomRecord.
private void selectRandomRecord(ISqlTransaction tran, Table table) {
Row row = null;
if (hasDependentColumns(table)) {
Map<Column, Object> dependent = getDependentColumnValues(table);
if (dependent.size() > 0) {
row = selectSpecificRow(tran, table, dependent.keySet().toArray(new Column[dependent.size()]), dependent.values().toArray(new Object[dependent.size()]));
saveDependentColumnValues(table, row);
} else {
row = selectRandomRow(tran, table);
saveDependentColumnValues(table, row);
}
} else {
row = selectRandomRow(tran, table);
}
currentRowValues.put(table.getName(), row);
}
use of org.jumpmind.db.model.Column in project symmetric-ds by JumpMind.
the class OracleBulkDatabaseWriter method convertObjectValues.
/**
* @param rowData
* @param columns
* @return
*/
protected Object[] convertObjectValues(Object[] values, Column[] columns) {
if (values != null) {
for (int i = 0; i < values.length; i++) {
Object value = values[i];
Column column = columns.length > i ? columns[i] : null;
if (column != null) {
values[i] = convertObjectValue(value, column);
}
}
}
return values;
}
use of org.jumpmind.db.model.Column in project symmetric-ds by JumpMind.
the class MySqlBulkDatabaseWriter method getCommaDeliminatedColumns.
protected String getCommaDeliminatedColumns(Column[] cols) {
DatabaseInfo dbInfo = platform.getDatabaseInfo();
String quote = dbInfo.getDelimiterToken();
StringBuilder columns = new StringBuilder();
columns.append("(");
// The target syntax is (where BLOB is involved):
// ('column1', 'column2', @hexColumn3) SET 'column3'=UNHEX(@hexColumn3);
Map<String, String> blobColumns = new LinkedHashMap<String, String>();
if (cols != null && cols.length > 0) {
for (Column column : cols) {
boolean blob = column.isOfBinaryType();
if (blob) {
String hexVariable = String.format("@%s_hex", column.getName());
blobColumns.put(column.getName(), hexVariable);
columns.append(hexVariable);
} else {
columns.append(quote);
columns.append(column.getName());
columns.append(quote);
}
columns.append(",");
}
columns.replace(columns.length() - 1, columns.length(), "");
columns.append(")");
// Build this (optional) clause:
// SET 'column3'=UNHEX(@hexColumn3);
StringBuilder setClause = new StringBuilder();
for (String columnName : blobColumns.keySet()) {
if (setClause.length() == 0) {
setClause.append(" SET ");
}
setClause.append(quote);
setClause.append(columnName);
setClause.append(quote);
setClause.append("=UNHEX(");
setClause.append(blobColumns.get(columnName));
setClause.append("),");
}
if (setClause.length() > 0) {
setClause.setLength(setClause.length() - 1);
columns.append(setClause);
}
return columns.toString();
} else {
return " ";
}
}
use of org.jumpmind.db.model.Column in project symmetric-ds by JumpMind.
the class SqlPersistenceManager method find.
public <T> List<T> find(Class<T> clazz, Map<String, Object> conditions, String catalogName, String schemaName, String tableName) {
if (conditions == null || conditions.size() == 0) {
return find(clazz, catalogName, schemaName, tableName);
} else {
try {
Table table = findTable(catalogName, schemaName, tableName);
T object = clazz.newInstance();
LinkedHashMap<String, Column> objectToTableMapping = mapObjectToTable(object, table);
LinkedHashMap<String, Object> objectValuesByColumnName = new LinkedHashMap<String, Object>();
Column[] keys = new Column[conditions.size()];
Set<String> keyPropertyNames = conditions.keySet();
boolean[] nullKeyValues = new boolean[conditions.size()];
int index = 0;
for (String propertyName : keyPropertyNames) {
Column column = objectToTableMapping.get(propertyName);
if (column != null) {
keys[index] = column;
nullKeyValues[index] = conditions.get(propertyName) == null;
objectValuesByColumnName.put(column.getName(), conditions.get(propertyName));
index++;
} else {
throw new IllegalStateException("Could not find a database column that maps to the " + propertyName + " property on " + clazz.getName() + ". Make sure the property is defined on the class and " + "the matching column is defined in the database table");
}
}
Column[] columns = objectToTableMapping.values().toArray(new Column[objectToTableMapping.size()]);
DmlStatement statement = databasePlatform.createDmlStatement(DmlType.SELECT, table.getCatalog(), table.getSchema(), table.getName(), keys, columns, nullKeyValues, null);
String sql = statement.getSql();
Object[] values = statement.getValueArray(objectValuesByColumnName);
int[] types = statement.getTypes();
List<Row> rows = databasePlatform.getSqlTemplate().query(sql, values, types);
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 SqlPersistenceManager method count.
@Override
public <T> int count(Class<T> clazz, String catalogName, String schemaName, String tableName, Map<String, Object> conditions) {
if (conditions == null || conditions.size() == 0) {
return count(catalogName, schemaName, tableName);
} else {
try {
Table table = findTable(catalogName, schemaName, tableName);
T object = clazz.newInstance();
LinkedHashMap<String, Column> objectToTableMapping = mapObjectToTable(object, table);
LinkedHashMap<String, Object> objectValuesByColumnName = new LinkedHashMap<String, Object>();
Column[] keys = new Column[conditions.size()];
Set<String> keyPropertyNames = conditions.keySet();
boolean[] nullKeyValues = new boolean[conditions.size()];
int index = 0;
for (String propertyName : keyPropertyNames) {
Column column = objectToTableMapping.get(propertyName);
if (column != null) {
keys[index] = column;
nullKeyValues[index] = conditions.get(propertyName) == null;
objectValuesByColumnName.put(column.getName(), conditions.get(propertyName));
index++;
} else {
throw new IllegalStateException("Could not find a database column that maps to the " + propertyName + " property on " + clazz.getName() + ". Make sure the property is defined on the class and " + "the matching column is defined in the database table");
}
}
DmlStatement statement = databasePlatform.createDmlStatement(DmlType.COUNT, table.getCatalog(), table.getSchema(), table.getName(), keys, keys, nullKeyValues, null);
String sql = statement.getSql();
Object[] values = statement.getValueArray(objectValuesByColumnName);
return databasePlatform.getSqlTemplate().queryForInt(sql, values);
} catch (Exception e) {
throw toRuntimeException(e);
}
}
}
Aggregations