Skip to content

Commit c0e6eed

Browse files
committed
Unification Mysql8UpsertAssembler.java and MysqlUpsertAssembler.java
1 parent 5bf0dd4 commit c0e6eed

File tree

3 files changed

+42
-107
lines changed

3 files changed

+42
-107
lines changed

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

Lines changed: 0 additions & 98 deletions
This file was deleted.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ public void offsetAndFetch(PreparedSqlBuilder buf, int offset, int limit) {
266266

267267
@Override
268268
public UpsertAssembler getUpsertAssembler(UpsertAssemblerContext context) {
269-
return new MysqlUpsertAssembler(context);
269+
return new MysqlUpsertAssembler(context, version);
270270
}
271271

272272
public enum MySqlVersion {

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

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@ public class MysqlUpsertAssembler implements UpsertAssembler {
2020
private final List<Tuple2<EntityPropertyType<?, ?>, InParameter<?>>> insertValues;
2121
private final List<Tuple2<EntityPropertyType<?, ?>, UpsertSetValue>> setValues;
2222
private final UpsertSetValue.Visitor upsertSetValueVisitor = new UpsertSetValueVisitor();
23+
private final MysqlDialect.MySqlVersion version;
2324

24-
public MysqlUpsertAssembler(UpsertAssemblerContext context) {
25+
public MysqlUpsertAssembler(UpsertAssemblerContext context, MysqlDialect.MySqlVersion version) {
2526
this.buf = context.buf;
2627
this.entityType = context.entityType;
2728
this.duplicateKeyType = context.duplicateKeyType;
2829
this.insertValues = context.insertValues;
2930
this.setValues = context.setValues;
31+
this.version = version;
3032
this.upsertAssemblerSupport = new UpsertAssemblerSupport(context.naming, context.dialect);
3133
}
3234

@@ -50,7 +52,17 @@ public void assemble() {
5052
buf.appendSql(", ");
5153
}
5254
buf.cutBackSql(2);
53-
buf.appendSql(") ");
55+
switch (version) {
56+
case V5:
57+
buf.appendSql(") ");
58+
break;
59+
case V8:
60+
buf.appendSql(") as ");
61+
excludeAlias();
62+
break;
63+
default:
64+
throw new IllegalStateException(version.toString());
65+
}
5466
if (duplicateKeyType == DuplicateKeyType.UPDATE) {
5567
buf.appendSql(" on duplicate key update ");
5668
for (Tuple2<EntityPropertyType<?, ?>, UpsertSetValue> setValue : setValues) {
@@ -70,6 +82,11 @@ private void tableNameOnly(EntityType<?> entityType) {
7082
buf.appendSql(sql);
7183
}
7284

85+
private void excludeAlias() {
86+
String sql = this.upsertAssemblerSupport.excludeAlias();
87+
buf.appendSql(sql);
88+
}
89+
7390
private void column(EntityPropertyType<?, ?> propertyType) {
7491
String sql = this.upsertAssemblerSupport.prop(propertyType);
7592
buf.appendSql(sql);
@@ -83,12 +100,28 @@ public void visit(UpsertSetValue.Param param) {
83100

84101
@Override
85102
public void visit(UpsertSetValue.Prop prop) {
86-
String sql =
87-
upsertAssemblerSupport.excludeProp(
88-
prop.propertyType, UpsertAssemblerSupport.ColumnNameType.NAME);
89-
buf.appendSql("values(");
90-
buf.appendSql(sql);
91-
buf.appendSql(")");
103+
switch (version) {
104+
case V5:
105+
{
106+
String sql =
107+
upsertAssemblerSupport.excludeProp(
108+
prop.propertyType, UpsertAssemblerSupport.ColumnNameType.NAME);
109+
buf.appendSql("values(");
110+
buf.appendSql(sql);
111+
buf.appendSql(")");
112+
break;
113+
}
114+
case V8:
115+
{
116+
String sql =
117+
upsertAssemblerSupport.excludeProp(
118+
prop.propertyType, UpsertAssemblerSupport.ColumnNameType.NAME_ALIAS);
119+
buf.appendSql(sql);
120+
break;
121+
}
122+
default:
123+
throw new IllegalStateException(version.toString());
124+
}
92125
}
93126
}
94127
}

0 commit comments

Comments
 (0)