Search in sources :

Example 1 with ORMConfigBean

use of org.eweb4j.orm.config.bean.ORMConfigBean in project eweb4j-framework by laiweiwei.

the class ModelHelper method fmtSql.

private static String fmtSql(Class<?> cls, ReflectUtil ru, String alias, Map<String, String> joins, String _sql) {
    ORMConfigBean ormBean = ORMConfigBeanCache.get(cls.getName());
    String sql = _sql;
    if (joins != null && !joins.isEmpty()) {
        for (Iterator<Entry<String, String>> it = joins.entrySet().iterator(); it.hasNext(); ) {
            Entry<String, String> e = it.next();
            String field = e.getKey();
            String _alias = e.getValue();
            sql = fmtSql(ru.getField(field).getType(), null, _alias, null, sql);
        }
    }
    if (alias != null && alias.trim().length() > 0)
        alias = alias + ".";
    sql = sql.replace("#" + alias + "table", ormBean.getTable()).replace("#" + alias + "id", alias + ORMConfigBeanUtil.getIdColumn(cls));
    for (Property p : ormBean.getProperty()) sql = sql.replace("#" + alias + p.getName(), alias + p.getColumn());
    return sql;
}
Also used : Entry(java.util.Map.Entry) ORMConfigBean(org.eweb4j.orm.config.bean.ORMConfigBean) Property(org.eweb4j.orm.config.bean.Property)

Example 2 with ORMConfigBean

use of org.eweb4j.orm.config.bean.ORMConfigBean in project eweb4j-framework by laiweiwei.

the class ORMConfigBeanCreator method getORMBean.

public static ORMConfigBean getORMBean() {
    ORMConfigBean result = new ORMConfigBean();
    List<Property> pList = new ArrayList<Property>();
    Property p = new Property();
    pList.add(p);
    result.setProperty(pList);
    return result;
}
Also used : ORMConfigBean(org.eweb4j.orm.config.bean.ORMConfigBean) ArrayList(java.util.ArrayList) Property(org.eweb4j.orm.config.bean.Property)

Example 3 with ORMConfigBean

use of org.eweb4j.orm.config.bean.ORMConfigBean in project eweb4j-framework by laiweiwei.

the class ORMConfigBeanUtil method getColumnsOrFields.

/**
	 * get columns through fields
	 * 
	 * @param clazz
	 * @param fields
	 * @param type
	 *            1.getColumns 2.getFields 3.allColumns 4.allFields
	 * 
	 * @return
	 */
