Skip to content

Commit 39544ba

Browse files
committed
Support DuplicateKeyType for multi-row insert in SQL Server
1 parent 50755f2 commit 39544ba

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/MssqlUpsertAssembler.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;
@@ -17,7 +19,7 @@ public class MssqlUpsertAssembler implements UpsertAssembler {
1719
private final DuplicateKeyType duplicateKeyType;
1820
private final UpsertAssemblerSupport upsertAssemblerSupport;
1921
private final List<? extends EntityPropertyType<?, ?>> keys;
20-
private final List<QueryOperandPair> insertValues;
22+
private final QueryRows insertValues;
2123
private final List<QueryOperandPair> setValues;
2224
private final QueryOperand.Visitor queryOperandVisitor = new QueryOperandVisitor();
2325

@@ -26,19 +28,16 @@ public MssqlUpsertAssembler(UpsertAssemblerContext context) {
2628
this.entityType = context.entityType;
2729
this.duplicateKeyType = context.duplicateKeyType;
2830
this.keys = context.keys;
29-
this.insertValues = context.insertValues.first().getPairs();
31+
this.insertValues = context.insertValues;
3032
this.setValues = context.setValues.getPairs();
3133
this.upsertAssemblerSupport = new UpsertAssemblerSupport(context.naming, context.dialect);
32-
if (context.insertValues.getRows().size() > 1) {
33-
throw new UnsupportedOperationException();
34-
}
3534
}
3635

3736
@Override
3837
public void assemble() {
3938
buf.appendSql("merge into ");
4039
tableNameAndAlias(entityType);
41-
buf.appendSql(" using (");
40+
buf.appendSql(" using ");
4241
excludeQuery();
4342
buf.appendSql(" on ");
4443
for (EntityPropertyType<?, ?> key : keys) {
@@ -49,13 +48,13 @@ public void assemble() {
4948
}
5049
buf.cutBackSql(5);
5150
buf.appendSql(" when not matched then insert (");
52-
for (QueryOperandPair pair : insertValues) {
51+
for (QueryOperandPair pair : insertValues.first().getPairs()) {
5352
column(pair.getLeft().getEntityPropertyType());
5453
buf.appendSql(", ");
5554
}
5655
buf.cutBackSql(2);
5756
buf.appendSql(") values (");
58-
for (QueryOperandPair pair : insertValues) {
57+
for (QueryOperandPair pair : insertValues.first().getPairs()) {
5958
excludeColumn(pair.getLeft().getEntityPropertyType());
6059
buf.appendSql(", ");
6160
}
@@ -75,16 +74,21 @@ public void assemble() {
7574
}
7675

7776
private void excludeQuery() {
78-
buf.appendSql("values (");
79-
for (QueryOperandPair pair : insertValues) {
80-
pair.getRight().accept(queryOperandVisitor);
81-
buf.appendSql(", ");
77+
buf.appendSql("(values ");
78+
for (QueryOperandPairList row : insertValues.getRows()) {
79+
buf.appendSql("(");
80+
for (QueryOperandPair pair : row.getPairs()) {
81+
pair.getRight().accept(queryOperandVisitor);
82+
buf.appendSql(", ");
83+
}
84+
buf.cutBackSql(2);
85+
buf.appendSql("), ");
8286
}
8387
buf.cutBackSql(2);
84-
buf.appendSql(")) as ");
88+
buf.appendSql(") as ");
8589
excludeAlias();
8690
buf.appendSql(" (");
87-
for (QueryOperandPair pair : insertValues) {
91+
for (QueryOperandPair pair : insertValues.first().getPairs()) {
8892
column(pair.getLeft().getEntityPropertyType());
8993
buf.appendSql(", ");
9094
}

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.SQLSERVER, Dbms.ORACLE})
417+
@Run(unless = {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.SQLSERVER, Dbms.ORACLE})
459+
@Run(unless = {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.SQLSERVER, Dbms.ORACLE})
497+
@Run(unless = {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.SQLSERVER, Dbms.ORACLE})
548+
@Run(unless = {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})
68+
@Run(unless = {Dbms.ORACLE})
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})
115+
@Run(unless = {Dbms.ORACLE})
116116
void testUpdate() {
117117
Department_ d = new Department_();
118118

0 commit comments

Comments
 (0)