Commit 8ddd11c1 authored by yanzg's avatar yanzg

常规BUG的修改

parent 7758a4eb
......@@ -102,6 +102,62 @@ public class DaoConst {
* 加载SQL语句模板
*/
public static final String LOAD_MODEL = "SELECT a.*{FIELD} FROM {TABLE} AS a {INNER} WHERE 1=1 {WHERE}";
/**
* 更新字段模板
*/
public static final String CODE_UPDATE_FIELD = "a.%s=%s";
/**
* 更新字段模板主键
*/
public static final String CODE_UPDATE_PRIMARY = "a.%s=a.%s";
/**
* 更新字段模板参数
*/
public static final String CODE_UPDATE_FIELD_PARA = "a.%s=?";
/**
* 更新字段模板参数
*/
public static final String CODE_UPDATE_FIELD_REMOVE = "a.%s=1";
/**
* 等于条件
*/
public static final String CODE_WHERE_EQUALS = "AND %s%s=%s";
/**
* 等于条件参数
*/
public static final String CODE_WHERE_EQUALS_PARA = "AND a.%s=?";
/**
* 未删除条件
*/
public static final String CODE_WHERE_EQUALS_NOT_REMOVE = "AND a.%s=0";
/**
* 版本好字段
*/
public static final String CODE_UPDATE_VERSION_FIELD = ",a.%s=1+a.%s";
/**
* 不等于条件
*/
public static final String CODE_WHERE_NOT_EQUALS = "AND %s%s<>%s";
/**
* 不等于条件
*/
public static final String CODE_WHERE_NOT_EQUALS_PARA = "AND a.%s<>?";
/**
* 默认表标记
*/
public static final String CODE_TAG = "a.";
/**
* 参数标记
*/
public static final String CODE_PARA = "?";
/**
* 增加修改语句的分割符号
*/
public static final String CODE_SPLIT = ",";
/**
* 统计语句增加字段模板
*/
public static final String CODE_GROUP_ADD = "{FIELD}={FIELD}+?";
/**
* 版本号字段名称
*/
......
......@@ -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;
......@@ -160,14 +161,39 @@ public class TableStruct {
/**
* 获取某个类型的所有字段
*
* @param type 某个类型
* @return
* @param type 某个类型,需要排除的类型
* @param exceptType 需要排除的字段的类型
* @return 字段列表
*/
private List<TableFieldVo> getFieldActionList(int type) {
private List<TableFieldVo> getFieldActionList(int type, int... exceptType) {
if (!typeFieldCache.containsKey(type)) {
typeFieldCache.put(type, new ArrayList<>());
}
return typeFieldCache.get(type);
List<TableFieldVo> from = typeFieldCache.get(type);
if (exceptType == null || exceptType.length == 0) {
return from;
} else {
// 缓存需要排除的子弹
Map<TableFieldVo, Boolean> exceptCache = new HashMap<>(10);
for (int except : exceptType) {
if (!typeFieldCache.containsKey(except)) {
continue;
}
List<TableFieldVo> exceptList = typeFieldCache.get(except);
for (TableFieldVo exceptField : exceptList) {
exceptCache.put(exceptField, true);
}
}
// 剩下的字段
List<TableFieldVo> to = new ArrayList<>();
for (TableFieldVo fromItem : from) {
if (exceptCache.containsKey(fromItem)) {
continue;
}
to.add(fromItem);
}
return to;
}
}
/**
......@@ -322,14 +348,19 @@ public class TableStruct {
String text = DaoConst.INSERT_MODEL.replace(DaoConst.TABLE_CODE, this.name);
SqlData sql = new SqlData(DaoConst.CREATE, text);
sql.setSqlType(DaoConst.SQL_TYPE_CREATE);
String flag = "";
// 第一个增加的字段,不需要增加 ","
String flag = StringHelper.EMPTY;
if (this.getKeyType() == String.class) {
sql.addParaConst(this.getKey().inputName, DaoConst.FIELD_CODE, this.getKey().name, DaoConst.VALUES_CODE, "?");
flag = ",";
sql.addParaConst(this.getKey().inputName, DaoConst.FIELD_CODE, this.getKey().name,
DaoConst.VALUES_CODE, DaoConst.CODE_PARA
);
flag = DaoConst.CODE_SPLIT;
}
for (TableFieldVo field : this.getFields()) {
sql.addParaConst(field.inputName, DaoConst.FIELD_CODE, flag + field.name, DaoConst.VALUES_CODE, flag + "?");
flag = ",";
sql.addParaConst(field.inputName, DaoConst.FIELD_CODE, flag + field.name,
DaoConst.VALUES_CODE, flag + DaoConst.CODE_PARA
);
flag = DaoConst.CODE_SPLIT;
}
return sql;
}
......@@ -345,35 +376,25 @@ public class TableStruct {
SqlData sql = new SqlData(DaoConst.UPDATE, text);
sql.setSqlType(DaoConst.SQL_TYPE_UPDATE);
// 特殊字段操作
TableFieldVo removeField = this.getRemove();
TableFieldVo versionField = this.getVersion();
// 主键字段操作
sql.addCode(DaoConst.FIELD_CODE, String.format(DaoConst.CODE_UPDATE_PRIMARY, this.getKey().name, this.getKey().name));
sql.addParaConst(this.getKey().inputName,
DaoConst.FIELD_CODE, "a." + this.getKey().name + "=a." + this.getKey().name,
DaoConst.WHERE_CODE, " AND a." + this.getKey().name + "=?");
DaoConst.FIELD_CODE, String.format(DaoConst.CODE_UPDATE_FIELD_PARA, this.getKey().name)
);
// 删除时更新字段
List<TableFieldVo> createFields = getFieldActionList(DaoConst.FIELD_CREATE);
Map<TableFieldVo, Boolean> mapCreate = new HashMap<>(createFields.size());
for (TableFieldVo field : createFields) {
mapCreate.put(field, true);
}
// 增加普通代码片段字段
for (TableFieldVo field : this.getFields()) {
if (field == removeField || field == versionField || mapCreate.containsKey(field)) {
continue;
}
sql.addParaConst(field.inputName, DaoConst.FIELD_CODE, ",a." + field.name + "=?");
for (TableFieldVo field : getFieldActionList(DaoConst.FIELD_COMMON,
DaoConst.FIELD_CREATE, DaoConst.FIELD_REMOVE, DaoConst.FIELD_VERSION)) {
sql.addParaConst(field.inputName,
DaoConst.FIELD_CODE, String.format(DaoConst.CODE_UPDATE_FIELD_PARA, field.name)
);
}
// 添加删除字段
if (removeField != null) {
sql.addParaConst(removeField.inputName, DaoConst.WHERE_CODE, " AND a." + removeField.name + "=0");
}
// 添加版本字段
if (versionField != null) {
sql.addParaConst(versionField.inputName,
DaoConst.FIELD_CODE, ",a." + versionField.name + "=1+a." + versionField.name,
DaoConst.WHERE_CODE, " AND a." + versionField.name + "=?");
addWhereRemove(sql);
addUpdateVersion(sql);
// 添加版本号条件
if (getVersion() != null) {
sql.addConst(getVersion().inputName, String.format(DaoConst.CODE_WHERE_EQUALS_PARA, getVersion().name));
}
return sql;
}
......@@ -391,19 +412,26 @@ public class TableStruct {
String text = DaoConst.UPDATE_MODEL.replace(DaoConst.TABLE_CODE, this.name);
SqlData sql = new SqlData(DaoConst.REMOVE, text);
sql.setSqlType(DaoConst.SQL_TYPE_REMOVE);
sql.addCode(DaoConst.FIELD_CODE, "a." + removeField.name + "=1");
// 设置删除字段标记
sql.addCode(DaoConst.FIELD_CODE, String.format(DaoConst.CODE_UPDATE_FIELD_REMOVE, removeField.name));
// 设置删除时需要修改的字段的值
for (TableFieldVo field : this.getFieldActionList(DaoConst.FIELD_REMOVE_UPDATE)) {
sql.addParaConst(field.inputName, DaoConst.FIELD_CODE, ",a." + field.name + "=?");
}
if (versionField != null) {
sql.addCode(DaoConst.FIELD_CODE, ",a." + versionField.name + "=1+a." + versionField.name);
}
addWhereField(sql, "a.", true);
sql.addParaConst(field.inputName,
DaoConst.FIELD_CODE, String.format(DaoConst.CODE_UPDATE_FIELD_PARA, field.name)
);
}
// 增加版本号字段的值
addUpdateVersion(sql);
// 生成逻辑删除WHERE条件
addWhereField(sql, DaoConst.CODE_TAG, true);
return sql;
} else {
String text = DaoConst.REMOVE_MODEL.replace(DaoConst.TABLE_CODE, this.name);
SqlData sql = new SqlData(DaoConst.REMOVE, text);
addWhereField(sql, "a.", true);
// 生成删除语句Where条件
addWhereField(sql, DaoConst.CODE_TAG, true);
return sql;
}
}
......@@ -418,7 +446,8 @@ public class TableStruct {
String text = DaoConst.LOAD_MODEL.replace(DaoConst.TABLE_CODE, this.name);
SqlData sql = new SqlData(DaoConst.LOAD, text);
sql.setSqlType(DaoConst.SQL_TYPE_LOAD);
addWhereField(sql, "a.", false);
// 生成加载语句的WHERE条件
addWhereField(sql, DaoConst.CODE_TAG, false);
return sql;
}
......@@ -428,26 +457,52 @@ public class TableStruct {
* @param sql
*/
private void addWhereField(SqlData sql, String tag, boolean isRemove) {
TableFieldVo removeField = this.getRemove();
sql.add(this.getKey().inputName, " AND " + tag + this.getKey().name + "=?");
// 删除时更新字段
List<TableFieldVo> removeUpdate = this.getRemoveUpdate();
Map<TableFieldVo, Boolean> updateField = new HashMap<>(removeUpdate.size());
if (isRemove) {
for (TableFieldVo field : removeUpdate) {
updateField.put(field, true);
sql.add(this.getKey().inputName,
String.format(DaoConst.CODE_WHERE_EQUALS, tag, this.getKey().name, DaoConst.CODE_PARA)
);
// Where条件包含的字段
List<TableFieldVo> fields;
if (!isRemove) {
// 所有字段
fields = this.getFields();
} else {
// 非删除时需要更新的字段
fields = this.getFieldActionList(DaoConst.FIELD_COMMON, DaoConst.FIELD_REMOVE_UPDATE);
}
// 添加普通的Where条件
for (TableFieldVo field : fields) {
sql.add(field.inputName,
String.format(DaoConst.CODE_WHERE_EQUALS, tag, field.name, DaoConst.CODE_PARA)
);
}
// 查询时,不能查询到非删除的字段
addWhereRemove(sql);
}
for (TableFieldVo field : this.getFields()) {
if (updateField.containsKey(field)) {
continue;
/**
* 添加删除WHERE条件字段
*
* @param sql
*/
private void addWhereRemove(SqlData sql) {
if (getRemove() != null) {
sql.addParaConst(DaoConst.REMOVE_FLAG_INPUT,
DaoConst.WHERE_CODE,
String.format(DaoConst.CODE_WHERE_EQUALS_NOT_REMOVE, this.getRemove().name)
);
}
sql.add(field.inputName, " AND " + tag + field.name + "=?");
}
if (removeField != null) {
sql.addConst(DaoConst.REMOVE_FLAG_INPUT, " AND " + tag + removeField.name + "=0");
/**
* 版本号进行累加
*
* @param sql 需要处理的SQL语句
*/
private void addUpdateVersion(SqlData sql) {
// 添加版本字段
if (getVersion() != null) {
sql.addCode(DaoConst.FIELD_CODE, String.format(DaoConst.CODE_UPDATE_VERSION_FIELD, getVersion().name, getVersion().name));
}
}
......@@ -461,7 +516,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.getKey().name)));
DaoConst.CODE_GROUP_ADD, updateFields,
new TableFieldString(this.getKey().name)));
}
/**
......@@ -473,7 +529,7 @@ public class TableStruct {
* @return
*/
public SqlData releaseSqlWhere(String name, String model, TableFieldString whereFields) {
return this.releaseSql(name, model, "", new TableFieldString(), whereFields);
return this.releaseSql(name, model, StringHelper.EMPTY, new TableFieldString(), whereFields);
}
/**
......@@ -511,7 +567,9 @@ public class TableStruct {
String[] fields = new String[paraFields.size()];
fields = paraFields.toArray(fields);
// 生成修改的SQL语句
String sql = model.replace(DaoConst.TABLE_CODE, this.name).replace(DaoConst.FIELD_CODE, sbField.toString()).replace(DaoConst.WHERE_CODE, sbWhere.toString());
String sql = model.replace(DaoConst.TABLE_CODE, this.name)
.replace(DaoConst.FIELD_CODE, sbField.toString())
.replace(DaoConst.WHERE_CODE, sbWhere.toString());
SqlData ret = new SqlData(name, sql, fields);
return ret;
}
......@@ -538,15 +596,17 @@ public class TableStruct {
String text = DaoConst.LOAD_MODEL.replace(DaoConst.TABLE_CODE, this.name);
SqlData sql = new SqlData(sqlName, text);
sql.setSqlType(DaoConst.SQL_TYPE_EXINTS);
sql.addPara(this.getKey().inputName, DaoConst.WHERE_CODE, " AND a." + this.getKey().name + "<>?");
sql.addPara(this.getKey().inputName,
String.format(DaoConst.CODE_WHERE_NOT_EQUALS_PARA, this.getKey().name)
);
for (String fieldName : fields) {
TableFieldVo field = this.getField(fieldName);
sql.addParaConst(field.inputName, DaoConst.WHERE_CODE, " AND a." + field.name + "=?");
}
if (getRemove() != null) {
sql.addParaConst(DaoConst.REMOVE_FLAG_INPUT, DaoConst.WHERE_CODE, " AND a." + this.getRemove().name + "=0");
sql.addParaConst(field.inputName,
String.format(DaoConst.CODE_WHERE_EQUALS_PARA, field.name)
);
}
addWhereRemove(sql);
sqlTableData.add(sql);
}
}
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