Class DatabaseUpdate<T>


  • public class DatabaseUpdate<T>
    extends java.lang.Object
    A class used to setup and execute a database insert or update operation
    Author:
    Xavier Witdouck
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      (package private) static class  DatabaseUpdate.Type  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int apply​(java.util.Collection<T> records)
      Applies this database operation the collection of records
      int apply​(java.util.Iterator<T> records)
      Applies this database operation the iterator of records
      int apply​(java.util.stream.Stream<T> records)
      Applies this database operation the stream of records
      void apply​(T record)
      Applies this operation to the record provided
      java.util.concurrent.Future<java.lang.Integer> applyAsync​(java.util.stream.Stream<T> records)
      Applies this operation asynchronously to the stream of records
      java.util.concurrent.Future<java.lang.Integer> applyAsync​(T record)
      Applies this operation asynchronously to the stream of records
      DatabaseUpdate<T> batchSize​(int batchSize)
      Configures the batch size for this operation
      DatabaseUpdate<T> binder​(DatabaseMapping.Binder<T> binder)
      Applies a custom binder to this update
      private int execute​(java.util.Iterator<T> iterator, java.lang.String sql, DatabaseMapping.Binder<T> binder)
      Executes this operation over the stream of records
      private DatabaseMapping.Binder<T> resolveBinder()
      Returns the binder for this update
      private java.lang.String resolveSql()
      Returns the SQL for this update
      DatabaseUpdate<T> sql​(java.lang.String sql)
      Applies a custom SQL expression to this update
      DatabaseUpdate<T> verbose​(boolean verbose)
      Turns verbose logging on/off for this operation
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • log

        private static final org.slf4j.Logger log
      • verbose

        private boolean verbose
      • dataType

        private java.lang.Class<T> dataType
      • sql

        private com.d3x.core.util.Option<java.lang.String> sql
      • batchSize

        private int batchSize
    • Constructor Detail

      • DatabaseUpdate

        DatabaseUpdate​(Database db,
                       java.lang.Class<T> dataType,
                       DatabaseUpdate.Type type)
        Constructor
        Parameters:
        db - the database reference
        dataType - the data type for this update
        type - the update type (INSERT, UPDATE, DELETE)
    • Method Detail

      • sql

        public DatabaseUpdate<T> sql​(java.lang.String sql)
        Applies a custom SQL expression to this update
        Parameters:
        sql - the SQL expression or classpath resource
        Returns:
        this update
      • verbose

        public DatabaseUpdate<T> verbose​(boolean verbose)
        Turns verbose logging on/off for this operation
        Parameters:
        verbose - true to enable verbose logging
        Returns:
        this update
      • batchSize

        public DatabaseUpdate<T> batchSize​(int batchSize)
        Configures the batch size for this operation
        Parameters:
        batchSize - the batch size
        Returns:
        this operation
      • apply

        public void apply​(T record)
        Applies this operation to the record provided
        Parameters:
        record - the record instance
      • apply

        public int apply​(java.util.Collection<T> records)
        Applies this database operation the collection of records
        Parameters:
        records - the collection of records
        Returns:
        the number of records processed
      • apply

        public int apply​(java.util.Iterator<T> records)
        Applies this database operation the iterator of records
        Parameters:
        records - the iterator of records
        Returns:
        the number of records processed
      • apply

        public int apply​(java.util.stream.Stream<T> records)
        Applies this database operation the stream of records
        Parameters:
        records - the stream of records
        Returns:
        the number of records processed
      • applyAsync

        public java.util.concurrent.Future<java.lang.Integer> applyAsync​(T record)
        Applies this operation asynchronously to the stream of records
        Parameters:
        record - the stream of records
        Returns:
        the Future which exposes the record count
      • applyAsync

        public java.util.concurrent.Future<java.lang.Integer> applyAsync​(java.util.stream.Stream<T> records)
        Applies this operation asynchronously to the stream of records
        Parameters:
        records - the stream of records
        Returns:
        the Future which exposes the record count
      • resolveBinder

        private DatabaseMapping.Binder<T> resolveBinder()
        Returns the binder for this update
        Returns:
        the binder for update
      • resolveSql

        private java.lang.String resolveSql()
        Returns the SQL for this update
        Returns:
        the SQL for this update
      • execute

        private int execute​(java.util.Iterator<T> iterator,
                            java.lang.String sql,
                            DatabaseMapping.Binder<T> binder)
        Executes this operation over the stream of records
        Parameters:
        iterator - the iterator of records
        sql - the SQL expression
        binder - the binder to bind args to a prepared statement
        Returns:
        the number of records processed