Commit 8ddd11c1 authored by yanzg's avatar yanzg

常规BUG的修改

parent 7758a4eb
...@@ -102,6 +102,62 @@ public class DaoConst { ...@@ -102,6 +102,62 @@ public class DaoConst {
* 加载SQL语句模板 * 加载SQL语句模板
*/ */
public static final String LOAD_MODEL = "SELECT a.*{FIELD} FROM {TABLE} AS a {INNER} WHERE 1=1 {WHERE}"; 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; ...@@ -6,6 +6,7 @@ import com.yanzuoguang.util.helper.StringHelper;
import com.yanzuoguang.util.base.MethodField; import com.yanzuoguang.util.base.MethodField;
import com.yanzuoguang.util.base.ObjectHelper; import com.yanzuoguang.util.base.ObjectHelper;
import java.lang.reflect.Field;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
...@@ -160,14 +161,39 @@ public class TableStruct { ...@@ -160,14 +161,39 @@ public class TableStruct {
/** /**
* 获取某个类型的所有字段 * 获取某个类型的所有字段
* *
* @param type 某个类型 * @param type 某个类型,需要排除的类型
* @return * @param exceptType 需要排除的字段的类型
* @return 字段列表
*/ */
private List<TableFieldVo> getFieldActionList(int type) { private List<TableFieldVo> getFieldActionList(int type, int... exceptType) {
if (!typeFieldCache.containsKey(type)) { if (!typeFieldCache.containsKey(type)) {
typeFieldCache.put(type, new ArrayList<>()); 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 { ...@@ -322,14 +348,19 @@ public class TableStruct {
String text = DaoConst.INSERT_MODEL.replace(DaoConst.TABLE_CODE, this.name); String text = DaoConst.INSERT_MODEL.replace(DaoConst.TABLE_CODE, this.name);
SqlData sql = new SqlData(DaoConst.CREATE, text); SqlData sql = new SqlData(DaoConst.CREATE, text);
sql.setSqlType(DaoConst.SQL_TYPE_CREATE); sql.setSqlType(DaoConst.SQL_TYPE_CREATE);
String flag = ""; // 第一个增加的字段,不需要增加 ","
String flag = StringHelper.EMPTY;
if (this.getKeyType() == String.class) { if (this.getKeyType() == String.class) {
sql.addParaConst(this.getKey().inputName, DaoConst.FIELD_CODE, this.getKey().name, DaoConst.VALUES_CODE, "?"); sql.addParaConst(this.getKey().inputName, DaoConst.FIELD_CODE, this.getKey().name,
flag = ","; DaoConst.VALUES_CODE, DaoConst.CODE_PARA
);
flag = DaoConst.CODE_SPLIT;
} }
for (TableFieldVo field : this.getFields()) { for (TableFieldVo field : this.getFields()) {
sql.addParaConst(field.inputName, DaoConst.FIELD_CODE, flag + field.name, DaoConst.VALUES_CODE, flag + "?"); sql.addParaConst(field.inputName, DaoConst.FIELD_CODE, flag + field.name,
flag = ","; DaoConst.VALUES_CODE, flag + DaoConst.CODE_PARA
);
flag = DaoConst.CODE_SPLIT;
} }
return sql; return sql;
} }
...@@ -345,35 +376,25 @@ public class TableStruct { ...@@ -345,35 +376,25 @@ public class TableStruct {
SqlData sql = new SqlData(DaoConst.UPDATE, text); SqlData sql = new SqlData(DaoConst.UPDATE, text);
sql.setSqlType(DaoConst.SQL_TYPE_UPDATE); sql.setSqlType(DaoConst.SQL_TYPE_UPDATE);
// 特殊字段操作 // 主键字段操作
TableFieldVo removeField = this.getRemove(); sql.addCode(DaoConst.FIELD_CODE, String.format(DaoConst.CODE_UPDATE_PRIMARY, this.getKey().name, this.getKey().name));
TableFieldVo versionField = this.getVersion();
sql.addParaConst(this.getKey().inputName, sql.addParaConst(this.getKey().inputName,
DaoConst.FIELD_CODE, "a." + this.getKey().name + "=a." + this.getKey().name, DaoConst.FIELD_CODE, String.format(DaoConst.CODE_UPDATE_FIELD_PARA, this.getKey().name)
DaoConst.WHERE_CODE, " AND a." + 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()) { for (TableFieldVo field : getFieldActionList(DaoConst.FIELD_COMMON,
if (field == removeField || field == versionField || mapCreate.containsKey(field)) { DaoConst.FIELD_CREATE, DaoConst.FIELD_REMOVE, DaoConst.FIELD_VERSION)) {
continue; sql.addParaConst(field.inputName,
} DaoConst.FIELD_CODE, String.format(DaoConst.CODE_UPDATE_FIELD_PARA, field.name)
sql.addParaConst(field.inputName, DaoConst.FIELD_CODE, ",a." + field.name + "=?"); );
} }
// 添加删除字段 // 添加删除字段
if (removeField != null) { addWhereRemove(sql);
sql.addParaConst(removeField.inputName, DaoConst.WHERE_CODE, " AND a." + removeField.name + "=0"); addUpdateVersion(sql);
} // 添加版本号条件
// 添加版本字段 if (getVersion() != null) {
if (versionField != null) { sql.addConst(getVersion().inputName, String.format(DaoConst.CODE_WHERE_EQUALS_PARA, getVersion().name));
sql.addParaConst(versionField.inputName,
DaoConst.FIELD_CODE, ",a." + versionField.name + "=1+a." + versionField.name,
DaoConst.WHERE_CODE, " AND a." + versionField.name + "=?");
} }
return sql; return sql;
} }
...@@ -391,19 +412,26 @@ public class TableStruct { ...@@ -391,19 +412,26 @@ public class TableStruct {
String text = DaoConst.UPDATE_MODEL.replace(DaoConst.TABLE_CODE, this.name); String text = DaoConst.UPDATE_MODEL.replace(DaoConst.TABLE_CODE, this.name);
SqlData sql = new SqlData(DaoConst.REMOVE, text); SqlData sql = new SqlData(DaoConst.REMOVE, text);
sql.setSqlType(DaoConst.SQL_TYPE_REMOVE); 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)) { for (TableFieldVo field : this.getFieldActionList(DaoConst.FIELD_REMOVE_UPDATE)) {
sql.addParaConst(field.inputName, DaoConst.FIELD_CODE, ",a." + field.name + "=?"); sql.addParaConst(field.inputName,
} DaoConst.FIELD_CODE, String.format(DaoConst.CODE_UPDATE_FIELD_PARA, field.name)
if (versionField != null) { );
sql.addCode(DaoConst.FIELD_CODE, ",a." + versionField.name + "=1+a." + versionField.name); }
} // 增加版本号字段的值
addWhereField(sql, "a.", true); addUpdateVersion(sql);
// 生成逻辑删除WHERE条件
addWhereField(sql, DaoConst.CODE_TAG, true);
return sql; return sql;
} else { } else {
String text = DaoConst.REMOVE_MODEL.replace(DaoConst.TABLE_CODE, this.name); String text = DaoConst.REMOVE_MODEL.replace(DaoConst.TABLE_CODE, this.name);
SqlData sql = new SqlData(DaoConst.REMOVE, text); SqlData sql = new SqlData(DaoConst.REMOVE, text);
addWhereField(sql, "a.", true); // 生成删除语句Where条件
addWhereField(sql, DaoConst.CODE_TAG, true);
return sql; return sql;
} }
} }
...@@ -418,7 +446,8 @@ public class TableStruct { ...@@ -418,7 +446,8 @@ public class TableStruct {
String text = DaoConst.LOAD_MODEL.replace(DaoConst.TABLE_CODE, this.name); String text = DaoConst.LOAD_MODEL.replace(DaoConst.TABLE_CODE, this.name);
SqlData sql = new SqlData(DaoConst.LOAD, text); SqlData sql = new SqlData(DaoConst.LOAD, text);
sql.setSqlType(DaoConst.SQL_TYPE_LOAD); sql.setSqlType(DaoConst.SQL_TYPE_LOAD);
addWhereField(sql, "a.", false); // 生成加载语句的WHERE条件
addWhereField(sql, DaoConst.CODE_TAG, false);
return sql; return sql;
} }
...@@ -428,26 +457,52 @@ public class TableStruct { ...@@ -428,26 +457,52 @@ public class TableStruct {
* @param sql * @param sql
*/ */
private void addWhereField(SqlData sql, String tag, boolean isRemove) { private void addWhereField(SqlData sql, String tag, boolean isRemove) {
TableFieldVo removeField = this.getRemove(); sql.add(this.getKey().inputName,
sql.add(this.getKey().inputName, " AND " + tag + this.getKey().name + "=?"); String.format(DaoConst.CODE_WHERE_EQUALS, tag, this.getKey().name, DaoConst.CODE_PARA)
);
// 删除时更新字段
List<TableFieldVo> removeUpdate = this.getRemoveUpdate(); // Where条件包含的字段
Map<TableFieldVo, Boolean> updateField = new HashMap<>(removeUpdate.size()); List<TableFieldVo> fields;
if (isRemove) { if (!isRemove) {
for (TableFieldVo field : removeUpdate) { // 所有字段
updateField.put(field, true); 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)) { * 添加删除WHERE条件字段
continue; *
* @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 { ...@@ -461,7 +516,8 @@ public class TableStruct {
public void addGroupSql(TableSqlCache sqlTableData, TableFieldString whereFields, TableFieldString updateFields) { public void addGroupSql(TableSqlCache sqlTableData, TableFieldString whereFields, TableFieldString updateFields) {
sqlTableData.add(this.releaseSqlWhere(DaoConst.GROUP_QUERY, DaoConst.LOAD_MODEL, whereFields)); sqlTableData.add(this.releaseSqlWhere(DaoConst.GROUP_QUERY, DaoConst.LOAD_MODEL, whereFields));
sqlTableData.add(this.releaseSql(DaoConst.GROUP_ADD, DaoConst.UPDATE_MODEL, 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 { ...@@ -473,7 +529,7 @@ public class TableStruct {
* @return * @return
*/ */
public SqlData releaseSqlWhere(String name, String model, TableFieldString whereFields) { 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 { ...@@ -511,7 +567,9 @@ public class TableStruct {
String[] fields = new String[paraFields.size()]; String[] fields = new String[paraFields.size()];
fields = paraFields.toArray(fields); fields = paraFields.toArray(fields);
// 生成修改的SQL语句 // 生成修改的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); SqlData ret = new SqlData(name, sql, fields);
return ret; return ret;
} }
...@@ -538,15 +596,17 @@ public class TableStruct { ...@@ -538,15 +596,17 @@ public class TableStruct {
String text = DaoConst.LOAD_MODEL.replace(DaoConst.TABLE_CODE, this.name); String text = DaoConst.LOAD_MODEL.replace(DaoConst.TABLE_CODE, this.name);
SqlData sql = new SqlData(sqlName, text); SqlData sql = new SqlData(sqlName, text);
sql.setSqlType(DaoConst.SQL_TYPE_EXINTS); 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) { for (String fieldName : fields) {
TableFieldVo field = this.getField(fieldName); TableFieldVo field = this.getField(fieldName);
sql.addParaConst(field.inputName, DaoConst.WHERE_CODE, " AND a." + field.name + "=?"); sql.addParaConst(field.inputName,
} String.format(DaoConst.CODE_WHERE_EQUALS_PARA, field.name)
if (getRemove() != null) { );
sql.addParaConst(DaoConst.REMOVE_FLAG_INPUT, DaoConst.WHERE_CODE, " AND a." + this.getRemove().name + "=0");
} }
addWhereRemove(sql);
sqlTableData.add(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