Skip to content

Commit

Permalink
Merge branch 'dev' into 5.6
Browse files Browse the repository at this point in the history
  • Loading branch information
chenrenfei committed Oct 19, 2024
2 parents cd300e5 + 3c484d5 commit ae09188
Show file tree
Hide file tree
Showing 19 changed files with 275 additions and 240 deletions.
4 changes: 2 additions & 2 deletions trunk/sqltoy-orm-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sagframe</groupId>
<version>5.6.27</version>
<version>5.6.28</version>
<name>sagacity-sqltoy</name>
<description>sqltoy core code</description>
<artifactId>sagacity-sqltoy</artifactId>
Expand All @@ -14,7 +14,7 @@
<ehcache.version>3.10.8</ehcache.version>
<httpclient.version>4.5.14</httpclient.version>
<httpclient-core.version>4.4.16</httpclient-core.version>
<elastic-rest-client.version>8.15.2</elastic-rest-client.version>
<elastic-rest-client.version>8.15.3</elastic-rest-client.version>
<fastjson.version>2.0.53</fastjson.version>
<mongo.version>5.2.0</mongo.version>
<junit-jupiter.version>5.11.2</junit-jupiter.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,6 @@ public class SqlToyConstants {
*/
public static boolean executeSqlBlankToNull = true;

/**
* 是否启用标准的if else 内容处理逻辑,默认为false
*/
public static boolean STANDARD_IF_LOGIC_STRATEGY = false;

/**
* 分页中间表名称
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,11 +370,6 @@ public TranslateManager getTranslateManager() {
*/
private boolean executeSqlBlankToNull = true;

/**
* 是否开启标准的if else 逻辑
*/
private boolean useStandardIfLogic = false;

/**
* sql 日志输出时LocalDateTime类型的输出格式
*/
Expand Down Expand Up @@ -462,7 +457,6 @@ public void initialize() throws Exception {
SqlToyConstants.DEFAULT_PAGE_SIZE = this.defaultPageSize;
SqlToyConstants.localDateTimeFormat = this.localDateTimeFormat;
SqlToyConstants.localTimeFormat = this.localTimeFormat;
SqlToyConstants.STANDARD_IF_LOGIC_STRATEGY = this.isUseStandardIfLogic();
// 初始化sql执行统计的基本参数
SqlExecuteStat.setDebug(this.debug);
SqlExecuteStat.setOverTimeSqlHandler(overTimeSqlHandler);
Expand Down Expand Up @@ -1329,19 +1323,4 @@ public IgnoreKeyCaseMap<String, String> getDialectReturnPrimaryColumnCase() {
public void setDialectReturnPrimaryColumnCase(IgnoreKeyCaseMap<String, String> dialectReturnPrimaryColumnCase) {
this.dialectReturnPrimaryColumnCase = dialectReturnPrimaryColumnCase;
}

/**
* @return the useStandardIfLogic
*/
public boolean isUseStandardIfLogic() {
return useStandardIfLogic;
}

/**
* @param useStandardIfLogic the useStandardIfLogic to set
*/
public void setUseStandardIfLogic(boolean useStandardIfLogic) {
this.useStandardIfLogic = useStandardIfLogic;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -561,28 +561,35 @@ else if (ifStart == -1 && ifLogicSignStart > 0) {
preParamCnt = StringUtil.matchCnt(preSql, ARG_NAME_PATTERN, 0);
markContentSql = processIfLogic(markContentSql, SQL_PSEUDO_START_MARK, SQL_PSEUDO_END_MARK,
ARG_NAME_PATTERN, paramValuesList, preSql, preParamCnt, ifLogicCnt, 0, 0);
isDynamicSql = isDynamicSql(markContentSql, SQL_PSEUDO_START_MARK, SQL_PSEUDO_END_MARK);
// 默认为false(原本模式),即#[@if() sqlPart] sqlPart中参数值为null即剔除sqlPart
if (SqlToyConstants.STANDARD_IF_LOGIC_STRATEGY) {
// 只在#[] 非if else 逻辑场景下,对内容中参数是否为null做判断并清理内容
if (ifLogicCnt == 0) {
// 存在if场景,重新获取内容中的参数数量
paramCnt = StringUtil.matchCnt(markContentSql, ARG_NAME_PATTERN, 0);
isEndWithAndOr = StringUtil.matches(markContentSql, SqlToyConstants.AND_OR_END);
markContentSql = processMarkContent(markContentSql, ARG_NAME_PATTERN, paramValuesList, preParamCnt,
paramCnt, true);
}
// 没有if逻辑,#[sqlPart] 中间的sqlPart中无参数,整体剔除,有参数则判断参数是否有为null的决定是否剔除sqlPart
if (ifLogicCnt == 0) {
isEndWithAndOr = StringUtil.matches(markContentSql, SqlToyConstants.AND_OR_END);
paramCnt = StringUtil.matchCnt(markContentSql, ARG_NAME_PATTERN, 0);
markContentSql = (paramCnt == 0) ? BLANK
: processMarkContent(markContentSql, ARG_NAME_PATTERN, paramValuesList, preParamCnt, paramCnt,
true);
} else {
// 旧模式,不区分if else场景,都对内容进行参数为null判断,为null则清空内容
if (!isDynamicSql || ifLogicCnt <= 1) {
// 存在if场景,重新获取内容中的参数数量
paramCnt = StringUtil.matchCnt(markContentSql, ARG_NAME_PATTERN, 0);
isDynamicSql = isDynamicSql(markContentSql, SQL_PSEUDO_START_MARK, SQL_PSEUDO_END_MARK);
// #[sqlPart] 中sqlPart里面没有#[]
if (!isDynamicSql) {
isEndWithAndOr = StringUtil.matches(markContentSql, SqlToyConstants.AND_OR_END);
paramCnt = StringUtil.matchCnt(markContentSql, ARG_NAME_PATTERN, 0);
// 判断sqlPart中参数是否为null,决定是否剔除sqlPart
markContentSql = processMarkContent(markContentSql, ARG_NAME_PATTERN, paramValuesList, preParamCnt,
paramCnt, true);
} // 在@if @else 场景下,将sql片段重新组装成#[sqlPart]放入原sql中再从最内层找#[]
else if (ifLogicCnt > 1 && !innerMark(markContentSql)) {
markContentSql = SQL_PSEUDO_START_MARK.concat(markContentSql).concat(SQL_PSEUDO_END_MARK);
} else {
// sqlPart中存在#[],剔除掉所有#[],再判断剩余sql中是否有动态参数
String clearSymMarkStr = StringUtil.clearSymMarkContent(markContentSql, SQL_PSEUDO_START_MARK,
SQL_PSEUDO_END_MARK);
// 剩余sql中的动态参数个数
int clearAfterArgCnt = StringUtil.matchCnt(clearSymMarkStr, ARG_NAME_PATTERN, 0);
// 动态参数大于0,类似 and status=:status #[xxx] 有:status参数,则变成#[and status=:status
// #[xxx]]继续利用sqltoy的判空剔除规则
if (clearAfterArgCnt > 0) {
markContentSql = SQL_PSEUDO_START_MARK.concat(markContentSql).concat(SQL_PSEUDO_END_MARK);
} else {
isEndWithAndOr = StringUtil.matches(markContentSql, SqlToyConstants.AND_OR_END);
}
}
}
queryStr = processWhereLinkAnd(preSql, markContentSql, isEndWithAndOr, tailSql);
Expand All @@ -606,7 +613,7 @@ public static boolean isDynamicSql(String sql, String startMark, String endMark)
}
return false;
}

/**
* @todo 找到@elseif 或@else 对应的@if位置
* @param preSql
Expand All @@ -625,7 +632,7 @@ public static int getStartIfIndex(String preSql, String startMark, String endMar
break;
}
// 通过] 逆向找#[对应位置
startIndex = StringUtil.getSymMarkReverseIndex(startMark, endMark, sql, endIndex);
startIndex = StringUtil.getSymMarkReverseIndex(startMark, endMark, sql, endIndex + endMark.length());
if (startIndex == -1) {
break;
}
Expand All @@ -643,19 +650,6 @@ public static int getStartIfIndex(String preSql, String startMark, String endMar
return ifIndex;
}

/**
* @todo 判断sql是否是#[sql],即#[]是否在最外层
* @param sql
* @return
*/
private static boolean innerMark(String sql) {
String tmp = sql.trim();
if (tmp.startsWith(SQL_PSEUDO_START_MARK) && tmp.endsWith(SQL_PSEUDO_END_MARK)) {
return true;
}
return false;
}

/**
* @todo 处理#[@if() sqlPart] if成立后sqlPart部分判断参数是否为null
* @param markContentSql
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -863,4 +863,27 @@ public Map<String, ForeignModel> getForeignFields() {
public void setForeignFields(Map<String, ForeignModel> foreignFields) {
this.foreignFields = foreignFields;
}

/**
* 提取唯一索引信息,用于saveAllIgnoreExist
*
* @return
*/
public IndexModel getUniqueIndex() {
if (this.indexModels == null || this.indexModels.length == 0) {
return null;
}
int count = 0;
IndexModel result = null;
for (IndexModel indexModel : indexModels) {
if (indexModel.isUnique()) {
count++;
result = indexModel;
}
}
if (count == 1) {
return result;
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ public static String wrapUniqueSql(EntityMeta entityMeta, String[] realParamName
* @param dbType
* @param entityMeta
* @param pkStrategy
* @param fromTable (不同数据库方言虚表,如dual)
* @param fromTable (不同数据库方言虚表,如dual)
* @param isNullFunction
* @param sequence
* @param isAssignPK
Expand All @@ -306,7 +306,7 @@ public static String mergeIgnore(IUnifyFieldsHandler unifyFieldsHandler, Integer
String tableName) {
// 在无主键的情况下产生insert sql语句
String realTable = entityMeta.getSchemaTable(tableName, dbType);
if (entityMeta.getIdArray() == null) {
if (entityMeta.getIdArray() == null && entityMeta.getUniqueIndex() == null) {
return generateInsertSql(unifyFieldsHandler, dbType, entityMeta, pkStrategy, isNullFunction, sequence,
isAssignPK, realTable);
}
Expand Down Expand Up @@ -360,9 +360,11 @@ public static String mergeIgnore(IUnifyFieldsHandler unifyFieldsHandler, Integer
// sql.append(") tv on (");
sql.append(SqlToyConstants.MERGE_ALIAS_ON);
StringBuilder idColumns = new StringBuilder();
// 组织on部分的主键条件判断
for (int i = 0, n = entityMeta.getIdArray().length; i < n; i++) {
columnName = entityMeta.getColumnName(entityMeta.getIdArray()[i]);
boolean hasId = (entityMeta.getIdArray() == null) ? false : true;
String[] fields = hasId ? entityMeta.getIdArray() : entityMeta.getUniqueIndex().getColumns();
// 组织on部分的主键条件判断(update 2024-10-16 增加无主键场景下,用唯一索引来替代的方式)
for (int i = 0, n = fields.length; i < n; i++) {
columnName = hasId ? entityMeta.getColumnName(fields[i]) : fields[i];
columnName = ReservedWordsUtil.convertWord(columnName, dbType);
if (i > 0) {
sql.append(" and ");
Expand All @@ -381,8 +383,9 @@ public static String mergeIgnore(IUnifyFieldsHandler unifyFieldsHandler, Integer
// 排除id的其他字段信息
StringBuilder insertRejIdCols = new StringBuilder();
StringBuilder insertRejIdColValues = new StringBuilder();
// 是否全部是ID,匹配上则无需进行更新,只需将未匹配上的插入即可
// 是否全部是ID,insert 按主键来构造语句
boolean allIds = (entityMeta.getRejectIdFieldArray() == null);
// 部分主键,则按非主键来构造insert字段
if (!allIds) {
int rejectIdColumnSize = entityMeta.getRejectIdFieldArray().length;
// update 只针对非主键字段进行修改
Expand Down Expand Up @@ -421,40 +424,46 @@ public static String mergeIgnore(IUnifyFieldsHandler unifyFieldsHandler, Integer
sql.append(idsColumnStr.replace("ta.", "tv."));
} else {
sql.append(insertRejIdCols.toString());
// sequence方式主键
if (pkStrategy.equals(PKStrategy.SEQUENCE)) {
columnName = entityMeta.getColumnName(entityMeta.getIdArray()[0]);
columnName = ReservedWordsUtil.convertWord(columnName, dbType);
sql.append(",");
sql.append(columnName);
// 无主键
if (pkStrategy == null) {
sql.append(") values (");
sql.append(insertRejIdColValues).append(",");
if (isAssignPK) {
sql.append(isNullFunction);
sql.append("(tv.").append(columnName).append(",");
sql.append(sequence).append(") ");
} else {
sql.append(sequence);
}
} else if (pkStrategy.equals(PKStrategy.IDENTITY)) {
columnName = entityMeta.getColumnName(entityMeta.getIdArray()[0]);
columnName = ReservedWordsUtil.convertWord(columnName, dbType);
if (isAssignPK) {
sql.append(insertRejIdColValues);
} else {
// sequence方式主键
if (pkStrategy.equals(PKStrategy.SEQUENCE)) {
columnName = entityMeta.getColumnName(entityMeta.getIdArray()[0]);
columnName = ReservedWordsUtil.convertWord(columnName, dbType);
sql.append(",");
sql.append(columnName);
sql.append(") values (");
sql.append(insertRejIdColValues).append(",");
if (isAssignPK) {
sql.append(isNullFunction);
sql.append("(tv.").append(columnName).append(",");
sql.append(sequence).append(") ");
} else {
sql.append(sequence);
}
} else if (pkStrategy.equals(PKStrategy.IDENTITY)) {
columnName = entityMeta.getColumnName(entityMeta.getIdArray()[0]);
columnName = ReservedWordsUtil.convertWord(columnName, dbType);
if (isAssignPK) {
sql.append(",");
sql.append(columnName);
}
sql.append(") values (");
// identity 模式insert无需写插入该字段语句
sql.append(insertRejIdColValues);
if (isAssignPK) {
sql.append(",").append("tv.").append(columnName);
}
} else {
sql.append(",");
sql.append(idsColumnStr.replace("ta.", ""));
sql.append(") values (");
sql.append(insertRejIdColValues).append(",");
sql.append(idsColumnStr.replace("ta.", "tv."));
}
sql.append(") values (");
// identity 模式insert无需写插入该字段语句
sql.append(insertRejIdColValues);
if (isAssignPK) {
sql.append(",").append("tv.").append(columnName);
}
} else {
sql.append(",");
sql.append(idsColumnStr.replace("ta.", ""));
sql.append(") values (");
sql.append(insertRejIdColValues).append(",");
sql.append(idsColumnStr.replace("ta.", "tv."));
}
}
sql.append(")");
Expand Down
Loading

0 comments on commit ae09188

Please sign in to comment.