mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
fix: insert row with custom rowKey
This commit is contained in:
parent
6557cea410
commit
f55c643a31
@ -75,8 +75,8 @@ public class CommonsDialectImpl implements IDialect {
|
||||
StringBuilder fields = new StringBuilder();
|
||||
StringBuilder paramsOrPlaceholder = new StringBuilder();
|
||||
|
||||
//插入数据时,需要包含主键
|
||||
Set<String> modifyAttrs = row.keySet();
|
||||
//插入数据时,可能包含主键
|
||||
Set<String> 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<String> attrs = RowCPI.getModifyAttrs(firstRow);
|
||||
Set<String> attrs = RowCPI.getInsertAttrs(firstRow);
|
||||
int index = 0;
|
||||
for (String column : attrs) {
|
||||
fields.append(wrap(column));
|
||||
|
||||
@ -155,7 +155,7 @@ public class OracleDialect extends CommonsDialectImpl {
|
||||
*/
|
||||
StringBuilder fields = new StringBuilder();
|
||||
Row firstRow = rows.get(0);
|
||||
Set<String> attrs = RowCPI.getModifyAttrs(firstRow);
|
||||
Set<String> attrs = RowCPI.getInsertAttrs(firstRow);
|
||||
int index = 0;
|
||||
for (String column : attrs) {
|
||||
fields.append(wrap(column));
|
||||
|
||||
@ -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);
|
||||
|
||||
|
||||
@ -547,6 +547,39 @@ public class Row extends LinkedHashMap<String, Object> implements UpdateWrapper<
|
||||
}
|
||||
|
||||
|
||||
public Object[] obtainInsertValues() {
|
||||
List<Object> 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<String> getInsertAttrs() {
|
||||
Set<String> 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) {
|
||||
|
||||
@ -28,6 +28,15 @@ public class RowCPI {
|
||||
private RowCPI() {
|
||||
}
|
||||
|
||||
public static Object[] obtainInsertValues(Row row) {
|
||||
return row.obtainInsertValues();
|
||||
}
|
||||
|
||||
|
||||
public static Set<String> 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();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -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<Row> rowList = Db.selectAll("tb_account");
|
||||
RowUtil.printPretty(rowList);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -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);
|
||||
@ -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
|
||||
);
|
||||
Loading…
x
Reference in New Issue
Block a user