Commit 46c74c22 authored by yanzg's avatar yanzg

常规BUG的修改

parent b7c1e8fc
......@@ -110,6 +110,10 @@ public class DaoConst {
* 删除字段名称
*/
public static final String REMOVE_FLAG = "remove";
/**
* MD5标记字段
*/
public static final String MD5_KEY_FLAG = "md5key";
/**
* 删除字符串长度变量
*/
......@@ -134,13 +138,17 @@ public class DaoConst {
/**
* 删除标记
*/
public static final int FIELD_REMOVE_FLAG = 1;
public static final int FIELD_REMOVE = 1;
/**
* 版本号字段
*/
public static final int FIELD_VERSION_FLAG = 2;
public static final int FIELD_VERSION = 2;
/**
* 主键字段
*/
public static final int FIELD_PRIMARY = 3;
/**
* MD5标记字段,用于统计字段
*/
public static final int FIELD_MD5 = 4;
}
......@@ -6,6 +6,7 @@ import com.yanzuoguang.util.helper.StringHelper;
import com.yanzuoguang.util.base.MethodField;
import com.yanzuoguang.util.base.ObjectHelper;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
......@@ -24,50 +25,123 @@ public class TableStruct {
private String name;
/**
* 主键名称
* 缓存的字段
*/
private TableFieldVo key;
private Map<Integer, List<TableFieldVo>> mapFields = new HashMap<>();
/**
* MD5KEY
* 获取某个类型的所有字段
*
* @param type 某个类型
* @return
*/
private TableFieldVo md5Key;
private List<TableFieldVo> getFieldActionList(int type) {
if (!mapFields.containsKey(type)) {
mapFields.put(type, new ArrayList<>());
}
return mapFields.get(type);
}
/**
* 其他字段
* 获取某个类型的字段
*
* @param type 类型
* @return
*/
private List<TableFieldVo> fields = new ArrayList<TableFieldVo>();
private TableFieldVo getFieldAction(int type) {
List<TableFieldVo> list = getFieldActionList(type);
return list.size() > 0 ? list.get(0) : null;
}
/**
* 获取所有普通子弹
*
* @return 所有的普通股字段
*/
public List<TableFieldVo> getFields() {
return fields;
return getFieldActionList(DaoConst.FIELD_COMMON);
}
/**
* 获取表名
*
* @return
*/
public String getName() {
return name;
}
/**
* 设置表名
*
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* 主键
*
* @return
*/
public TableFieldVo getKey() {
return key;
return getFieldAction(DaoConst.FIELD_PRIMARY);
}
public TableFieldVo getMd5Key() {
return md5Key;
/**
* 主键名称
*
* @return
*/
public String getKeyName() {
return this.getKey().inputName;
}
public String getKeyName() {
return this.key.inputName;
/**
* 主键数据类型
*
* @return
*/
public Class<?> getKeyType() {
return this.getKey().type;
}
/**
* MD5缓存字段
*
* @return
*/
public TableFieldVo getMd5Key() {
return getFieldAction(DaoConst.FIELD_MD5);
}
/**
* MD5缓存键值
*
* @return
*/
public String getMD5KeyName() {
TableFieldVo md5Key = this.getMd5Key();
return md5Key != null ? md5Key.inputName : null;
}
public Class<?> getKeyType() {
return this.key.type;
/**
* 判断是否包含版本号字段
*
* @return
*/
public TableFieldVo getVersion() {
return getFieldAction(DaoConst.FIELD_VERSION);
}
/**
* 判断是否包含remove字段
*
* @return
*/
private TableFieldVo getRemove() {
return getFieldAction(DaoConst.FIELD_VERSION);
}
/**
......@@ -75,7 +149,6 @@ public class TableStruct {
*/
public TableStruct() {
this.name = "";
this.key = new TableFieldVo(DaoConst.ID_FIELD);
}
/**
......@@ -92,16 +165,28 @@ public class TableStruct {
// 遍历字段
for (Map.Entry<String, MethodField> entry : fields.entrySet()) {
// 字段信息获取
MethodField field = entry.getValue();
if (field.getField() == null && field.getGetMethod() == null) {
continue;
}
addMethodField(field);
}
}
/**
* 添加字段
*
* @param field 添加字段
*/
private void addMethodField(MethodField field) {
int fieldAction = DaoConst.FIELD_COMMON;
String fieldName = field.getName();
String fieldInputName = field.getName();
Class<?> fieldType = String.class;
TableAnnotation annotation = null;
if (field.getField() != null) {
annotation = field.getField().getAnnotation(TableAnnotation.class);
......@@ -110,53 +195,82 @@ public class TableStruct {
annotation = field.getGetMethod().getAnnotation(TableAnnotation.class);
fieldType = field.getGetMethod().getReturnType();
}
if (annotation != null) {
fieldName = annotation.value();
fieldAction = annotation.type();
}
TableFieldVo vo = new TableFieldVo(fieldName, fieldInputName, fieldType);
// 判断是否属于统计字段
if ("md5key".equals(vo.inputLName)) {
this.md5Key = vo;
}
// 判断是否是主键,获取主键数据类型
if (this.key == null) {
this.key = vo;
int stringAction = getStringAction(vo);
// 判断是否属于主键
List<TableFieldVo> commonActionList = this.getFieldActionList(DaoConst.FIELD_COMMON);
if (fieldAction == DaoConst.FIELD_PRIMARY || stringAction == DaoConst.FIELD_PRIMARY) {
// 移除历史主键,并且将历史主键添加到普通列
List<TableFieldVo> primaryActionList = this.getFieldActionList(DaoConst.FIELD_PRIMARY);
commonActionList.addAll(primaryActionList);
primaryActionList.clear();
// 将现有列添加到主键
primaryActionList.add(vo);
} else {
this.fields.add(vo);
}
}
}
public TableFieldVo getField(String name) {
if (StringHelper.isEmpty(name)) {
return null;
// 将所有非主键列添加到普通列
commonActionList.add(vo);
// 处理其他特殊列
if (fieldAction != DaoConst.FIELD_COMMON) {
// 假如特殊列已经存在,则将已经存在的特殊列删除,并且添加新的特殊列
List<TableFieldVo> actionList = this.getFieldActionList(fieldAction);
actionList.clear();
actionList.add(vo);
} else if (stringAction != DaoConst.FIELD_COMMON) {
// 假如是默认的,并且特殊列已经存在,则不进行任何处理
List<TableFieldVo> actionList = this.getFieldActionList(stringAction);
if (actionList.isEmpty()) {
actionList.add(vo);
}
for (TableFieldVo fieldVo : this.fields) {
if (fieldVo.lName.equals(name.toLowerCase()) || fieldVo.inputLName.equals(name.toLowerCase())) {
return fieldVo;
}
}
return null;
}
/**
* 判断是否包含版本号字段
* 获取字符串动作类型
*
* @param vo 输入子弹
* @return
*/
public TableFieldVo getVersion() {
return this.getField(DaoConst.VERSION_FLAG);
private int getStringAction(TableFieldVo vo) {
if (getKey() == null) {
return DaoConst.FIELD_PRIMARY;
} else if (DaoConst.REMOVE_FLAG.equals(vo.inputLName)) {
return DaoConst.FIELD_REMOVE;
} else if (DaoConst.VERSION_FLAG.equals(vo.inputLName)) {
return DaoConst.FIELD_VERSION;
}
// 判断是否属于统计字段
else if (DaoConst.MD5_KEY_FLAG.equals(vo.inputLName)) {
return DaoConst.FIELD_MD5;
} else {
return DaoConst.FIELD_COMMON;
}
}
/**
* 判断是否包含remove字段
* 获取某一列
*
* @param name 列名
* @return
*/
private TableFieldVo getRemove() {
return this.getField(DaoConst.REMOVE_FLAG);
public TableFieldVo getField(String name) {
if (StringHelper.isEmpty(name)) {
return null;
}
for (TableFieldVo fieldVo : this.getFields()) {
if (fieldVo.lName.equals(name.toLowerCase()) || fieldVo.inputLName.equals(name.toLowerCase())) {
return fieldVo;
}
}
return null;
}
/**
......@@ -182,10 +296,10 @@ public class TableStruct {
SqlData sql = new SqlData(DaoConst.CREATE, text);
String flag = "";
if (this.getKeyType() == String.class) {
sql.addParaConst(this.key.inputName, DaoConst.FIELD_CODE, this.key.name, DaoConst.VALUES_CODE, "?");
sql.addParaConst(this.getKey().inputName, DaoConst.FIELD_CODE, this.getKey().name, DaoConst.VALUES_CODE, "?");
flag = ",";
}
for (TableFieldVo field : this.fields) {
for (TableFieldVo field : this.getFields()) {
sql.addParaConst(field.inputName, DaoConst.FIELD_CODE, flag + field.name, DaoConst.VALUES_CODE, flag + "?");
flag = ",";
}
......@@ -203,10 +317,10 @@ public class TableStruct {
SqlData sql = new SqlData(DaoConst.UPDATE, text);
TableFieldVo removeField = this.getRemove();
TableFieldVo versionField = this.getVersion();
sql.addParaConst(this.key.inputName,
DaoConst.FIELD_CODE, "" + this.key.name + "=" + this.key.name,
DaoConst.WHERE_CODE, " AND " + this.key.name + "=?");
for (TableFieldVo field : this.fields) {
sql.addParaConst(this.getKey().inputName,
DaoConst.FIELD_CODE, "" + this.getKey().name + "=" + this.getKey().name,
DaoConst.WHERE_CODE, " AND " + this.getKey().name + "=?");
for (TableFieldVo field : this.getFields()) {
if (field == removeField || field == versionField) {
continue;
}
......@@ -269,8 +383,8 @@ public class TableStruct {
*/
private void addWhereField(SqlData sql, String tag) {
TableFieldVo removeField = this.getRemove();
sql.add(this.key.inputName, " AND " + tag + this.key.name + "=?");
for (TableFieldVo field : this.fields) {
sql.add(this.getKey().inputName, " AND " + tag + this.getKey().name + "=?");
for (TableFieldVo field : this.getFields()) {
sql.add(field.inputName, " AND " + tag + field.name + "=?");
}
if (removeField != null) {
......@@ -287,7 +401,8 @@ public class TableStruct {
*/
public void addGroupSql(TableSqlCache sqlTableData, TableFieldString whereFields, TableFieldString updateFields) {
sqlTableData.add(this.releaseSqlWhere(DaoConst.GROUP_QUERY, DaoConst.LOAD_MODEL, whereFields));
sqlTableData.add(this.releaseSql(DaoConst.GROUP_ADD, DaoConst.UPDATE_MODEL, "{FIELD}={FIELD}+?", updateFields, new TableFieldString(this.key.name)));
sqlTableData.add(this.releaseSql(DaoConst.GROUP_ADD, DaoConst.UPDATE_MODEL,
"{FIELD}={FIELD}+?", updateFields, new TableFieldString(this.getKey().name)));
}
/**
......@@ -353,7 +468,6 @@ public class TableStruct {
tableStruct.add(this.releaseSqlWhere(sqlName, DaoConst.LOAD_MODEL, whereFields));
}
/**
* 生成判断数据是否存在的SQL语句
*
......@@ -364,7 +478,7 @@ public class TableStruct {
public void addExist(TableSqlCache sqlTableData, String sqlName, String[] fields) {
String text = DaoConst.LOAD_MODEL.replace(DaoConst.TABLE_CODE, this.name);
SqlData sql = new SqlData(sqlName, text);
sql.addPara(this.key.inputName, DaoConst.WHERE_CODE, " AND a." + this.key.name + "<>?");
sql.addPara(this.getKey().inputName, DaoConst.WHERE_CODE, " AND a." + this.getKey().name + "<>?");
for (String field : fields) {
sql.addParaConst(field, DaoConst.WHERE_CODE, " AND a." + field + "=?");
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment