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;
}
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;
}
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[] {});
}
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[] {});
}
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;
}
Aggregations