Skip to content

Commit 4aed99a

Browse files
committed
Refactor
1 parent 39544ba commit 4aed99a

File tree

11 files changed

+166
-219
lines changed

11 files changed

+166
-219
lines changed

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

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55
import org.seasar.doma.jdbc.entity.EntityPropertyType;
66
import org.seasar.doma.jdbc.entity.EntityType;
77
import org.seasar.doma.jdbc.query.DuplicateKeyType;
8+
import org.seasar.doma.jdbc.query.InsertRow;
89
import org.seasar.doma.jdbc.query.QueryOperand;
910
import org.seasar.doma.jdbc.query.QueryOperandPair;
10-
import org.seasar.doma.jdbc.query.QueryOperandPairList;
11-
import org.seasar.doma.jdbc.query.QueryRows;
1211
import org.seasar.doma.jdbc.query.UpsertAssembler;
1312
import org.seasar.doma.jdbc.query.UpsertAssemblerContext;
1413
import org.seasar.doma.jdbc.query.UpsertAssemblerSupport;
@@ -25,7 +24,9 @@ public class H2UpsertAssembler implements UpsertAssembler {
2524

2625
private final List<? extends EntityPropertyType<?, ?>> keys;
2726

28-
private final QueryRows insertValues;
27+
private final List<? extends EntityPropertyType<?, ?>> insertPropertyTypes;
28+
29+
private final List<InsertRow> insertRows;
2930

3031
private final List<QueryOperandPair> setValues;
3132

@@ -36,8 +37,9 @@ public H2UpsertAssembler(UpsertAssemblerContext context) {
3637
this.entityType = context.entityType;
3738
this.duplicateKeyType = context.duplicateKeyType;
3839
this.keys = context.keys;
39-
this.insertValues = context.insertValues;
40-
this.setValues = context.setValues.getPairs();
40+
this.insertPropertyTypes = context.insertPropertyTypes;
41+
this.insertRows = context.insertRows;
42+
this.setValues = context.setValues;
4143
this.upsertAssemblerSupport = new UpsertAssemblerSupport(context.naming, context.dialect);
4244
}
4345

@@ -58,14 +60,14 @@ public void assemble() {
5860
}
5961
buf.cutBackSql(5);
6062
buf.appendSql(" when not matched then insert (");
61-
for (QueryOperandPair pair : insertValues.first().getPairs()) {
62-
column(pair.getLeft().getEntityPropertyType());
63+
for (EntityPropertyType<?, ?> p : insertPropertyTypes) {
64+
column(p);
6365
buf.appendSql(", ");
6466
}
6567
buf.cutBackSql(2);
6668
buf.appendSql(") values (");
67-
for (QueryOperandPair pair : insertValues.first().getPairs()) {
68-
excludeColumn(pair.getLeft().getEntityPropertyType());
69+
for (EntityPropertyType<?, ?> p : insertPropertyTypes) {
70+
excludeColumn(p);
6971
buf.appendSql(", ");
7072
}
7173
buf.cutBackSql(2);
@@ -84,25 +86,25 @@ public void assemble() {
8486

8587
private void excludeQuery() {
8688
buf.appendSql("select ");
87-
for (QueryOperandPair pair : insertValues.first().getPairs()) {
88-
column(pair.getLeft().getEntityPropertyType());
89+
for (EntityPropertyType<?, ?> p : insertPropertyTypes) {
90+
column(p);
8991
buf.appendSql(", ");
9092
}
9193
buf.cutBackSql(2);
9294
buf.appendSql(" from values ");
93-
for (QueryOperandPairList row : insertValues.getRows()) {
95+
for (InsertRow row : insertRows) {
9496
buf.appendSql("(");
95-
for (QueryOperandPair pair : row.getPairs()) {
96-
pair.getRight().accept(queryOperandVisitor);
97+
for (QueryOperand value : row) {
98+
value.accept(queryOperandVisitor);
9799
buf.appendSql(", ");
98100
}
99101
buf.cutBackSql(2);
100102
buf.appendSql("), ");
101103
}
102104
buf.cutBackSql(2);
103105
buf.appendSql(" as x (");
104-
for (QueryOperandPair pair : insertValues.first().getPairs()) {
105-
column(pair.getLeft().getEntityPropertyType());
106+
for (EntityPropertyType<?, ?> p : insertPropertyTypes) {
107+
column(p);
106108
buf.appendSql(", ");
107109
}
108110
buf.cutBackSql(2);

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

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55
import org.seasar.doma.jdbc.entity.EntityPropertyType;
66
import org.seasar.doma.jdbc.entity.EntityType;
77
import org.seasar.doma.jdbc.query.DuplicateKeyType;
8+
import org.seasar.doma.jdbc.query.InsertRow;
89
import org.seasar.doma.jdbc.query.QueryOperand;
910
import org.seasar.doma.jdbc.query.QueryOperandPair;
10-
import org.seasar.doma.jdbc.query.QueryOperandPairList;
11-
import org.seasar.doma.jdbc.query.QueryRows;
1211
import org.seasar.doma.jdbc.query.UpsertAssembler;
1312
import org.seasar.doma.jdbc.query.UpsertAssemblerContext;
1413
import org.seasar.doma.jdbc.query.UpsertAssemblerSupport;
@@ -19,7 +18,9 @@ public class MssqlUpsertAssembler implements UpsertAssembler {
1918
private final DuplicateKeyType duplicateKeyType;
2019
private final UpsertAssemblerSupport upsertAssemblerSupport;
2120
private final List<? extends EntityPropertyType<?, ?>> keys;
22-
private final QueryRows insertValues;
21+
22+
private final List<? extends EntityPropertyType<?, ?>> insertPropertyTypes;
23+
private final List<InsertRow> insertRows;
2324
private final List<QueryOperandPair> setValues;
2425
private final QueryOperand.Visitor queryOperandVisitor = new QueryOperandVisitor();
2526

@@ -28,8 +29,9 @@ public MssqlUpsertAssembler(UpsertAssemblerContext context) {
2829
this.entityType = context.entityType;
2930
this.duplicateKeyType = context.duplicateKeyType;
3031
this.keys = context.keys;
31-
this.insertValues = context.insertValues;
32-
this.setValues = context.setValues.getPairs();
32+
this.insertPropertyTypes = context.insertPropertyTypes;
33+
this.insertRows = context.insertRows;
34+
this.setValues = context.setValues;
3335
this.upsertAssemblerSupport = new UpsertAssemblerSupport(context.naming, context.dialect);
3436
}
3537

@@ -48,14 +50,14 @@ public void assemble() {
4850
}
4951
buf.cutBackSql(5);
5052
buf.appendSql(" when not matched then insert (");
51-
for (QueryOperandPair pair : insertValues.first().getPairs()) {
52-
column(pair.getLeft().getEntityPropertyType());
53+
for (EntityPropertyType<?, ?> p : insertPropertyTypes) {
54+
column(p);
5355
buf.appendSql(", ");
5456
}
5557
buf.cutBackSql(2);
5658
buf.appendSql(") values (");
57-
for (QueryOperandPair pair : insertValues.first().getPairs()) {
58-
excludeColumn(pair.getLeft().getEntityPropertyType());
59+
for (EntityPropertyType<?, ?> p : insertPropertyTypes) {
60+
excludeColumn(p);
5961
buf.appendSql(", ");
6062
}
6163
buf.cutBackSql(2);
@@ -75,10 +77,10 @@ public void assemble() {
7577

7678
private void excludeQuery() {
7779
buf.appendSql("(values ");
78-
for (QueryOperandPairList row : insertValues.getRows()) {
80+
for (InsertRow row : insertRows) {
7981
buf.appendSql("(");
80-
for (QueryOperandPair pair : row.getPairs()) {
81-
pair.getRight().accept(queryOperandVisitor);
82+
for (QueryOperand value : row) {
83+
value.accept(queryOperandVisitor);
8284
buf.appendSql(", ");
8385
}
8486
buf.cutBackSql(2);
@@ -88,8 +90,8 @@ private void excludeQuery() {
8890
buf.appendSql(") as ");
8991
excludeAlias();
9092
buf.appendSql(" (");
91-
for (QueryOperandPair pair : insertValues.first().getPairs()) {
92-
column(pair.getLeft().getEntityPropertyType());
93+
for (EntityPropertyType<?, ?> p : insertPropertyTypes) {
94+
column(p);
9395
buf.appendSql(", ");
9496
}
9597
buf.cutBackSql(2);

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

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
import org.seasar.doma.jdbc.entity.EntityPropertyType;
77
import org.seasar.doma.jdbc.entity.EntityType;
88
import org.seasar.doma.jdbc.query.DuplicateKeyType;
9+
import org.seasar.doma.jdbc.query.InsertRow;
910
import org.seasar.doma.jdbc.query.QueryOperand;
10-
import org.seasar.doma.jdbc.query.QueryOperandPairList;
11-
import org.seasar.doma.jdbc.query.QueryRows;
11+
import org.seasar.doma.jdbc.query.QueryOperandPair;
1212
import org.seasar.doma.jdbc.query.UpsertAssembler;
1313
import org.seasar.doma.jdbc.query.UpsertAssemblerContext;
1414
import org.seasar.doma.jdbc.query.UpsertAssemblerSupport;
@@ -18,16 +18,18 @@ public class MysqlUpsertAssembler implements UpsertAssembler {
1818
private final EntityType<?> entityType;
1919
private final DuplicateKeyType duplicateKeyType;
2020
private final UpsertAssemblerSupport upsertAssemblerSupport;
21-
private final QueryRows insertValues;
22-
private final QueryOperandPairList setValues;
21+
private final List<? extends EntityPropertyType<?, ?>> insertPropertyTypes;
22+
private final List<InsertRow> insertRows;
23+
private final List<QueryOperandPair> setValues;
2324
private final QueryOperand.Visitor queryOperandVisitor = new QueryOperandVisitor();
2425
private final MysqlDialect.MySqlVersion version;
2526

2627
public MysqlUpsertAssembler(UpsertAssemblerContext context, MysqlDialect.MySqlVersion version) {
2728
this.buf = context.buf;
2829
this.entityType = context.entityType;
2930
this.duplicateKeyType = context.duplicateKeyType;
30-
this.insertValues = context.insertValues;
31+
this.insertPropertyTypes = context.insertPropertyTypes;
32+
this.insertRows = context.insertRows;
3133
this.setValues = context.setValues;
3234
this.version = version;
3335
this.upsertAssemblerSupport = new UpsertAssemblerSupport(context.naming, context.dialect);
@@ -41,33 +43,15 @@ public void assemble() {
4143
}
4244
buf.appendSql(" into ");
4345
tableNameOnly(entityType);
44-
join(
45-
insertValues.first().getPairs(),
46-
", ",
47-
" (",
48-
")",
49-
buf,
50-
p -> {
51-
column(p.getLeft().getEntityPropertyType());
52-
});
46+
join(insertPropertyTypes, ", ", " (", ")", buf, this::column);
5347
buf.appendSql(" values ");
5448
join(
55-
insertValues.getRows(),
49+
insertRows,
5650
", ",
5751
"",
5852
"",
5953
buf,
60-
row -> {
61-
join(
62-
row.getPairs(),
63-
", ",
64-
"(",
65-
")",
66-
buf,
67-
p -> {
68-
p.getRight().accept(queryOperandVisitor);
69-
});
70-
});
54+
row -> join(row, ", ", "(", ")", buf, p -> p.accept(queryOperandVisitor)));
7155
switch (version) {
7256
case V5:
7357
buf.appendSql(" ");
@@ -82,7 +66,7 @@ public void assemble() {
8266
if (duplicateKeyType == DuplicateKeyType.UPDATE) {
8367
buf.appendSql(" on duplicate key update ");
8468
join(
85-
setValues.getPairs(),
69+
setValues,
8670
", ",
8771
"",
8872
"",
@@ -113,14 +97,14 @@ private void column(EntityPropertyType<?, ?> propertyType) {
11397
}
11498

11599
private <T> void join(
116-
List<T> list,
100+
Iterable<T> iterable,
117101
String delimiter,
118102
String start,
119103
String end,
120104
PreparedSqlBuilder buf,
121105
Consumer<T> consumer) {
122106
buf.appendSql(start);
123-
for (T val : list) {
107+
for (T val : iterable) {
124108
consumer.accept(val);
125109
buf.appendSql(delimiter);
126110
}

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

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package org.seasar.doma.jdbc.dialect;
22

33
import java.util.List;
4+
import java.util.stream.Collectors;
45
import org.seasar.doma.internal.jdbc.sql.PreparedSqlBuilder;
6+
import org.seasar.doma.internal.util.Pair;
7+
import org.seasar.doma.internal.util.Zip;
58
import org.seasar.doma.jdbc.entity.EntityPropertyType;
69
import org.seasar.doma.jdbc.entity.EntityType;
710
import org.seasar.doma.jdbc.query.DuplicateKeyType;
11+
import org.seasar.doma.jdbc.query.InsertRow;
812
import org.seasar.doma.jdbc.query.QueryOperand;
913
import org.seasar.doma.jdbc.query.QueryOperandPair;
1014
import org.seasar.doma.jdbc.query.UpsertAssembler;
@@ -17,7 +21,10 @@ public class OracleUpsertAssembler implements UpsertAssembler {
1721
private final DuplicateKeyType duplicateKeyType;
1822
private final UpsertAssemblerSupport upsertAssemblerSupport;
1923
private final List<? extends EntityPropertyType<?, ?>> keys;
20-
private final List<QueryOperandPair> insertValues;
24+
25+
private final List<? extends EntityPropertyType<?, ?>> insertPropertyTypes;
26+
27+
private final List<InsertRow> insertValues;
2128
private final List<QueryOperandPair> setValues;
2229
private final QueryOperand.Visitor queryOperandVisitor = new QueryOperandVisitor();
2330

@@ -26,10 +33,11 @@ public OracleUpsertAssembler(UpsertAssemblerContext context) {
2633
this.entityType = context.entityType;
2734
this.duplicateKeyType = context.duplicateKeyType;
2835
this.keys = context.keys;
29-
this.insertValues = context.insertValues.first().getPairs();
30-
this.setValues = context.setValues.getPairs();
36+
this.insertPropertyTypes = context.insertPropertyTypes;
37+
this.insertValues = context.insertRows;
38+
this.setValues = context.setValues;
3139
this.upsertAssemblerSupport = new UpsertAssemblerSupport(context.naming, context.dialect);
32-
if (context.insertValues.getRows().size() > 1) {
40+
if (context.insertRows.size() > 1) {
3341
throw new UnsupportedOperationException();
3442
}
3543
}
@@ -51,14 +59,14 @@ public void assemble() {
5159
}
5260
buf.cutBackSql(5);
5361
buf.appendSql(") when not matched then insert (");
54-
for (QueryOperandPair pair : insertValues) {
55-
column(pair.getLeft().getEntityPropertyType());
62+
for (EntityPropertyType<?, ?> p : insertPropertyTypes) {
63+
column(p);
5664
buf.appendSql(", ");
5765
}
5866
buf.cutBackSql(2);
5967
buf.appendSql(") values (");
60-
for (QueryOperandPair pair : insertValues) {
61-
excludeColumn(pair.getLeft().getEntityPropertyType());
68+
for (EntityPropertyType<?, ?> p : insertPropertyTypes) {
69+
excludeColumn(p);
6270
buf.appendSql(", ");
6371
}
6472
buf.cutBackSql(2);
@@ -76,11 +84,17 @@ public void assemble() {
7684
}
7785

7886
private void excludeQuery() {
87+
// TODO
88+
InsertRow row =
89+
insertValues.stream().findFirst().orElseThrow(UnsupportedOperationException::new);
90+
List<Pair<? extends EntityPropertyType<?, ?>, QueryOperand>> pairs =
91+
Zip.stream(insertPropertyTypes, row).collect(Collectors.toList());
92+
7993
buf.appendSql("select ");
80-
for (QueryOperandPair pair : insertValues) {
81-
pair.getRight().accept(queryOperandVisitor);
94+
for (Pair<? extends EntityPropertyType<?, ?>, QueryOperand> pair : pairs) {
95+
pair.snd.accept(queryOperandVisitor);
8296
buf.appendSql(" as ");
83-
column(pair.getLeft().getEntityPropertyType());
97+
column(pair.fst);
8498
buf.appendSql(", ");
8599
}
86100
buf.cutBackSql(2);

0 commit comments

Comments
 (0)