77import org .seasar .doma .jdbc .query .DuplicateKeyType ;
88import org .seasar .doma .jdbc .query .QueryOperand ;
99import org .seasar .doma .jdbc .query .QueryOperandPair ;
10+ import org .seasar .doma .jdbc .query .QueryOperandPairList ;
11+ import org .seasar .doma .jdbc .query .QueryRows ;
1012import org .seasar .doma .jdbc .query .UpsertAssembler ;
1113import org .seasar .doma .jdbc .query .UpsertAssemblerContext ;
1214import 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 }
0 commit comments