Commit 24d4ff91 authored by yanzg's avatar yanzg

表结构修改表结构修改

parent c5c9feb3
...@@ -141,7 +141,7 @@ public class DaoConst { ...@@ -141,7 +141,7 @@ public class DaoConst {
/** /**
* 更新字段模板参数 * 更新字段模板参数
*/ */
public static final String CODE_UPDATE_FIELD_REMOVE = "a.%s=1"; public static final String CODE_UPDATE_FIELD_REMOVE = "a.%s=IFNULL(?,1)";
/** /**
* 等于条件 * 等于条件
*/ */
...@@ -173,7 +173,7 @@ public class DaoConst { ...@@ -173,7 +173,7 @@ public class DaoConst {
/** /**
* 未删除条件 * 未删除条件
*/ */
public static final String CODE_WHERE_EQUALS_NOT_REMOVE = "AND a.%s=0"; public static final String CODE_WHERE_EQUALS_NOT_REMOVE = "AND a.%s=IFNULL(?,0)";
/** /**
* 版本好字段 * 版本好字段
*/ */
...@@ -225,7 +225,7 @@ public class DaoConst { ...@@ -225,7 +225,7 @@ public class DaoConst {
/** /**
* 删除字符串长度变量 * 删除字符串长度变量
*/ */
public static final String REMOVE_FLAG_INPUT = "remove_input"; public static final String OLD_FLAG = "Old";
/** /**
* ID字段 * ID字段
*/ */
......
...@@ -6,37 +6,22 @@ import io.swagger.annotations.ApiModelProperty; ...@@ -6,37 +6,22 @@ import io.swagger.annotations.ApiModelProperty;
/** /**
* 查询参数 * 查询参数
* *
* @author 颜佐光 * @author 颜佐光
*/ */
@ApiModel(description = "查询参数") @ApiModel(description = "查询参数")
public class QueryPara { public class QueryPara {
/**
* 查询删除
*/
public static QueryPara LOAD_REMOVE = new QueryPara(false, true);
/** /**
* 所有条件生效 * 所有条件生效
*/ */
public static QueryPara FULL_COND = new QueryPara(true, false); public static QueryPara FULL_COND = new QueryPara(true);
/** /**
* 所有条件生效 * 所有条件生效
*/ */
@ApiModelProperty(notes = "所有条件生效,而不仅仅在主键存在时,通过主键加载") @ApiModelProperty(notes = "所有条件生效,而不仅仅在主键存在时,通过主键加载")
private boolean fullCond; private boolean fullCond;
/** public QueryPara(boolean fullCond) {
* 查询删除标记的结果
*/
@ApiModelProperty(notes = "在数据通过删除标记删除时,可以通过该参数查询出来")
private boolean loadRemove;
public QueryPara(boolean fullCond, boolean loadRemove) {
this.fullCond = fullCond; this.fullCond = fullCond;
this.loadRemove = loadRemove;
} }
public boolean isFullCond() { public boolean isFullCond() {
...@@ -46,12 +31,4 @@ public class QueryPara { ...@@ -46,12 +31,4 @@ public class QueryPara {
public void setFullCond(boolean fullCond) { public void setFullCond(boolean fullCond) {
this.fullCond = fullCond; this.fullCond = fullCond;
} }
public boolean isLoadRemove() {
return loadRemove;
}
public void setLoadRemove(boolean loadRemove) {
this.loadRemove = loadRemove;
}
} }
...@@ -61,8 +61,7 @@ public abstract class BaseDaoImpl extends BaseDaoSql implements BaseDao { ...@@ -61,8 +61,7 @@ public abstract class BaseDaoImpl extends BaseDaoSql implements BaseDao {
// 最终处理 // 最终处理
this.created(model); this.created(model);
// 返回执行结果 // 返回执行结果
String retVal = ret > 0 ? keyString : ""; return ret > 0 ? keyString : "";
return retVal;
} }
protected List<String> createReplaceList(String sqlName, Collection collection) { protected List<String> createReplaceList(String sqlName, Collection collection) {
...@@ -403,8 +402,7 @@ public abstract class BaseDaoImpl extends BaseDaoSql implements BaseDao { ...@@ -403,8 +402,7 @@ public abstract class BaseDaoImpl extends BaseDaoSql implements BaseDao {
return model; return model;
} }
// 获取来源主键 // 获取来源主键
Object from = this.table.getKeyObject(model); return this.table.getKeyObject(model);
return from;
} }
private <T> void checkLoadResult(T toItem, List<T> tos) { private <T> void checkLoadResult(T toItem, List<T> tos) {
......
...@@ -213,16 +213,7 @@ public abstract class BaseDaoSql { ...@@ -213,16 +213,7 @@ public abstract class BaseDaoSql {
* @param copy 是否复制 * @param copy 是否复制
*/ */
protected SqlData getSqlQueryPara(SqlData sqlData, QueryPara queryPara, boolean copy) { protected SqlData getSqlQueryPara(SqlData sqlData, QueryPara queryPara, boolean copy) {
SqlData copyTo = sqlData; return sqlData;
if (queryPara != null && queryPara.isLoadRemove()) {
if (copyTo.getField(DaoConst.REMOVE_FLAG_INPUT) != null) {
if (copy) {
copyTo = sqlData.copy();
}
copyTo.removeField(DaoConst.REMOVE_FLAG_INPUT);
}
}
return copyTo;
} }
/** /**
......
package com.yanzuoguang.dao.impl; package com.yanzuoguang.dao.impl;
import com.yanzuoguang.dao.DaoConst;
import com.yanzuoguang.dao.TableAnnotation; import com.yanzuoguang.dao.TableAnnotation;
import com.yanzuoguang.dao.cond.SqlCondDefault; import com.yanzuoguang.dao.cond.SqlCondDefault;
import com.yanzuoguang.util.base.MethodField; import com.yanzuoguang.util.base.MethodField;
...@@ -13,6 +12,8 @@ import java.util.HashMap; ...@@ -13,6 +12,8 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import static com.yanzuoguang.dao.DaoConst.*;
/** /**
* 表结构的基本信息 * 表结构的基本信息
* *
...@@ -75,7 +76,7 @@ public class TableStruct { ...@@ -75,7 +76,7 @@ public class TableStruct {
*/ */
private void addMethodField(MethodField field) { private void addMethodField(MethodField field) {
int fieldAction = DaoConst.FIELD_NONE; int fieldAction = FIELD_NONE;
// 默认后台数据库字段和前台参数字段为字段名,字段类型为class // 默认后台数据库字段和前台参数字段为字段名,字段类型为class
String fieldName = field.getName(); String fieldName = field.getName();
...@@ -117,10 +118,10 @@ public class TableStruct { ...@@ -117,10 +118,10 @@ public class TableStruct {
int resultActionType = getActionType(fieldAction, stringAction); int resultActionType = getActionType(fieldAction, stringAction);
// 获取普通类型字段列表 // 获取普通类型字段列表
List<TableFieldVo> commonActionList = this.getFieldActionList(DaoConst.FIELD_COMMON); List<TableFieldVo> commonActionList = this.getFieldActionList(FIELD_COMMON);
if (resultActionType == DaoConst.FIELD_PRIMARY) { if (resultActionType == FIELD_PRIMARY) {
// 将历史主键添加到普通列,并且移除历史主键 // 将历史主键添加到普通列,并且移除历史主键
List<TableFieldVo> primaryActionList = this.getFieldActionList(DaoConst.FIELD_PRIMARY); List<TableFieldVo> primaryActionList = this.getFieldActionList(FIELD_PRIMARY);
commonActionList.addAll(primaryActionList); commonActionList.addAll(primaryActionList);
primaryActionList.clear(); primaryActionList.clear();
// 将现有列添加到主键 // 将现有列添加到主键
...@@ -129,12 +130,12 @@ public class TableStruct { ...@@ -129,12 +130,12 @@ public class TableStruct {
// 将所有非主键列添加到普通列 // 将所有非主键列添加到普通列
commonActionList.add(vo); commonActionList.add(vo);
boolean isTypeMany = resultActionType != DaoConst.FIELD_MD5 boolean isTypeMany = resultActionType != FIELD_MD5
&& resultActionType != DaoConst.FIELD_REMOVE && resultActionType != FIELD_REMOVE
&& resultActionType != DaoConst.FIELD_VERSION && resultActionType != FIELD_VERSION
&& resultActionType != DaoConst.FIELD_COMMON; && resultActionType != FIELD_COMMON;
if (resultActionType != DaoConst.FIELD_COMMON) { if (resultActionType != FIELD_COMMON) {
List<TableFieldVo> actionList = this.getFieldActionList(resultActionType); List<TableFieldVo> actionList = this.getFieldActionList(resultActionType);
// 处理其他特殊列 // 处理其他特殊列
if (isTypeMany) { if (isTypeMany) {
...@@ -161,7 +162,7 @@ public class TableStruct { ...@@ -161,7 +162,7 @@ public class TableStruct {
* @return 最终类型 * @return 最终类型
*/ */
private int getActionType(int fieldAction, int stringAction) { private int getActionType(int fieldAction, int stringAction) {
if (fieldAction != DaoConst.FIELD_NONE) { if (fieldAction != FIELD_NONE) {
return fieldAction; return fieldAction;
} else { } else {
return stringAction; return stringAction;
...@@ -176,21 +177,21 @@ public class TableStruct { ...@@ -176,21 +177,21 @@ public class TableStruct {
*/ */
private int getStringAction(TableFieldVo vo) { private int getStringAction(TableFieldVo vo) {
if (getKey() == null) { if (getKey() == null) {
return DaoConst.FIELD_PRIMARY; return FIELD_PRIMARY;
} else if (DaoConst.REMOVE_FLAG.equals(vo.inputLName)) { } else if (REMOVE_FLAG.equals(vo.inputLName)) {
return DaoConst.FIELD_REMOVE; return FIELD_REMOVE;
} else if (DaoConst.VERSION_FLAG.equals(vo.inputLName)) { } else if (VERSION_FLAG.equals(vo.inputLName)) {
return DaoConst.FIELD_VERSION; return FIELD_VERSION;
} }
// 判断是否属于统计字段 // 判断是否属于统计字段
else if (DaoConst.MD5_KEY_FLAG.equals(vo.inputLName)) { else if (MD5_KEY_FLAG.equals(vo.inputLName)) {
return DaoConst.FIELD_MD5; return FIELD_MD5;
} else if (vo.inputLName.startsWith(DaoConst.UPDATE_FLAG)) { } else if (vo.inputLName.startsWith(UPDATE_FLAG)) {
return DaoConst.FIELD_REMOVE_UPDATE; return FIELD_REMOVE_UPDATE;
} else if (vo.inputLName.startsWith(DaoConst.CREATE_FLAG)) { } else if (vo.inputLName.startsWith(CREATE_FLAG)) {
return DaoConst.FIELD_CREATE; return FIELD_CREATE;
} else { } else {
return DaoConst.FIELD_COMMON; return FIELD_COMMON;
} }
} }
...@@ -250,7 +251,7 @@ public class TableStruct { ...@@ -250,7 +251,7 @@ public class TableStruct {
* @return 所有的普通股字段 * @return 所有的普通股字段
*/ */
public List<TableFieldVo> getFields() { public List<TableFieldVo> getFields() {
return getFieldActionList(DaoConst.FIELD_COMMON); return getFieldActionList(FIELD_COMMON);
} }
/** /**
...@@ -259,10 +260,10 @@ public class TableStruct { ...@@ -259,10 +260,10 @@ public class TableStruct {
* @return 获取统计纬度列 * @return 获取统计纬度列
*/ */
public List<TableFieldVo> getGroupLatitudeFields() { public List<TableFieldVo> getGroupLatitudeFields() {
return getFieldActionList(DaoConst.FIELD_COMMON, return getFieldActionList(FIELD_COMMON,
DaoConst.FIELD_ADD_GROUP, FIELD_ADD_GROUP,
DaoConst.FIELD_REPLACE_GROUP, FIELD_REPLACE_GROUP,
DaoConst.FIELD_MD5 FIELD_MD5
); );
} }
...@@ -290,7 +291,7 @@ public class TableStruct { ...@@ -290,7 +291,7 @@ public class TableStruct {
* @return 获取到的键值 * @return 获取到的键值
*/ */
public TableFieldVo getKey() { public TableFieldVo getKey() {
return getFieldAction(DaoConst.FIELD_PRIMARY); return getFieldAction(FIELD_PRIMARY);
} }
/** /**
...@@ -317,7 +318,7 @@ public class TableStruct { ...@@ -317,7 +318,7 @@ public class TableStruct {
* @return md5字段 * @return md5字段
*/ */
public TableFieldVo getMd5Key() { public TableFieldVo getMd5Key() {
return getFieldAction(DaoConst.FIELD_MD5); return getFieldAction(FIELD_MD5);
} }
/** /**
...@@ -336,7 +337,7 @@ public class TableStruct { ...@@ -336,7 +337,7 @@ public class TableStruct {
* @return 版本号字段 * @return 版本号字段
*/ */
public TableFieldVo getVersion() { public TableFieldVo getVersion() {
return getFieldAction(DaoConst.FIELD_VERSION); return getFieldAction(FIELD_VERSION);
} }
/** /**
...@@ -345,17 +346,17 @@ public class TableStruct { ...@@ -345,17 +346,17 @@ public class TableStruct {
* @return 删除字段 * @return 删除字段
*/ */
public TableFieldVo getRemove() { public TableFieldVo getRemove() {
return getFieldAction(DaoConst.FIELD_REMOVE); return getFieldAction(FIELD_REMOVE);
} }
/** /**
* 获取删除时更新子弹 * 获取删除时更新字段
* *
* @return 删除时更新字段 * @return 删除时更新字段
*/ */
public List<TableFieldVo> getRemoveUpdate() { public List<TableFieldVo> getRemoveUpdate() {
return getFieldActionList(DaoConst.FIELD_REMOVE_UPDATE); return getFieldActionList(FIELD_REMOVE_UPDATE);
} }
/** /**
...@@ -413,11 +414,11 @@ public class TableStruct { ...@@ -413,11 +414,11 @@ public class TableStruct {
* @return 初始化的表SQL语句 * @return 初始化的表SQL语句
*/ */
private void initSaveWith(TableSqlCache table) { private void initSaveWith(TableSqlCache table) {
List<TableFieldVo> saveWithField = getFieldActionList(DaoConst.FIELD_SAVE_WITH); List<TableFieldVo> saveWithField = getFieldActionList(FIELD_SAVE_WITH);
if (saveWithField == null || saveWithField.isEmpty()) { if (saveWithField == null || saveWithField.isEmpty()) {
return; return;
} }
this.addSaveWithSql(table, DaoConst.SAVE_WITH, saveWithField); this.addSaveWithSql(table, SAVE_WITH, saveWithField);
} }
/** /**
...@@ -426,8 +427,8 @@ public class TableStruct { ...@@ -426,8 +427,8 @@ public class TableStruct {
* @return 初始化的表SQL语句 * @return 初始化的表SQL语句
*/ */
private void initAddGroup(TableSqlCache table) { private void initAddGroup(TableSqlCache table) {
List<TableFieldVo> addGroupField = getFieldActionList(DaoConst.FIELD_ADD_GROUP); List<TableFieldVo> addGroupField = getFieldActionList(FIELD_ADD_GROUP);
List<TableFieldVo> replaceGroupField = getFieldActionList(DaoConst.FIELD_REPLACE_GROUP); List<TableFieldVo> replaceGroupField = getFieldActionList(FIELD_REPLACE_GROUP);
List<TableFieldVo> commonField = this.getGroupLatitudeFields(); List<TableFieldVo> commonField = this.getGroupLatitudeFields();
this.addGroupSql(table, commonField, replaceGroupField, addGroupField); this.addGroupSql(table, commonField, replaceGroupField, addGroupField);
} }
...@@ -438,7 +439,7 @@ public class TableStruct { ...@@ -438,7 +439,7 @@ public class TableStruct {
* @return 生成的语句 * @return 生成的语句
*/ */
private SqlData releaseSqlReplace() { private SqlData releaseSqlReplace() {
return releaseSqlCreateReplace(DaoConst.SQL_REPLACE, DaoConst.REPLACE, DaoConst.SQL_TYPE_CREATE); return releaseSqlCreateReplace(SQL_REPLACE, REPLACE, SQL_TYPE_CREATE);
} }
/** /**
...@@ -447,27 +448,29 @@ public class TableStruct { ...@@ -447,27 +448,29 @@ public class TableStruct {
* @return 生成的语句 * @return 生成的语句
*/ */
private SqlData releaseSqlCreate() { private SqlData releaseSqlCreate() {
return releaseSqlCreateReplace(DaoConst.SQL_INSERT, DaoConst.CREATE, DaoConst.SQL_TYPE_CREATE); return releaseSqlCreateReplace(SQL_INSERT, CREATE, SQL_TYPE_CREATE);
} }
private SqlData releaseSqlCreateReplace(String sqlModel, String sqlName, int sqlType) { private SqlData releaseSqlCreateReplace(String sqlModel, String sqlName, int sqlType) {
// 生成添加的SQL语句 // 生成添加的SQL语句
String text = sqlModel.replace(DaoConst.CODE_TABLE, this.name); String text = sqlModel.replace(CODE_TABLE, this.name);
SqlData sql = new SqlData(sqlName, text); SqlData sql = new SqlData(sqlName, text);
sql.setSqlType(sqlType); sql.setSqlType(sqlType);
TableFieldVo key = this.getKey();
// 第一个增加的字段,不需要增加 "," // 第一个增加的字段,不需要增加 ","
String flag = StringHelper.EMPTY; String flag = StringHelper.EMPTY;
if (this.getKeyType() == String.class) { if (this.getKeyType() == String.class) {
sql.addParaConst(this.getKey().inputName, DaoConst.CODE_FIELD, this.getKey().name, sql.addParaConst(key.inputName, CODE_FIELD, key.name,
DaoConst.CODE_VALUES, DaoConst.CODE_PARA CODE_VALUES, CODE_PARA
); );
flag = DaoConst.CODE_SPLIT; flag = CODE_SPLIT;
} }
for (TableFieldVo field : this.getFields()) { for (TableFieldVo field : this.getFields()) {
sql.addParaConst(field.inputName, DaoConst.CODE_FIELD, flag + field.name, sql.addParaConst(field.inputName, CODE_FIELD, flag + field.name,
DaoConst.CODE_VALUES, flag + DaoConst.CODE_PARA CODE_VALUES, flag + CODE_PARA
); );
flag = DaoConst.CODE_SPLIT; flag = CODE_SPLIT;
} }
return sql; return sql;
} }
...@@ -478,36 +481,32 @@ public class TableStruct { ...@@ -478,36 +481,32 @@ public class TableStruct {
* @return 生成的语句 * @return 生成的语句
*/ */
private SqlData releaseSqlUpdate() { private SqlData releaseSqlUpdate() {
TableFieldVo remove = this.getRemove();
// 生成添加的SQL语句 // 生成添加的SQL语句
String text = DaoConst.SQL_UPDATE.replace(DaoConst.CODE_TABLE, this.name); String text = SQL_UPDATE.replace(CODE_TABLE, this.name);
SqlData sql = new SqlData(DaoConst.UPDATE, text); SqlData sql = new SqlData(UPDATE, text);
sql.setSqlType(DaoConst.SQL_TYPE_UPDATE); sql.setSqlType(SQL_TYPE_UPDATE);
TableFieldVo key = this.getKey();
// 主键字段操作 // 主键字段操作
sql.addCode(DaoConst.CODE_FIELD, String.format(DaoConst.CODE_UPDATE_PRIMARY, this.getKey().name, this.getKey().name)); sql.addCode(CODE_FIELD, String.format(CODE_UPDATE_PRIMARY, key.name, key.name));
sql.addConst(this.getKey().inputName, String.format(DaoConst.CODE_WHERE_EQUALS_PARA, this.getKey().name)); sql.addConst(key.inputName, String.format(CODE_WHERE_EQUALS_PARA, key.name));
// 增加普通代码片段字段 // 增加普通代码片段字段
List<TableFieldVo> updateList = getFieldActionList(DaoConst.FIELD_COMMON, List<TableFieldVo> updateList = getFieldActionList(FIELD_COMMON,
DaoConst.FIELD_CREATE, DaoConst.FIELD_REMOVE, DaoConst.FIELD_VERSION); FIELD_CREATE, FIELD_REMOVE, FIELD_VERSION);
for (TableFieldVo field : updateList) { for (TableFieldVo field : updateList) {
sql.addParaConst(field.inputName, sql.addParaConst(field.inputName,
DaoConst.CODE_FIELD, String.format(DaoConst.CODE_UPDATE_FIELD_PARA, field.name) CODE_FIELD, String.format(CODE_UPDATE_FIELD_PARA, field.name)
); );
} }
// 设置删除标记可以通过前台传入
if (remove != null) {
sql.addParaConst(remove.inputName, DaoConst.CODE_FIELD, String.format(DaoConst.CODE_UPDATE_FIELD_PARA, remove.name));
}
// 添加删除字段 // 添加删除字段
// addWhereRemove(sql); // addWhereRemove(sql);
addUpdateVersion(sql); addUpdateRemoveField(sql);
addUpdateVersionField(sql);
// 添加版本号条件 // 添加版本号条件
if (getVersion() != null) { if (getVersion() != null) {
sql.addConst(getVersion().inputName, String.format(DaoConst.CODE_WHERE_EQUALS_PARA, getVersion().name)); sql.addConst(getVersion().inputName, String.format(CODE_WHERE_EQUALS_PARA, getVersion().name));
} }
return sql; return sql;
} }
...@@ -521,41 +520,39 @@ public class TableStruct { ...@@ -521,41 +520,39 @@ public class TableStruct {
TableFieldVo remove = this.getRemove(); TableFieldVo remove = this.getRemove();
if (remove != null) { if (remove != null) {
// 生成添加的SQL语句 // 生成添加的SQL语句
String text = DaoConst.SQL_UPDATE.replace(DaoConst.CODE_TABLE, this.name); String text = SQL_UPDATE.replace(CODE_TABLE, this.name);
SqlData sql = new SqlData(DaoConst.REMOVE, text); SqlData sql = new SqlData(REMOVE, text);
sql.setSqlType(DaoConst.SQL_TYPE_REMOVE); sql.setSqlType(SQL_TYPE_REMOVE);
// 设置删除字段标记 // 设置删除字段标记
sql.addCode(DaoConst.CODE_FIELD, String.format(DaoConst.CODE_UPDATE_FIELD_REMOVE, remove.name)); addUpdateRemoveField(sql);
// 增加版本号字段的值
addUpdateVersionField(sql);
// 设置删除时需要修改的字段的值 // 设置删除时需要修改的字段的值
for (TableFieldVo field : this.getFieldActionList(DaoConst.FIELD_REMOVE_UPDATE)) { for (TableFieldVo field : this.getFieldActionList(FIELD_REMOVE_UPDATE)) {
sql.addParaConst(field.inputName, sql.addParaConst(field.inputName,
DaoConst.CODE_FIELD, String.format(DaoConst.CODE_UPDATE_FIELD_PARA, field.name) CODE_FIELD, String.format(CODE_UPDATE_FIELD_PARA, field.name)
); );
} }
// 增加版本号字段的值
addUpdateVersion(sql);
// 生成逻辑删除WHERE条件 // 生成逻辑删除WHERE条件
addWhereField(sql, DaoConst.CODE_TAG, true); addWhereField(sql, CODE_TAG, true);
return sql; return sql;
} else { } else {
String text = DaoConst.SQL_REMOVE.replace(DaoConst.CODE_TABLE, this.name); String text = SQL_REMOVE.replace(CODE_TABLE, this.name);
SqlData sql = new SqlData(DaoConst.REMOVE, text); SqlData sql = new SqlData(REMOVE, text);
// 生成删除语句Where条件 // 生成删除语句Where条件
addWhereField(sql, DaoConst.CODE_TAG, true); addWhereField(sql, CODE_TAG, true);
return sql; return sql;
} }
} }
private SqlData releaseSqlQuery(String sqlName, int sqlType) { private SqlData releaseSqlQuery(String sqlName, int sqlType) {
// 生成添加的SQL语句 // 生成添加的SQL语句
String text = DaoConst.SQL_LOAD.replace(DaoConst.CODE_TABLE, this.name); String text = SQL_LOAD.replace(CODE_TABLE, this.name);
SqlData sql = new SqlData(sqlName, text); SqlData sql = new SqlData(sqlName, text);
sql.addParaConst(DaoConst.CODE_FIELD_DEFAULT_NAME, DaoConst.CODE_FIELD, DaoConst.CODE_FIELD_DEFAULT); sql.addParaConst(CODE_FIELD_DEFAULT_NAME, CODE_FIELD, CODE_FIELD_DEFAULT);
sql.setSqlType(sqlType); sql.setSqlType(sqlType);
// 生成加载语句的WHERE条件 // 生成加载语句的WHERE条件
addWhereField(sql, DaoConst.CODE_TAG, false); addWhereField(sql, CODE_TAG, false);
return sql; return sql;
} }
...@@ -566,7 +563,7 @@ public class TableStruct { ...@@ -566,7 +563,7 @@ public class TableStruct {
* @return 生成的语句 * @return 生成的语句
*/ */
private SqlData releaseSqlLoad() { private SqlData releaseSqlLoad() {
return releaseSqlQuery(DaoConst.LOAD, DaoConst.SQL_TYPE_LOAD); return releaseSqlQuery(LOAD, SQL_TYPE_LOAD);
} }
/** /**
...@@ -575,7 +572,7 @@ public class TableStruct { ...@@ -575,7 +572,7 @@ public class TableStruct {
* @return * @return
*/ */
private SqlData releaseSqlQuery() { private SqlData releaseSqlQuery() {
return releaseSqlQuery(DaoConst.QUERY, DaoConst.SQL_TYPE_COMMON); return releaseSqlQuery(QUERY, SQL_TYPE_COMMON);
} }
/** /**
...@@ -587,7 +584,7 @@ public class TableStruct { ...@@ -587,7 +584,7 @@ public class TableStruct {
* @return 生成的语句 * @return 生成的语句
*/ */
private void addWhereField(SqlData sql, String tag, boolean isRemove) { private void addWhereField(SqlData sql, String tag, boolean isRemove) {
addWhereBase(sql, DaoConst.CODE_WHERE, tag, isRemove, false); addWhereBase(sql, CODE_WHERE, tag, isRemove, false);
// 查询时,不能查询到非删除的字段 // 查询时,不能查询到非删除的字段
addWhereRemove(sql); addWhereRemove(sql);
} }
...@@ -600,7 +597,7 @@ public class TableStruct { ...@@ -600,7 +597,7 @@ public class TableStruct {
* @param tag 扩展标签 * @param tag 扩展标签
*/ */
public void addWhereExtend(SqlData sql, String tag, boolean removeHistory, String... codes) { public void addWhereExtend(SqlData sql, String tag, boolean removeHistory, String... codes) {
addWhereBase(sql, DaoConst.CODE_WHERE, tag, false, removeHistory, codes); addWhereBase(sql, CODE_WHERE, tag, false, removeHistory, codes);
} }
/** /**
...@@ -623,7 +620,7 @@ public class TableStruct { ...@@ -623,7 +620,7 @@ public class TableStruct {
fields = this.getFields(); fields = this.getFields();
} else { } else {
// 非删除时需要更新的字段 // 非删除时需要更新的字段
fields = this.getFieldActionList(DaoConst.FIELD_COMMON, DaoConst.FIELD_REMOVE_UPDATE); fields = this.getFieldActionList(FIELD_COMMON, FIELD_REMOVE_UPDATE);
} }
List<String> codesField = new ArrayList<>(); List<String> codesField = new ArrayList<>();
...@@ -641,11 +638,11 @@ public class TableStruct { ...@@ -641,11 +638,11 @@ public class TableStruct {
private int addWhereFieldCommon(SqlData sql, String codeName, TableFieldVo field, String tag, boolean removeHistory) { private int addWhereFieldCommon(SqlData sql, String codeName, TableFieldVo field, String tag, boolean removeHistory) {
addWhereFieldNotExtend(sql, field.inputName, codeName, addWhereFieldNotExtend(sql, field.inputName, codeName,
String.format(DaoConst.CODE_WHERE_EQUALS, tag, field.name, DaoConst.CODE_PARA), removeHistory); String.format(CODE_WHERE_EQUALS, tag, field.name, CODE_PARA), removeHistory);
// 添加in条件 // 添加in条件
String sqlIn = String.format(DaoConst.CODE_WHERE_IN, tag, field.name, DaoConst.CODE_PARA); String sqlIn = String.format(CODE_WHERE_IN, tag, field.name, CODE_PARA);
return addWhereFieldNotExtend(sql, DaoConst.getArrayParameterName(field.inputName), codeName, return addWhereFieldNotExtend(sql, getArrayParameterName(field.inputName), codeName,
sqlIn, removeHistory); sqlIn, removeHistory);
} }
...@@ -671,23 +668,39 @@ public class TableStruct { ...@@ -671,23 +668,39 @@ public class TableStruct {
* @param sql SQL语句实体 * @param sql SQL语句实体
*/ */
private void addWhereRemove(SqlData sql) { private void addWhereRemove(SqlData sql) {
if (getRemove() != null) { TableFieldVo remove = this.getRemove();
sql.addParaConst(DaoConst.REMOVE_FLAG_INPUT, if (remove != null) {
DaoConst.CODE_WHERE, sql.addParaConst(remove.inputName + OLD_FLAG,
String.format(DaoConst.CODE_WHERE_EQUALS_NOT_REMOVE, this.getRemove().name) CODE_WHERE,
String.format(CODE_WHERE_EQUALS_NOT_REMOVE, this.getRemove().name)
); );
} }
} }
/**
* 添加删除到更新SQL语句
*
* @param sql 删除语句
*/
private void addUpdateRemoveField(SqlData sql) {
TableFieldVo remove = this.getRemove();
// 设置删除标记可以通过前台传入
if (remove != null) {
sql.addParaConst(remove.inputName,
CODE_FIELD, String.format(CODE_UPDATE_FIELD_PARA, remove.name));
}
}
/** /**
* 版本号进行累加 * 版本号进行累加
* *
* @param sql 需要处理的SQL语句 * @param sql 需要处理的SQL语句
*/ */
private void addUpdateVersion(SqlData sql) { private void addUpdateVersionField(SqlData sql) {
// 添加版本字段 // 添加版本字段
if (getVersion() != null) { TableFieldVo version = getVersion();
sql.addCode(DaoConst.CODE_FIELD, String.format(DaoConst.CODE_UPDATE_VERSION_FIELD, getVersion().name, getVersion().name)); if (version != null) {
sql.addCode(CODE_FIELD, String.format(CODE_UPDATE_VERSION_FIELD, version.name, version.name));
} }
} }
...@@ -704,17 +717,17 @@ public class TableStruct { ...@@ -704,17 +717,17 @@ public class TableStruct {
*/ */
private SqlData releaseSql(int sqlType, String name, String model, String valueModel, List<TableFieldVo> valueFields, List<TableFieldVo> whereFields) { private SqlData releaseSql(int sqlType, String name, String model, String valueModel, List<TableFieldVo> valueFields, List<TableFieldVo> whereFields) {
// 生成修改的SQL语句 // 生成修改的SQL语句
SqlData sql = new SqlData(name, model.replace(DaoConst.CODE_TABLE, this.name)); SqlData sql = new SqlData(name, model.replace(CODE_TABLE, this.name));
sql.setSqlType(sqlType); sql.setSqlType(sqlType);
// 生成添加的SQL语句 // 生成添加的SQL语句
String flag = StringHelper.EMPTY; String flag = StringHelper.EMPTY;
for (TableFieldVo field : valueFields) { for (TableFieldVo field : valueFields) {
sql.addParaConst(field.inputName, DaoConst.CODE_FIELD, flag + valueModel.replace(DaoConst.CODE_FIELD, field.name)); sql.addParaConst(field.inputName, CODE_FIELD, flag + valueModel.replace(CODE_FIELD, field.name));
flag = DaoConst.CODE_SPLIT; flag = CODE_SPLIT;
} }
for (TableFieldVo field : whereFields) { for (TableFieldVo field : whereFields) {
sql.addConst(field.inputName, String.format(DaoConst.CODE_WHERE_EQUALS_PARA, field.name)); sql.addConst(field.inputName, String.format(CODE_WHERE_EQUALS_PARA, field.name));
} }
return sql; return sql;
...@@ -755,8 +768,8 @@ public class TableStruct { ...@@ -755,8 +768,8 @@ public class TableStruct {
* @param whereFields WHERE条件 * @param whereFields WHERE条件
*/ */
private void addSaveWithSql(TableSqlCache tableStruct, String sqlName, List<TableFieldVo> whereFields) { private void addSaveWithSql(TableSqlCache tableStruct, String sqlName, List<TableFieldVo> whereFields) {
SqlData sqlData = this.releaseSql(DaoConst.SQL_TYPE_SAVE_WITH, sqlName, DaoConst.SQL_LOAD, StringHelper.EMPTY, new ArrayList<>(), whereFields); SqlData sqlData = this.releaseSql(SQL_TYPE_SAVE_WITH, sqlName, SQL_LOAD, StringHelper.EMPTY, new ArrayList<>(), whereFields);
sqlData.addParaConst(DaoConst.CODE_FIELD_DEFAULT_NAME, DaoConst.CODE_FIELD, DaoConst.CODE_FIELD_DEFAULT); sqlData.addParaConst(CODE_FIELD_DEFAULT_NAME, CODE_FIELD, CODE_FIELD_DEFAULT);
tableStruct.add(sqlData); tableStruct.add(sqlData);
} }
...@@ -790,23 +803,23 @@ public class TableStruct { ...@@ -790,23 +803,23 @@ public class TableStruct {
* @param addFields 需要增加的值的字段 * @param addFields 需要增加的值的字段
*/ */
private void addGroupSql(TableSqlCache sqlTableData, List<TableFieldVo> whereFields, List<TableFieldVo> replaceFields, List<TableFieldVo> addFields) { private void addGroupSql(TableSqlCache sqlTableData, List<TableFieldVo> whereFields, List<TableFieldVo> replaceFields, List<TableFieldVo> addFields) {
List<TableFieldVo> prmaryKey = this.getFieldActionList(DaoConst.FIELD_PRIMARY); List<TableFieldVo> prmaryKey = this.getFieldActionList(FIELD_PRIMARY);
// 生成统计加载SQL语句 // 生成统计加载SQL语句
SqlData sqlLoad = this.releaseSql(DaoConst.SQL_TYPE_ADD_GROUP, DaoConst.GROUP_QUERY, DaoConst.SQL_LOAD, SqlData sqlLoad = this.releaseSql(SQL_TYPE_ADD_GROUP, GROUP_QUERY, SQL_LOAD,
StringHelper.EMPTY, new ArrayList<>(), whereFields); StringHelper.EMPTY, new ArrayList<>(), whereFields);
sqlLoad.addParaConst(DaoConst.CODE_FIELD_DEFAULT_NAME, DaoConst.CODE_FIELD, DaoConst.CODE_FIELD_DEFAULT); sqlLoad.addParaConst(CODE_FIELD_DEFAULT_NAME, CODE_FIELD, CODE_FIELD_DEFAULT);
// 生成统计累加SQL语句 // 生成统计累加SQL语句
SqlData sqlGroupAdd = this.releaseSql(DaoConst.SQL_TYPE_ADD_GROUP, DaoConst.GROUP_ADD, DaoConst.SQL_UPDATE, SqlData sqlGroupAdd = this.releaseSql(SQL_TYPE_ADD_GROUP, GROUP_ADD, SQL_UPDATE,
DaoConst.CODE_GROUP_ADD, addFields, prmaryKey); CODE_GROUP_ADD, addFields, prmaryKey);
// 当没有字段时,直接修改主键 // 当没有字段时,直接修改主键
if (addFields.isEmpty()) { if (addFields.isEmpty()) {
sqlGroupAdd.addCode(DaoConst.CODE_FIELD, String.format("%s=%s", prmaryKey.get(0).inputName, prmaryKey.get(0).inputName)); sqlGroupAdd.addCode(CODE_FIELD, String.format("%s=%s", prmaryKey.get(0).inputName, prmaryKey.get(0).inputName));
} }
// 生成覆盖值 // 生成覆盖值
if (replaceFields != null) { if (replaceFields != null) {
for (TableFieldVo field : replaceFields) { for (TableFieldVo field : replaceFields) {
sqlGroupAdd.addPara(field.inputName, DaoConst.CODE_FIELD, String.format(",a.%s=?", field.name)); sqlGroupAdd.addPara(field.inputName, CODE_FIELD, String.format(",a.%s=?", field.name));
} }
} }
sqlTableData.add(sqlLoad); sqlTableData.add(sqlLoad);
...@@ -823,12 +836,12 @@ public class TableStruct { ...@@ -823,12 +836,12 @@ public class TableStruct {
*/ */
public void addExist(TableSqlCache sqlTableData, String sqlName, TableFieldString mustField, TableFieldString allowNullField) { public void addExist(TableSqlCache sqlTableData, String sqlName, TableFieldString mustField, TableFieldString allowNullField) {
// String[] fields // String[] fields
SqlData sql = new SqlData(sqlName, DaoConst.SQL_LOAD.replace(DaoConst.CODE_TABLE, this.name)); SqlData sql = new SqlData(sqlName, SQL_LOAD.replace(CODE_TABLE, this.name));
sql.addParaConst(DaoConst.CODE_FIELD_DEFAULT_NAME, DaoConst.CODE_FIELD, DaoConst.CODE_FIELD_DEFAULT); sql.addParaConst(CODE_FIELD_DEFAULT_NAME, CODE_FIELD, CODE_FIELD_DEFAULT);
sql.setSqlType(DaoConst.SQL_TYPE_EXISTS); sql.setSqlType(SQL_TYPE_EXISTS);
sql.addConst(this.getKey().inputName, sql.addConst(this.getKey().inputName,
String.format(DaoConst.CODE_WHERE_NOT_EQUALS_PARA, this.getKey().name) String.format(CODE_WHERE_NOT_EQUALS_PARA, this.getKey().name)
); );
// 必须判断的字段 // 必须判断的字段
...@@ -836,7 +849,7 @@ public class TableStruct { ...@@ -836,7 +849,7 @@ public class TableStruct {
for (String fieldName : mustField.getFields()) { for (String fieldName : mustField.getFields()) {
TableFieldVo field = this.getField(fieldName); TableFieldVo field = this.getField(fieldName);
sql.addConst(field.inputName, sql.addConst(field.inputName,
String.format(DaoConst.CODE_WHERE_EQUALS_PARA, field.name) String.format(CODE_WHERE_EQUALS_PARA, field.name)
); );
} }
} }
...@@ -846,7 +859,7 @@ public class TableStruct { ...@@ -846,7 +859,7 @@ public class TableStruct {
for (String fieldName : allowNullField.getFields()) { for (String fieldName : allowNullField.getFields()) {
TableFieldVo field = this.getField(fieldName); TableFieldVo field = this.getField(fieldName);
sql.addConst(field.inputName, sql.addConst(field.inputName,
String.format(DaoConst.CODE_WHERE_EQUALS_NOT_EMPTY_PARA, field.name) String.format(CODE_WHERE_EQUALS_NOT_EMPTY_PARA, field.name)
); );
} }
} }
......
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