use of siena.jdbc.JdbcPersistenceManager.JdbcClassInfo in project siena by mandubian.
the class JdbcMappingUtils method mapField.
public static void mapField(Object obj, Field field, ResultSet rs, String tableName, List<Field> joinFields) {
Class<?> type = field.getType();
//field.setAccessible(true);
try {
if (ClassInfo.isModel(type) && !ClassInfo.isEmbedded(field)) {
JdbcClassInfo fieldClassInfo = JdbcClassInfo.getClassInfo(type);
if (joinFields == null || joinFields.size() == 0 || !joinFields.contains(field)) {
String[] fks = ClassInfo.getColumnNames(field, tableName);
Object rel = Util.createObjectInstance(type);
boolean none = false;
int i = 0;
checkForeignKeyMapping(fieldClassInfo.keys, fks, obj.getClass(), field);
for (Field f : fieldClassInfo.keys) {
Object o = rs.getObject(JdbcClassInfo.aliasFromCol(fks[i++]));
if (o == null) {
none = true;
break;
}
setFromObject(rel, f, o);
}
if (!none) {
Util.setField(obj, field, rel);
//field.set(obj, rel);
}
} else {
// this is a JOIN field
// first verifies the field is not null
Object val = rs.getObject(JdbcClassInfo.aliasFromCol(ClassInfo.getColumnNames(field, tableName)[0]));
if (val == null) {
Util.setField(obj, field, null);
return;
}
// uses join field alias
// Object rel = mapObject(type, rs, fieldClassInfo.tableName, null);
Object rel = mapObject(type, rs, fieldClassInfo.joinFieldAliases.get(field.getName()), null);
Util.setField(obj, field, rel);
}
} else {
Object val = rs.getObject(ClassInfo.getColumnNames(field, tableName)[0].replace('.', '_'));
setFromObject(obj, field, val);
}
} catch (SienaException e) {
throw e;
} catch (Exception e) {
throw new SienaException(e);
}
}
use of siena.jdbc.JdbcPersistenceManager.JdbcClassInfo in project siena by mandubian.
the class JdbcDBUtils method buildSqlSelect.
public static <T> StringBuilder buildSqlSelect(Query<T> query) {
Class<T> clazz = query.getQueriedClass();
JdbcClassInfo info = JdbcClassInfo.getClassInfo(clazz);
List<String> cols = new ArrayList<String>();
List<Field> joinFields = JdbcMappingUtils.getJoinFields(query, info);
if (joinFields == null) {
JdbcClassInfo.calculateColumnsAliases(info.allFields, cols, info.tableName, "");
StringBuilder sql = new StringBuilder("SELECT " + Util.join(cols, ", ") + " FROM " + info.tableName);
return sql;
}
// builds fields from primary class
JdbcClassInfo.calculateColumnsAliases(info.allFields, cols, info.tableName, "");
StringBuilder sql = new StringBuilder(" FROM " + info.tableName);
int i = 0;
String alias;
for (Field field : joinFields) {
JdbcClassInfo fieldInfo = JdbcClassInfo.getClassInfo(field.getType());
if (!ClassInfo.isModel(field.getType())) {
throw new SienaException("Join not possible: Field " + field.getName() + " is not a relation field");
}
alias = fieldInfo.tableName + i++;
fieldInfo.joinFieldAliases.put(field.getName(), alias);
// DO NOT remove the field itself from columns because it allows to find NULL fields
// cols.remove( info.tableName+"."+field.getName());
// adds all field columns using Alias
JdbcClassInfo.calculateColumnsAliases(fieldInfo.allFields, cols, alias, "");
String[] columns = ClassInfo.getColumnNames(field, info.tableName);
if (columns.length > 1 || fieldInfo.keys.size() > 1) {
throw new SienaException("Join not possible: join field " + field.getName() + " has multiple keys");
}
// LEFT INNER JOIN TO GET NULL FIELDS
sql.append(" LEFT JOIN " + fieldInfo.tableName + " AS " + alias + " ON " + columns[0] + " = " + alias + "." + fieldInfo.keys.get(0).getName());
}
sql.insert(0, "SELECT " + Util.join(cols, ", "));
return sql;
}
use of siena.jdbc.JdbcPersistenceManager.JdbcClassInfo in project siena by mandubian.
the class JdbcDBUtils method appendSqlOrder.
public static <T> void appendSqlOrder(Query<T> query, StringBuilder sql) {
Class<T> clazz = query.getQueriedClass();
JdbcClassInfo info = JdbcClassInfo.getClassInfo(clazz);
List<QueryOrder> orders = query.getOrders();
List<QueryJoin> joins = query.getJoins();
if (orders.isEmpty() && joins.isEmpty()) {
return;
}
sql.append(" ORDER BY ");
boolean first = true;
for (QueryOrder order : orders) {
if (!first) {
sql.append(", ");
}
first = false;
if (order.parentField == null) {
String[] columns = ClassInfo.getColumnNames(order.field, info.tableName);
for (String column : columns) {
sql.append(column + (order.ascending ? "" : " DESC"));
}
} else {
try {
JdbcClassInfo parentCi = JdbcClassInfo.getClassInfo(order.parentField.getType());
Field subField = order.parentField.getType().getField(order.field.getName());
// get columns using join field alias
//String[] columns = ClassInfo.getColumnNames(subField, parentCi.tableName);
String[] columns = ClassInfo.getColumnNames(subField, parentCi.joinFieldAliases.get(order.parentField.getName()));
for (String column : columns) {
sql.append(column + (order.ascending ? "" : " DESC"));
}
} catch (NoSuchFieldException ex) {
throw new SienaException("Order not possible: join sort field " + order.field.getName() + " is not a known field of " + order.parentField.getName(), ex);
}
}
}
}
Aggregations