Search in sources :

Example 1 with JdbcClassInfo

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);
    }
}
Also used : Field(java.lang.reflect.Field) JdbcClassInfo(siena.jdbc.JdbcPersistenceManager.JdbcClassInfo) SienaException(siena.SienaException) IOException(java.io.IOException) SQLException(java.sql.SQLException) SienaException(siena.SienaException)

Example 2 with JdbcClassInfo

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;
}
Also used : Field(java.lang.reflect.Field) ArrayList(java.util.ArrayList) JdbcClassInfo(siena.jdbc.JdbcPersistenceManager.JdbcClassInfo) SienaException(siena.SienaException)

Example 3 with JdbcClassInfo

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);
            }
        }
    }
}
Also used : QueryJoin(siena.QueryJoin) QueryOrder(siena.QueryOrder) Field(java.lang.reflect.Field) JdbcClassInfo(siena.jdbc.JdbcPersistenceManager.JdbcClassInfo) SienaException(siena.SienaException)

Aggregations

Field (java.lang.reflect.Field)3 SienaException (siena.SienaException)3 JdbcClassInfo (siena.jdbc.JdbcPersistenceManager.JdbcClassInfo)3 IOException (java.io.IOException)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 QueryJoin (siena.QueryJoin)1 QueryOrder (siena.QueryOrder)1