From f55c643a31fc5138312d48918cf429d77bb6fa0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=80=E6=BA=90=E6=B5=B7=E5=93=A5?= Date: Tue, 22 Aug 2023 12:54:10 +0800 Subject: [PATCH] fix: insert row with custom rowKey --- .../core/dialect/impl/CommonsDialectImpl.java | 6 ++-- .../core/dialect/impl/OracleDialect.java | 2 +- .../core/provider/RowSqlProvider.java | 4 +-- .../java/com/mybatisflex/core/row/Row.java | 33 +++++++++++++++++++ .../java/com/mybatisflex/core/row/RowCPI.java | 10 ++++++ .../com/mybatisflex/test/RowTestStarter.java | 20 +++++++++-- .../src/main/resources/data_row.sql | 9 +++++ .../src/main/resources/schema_row.sql | 20 +++++++++++ 8 files changed, 96 insertions(+), 8 deletions(-) create mode 100644 mybatis-flex-test/mybatis-flex-native-test/src/main/resources/data_row.sql create mode 100644 mybatis-flex-test/mybatis-flex-native-test/src/main/resources/schema_row.sql diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/impl/CommonsDialectImpl.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/impl/CommonsDialectImpl.java index ff9a757a..894ed878 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/impl/CommonsDialectImpl.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/impl/CommonsDialectImpl.java @@ -75,8 +75,8 @@ public class CommonsDialectImpl implements IDialect { StringBuilder fields = new StringBuilder(); StringBuilder paramsOrPlaceholder = new StringBuilder(); - //插入数据时,需要包含主键 - Set modifyAttrs = row.keySet(); + //插入数据时,可能包含主键 + Set modifyAttrs = RowCPI.getInsertAttrs(row); int index = 0; for (String attr : modifyAttrs) { fields.append(wrap(attr)); @@ -112,7 +112,7 @@ public class CommonsDialectImpl implements IDialect { StringBuilder questions = new StringBuilder(); Row firstRow = rows.get(0); - Set attrs = RowCPI.getModifyAttrs(firstRow); + Set attrs = RowCPI.getInsertAttrs(firstRow); int index = 0; for (String column : attrs) { fields.append(wrap(column)); diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/impl/OracleDialect.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/impl/OracleDialect.java index 0e5dec7a..9289ca4e 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/impl/OracleDialect.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/impl/OracleDialect.java @@ -155,7 +155,7 @@ public class OracleDialect extends CommonsDialectImpl { */ StringBuilder fields = new StringBuilder(); Row firstRow = rows.get(0); - Set attrs = RowCPI.getModifyAttrs(firstRow); + Set attrs = RowCPI.getInsertAttrs(firstRow); int index = 0; for (String column : attrs) { fields.append(wrap(column)); diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/provider/RowSqlProvider.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/provider/RowSqlProvider.java index 21d28ccd..8bf1512d 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/provider/RowSqlProvider.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/provider/RowSqlProvider.java @@ -68,7 +68,7 @@ public class RowSqlProvider { // 先生成 SQL,再设置参数 String sql = DialectFactory.getDialect().forInsertRow(schema, tableName, row); - ProviderUtil.setSqlArgs(params, RowCPI.obtainAllModifyValues(row)); + ProviderUtil.setSqlArgs(params, RowCPI.obtainInsertValues(row)); return sql; } @@ -97,7 +97,7 @@ public class RowSqlProvider { Object[] values = new Object[]{}; for (Row row : rows) { - values = ArrayUtil.concat(values, RowCPI.obtainModifyValues(row)); + values = ArrayUtil.concat(values, RowCPI.obtainInsertValues(row)); } ProviderUtil.setSqlArgs(params, values); diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/Row.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/Row.java index e76ff077..585b1393 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/Row.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/Row.java @@ -547,6 +547,39 @@ public class Row extends LinkedHashMap implements UpdateWrapper< } + public Object[] obtainInsertValues() { + List values = new ArrayList<>(); + if (primaryKeys != null && !primaryKeys.isEmpty()) { + for (RowKey primaryKey : primaryKeys) { + if (primaryKey.before) { + values.add(get(primaryKey.keyColumn)); + } + } + } + + for (String key : keySet()) { + Object value = get(key); + if (!isPk(key) && !(value instanceof RawValue)) { + values.add(value); + } + } + + return values.toArray(); + } + + public Set getInsertAttrs() { + Set attrs = new LinkedHashSet<>(); + if (primaryKeys != null && !primaryKeys.isEmpty()) { + for (RowKey primaryKey : primaryKeys) { + if (primaryKey.before) { + attrs.add(primaryKey.keyColumn); + } + } + } + attrs.addAll(keySet()); + return attrs; + } + private boolean isPk(String attr) { if (primaryKeys != null && !primaryKeys.isEmpty()) { for (RowKey primaryKey : primaryKeys) { diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/RowCPI.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/RowCPI.java index 7b35a060..5be7598e 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/RowCPI.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/RowCPI.java @@ -28,6 +28,15 @@ public class RowCPI { private RowCPI() { } + public static Object[] obtainInsertValues(Row row) { + return row.obtainInsertValues(); + } + + + public static Set getInsertAttrs(Row row) { + return row.getInsertAttrs(); + } + public static Object[] obtainModifyValues(Row row) { return row.obtainModifyValuesWithoutPk(); } @@ -56,4 +65,5 @@ public class RowCPI { return row.getRawValueMap(); } + } diff --git a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/RowTestStarter.java b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/RowTestStarter.java index 3d65c19c..1bf1e9ee 100644 --- a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/RowTestStarter.java +++ b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/RowTestStarter.java @@ -15,11 +15,14 @@ */ package com.mybatisflex.test; +import com.mybatisflex.annotation.KeyType; import com.mybatisflex.core.MybatisFlexBootstrap; import com.mybatisflex.core.audit.AuditManager; import com.mybatisflex.core.audit.ConsoleMessageCollector; +import com.mybatisflex.core.keygen.KeyGenerators; import com.mybatisflex.core.row.Db; import com.mybatisflex.core.row.Row; +import com.mybatisflex.core.row.RowKey; import com.mybatisflex.core.row.RowUtil; import org.apache.ibatis.logging.stdout.StdOutImpl; import org.junit.BeforeClass; @@ -36,8 +39,8 @@ public class RowTestStarter { public static void init() { DataSource dataSource = new EmbeddedDatabaseBuilder() .setType(EmbeddedDatabaseType.H2) - .addScript("schema.sql") - .addScript("data.sql") + .addScript("schema_row.sql") + .addScript("data_row.sql") .build(); MybatisFlexBootstrap.getInstance() @@ -61,4 +64,17 @@ public class RowTestStarter { RowUtil.printPretty(rowList); } + @Test + public void testCustomRowKey(){ + RowKey rowKey = RowKey.of("id", KeyType.Generator, KeyGenerators.flexId); + + Row row = Row.ofKey(rowKey); + row.set("user_name","michael"); + row.setRaw("birthday","now()"); + + Db.insert("tb_account",row); + List rowList = Db.selectAll("tb_account"); + RowUtil.printPretty(rowList); + } + } diff --git a/mybatis-flex-test/mybatis-flex-native-test/src/main/resources/data_row.sql b/mybatis-flex-test/mybatis-flex-native-test/src/main/resources/data_row.sql new file mode 100644 index 00000000..2a4051d2 --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-native-test/src/main/resources/data_row.sql @@ -0,0 +1,9 @@ +INSERT INTO tb_account +VALUES (1, '张三', 18, 0,'2020-01-11', '{"key":"value1"}',0), + (2, '王麻子叔叔', 19, 1, '2021-03-21', '{"key":"value2"}',0); + + +INSERT INTO tb_article +VALUES (1, 1,'标题1', '内容1',0), + (2, 2,'标题2', '内容2',0), + (3, 1,'标题3', '内容3',0); diff --git a/mybatis-flex-test/mybatis-flex-native-test/src/main/resources/schema_row.sql b/mybatis-flex-test/mybatis-flex-native-test/src/main/resources/schema_row.sql new file mode 100644 index 00000000..67c069d6 --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-native-test/src/main/resources/schema_row.sql @@ -0,0 +1,20 @@ +CREATE TABLE IF NOT EXISTS `tb_account` +( + `id` VARCHAR(100), + `user_name` VARCHAR(100), + `age` Integer, + `sex` Integer, + `birthday` DATETIME, + `options` VARCHAR(1024), + `is_delete` Integer +); + + +CREATE TABLE IF NOT EXISTS `tb_article` +( + `id` INTEGER auto_increment, + `account_id` Integer, + `title` VARCHAR(100), + `content` text, + `is_delete` Integer +);