Package net.morimekta.providence.jdbi.v3
Class MessageInserter<M extends net.morimekta.providence.PMessage<M,F>,F extends net.morimekta.providence.descriptor.PField>
- java.lang.Object
-
- net.morimekta.providence.jdbi.v3.MessageInserter<M,F>
-
public class MessageInserter<M extends net.morimekta.providence.PMessage<M,F>,F extends net.morimekta.providence.descriptor.PField> extends java.lang.ObjectHelper class to handle inserting content from messages into a table. The helper will only select values form the message itself, not using nested structure or anything like that. The inserter is built in such a way that you can create the inserter (even as a static field), and use it any number of times with a handle to do the pre-programmed insert. The execute method is thread safe, as long as none of the modification methods are called.
Or it can be handled in line where needed. The building process is pretty cheap, so this should not be a problem unless it is called a lot for very small message.class MyInserter { private static final MessageInserter<MyMessage,MyMessage._Field> INSERTER = new MessageInserter.Builder<>("my_message") .set(MyMessage.UUID, MyMessage.NAME) .set("amount", MyMessage.VALUE, Types.INTEGER) // DOUBLE -> INTEGER .onDuplicateKeyUpdate(MyMessage.VALUE) .build(); private final Jdbi dbi; public MyInserter(Jdbi dbi) { this.dbi = dbi; } int insert(HandleMyMessage... messages) { try (Handle handle = dbi.open()) { return INSERTER.execute(handle, messages); } } }
The rules for using this is pretty simple:class MyInserter { int insert(HandleMyMessage... messages) { try (Handle handle = dbi.open()) { return new MessageInserter.Builder<MyMessage,MyMessage._Field>("my_message") .set(MyMessage.UUID, MyMessage.NAME) .set("amount", MyMessage.VALUE, Types.INTEGER) // DOUBLE -> INTEGER .onDuplicateKeyUpdateAllExcept(MyMessage.UUID) .build() .execute(handle, messages); } } }- All fields set must be specified before onDuplicateKey* behavior.
-
Only one of
onDuplicateKeyIgnoreandonDuplicateKeyUpdatecan be set. -
execute(...)can be called any number of times, and is thread safe.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classMessageInserter.Builder<M extends net.morimekta.providence.PMessage<M,F>,F extends net.morimekta.providence.descriptor.PField>
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intexecute(org.jdbi.v3.core.Handle handle, java.util.Collection<M> items)intexecute(org.jdbi.v3.core.Handle handle, M... items)
-