Skip to content

Commit 50755f2

Browse files
committed
Support DuplicateKeyType for multi-row insert in H2 database
1 parent 3b5e86f commit 50755f2

File tree

3 files changed

+24
-20
lines changed

3 files changed

+24
-20
lines changed

doma-core/src/main/java/org/seasar/doma/jdbc/dialect/H2UpsertAssembler.java

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import org.seasar.doma.jdbc.query.DuplicateKeyType;
88
import org.seasar.doma.jdbc.query.QueryOperand;
99
import org.seasar.doma.jdbc.query.QueryOperandPair;
10+
import org.seasar.doma.jdbc.query.QueryOperandPairList;
11+
import org.seasar.doma.jdbc.query.QueryRows;
1012
import org.seasar.doma.jdbc.query.UpsertAssembler;
1113
import org.seasar.doma.jdbc.query.UpsertAssemblerContext;
1214
import org.seasar.doma.jdbc.query.UpsertAssemblerSupport;
@@ -23,7 +25,7 @@ public class H2UpsertAssembler implements UpsertAssembler {
2325

2426
private final List<? extends EntityPropertyType<?, ?>> keys;
2527

26-
private final List<QueryOperandPair> insertValues;
28+
private final QueryRows insertValues;
2729

2830
private final List<QueryOperandPair> setValues;
2931

@@ -34,12 +36,9 @@ public H2UpsertAssembler(UpsertAssemblerContext context) {
3436
this.entityType = context.entityType;
3537
this.duplicateKeyType = context.duplicateKeyType;
3638
this.keys = context.keys;
37-
this.insertValues = context.insertValues.first().getPairs();
39+
this.insertValues = context.insertValues;
3840
this.setValues = context.setValues.getPairs();
3941
this.upsertAssemblerSupport = new UpsertAssemblerSupport(context.naming, context.dialect);
40-
if (context.insertValues.getRows().size() > 1) {
41-
throw new UnsupportedOperationException();
42-
}
4342
}
4443

4544
@Override
@@ -59,13 +58,13 @@ public void assemble() {
5958
}
6059
buf.cutBackSql(5);
6160
buf.appendSql(" when not matched then insert (");
62-
for (QueryOperandPair pair : insertValues) {
61+
for (QueryOperandPair pair : insertValues.first().getPairs()) {
6362
column(pair.getLeft().getEntityPropertyType());
6463
buf.appendSql(", ");
6564
}
6665
buf.cutBackSql(2);
6766
buf.appendSql(") values (");
68-
for (QueryOperandPair pair : insertValues) {
67+
for (QueryOperandPair pair : insertValues.first().getPairs()) {
6968
excludeColumn(pair.getLeft().getEntityPropertyType());
7069
buf.appendSql(", ");
7170
}
@@ -85,19 +84,24 @@ public void assemble() {
8584

8685
private void excludeQuery() {
8786
buf.appendSql("select ");
88-
for (QueryOperandPair pair : insertValues) {
87+
for (QueryOperandPair pair : insertValues.first().getPairs()) {
8988
column(pair.getLeft().getEntityPropertyType());
9089
buf.appendSql(", ");
9190
}
9291
buf.cutBackSql(2);
93-
buf.appendSql(" from values (");
94-
for (QueryOperandPair pair : insertValues) {
95-
pair.getRight().accept(queryOperandVisitor);
96-
buf.appendSql(", ");
92+
buf.appendSql(" from values ");
93+
for (QueryOperandPairList row : insertValues.getRows()) {
94+
buf.appendSql("(");
95+
for (QueryOperandPair pair : row.getPairs()) {
96+
pair.getRight().accept(queryOperandVisitor);
97+
buf.appendSql(", ");
98+
}
99+
buf.cutBackSql(2);
100+
buf.appendSql("), ");
97101
}
98102
buf.cutBackSql(2);
99-
buf.appendSql(") as x (");
100-
for (QueryOperandPair pair : insertValues) {
103+
buf.appendSql(" as x (");
104+
for (QueryOperandPair pair : insertValues.first().getPairs()) {
101105
column(pair.getLeft().getEntityPropertyType());
102106
buf.appendSql(", ");
103107
}

integration-test-java/src/test/java/org/seasar/doma/it/auto/AutoMultiInsertTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ public void whenListIsEmpty(Config config) throws Exception {
414414
}
415415

416416
@Test
417-
@Run(unless = {Dbms.H2, Dbms.SQLSERVER, Dbms.ORACLE})
417+
@Run(unless = {Dbms.SQLSERVER, Dbms.ORACLE})
418418
public void insert_DuplicateKeyType_UPDATE(Config config) throws Exception {
419419
DeptDao dao = new DeptDaoImpl(config);
420420
Dept dept1 = new Dept(new Identity<>(5), 50, "PLANNING", new Location<>("TOKYO"), null);
@@ -456,7 +456,7 @@ public void insert_DuplicateKeyType_UPDATE(Config config) throws Exception {
456456
}
457457

458458
@Test
459-
@Run(unless = {Dbms.H2, Dbms.SQLSERVER, Dbms.ORACLE})
459+
@Run(unless = {Dbms.SQLSERVER, Dbms.ORACLE})
460460
public void insert_DuplicateKeyType_IGNORE(Config config) throws Exception {
461461
DeptDao dao = new DeptDaoImpl(config);
462462
Dept dept1 = new Dept(new Identity<>(5), 50, "PLANNING", new Location<>("TOKYO"), null);
@@ -494,7 +494,7 @@ public void insert_DuplicateKeyType_IGNORE(Config config) throws Exception {
494494
}
495495

496496
@Test
497-
@Run(unless = {Dbms.H2, Dbms.SQLSERVER, Dbms.ORACLE})
497+
@Run(unless = {Dbms.SQLSERVER, Dbms.ORACLE})
498498
public void insert_DuplicateKeyType_UPDATE_compositeKey(Config config) throws Exception {
499499
CompKeyDeptDao dao = new CompKeyDeptDaoImpl(config);
500500
CompKeyDept dept1 =
@@ -545,7 +545,7 @@ public void insert_DuplicateKeyType_UPDATE_compositeKey(Config config) throws Ex
545545
}
546546

547547
@Test
548-
@Run(unless = {Dbms.H2, Dbms.SQLSERVER, Dbms.ORACLE})
548+
@Run(unless = {Dbms.SQLSERVER, Dbms.ORACLE})
549549
public void insert_DuplicateKeyType_IGNORE_compositeKey(Config config) throws Exception {
550550
CompKeyDeptDao dao = new CompKeyDeptDaoImpl(config);
551551
CompKeyDept dept1 =

integration-test-java/src/test/java/org/seasar/doma/it/criteria/EntityqlMultiInsertTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ void test() {
6565
}
6666

6767
@Test
68-
@Run(unless = {Dbms.ORACLE, Dbms.SQLSERVER, Dbms.H2})
68+
@Run(unless = {Dbms.ORACLE, Dbms.SQLSERVER})
6969
void testIgnore() {
7070
Department_ d = new Department_();
7171

@@ -112,7 +112,7 @@ void testIgnore() {
112112
}
113113

114114
@Test
115-
@Run(unless = {Dbms.ORACLE, Dbms.SQLSERVER, Dbms.H2})
115+
@Run(unless = {Dbms.ORACLE, Dbms.SQLSERVER})
116116
void testUpdate() {
117117
Department_ d = new Department_();
118118

0 commit comments

Comments
 (0)