Skip to content

Commit 17db9fd

Browse files
committed
Add Mysql8UpsertAssembler
1 parent 6cc94c2 commit 17db9fd

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package org.seasar.doma.jdbc.dialect;
2+
3+
import java.util.List;
4+
import org.seasar.doma.internal.jdbc.sql.PreparedSqlBuilder;
5+
import org.seasar.doma.jdbc.InParameter;
6+
import org.seasar.doma.jdbc.criteria.tuple.Tuple2;
7+
import org.seasar.doma.jdbc.entity.EntityPropertyType;
8+
import org.seasar.doma.jdbc.entity.EntityType;
9+
import org.seasar.doma.jdbc.query.DuplicateKeyType;
10+
import org.seasar.doma.jdbc.query.UpsertAssembler;
11+
import org.seasar.doma.jdbc.query.UpsertAssemblerContext;
12+
import org.seasar.doma.jdbc.query.UpsertAssemblerSupport;
13+
import org.seasar.doma.jdbc.query.UpsertSetValue;
14+
15+
public class Mysql8UpsertAssembler implements UpsertAssembler {
16+
private final PreparedSqlBuilder buf;
17+
private final EntityType<?> entityType;
18+
private final DuplicateKeyType duplicateKeyType;
19+
private final UpsertAssemblerSupport upsertAssemblerSupport;
20+
private final List<Tuple2<EntityPropertyType<?, ?>, InParameter<?>>> insertValues;
21+
private final List<Tuple2<EntityPropertyType<?, ?>, UpsertSetValue>> setValues;
22+
private final UpsertSetValue.Visitor upsertSetValueVisitor = new UpsertSetValueVisitor();
23+
24+
public Mysql8UpsertAssembler(UpsertAssemblerContext context) {
25+
this.buf = context.buf;
26+
this.entityType = context.entityType;
27+
this.duplicateKeyType = context.duplicateKeyType;
28+
this.insertValues = context.insertValues;
29+
this.setValues = context.setValues;
30+
this.upsertAssemblerSupport = new UpsertAssemblerSupport(context.naming, context.dialect);
31+
}
32+
33+
@Override
34+
public void assemble() {
35+
buf.appendSql("insert");
36+
if (duplicateKeyType == DuplicateKeyType.IGNORE) {
37+
buf.appendSql(" ignore");
38+
}
39+
buf.appendSql(" into ");
40+
tableNameOnly(entityType);
41+
buf.appendSql(" (");
42+
for (Tuple2<EntityPropertyType<?, ?>, InParameter<?>> insertValue : insertValues) {
43+
column(insertValue.component1());
44+
buf.appendSql(", ");
45+
}
46+
buf.cutBackSql(2);
47+
buf.appendSql(") values (");
48+
for (Tuple2<EntityPropertyType<?, ?>, InParameter<?>> insertValue : insertValues) {
49+
buf.appendParameter(insertValue.component2());
50+
buf.appendSql(", ");
51+
}
52+
buf.cutBackSql(2);
53+
buf.appendSql(") as ");
54+
excludeAlias();
55+
if (duplicateKeyType == DuplicateKeyType.UPDATE) {
56+
buf.appendSql(" on duplicate key update ");
57+
for (Tuple2<EntityPropertyType<?, ?>, UpsertSetValue> setValue : setValues) {
58+
column(setValue.component1());
59+
buf.appendSql(" = ");
60+
setValue.component2().accept(upsertSetValueVisitor);
61+
buf.appendSql(", ");
62+
}
63+
buf.cutBackSql(2);
64+
}
65+
}
66+
67+
private void tableNameOnly(EntityType<?> entityType) {
68+
String sql =
69+
this.upsertAssemblerSupport.targetTable(
70+
entityType, UpsertAssemblerSupport.TableNameType.NAME);
71+
buf.appendSql(sql);
72+
}
73+
74+
private void excludeAlias() {
75+
String sql = this.upsertAssemblerSupport.excludeAlias();
76+
buf.appendSql(sql);
77+
}
78+
79+
private void column(EntityPropertyType<?, ?> propertyType) {
80+
String sql = this.upsertAssemblerSupport.prop(propertyType);
81+
buf.appendSql(sql);
82+
}
83+
84+
class UpsertSetValueVisitor implements UpsertSetValue.Visitor {
85+
@Override
86+
public void visit(UpsertSetValue.Param param) {
87+
buf.appendParameter(param.inParameter);
88+
}
89+
90+
@Override
91+
public void visit(UpsertSetValue.Prop prop) {
92+
String sql =
93+
upsertAssemblerSupport.excludeProp(
94+
prop.propertyType, UpsertAssemblerSupport.ColumnNameType.NAME_ALIAS);
95+
buf.appendSql(sql);
96+
}
97+
}
98+
}

0 commit comments

Comments
 (0)