optimize Row

This commit is contained in:
开源海哥 2023-05-28 16:46:56 +08:00
parent 45258ff765
commit 2145114d31
7 changed files with 83 additions and 22 deletions

View File

@ -85,7 +85,7 @@ public class RowSqlProvider {
// 让所有 row 的列顺序和值的数量与第条数据保持一致
// 这个必须 new 一个 LinkedHashSet因为 keepModifyAttrs 会清除 row 所有的 modifyAttrs
Set<String> modifyAttrs = new LinkedHashSet<>(rows.get(0).obtainModifyAttrs());
rows.forEach(row -> RowCPI.keepModifyAttrs(row, modifyAttrs));
rows.forEach(row -> row.prepareAttrs(modifyAttrs));
Object[] values = new Object[]{};
@ -244,6 +244,7 @@ public class RowSqlProvider {
return DialectFactory.getDialect().forUpdateEntity(tableInfo, entity, false);
}
/**
* 执行类似 update table set field=field+1 where ... 的场景
*

View File

@ -316,8 +316,29 @@ public class Row extends LinkedHashMap<String, Object> implements ModifyAttrsRec
return ret;
}
public void prepareAttrsByKeySet(){
this.modifyAttrs.clear();
this.modifyAttrs.addAll(keySet());
void keepModifyAttrs(Collection<String> attrs) {
if (this.primaryKeys != null){
for (RowKey primaryKey : primaryKeys) {
this.modifyAttrs.removeIf(s -> s.equalsIgnoreCase(primaryKey.getKeyColumn()));
}
}
}
public void prepareAttrsByKeySet(RowKey ... primaryKeys){
this.modifyAttrs.clear();
this.modifyAttrs.addAll(keySet());
this.primaryKeys = primaryKeys;
for (RowKey primaryKey : primaryKeys) {
this.modifyAttrs.removeIf(s -> s.equalsIgnoreCase(primaryKey.getKeyColumn()));
}
}
public void prepareAttrs(Collection<String> attrs) {
if (attrs == null) {
throw new NullPointerException("attrs is null.");
}
@ -325,6 +346,17 @@ public class Row extends LinkedHashMap<String, Object> implements ModifyAttrsRec
modifyAttrs.addAll(attrs);
}
public RowKey[] getPrimaryKeys() {
return primaryKeys;
}
public void setPrimaryKeys(RowKey... primaryKeys) {
this.primaryKeys = primaryKeys;
for (RowKey primaryKey : primaryKeys) {
this.modifyAttrs.removeIf(s -> s.equalsIgnoreCase(primaryKey.getKeyColumn()));
}
}
/**
* 获取修改的值值需要保持顺序返回的内容不包含主键的值
*/

View File

@ -15,17 +15,11 @@
*/
package com.mybatisflex.core.row;
import java.util.Collection;
/**
* cross package invoker
*/
public class RowCPI {
public static void keepModifyAttrs(Row row, Collection<String> attrs) {
row.keepModifyAttrs(attrs);
}
public static Object[] obtainModifyValues(Row row) {
return row.obtainModifyValues();
}

View File

@ -16,6 +16,7 @@
package com.mybatisflex.core.row;
import com.mybatisflex.annotation.KeyType;
import com.mybatisflex.core.keygen.KeyGenerators;
import com.mybatisflex.core.util.SqlUtil;
/**
@ -26,12 +27,22 @@ public class RowKey {
/**
* 自增 ID
*/
public static final RowKey ID_AUTO = RowKey.of("id", KeyType.Auto, null, false);
public static final RowKey AUTO = RowKey.of("id", KeyType.Auto, null, false);
/**
* UUID ID
*/
public static final RowKey ID_UUID = RowKey.of("id", KeyType.Generator, "uuid", true);
public static final RowKey UUID = RowKey.of("id", KeyType.Generator, KeyGenerators.uuid, true);
/**
* flexId
*/
public static final RowKey FLEX_ID = RowKey.of("id", KeyType.Generator, KeyGenerators.flexId, true);
/**
* snowFlakeId
*/
public static final RowKey SNOW_FLAKE_ID = RowKey.of("id", KeyType.Generator, KeyGenerators.snowFlakeId, true);
public static RowKey of(String keyColumn) {

View File

@ -133,6 +133,9 @@ public class RowUtil {
public static void printPretty(Row row) {
if (row == null) {
return;
}
printPretty(Collections.singletonList(row));
}

View File

@ -16,10 +16,7 @@
package com.mybatisflex.test;
import com.mybatisflex.core.MybatisFlexBootstrap;
import com.mybatisflex.core.row.BatchArgsSetter;
import com.mybatisflex.core.row.Db;
import com.mybatisflex.core.row.Row;
import com.mybatisflex.core.row.RowKey;
import com.mybatisflex.core.row.*;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
@ -33,7 +30,7 @@ public class DbTestStarter {
DataSource dataSource = new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.H2)
.addScript("schema.sql")
.addScript("data.sql")
// .addScript("data.sql")
.build();
MybatisFlexBootstrap.getInstance()
@ -41,15 +38,18 @@ public class DbTestStarter {
.start();
Row row1 = Db.selectOneById("tb_account", "id", 1);
System.out.println(row1);
RowUtil.printPretty(row1);
//查询全部
List<Row> rows = Db.selectAll("tb_account");
System.out.println(rows);
RowUtil.printPretty(rows);
//插入 1 条数据
Row row = Row.ofKey(RowKey.ID_AUTO);
Row row = Row.ofKey(RowKey.AUTO);
// Row row = new Row();
// row.set("id", 3);
row.set("user_name", "michael yang");
row.set("age", 18);
row.set("birthday", new Date());
@ -80,6 +80,26 @@ public class DbTestStarter {
//再次查询全部数据
rows = Db.selectAll("tb_account");
System.out.println(rows);
RowUtil.printPretty(rows);
// for (Row row2 : rows) {
//// for (String s : row2.keySet()) {
//// if (!s.equalsIgnoreCase("id")) {
//// row2.set(s, row2.get(s));
//// }
//// }
// rows.remove("id");
// }
// rows.forEach(row2 -> row2.setPrimaryKeys(RowKey.AUTO));
rows.forEach(r -> {
r.prepareAttrsByKeySet();
r.setPrimaryKeys(RowKey.AUTO);
});
Db.insertBatch("tb_account", rows, 100);
//再次查询全部数据
rows = Db.selectAll("tb_account");
RowUtil.printPretty(rows);
}
}

View File

@ -75,7 +75,7 @@ public class RowTestStarter {
List<Row> rowList = new ArrayList<>();
for (int i = 0; i < 10; i++) {
Row row = Row.ofKey(RowKey.ID_AUTO);
Row row = Row.ofKey(RowKey.AUTO);
row.set(ACCOUNT.USER_NAME,"zhang" + i);
row.set(ACCOUNT.AGE,18);
// row.set(ACCOUNT.BIRTHDAY,new Date());