private static <T> String[] getColumnsOrFields(T t, String[] strs, int type) {
    Class<?> clazz;
    if (t instanceof Class) {
        clazz = (Class<?>) t;
    } else {
        clazz = t.getClass();
    }
    if (!(t instanceof Class) && Map.class.isAssignableFrom(clazz)) {
        HashMap<String, Object> map = (HashMap<String, Object>) t;
        return (String[]) map.get("columns");
    }
    if (strs == null)
        strs = new String[] { "" };
    String[] result = strs.clone();
    List<String> list = new ArrayList<String>();
    ORMConfigBean ormBean = ORMConfigBeanCache.get(clazz.getName());
    if (ormBean == null) {
        try {
            ReflectUtil ru = new ReflectUtil(clazz);
            // String idColumn = getIdColumn(clazz);
            for (int i = 0; i < strs.length; i++) {
                boolean finished = false;
                Field[] _fields = ru.getFields();
                for (Field _f : _fields) {
                    String pName = _f.getName();
                    JoinColumn jc = _f.getAnnotation(JoinColumn.class);
                    Column c = _f.getAnnotation(Column.class);
                    String col = pName;
                    if (c != null && c.name().trim().length() > 0)
                        col = c.name();
                    if (jc != null && jc.name().trim().length() > 0)
                        col = jc.name();
                    if (finished)
                        break;
                    switch(type) {
                        case 1:
                            int dotIndex = strs[i].indexOf(".");
                            if (dotIndex > 0 && dotIndex < strs[i].length() - 1) {
                                String[] dots = strs[i].split("\\.");
                                StringBuilder builder = new StringBuilder();
                                Class<?> prevCls = null;
                                for (int j = 0; j < dots.length; j++) {
                                    String dot = dots[j];
                                    Field field = ru.getField(dot);
                                    if (field == null && dot.equals(clazz.getSimpleName().toLowerCase())) {
                                        if (builder.length() > 0)
                                            builder.append(".");
                                        builder.append(dot);
                                        prevCls = clazz;
                                    } else if (field != null) {
                                        if (j == dots.length - 1) {
                                            if (builder.length() > 0)
                                                builder.append(".");
                                            builder.append(ORMConfigBeanUtil.getColumn(prevCls, dot));
                                        } else {
                                            Class<?> cls = ClassUtil.getGenericType(field);
                                            if (cls != null) {
                                                if (ORMConfigBeanCache.get(cls.getName()) != null) {
                                                    if (builder.length() > 0)
                                                        builder.append(".");
                                                    builder.append(cls.getSimpleName().toLowerCase());
                                                    prevCls = cls;
                                                    ru = new ReflectUtil(cls);
                                                }
                                            }
                                        }
                                    }
                                }
                                if (builder.length() > 0) {
                                    result[i] = builder.toString();
                                    finished = true;
                                    break;
                                }
                            }
                            if (pName.equals(strs[i])) {
                                result[i] = col;
                                finished = true;
                            }
                            break;
                        case 2:
                            if (col.equals(strs[i])) {
                                result[i] = pName;
                                finished = true;
                            }
                            break;
                        case 3:
                            list.add(col);
                            break;
                        case 4:
                            list.add(pName);
                    }
                }
            }
        } catch (Throwable e) {
        }
    } else {
        try {
            ReflectUtil ru = new ReflectUtil(clazz);
            // String idColumn = getIdColumn(clazz);
            for (int i = 0; i < strs.length; i++) {
                boolean finished = false;
                List<Property> properties = ormBean.getProperty();
                for (Property p : properties) {
                    if (finished)
                        break;
                    switch(type) {
                        case 1:
                            int dotIndex = strs[i].indexOf(".");
                            if (dotIndex > 0 && dotIndex < strs[i].length() - 1) {
                                String[] dots = strs[i].split("\\.");
                                StringBuilder builder = new StringBuilder();
                                Class<?> prevCls = null;
                                for (int j = 0; j < dots.length; j++) {
                                    String dot = dots[j];
                                    Field field = ru.getField(dot);
                                    if (field == null && dot.equals(clazz.getSimpleName().toLowerCase())) {
                                        if (builder.length() > 0)
                                            builder.append(".");
                                        builder.append(dot);
                                        prevCls = clazz;
                                    } else if (field != null) {
                                        if (j == dots.length - 1) {
                                            if (builder.length() > 0)
                                                builder.append(".");
                                            builder.append(ORMConfigBeanUtil.getColumn(prevCls, dot));
                                        } else {
                                            Class<?> cls = ClassUtil.getGenericType(field);
                                            if (cls != null) {
                                                if (ORMConfigBeanCache.get(cls.getName()) != null) {
                                                    if (builder.length() > 0)
                                                        builder.append(".");
                                                    builder.append(cls.getSimpleName().toLowerCase());
                                                    prevCls = cls;
                                                    ru = new ReflectUtil(cls);
                                                }
                                            }
                                        }
                                    }
                                }
                                if (builder.length() > 0) {
                                    result[i] = builder.toString();
                                    finished = true;
                                    break;
                                }
                            }
                            if (p.getName().equals(strs[i])) {
                                result[i] = p.getColumn();
                                finished = true;
                            }
                            break;
                        case 2:
                            if (p.getColumn().equals(strs[i])) {
                                result[i] = p.getName();
                                finished = true;
                            }
                            break;
                        case 3:
                            list.add(p.getColumn());
                            break;
                        case 4:
                            list.add(p.getName());
                    }
                }
            }
        } catch (Exception e) {
        }
    }
    return list.isEmpty() ? result : list.toArray(new String[] {});
}
Also used : HashMap(java.util.HashMap) ORMConfigBean(org.eweb4j.orm.config.bean.ORMConfigBean) ArrayList(java.util.ArrayList) InvocationTargetException(java.lang.reflect.InvocationTargetException) ReflectUtil(org.eweb4j.util.ReflectUtil) Field(java.lang.reflect.Field) JoinColumn(javax.persistence.JoinColumn) JoinColumn(javax.persistence.JoinColumn) Column(javax.persistence.Column) HashMap(java.util.HashMap) Map(java.util.Map) Property(org.eweb4j.orm.config.bean.Property)

