use of org.eweb4j.util.ReflectUtil in project eweb4j-framework by laiweiwei.
the class InsertSqlCreator method create.
public Sql[] create(String condition) throws SqlCreateException {
Sql[] sqls = new Sql[ts.length];
for (int index = 0; index < ts.length; ++index) {
String table = null;
StringBuilder columnSB = new StringBuilder();
StringBuilder valueSB = new StringBuilder();
T t = ts[index];
Class<?> clazz = t.getClass();
String[] fields;
String[] columns;
String idColumn;
Object[] values = null;
sqls[index] = new Sql();
HashMap<String, Object> map = null;
if (Map.class.isAssignableFrom(clazz)) {
map = (HashMap<String, Object>) t;
table = String.valueOf(map.get("table"));
idColumn = String.valueOf(map.get("idColumn"));
if (idColumn == null)
idColumn = "id";
fields = (String[]) map.get("columns");
columns = fields;
values = (Object[]) map.get("values");
} else {
table = ORMConfigBeanUtil.getTable(clazz, false);
idColumn = ORMConfigBeanUtil.getIdColumn(t.getClass());
fields = ORMConfigBeanUtil.getFields(clazz);
columns = ORMConfigBeanUtil.getColumns(clazz);
}
ReflectUtil ru = new ReflectUtil(t);
for (int i = 0; i < columns.length; i++) {
String column = columns[i];
Object value = null;
if (map != null && values != null) {
value = values[i];
} else {
String name = fields[i];
Method getter = ru.getGetter(name);
if (getter == null)
continue;
Object _value = null;
try {
_value = getter.invoke(t);
if (_value == null)
continue;
if (ClassUtil.isPojo(_value.getClass())) {
Field f = ru.getField(name);
OneToOne oneAnn = getter.getAnnotation(OneToOne.class);
if (oneAnn == null)
oneAnn = f.getAnnotation(OneToOne.class);
ManyToOne manyToOneAnn = null;
if (oneAnn == null) {
manyToOneAnn = getter.getAnnotation(ManyToOne.class);
if (manyToOneAnn == null)
manyToOneAnn = f.getAnnotation(ManyToOne.class);
}
if (oneAnn != null || manyToOneAnn != null) {
JoinColumn joinColAnn = getter.getAnnotation(JoinColumn.class);
if (joinColAnn == null)
joinColAnn = f.getAnnotation(JoinColumn.class);
if (joinColAnn != null && joinColAnn.referencedColumnName().trim().length() > 0) {
String refCol = joinColAnn.referencedColumnName();
String refField = ORMConfigBeanUtil.getField(_value.getClass(), refCol);
ReflectUtil tarRu = new ReflectUtil(_value);
Method tarFKGetter = tarRu.getGetter(refField);
value = tarFKGetter.invoke(_value);
} else {
ReflectUtil tarRu = new ReflectUtil(_value);
String tarFKField = ORMConfigBeanUtil.getIdField(_value.getClass());
if (tarFKField != null) {
Method tarFKGetter = tarRu.getGetter(tarFKField);
value = tarFKGetter.invoke(_value);
}
}
}
if (value == null)
continue;
} else {
value = _value;
}
} catch (Exception e) {
throw new SqlCreateException(getter + " invoke exception " + e.toString(), e);
}
}
// id 字段不允许插入表中
if (idColumn != null && idColumn.equalsIgnoreCase(column))
continue;
if (columnSB.length() > 0)
columnSB.append(",");
columnSB.append(column);
if (valueSB.length() > 0)
valueSB.append(",");
// valueSB.append("'").append(value).append("'");
valueSB.append("?");
sqls[index].args.add(value);
}
String format = "INSERT INTO ${table}(${columns}) VALUES(${values}) ${condition} ;";
format = format.replace("${table}", table);
format = format.replace("${columns}", columnSB.toString());
format = format.replace("${values}", valueSB.toString());
if (condition != null)
format = format.replace("${condition}", " WHERE " + condition);
else
format = format.replace("${condition} ", "");
sqls[index].sql = format;
}
return sqls;
}
use of org.eweb4j.util.ReflectUtil in project eweb4j-framework by laiweiwei.
the class InsertSqlCreator method createByFieldsIsValues.
public Sql[] createByFieldsIsValues(String[][] fields, Object[][] values) throws SqlCreateException {
Sql[] sqls = new Sql[ts.length];
for (int i = 0; i < ts.length; ++i) {
T t = ts[i];
ReflectUtil ru = new ReflectUtil(t);
Class<?> clazz = t.getClass();
String table = ORMConfigBeanUtil.getTable(clazz, false);
StringBuilder columnSB = new StringBuilder();
StringBuilder valueSB = new StringBuilder();
sqls[i] = new Sql();
for (int j = 0; j < fields[i].length; ++j) {
String name = fields[i][j];
Object _value = null;
Object value = null;
if (values == null) {
Method getter = null;
try {
getter = ru.getGetter(name);
if (getter == null)
continue;
_value = getter.invoke(t);
if (_value == null)
continue;
if (ClassUtil.isPojo(_value.getClass())) {
Field f = ru.getField(fields[i][j]);
OneToOne oneAnn = getter.getAnnotation(OneToOne.class);
if (oneAnn == null)
oneAnn = f.getAnnotation(OneToOne.class);
ManyToOne manyToOneAnn = null;
if (oneAnn == null) {
manyToOneAnn = getter.getAnnotation(ManyToOne.class);
if (manyToOneAnn == null)
manyToOneAnn = f.getAnnotation(ManyToOne.class);
}
if (oneAnn != null || manyToOneAnn != null) {
JoinColumn joinColAnn = getter.getAnnotation(JoinColumn.class);
if (joinColAnn == null)
joinColAnn = f.getAnnotation(JoinColumn.class);
if (joinColAnn != null && joinColAnn.referencedColumnName().trim().length() > 0) {
String refCol = joinColAnn.referencedColumnName();
String refField = ORMConfigBeanUtil.getField(_value.getClass(), refCol);
ReflectUtil tarRu = new ReflectUtil(_value);
Method tarFKGetter = tarRu.getGetter(refField);
value = tarFKGetter.invoke(_value);
} else {
ReflectUtil tarRu = new ReflectUtil(_value);
String tarFKField = ORMConfigBeanUtil.getIdField(_value.getClass());
if (tarFKField != null) {
Method tarFKGetter = tarRu.getGetter(tarFKField);
value = tarFKGetter.invoke(_value);
}
}
}
if (value == null)
continue;
} else {
value = _value;
}
} catch (Exception e) {
throw new SqlCreateException(getter + " invoke exception " + e.toString(), e);
}
} else {
value = values[i][j];
}
String column = ORMConfigBeanUtil.getColumn(clazz, name);
if (valueSB.length() > 0) {
columnSB.append(",");
valueSB.append(",");
}
columnSB.append(column);
// valueSB.append("'").append(value).append("'");
valueSB.append("?");
sqls[i].args.add(value);
}
sqls[i].sql = String.format("INSERT INTO %s(%s) VALUES(%s) ;", table, columnSB.toString(), valueSB.toString());
}
return sqls;
}
use of org.eweb4j.util.ReflectUtil in project eweb4j-framework by laiweiwei.
the class Model2Table method generateOne.
public static String generateOne(final Class<?> entityClass, final boolean isDrop, boolean isCreateFile) {
StringBuilder sql = new StringBuilder();
StringBuilder manyMany = new StringBuilder();
final ORMConfigBean ocb = ORMConfigBeanCache.get(entityClass.getName());
final String table = ocb.getTable();
List<Property> properties = ocb.getProperty();
StringBuilder sb = new StringBuilder();
StringBuilder fkSb = new StringBuilder();
final String fk = ", \n\tKEY %s (%s), \n\tCONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s (%s)";
StringBuilder pkSb = new StringBuilder();
final String pk = ", \n\tPRIMARY KEY (%s)";
StringBuilder uniqueSb = new StringBuilder();
String unique = ", \n\tUNIQUE KEY %s (%s)";
final String idCol = ORMConfigBeanUtil.getIdColumn(properties);
for (Property p : properties) {
if (sb.length() > 0)
sb.append(", \n");
String col = p.getColumn();
String type = p.getType();
String size = null;
if (p.getSize().trim().length() > 0 && CommonUtil.isNumeric(p.getSize().trim()))
size = String.format(" (%s) ", p.getSize());
if ("1".equals(p.getUnique()) || "true".equalsIgnoreCase(p.getUnique()))
uniqueSb.append(String.format(unique, col, col));
String notNull = "";
if ("1".equals(p.getNotNull()) || "true".equalsIgnoreCase(p.getNotNull()))
notNull = " NOT NULL ";
if ("1".equals(p.getPk()) || "true".equalsIgnoreCase(p.getPk())) {
if (pkSb.length() > 0)
pkSb.append(",");
pkSb.append(col);
}
String auto = "";
if ("1".equals(p.getAutoIncrement()) || "true".equalsIgnoreCase(p.getAutoIncrement()))
auto = " AUTO_INCREMENT ";
if (PropType.ONE_ONE.equals(p.getType()) || PropType.MANY_ONE.equals(p.getType())) {
final String relTable = ORMConfigBeanUtil.getTable(p.getRelClass(), false);
if (p.getRelProperty() == null || p.getRelProperty().trim().length() == 0)
p.setRelProperty(ORMConfigBeanUtil.getIdField(p.getRelClass()));
final String relColumn = ORMConfigBeanUtil.getColumn(p.getRelClass(), p.getRelProperty());
fkSb.append(String.format(fk, col, col, table + "_" + col, col, relTable, relColumn));
}
sb.append("\t").append(col).append(" ").append(getType(type)).append(size == null ? "" : size).append(notNull).append(auto);
}
sb.append(uniqueSb.toString());
if (pkSb.length() > 0)
sb.append(String.format(pk, pkSb.toString()));
sb.append(fkSb.toString());
String _sql = String.format(create_table_script, table, sb.toString());
if (isDrop) {
_sql = String.format(drop_table_script, table) + _sql;
;
}
sql.append(_sql);
try {
ReflectUtil ru = new ReflectUtil(entityClass);
for (Field f : ru.getFields()) {
if (!ClassUtil.isListClass(f))
continue;
String name = f.getName();
Method getter = ru.getGetter(name);
if (getter == null)
continue;
ManyToMany mmAnn = getter.getAnnotation(ManyToMany.class);
if (mmAnn == null)
mmAnn = f.getAnnotation(ManyToMany.class);
if (mmAnn != null) {
JoinTable join = getter.getAnnotation(JoinTable.class);
if (join == null) {
join = f.getAnnotation(JoinTable.class);
if (join == null)
continue;
}
JoinColumn[] froms = join.joinColumns();
if (froms == null || froms.length == 0)
continue;
JoinColumn[] tos = join.inverseJoinColumns();
if (tos == null || tos.length == 0)
continue;
final String relTable = join.name();
final String relFrom = froms[0].name();
final String relTo = tos[0].name();
final Class<?> targetClass = ClassUtil.getGenericType(f);
StringBuilder manyManyField = new StringBuilder();
//handle the many to many
manyManyField.append("\tid ").append(getType("long")).append(" (20) NOT NULL AUTO_INCREMENT,");
manyManyField.append("\n\t").append(relFrom).append(" ").append(getType("long")).append(" (20) ,");
manyManyField.append("\n\t").append(relTo).append(" ").append(getType("long")).append(" (20) ,");
manyManyField.append("\n\t").append("PRIMARY KEY (id)");
String tarTable = ORMConfigBeanUtil.getTable(targetClass, false);
String tarIdCol = ORMConfigBeanUtil.getIdColumn(targetClass);
String fk1 = String.format(fk, relFrom, relFrom, relTable + "_" + relFrom, relFrom, table, idCol);
manyManyField.append(fk1);
String fk2 = String.format(fk, relTo, relTo, relTable + "_" + relTo, relTo, tarTable, tarIdCol);
manyManyField.append(fk2);
manyMany.append(String.format(create_table_script, relTable, relTable, manyManyField.toString()));
}
}
} catch (Exception e1) {
e1.printStackTrace();
}
sql.append(manyMany.toString());
String script = String.format("SET FOREIGN_KEY_CHECKS=0;\n%s", sql.toString());
if (!isCreateFile)
return script;
try {
ConfigBean cb = (ConfigBean) SingleBeanCache.get(ConfigBean.class.getName());
File file = new File(ConfigConstant.CONFIG_BASE_PATH() + cb.getOrm().getDdl().getDs() + "-create.sql");
FileWriter writer = new FileWriter(file);
writer.write(script);
writer.flush();
writer.close();
LogFactory.getORMLogger(Model2Table.class).debug("create models sql script file success -> " + file.getAbsoluteFile());
return script;
} catch (IOException e2) {
e2.printStackTrace();
return null;
}
}
use of org.eweb4j.util.ReflectUtil in project eweb4j-framework by laiweiwei.
the class Model2Table method generateAll.
public static String generateAll(final boolean isDrop, final boolean isCreateFile) {
StringBuilder sql = new StringBuilder();
StringBuilder manyMany = new StringBuilder();
for (Iterator<Entry<Object, ORMConfigBean>> it = ORMConfigBeanCache.entrySet().iterator(); it.hasNext(); ) {
final Entry<Object, ORMConfigBean> e = it.next();
final ORMConfigBean ocb = e.getValue();
final String table = ocb.getTable();
List<Property> properties = ocb.getProperty();
StringBuilder sb = new StringBuilder();
StringBuilder fkSb = new StringBuilder();
final String fk = ", \n\tKEY %s (%s), \n\tCONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s (%s)";
StringBuilder pkSb = new StringBuilder();
final String pk = ", \n\tPRIMARY KEY (%s)";
StringBuilder uniqueSb = new StringBuilder();
String unique = ", \n\tUNIQUE KEY %s (%s)";
final String idCol = ORMConfigBeanUtil.getIdColumn(properties);
for (Property p : properties) {
if (sb.length() > 0)
sb.append(", \n");
String col = p.getColumn();
String type = p.getType();
String size = null;
if (p.getSize().trim().length() > 0 && CommonUtil.isNumeric(p.getSize().trim()))
size = String.format(" (%s) ", p.getSize());
if ("1".equals(p.getUnique()) || "true".equalsIgnoreCase(p.getUnique()))
uniqueSb.append(String.format(unique, col, col));
String notNull = "";
if ("1".equals(p.getNotNull()) || "true".equalsIgnoreCase(p.getNotNull()))
notNull = " NOT NULL ";
if ("1".equals(p.getPk()) || "true".equalsIgnoreCase(p.getPk())) {
if (pkSb.length() > 0)
pkSb.append(",");
pkSb.append(col);
}
String auto = "";
if ("1".equals(p.getAutoIncrement()) || "true".equalsIgnoreCase(p.getAutoIncrement()))
auto = " AUTO_INCREMENT ";
if (PropType.ONE_ONE.equals(p.getType()) || PropType.MANY_ONE.equals(p.getType())) {
final String relTable = ORMConfigBeanUtil.getTable(p.getRelClass(), false);
if (p.getRelProperty() == null || p.getRelProperty().trim().length() == 0)
p.setRelProperty(ORMConfigBeanUtil.getIdField(p.getRelClass()));
final String relColumn = ORMConfigBeanUtil.getColumn(p.getRelClass(), p.getRelProperty());
fkSb.append(String.format(fk, col, col, table + "_" + col, col, relTable, relColumn));
}
sb.append("\t").append(col).append(" ").append(getType(type)).append(size == null ? "" : size).append(notNull).append(auto);
}
sb.append(uniqueSb.toString());
if (pkSb.length() > 0)
sb.append(String.format(pk, pkSb.toString()));
sb.append(fkSb.toString());
String _sql = String.format(create_table_script, table, sb.toString());
if (isDrop) {
_sql = String.format(drop_table_script, table) + _sql;
;
}
sql.append(_sql);
try {
Class<?> clazz = Thread.currentThread().getContextClassLoader().loadClass((String) e.getKey());
ReflectUtil ru = new ReflectUtil(clazz);
for (Field f : ru.getFields()) {
if (!ClassUtil.isListClass(f))
continue;
String name = f.getName();
Method getter = ru.getGetter(name);
if (getter == null)
continue;
ManyToMany mmAnn = getter.getAnnotation(ManyToMany.class);
if (mmAnn == null)
mmAnn = f.getAnnotation(ManyToMany.class);
if (mmAnn != null) {
JoinTable join = getter.getAnnotation(JoinTable.class);
if (join == null) {
join = f.getAnnotation(JoinTable.class);
if (join == null)
continue;
}
JoinColumn[] froms = join.joinColumns();
if (froms == null || froms.length == 0)
continue;
JoinColumn[] tos = join.inverseJoinColumns();
if (tos == null || tos.length == 0)
continue;
final String relTable = join.name();
final String relFrom = froms[0].name();
final String relTo = tos[0].name();
final Class<?> targetClass = ClassUtil.getGenericType(f);
StringBuilder manyManyField = new StringBuilder();
//handle the many to many
manyManyField.append("\tid ").append(getType("long")).append(" (20) NOT NULL AUTO_INCREMENT,");
manyManyField.append("\n\t").append(relFrom).append(" ").append(getType("long")).append(" (20) ,");
manyManyField.append("\n\t").append(relTo).append(" ").append(getType("long")).append(" (20) ,");
manyManyField.append("\n\t").append("PRIMARY KEY (id)");
String tarTable = ORMConfigBeanUtil.getTable(targetClass, false);
String tarIdCol = ORMConfigBeanUtil.getIdColumn(targetClass);
String fk1 = String.format(fk, relFrom, relFrom, relTable + "_" + relFrom, relFrom, table, idCol);
manyManyField.append(fk1);
String fk2 = String.format(fk, relTo, relTo, relTable + "_" + relTo, relTo, tarTable, tarIdCol);
manyManyField.append(fk2);
manyMany.append(String.format(create_table_script, relTable, relTable, manyManyField.toString()));
}
}
} catch (Exception e1) {
continue;
}
}
sql.append(manyMany.toString());
String script = String.format("SET FOREIGN_KEY_CHECKS=0;\n%s", sql.toString());
if (!isCreateFile)
return script;
try {
ConfigBean cb = (ConfigBean) SingleBeanCache.get(ConfigBean.class.getName());
File file = new File(ConfigConstant.CONFIG_BASE_PATH() + cb.getOrm().getDdl().getDs() + "-create.sql");
FileWriter writer = new FileWriter(file);
writer.write(script);
writer.flush();
writer.close();
LogFactory.getORMLogger(Model2Table.class).debug("create models sql script file success -> " + file.getAbsoluteFile());
return script;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
use of org.eweb4j.util.ReflectUtil in project eweb4j-framework by laiweiwei.
the class SelectSqlCreator method nextOrPre.
public String nextOrPre(int num, boolean isNext) throws SqlCreateException {
Method m = new ReflectUtil(t).getGetter(idField);
if (m == null)
throw new SqlCreateException("can not fine id getter");
String idVal = "";
try {
idVal = String.valueOf(m.invoke(t));
} catch (Exception e) {
throw new SqlCreateException(m + " invoke exception " + e.toString());
}
return nextOrPre(null, Integer.parseInt(idVal), num, isNext);
}
Aggregations