fix: Db.insertBatchWithFirstRowColumns 当 row key 的顺序没保持一致时,出错的问题 #273

This commit is contained in:
Michael Yang 2024-02-02 11:11:11 +08:00
parent bf3dcbc625
commit 7cd79889ae
4 changed files with 42 additions and 12 deletions

View File

@ -70,7 +70,7 @@ public class RowSqlProvider {
// 先生成 SQL再设置参数 // 先生成 SQL再设置参数
String sql = DialectFactory.getDialect().forInsertRow(schema, tableName, row); String sql = DialectFactory.getDialect().forInsertRow(schema, tableName, row);
ProviderUtil.setSqlArgs(params, RowCPI.obtainInsertValues(row)); ProviderUtil.setSqlArgs(params, row.obtainInsertValues(null));
return sql; return sql;
} }
@ -99,7 +99,7 @@ public class RowSqlProvider {
Object[] values = new Object[]{}; Object[] values = new Object[]{};
for (Row row : rows) { for (Row row : rows) {
values = ArrayUtil.concat(values, RowCPI.obtainInsertValues(row)); values = ArrayUtil.concat(values, row.obtainInsertValues(modifyAttrs));
} }
ProviderUtil.setSqlArgs(params, values); ProviderUtil.setSqlArgs(params, values);

View File

@ -551,17 +551,22 @@ public class Row extends LinkedHashMap<String, Object> implements UpdateWrapper<
} }
public Object[] obtainInsertValues() { public Object[] obtainInsertValues(Set<String> withAttrs) {
List<Object> values = new ArrayList<>(); List<Object> values = new ArrayList<>();
if (primaryKeys != null && !primaryKeys.isEmpty()) {
for (RowKey primaryKey : primaryKeys) { if (withAttrs == null || withAttrs.isEmpty()) {
if (primaryKey.before) { withAttrs = keySet();
values.add(get(primaryKey.keyColumn));
if (primaryKeys != null && !primaryKeys.isEmpty()) {
for (RowKey primaryKey : primaryKeys) {
if (primaryKey.before) {
values.add(get(primaryKey.keyColumn));
}
} }
} }
} }
for (String key : keySet()) { for (String key : withAttrs) {
Object value = get(key); Object value = get(key);
if (!isPk(key) && !(value instanceof RawValue)) { if (!isPk(key) && !(value instanceof RawValue)) {
values.add(value); values.add(value);

View File

@ -28,10 +28,6 @@ public class RowCPI {
private RowCPI() { private RowCPI() {
} }
public static Object[] obtainInsertValues(Row row) {
return row.obtainInsertValues();
}
public static Set<String> getInsertAttrs(Row row) { public static Set<String> getInsertAttrs(Row row) {
return row.getInsertAttrs(); return row.getInsertAttrs();

View File

@ -21,6 +21,7 @@ import com.mybatisflex.core.audit.AuditManager;
import com.mybatisflex.core.query.QueryWrapper; import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.row.Db; import com.mybatisflex.core.row.Db;
import com.mybatisflex.core.row.Row; import com.mybatisflex.core.row.Row;
import com.mybatisflex.core.row.RowUtil;
import com.mybatisflex.core.update.UpdateWrapper; import com.mybatisflex.core.update.UpdateWrapper;
import com.mybatisflex.core.util.UpdateEntity; import com.mybatisflex.core.util.UpdateEntity;
import org.apache.ibatis.session.Configuration; import org.apache.ibatis.session.Configuration;
@ -106,4 +107,32 @@ public class DbTest {
assert false; assert false;
} }
} }
@Test
public void testDbInsertBatchWithFirstRowColumns() {
List<Row> rows = new ArrayList<>();
Row row1 = new Row();
row1.put("id", 111);
row1.put("user_name", "张三");
row1.put("age", 20);
rows.add(row1);
Row row2 = new Row();
row2.put("age", 30);
row2.put("id", 20);
row2.put("user_name", "李四");
rows.add(row2);
Db.insertBatchWithFirstRowColumns("tb_account", rows);
Row row3= new Row();
row3.put("age", 30);
row3.put("id", 333);
row3.put("user_name", "李四3");
Db.insert("tb_account",row3);
RowUtil.printPretty(Db.selectAll("tb_account"));
}
} }