Example 4 with ORMConfigBean

use of org.eweb4j.orm.config.bean.ORMConfigBean in project eweb4j-framework by laiweiwei.

the class ORMConfigBeanUtil method getToOneField.

public static String[] getToOneField(Class<?> clazz) {
    ORMConfigBean ormBean = ORMConfigBeanCache.get(clazz.getName());
    if (ormBean == null)
        return null;
    List<String> list = new ArrayList<String>();
    for (Property p : ormBean.getProperty()) {
        if (PropType.ONE_ONE.equals(p.getType()) || PropType.MANY_ONE.equals(p.getType()))
            list.add(p.getName());
    }
    return list.toArray(new String[] {});
}
Also used : ORMConfigBean(org.eweb4j.orm.config.bean.ORMConfigBean) ArrayList(java.util.ArrayList) Property(org.eweb4j.orm.config.bean.Property)

Example 5 with ORMConfigBean

use of org.eweb4j.orm.config.bean.ORMConfigBean in project eweb4j-framework by laiweiwei.

the class ORMConfigBeanUtil method getTable.

/**
	 * get table name
	 * 
	 * @param clazz
	 * @return
	 */
public static <T> String getTable(T t, boolean isAlias) {
    String table = null;
    Class<?> clazz;
    if (t instanceof Class) {
        clazz = (Class<?>) t;
    } else {
        clazz = t.getClass();
    }
    if (!(t instanceof Class) && Map.class.isAssignableFrom(clazz)) {
        HashMap<String, Object> map = (HashMap<String, Object>) t;
        table = (String) map.get("table");
        if (isAlias)
            return table + " map";
        return table;
    }
    ORMConfigBean ormBean = ORMConfigBeanCache.get(clazz.getName());
    table = ormBean == null ? clazz.getSimpleName() : ormBean.getTable();
    if (isAlias)
        return table + " " + clazz.getSimpleName().toLowerCase();
    return table;
}
Also used : HashMap(java.util.HashMap) ORMConfigBean(org.eweb4j.orm.config.bean.ORMConfigBean) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

ORMConfigBean (org.eweb4j.orm.config.bean.ORMConfigBean)16 Property (org.eweb4j.orm.config.bean.Property)11 Field (java.lang.reflect.Field)6 ArrayList (java.util.ArrayList)6 ConfigBean (org.eweb4j.config.bean.ConfigBean)5 ReflectUtil (org.eweb4j.util.ReflectUtil)5 File (java.io.File)4 Method (java.lang.reflect.Method)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Entry (java.util.Map.Entry)3 JoinColumn (javax.persistence.JoinColumn)3 FileWriter (java.io.FileWriter)2 IOException (java.io.IOException)2 Column (javax.persistence.Column)2 Entity (javax.persistence.Entity)2 JoinTable (javax.persistence.JoinTable)2 ManyToMany (javax.persistence.ManyToMany)2 Table (javax.persistence.Table)2 LogConfigBean (org.eweb4j.config.bean.LogConfigBean)2