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