refactor: optimize forInsertEntityBatch

This commit is contained in:
Michael Yang 2024-08-11 16:00:44 +08:00
parent 792bfde75f
commit 9fcf4e20ea
5 changed files with 11 additions and 14 deletions

View File

@ -20,6 +20,7 @@ import com.mybatisflex.core.row.Row;
import com.mybatisflex.core.table.TableInfo; import com.mybatisflex.core.table.TableInfo;
import com.mybatisflex.core.table.TableManager; import com.mybatisflex.core.table.TableManager;
import java.util.Collection;
import java.util.List; import java.util.List;
/** /**
@ -75,7 +76,7 @@ public interface IDialect {
String forInsertEntityWithPk(TableInfo tableInfo, Object entity, boolean ignoreNulls); String forInsertEntityWithPk(TableInfo tableInfo, Object entity, boolean ignoreNulls);
String forInsertEntityBatch(TableInfo tableInfo, List<?> entities); String forInsertEntityBatch(TableInfo tableInfo, Collection<?> entities);
String forDeleteEntityById(TableInfo tableInfo); String forDeleteEntityById(TableInfo tableInfo);

View File

@ -653,7 +653,7 @@ public class CommonsDialectImpl implements IDialect {
@Override @Override
public String forInsertEntityBatch(TableInfo tableInfo, List<?> entities) { public String forInsertEntityBatch(TableInfo tableInfo, Collection<?> entities) {
StringBuilder sql = new StringBuilder(); StringBuilder sql = new StringBuilder();
sql.append(INSERT_INTO).append(tableInfo.getWrapSchemaAndTableName(this, OperateType.INSERT)); sql.append(INSERT_INTO).append(tableInfo.getWrapSchemaAndTableName(this, OperateType.INSERT));
String[] insertColumns = tableInfo.obtainInsertColumns(null, false); String[] insertColumns = tableInfo.obtainInsertColumns(null, false);

View File

@ -26,10 +26,7 @@ import com.mybatisflex.core.util.CollectionUtil;
import com.mybatisflex.core.util.SqlUtil; import com.mybatisflex.core.util.SqlUtil;
import com.mybatisflex.core.util.StringUtil; import com.mybatisflex.core.util.StringUtil;
import java.util.List; import java.util.*;
import java.util.Map;
import java.util.Set;
import java.util.StringJoiner;
import static com.mybatisflex.core.constant.SqlConsts.*; import static com.mybatisflex.core.constant.SqlConsts.*;
@ -100,7 +97,7 @@ public class OracleDialect extends CommonsDialectImpl {
} }
@Override @Override
public String forInsertEntityBatch(TableInfo tableInfo, List<?> entities) { public String forInsertEntityBatch(TableInfo tableInfo, Collection<?> entities) {
/** /**
* INSERT ALL * INSERT ALL
* INTO t (col1, col2, col3) VALUES ('val1_1', 'val1_2', 'val1_3') * INTO t (col1, col2, col3) VALUES ('val1_1', 'val1_2', 'val1_3')

View File

@ -119,11 +119,11 @@ public class EntitySqlProvider {
* @param params 方法参数 * @param params 方法参数
* @param context 上下文对象 * @param context 上下文对象
* @return SQL 语句 * @return SQL 语句
* @see com.mybatisflex.core.BaseMapper#insertBatch(List) * @see com.mybatisflex.core.BaseMapper#insertBatch(Collection)
* @see com.mybatisflex.core.FlexConsts#METHOD_INSERT_BATCH * @see com.mybatisflex.core.FlexConsts#METHOD_INSERT_BATCH
*/ */
public static String insertBatch(Map params, ProviderContext context) { public static String insertBatch(Map params, ProviderContext context) {
List<Object> entities = ProviderUtil.getEntities(params); Collection<Object> entities = ProviderUtil.getEntities(params);
FlexAssert.notEmpty(entities, "entities"); FlexAssert.notEmpty(entities, "entities");
@ -263,7 +263,7 @@ public class EntitySqlProvider {
boolean ignoreNulls = ProviderUtil.isIgnoreNulls(params); boolean ignoreNulls = ProviderUtil.isIgnoreNulls(params);
QueryWrapper queryWrapper = ProviderUtil.getQueryWrapper(params); QueryWrapper queryWrapper = ProviderUtil.getQueryWrapper(params);
appendTableConditions(context,queryWrapper,false); appendTableConditions(context, queryWrapper, false);
TableInfo tableInfo = ProviderUtil.getTableInfo(context); TableInfo tableInfo = ProviderUtil.getTableInfo(context);
@ -280,7 +280,7 @@ public class EntitySqlProvider {
Object[] values = tableInfo.buildUpdateSqlArgs(entity, ignoreNulls, true); Object[] values = tableInfo.buildUpdateSqlArgs(entity, ignoreNulls, true);
Object[] queryParams = CPI.getConditionValueArray(queryWrapper); Object[] queryParams = CPI.getConditionValueArray(queryWrapper);
Object[] paramValues = ArrayUtil.concat(joinValueArray,ArrayUtil.concat(values,queryParams)); Object[] paramValues = ArrayUtil.concat(joinValueArray, ArrayUtil.concat(values, queryParams));
ProviderUtil.setSqlArgs(params, paramValues); ProviderUtil.setSqlArgs(params, paramValues);
@ -288,7 +288,6 @@ public class EntitySqlProvider {
} }
/** /**
* selectOneById SQL 构建 * selectOneById SQL 构建
* *

View File

@ -119,8 +119,8 @@ class ProviderUtil {
return params.get(FlexConsts.VALUE); return params.get(FlexConsts.VALUE);
} }
public static List<Object> getEntities(Map params) { public static Collection<Object> getEntities(Map params) {
return (List<Object>) params.get(FlexConsts.ENTITIES); return (Collection<Object>) params.get(FlexConsts.ENTITIES);
} }
public static boolean isIgnoreNulls(Map params) { public static boolean isIgnoreNulls(Map params) {