Class MessageUpserter<M extends net.morimekta.providence.PMessage<M>>


  • public class MessageUpserter<M extends net.morimekta.providence.PMessage<M>>
    extends java.lang.Object
    Helper 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.
    
     class MyInserter {
         private static final MessageUpserter&lt;MyMessage,MyMessage._Field&gt; INSERTER =
                 new MessageUpserter.Builder&lt;&gt;("my_message")
                         .set(MyMessage.UUID, MyMessage.NAME)
                         .set("amount", MyMessage.VALUE, Types.INTEGER)  // DOUBLE -&gt; 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);
             }
         }
     }
     
    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 {
         int insert(HandleMyMessage... messages) {
             try (Handle handle = dbi.open()) {
                 return new MessageUpserter.Builder&lt;MyMessage,MyMessage._Field&gt;("my_message")
                         .set(MyMessage.UUID, MyMessage.NAME)
                         .set("amount", MyMessage.VALUE, Types.INTEGER)  // DOUBLE -&gt; INTEGER
                         .onDuplicateKeyUpdateAllExcept(MyMessage.UUID)
                         .build()
                         .execute(handle, messages);
             }
         }
     }
     
    The rules for using this is pretty simple:
    • All fields set must be specified before onDuplicateKey* behavior.
    • Only one of onDuplicateKeyIgnore and onDuplicateKeyUpdate can 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 class  MessageUpserter.Builder<M extends net.morimekta.providence.PMessage<M>>  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int execute​(org.skife.jdbi.v2.Handle handle, java.util.Collection<net.morimekta.providence.PMessageOrBuilder<M>> items)  
      int execute​(org.skife.jdbi.v2.Handle handle, net.morimekta.providence.PMessageOrBuilder<M>... items)  
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Method Detail

      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • execute

        @SafeVarargs
        public final int execute​(@Nonnull
                                 org.skife.jdbi.v2.Handle handle,
                                 @Nonnull
                                 net.morimekta.providence.PMessageOrBuilder<M>... items)
      • execute

        public int execute​(@Nonnull
                           org.skife.jdbi.v2.Handle handle,
                           @Nonnull
                           java.util.Collection<net.morimekta.providence.PMessageOrBuilder<M>> items)