All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.fluentjdbc.DatabaseBulkUpdatable Maven / Gradle / Ivy

There is a newer version: 0.5.3
Show newest version
package org.fluentjdbc;

import javax.annotation.CheckReturnValue;
import java.sql.PreparedStatement;
import java.util.List;
import java.util.function.Function;

/**
 * Fluently create a statement for a list of objects which updates values, ie INSERT
 * or UPDATE
 */
public interface DatabaseBulkUpdatable> {

    /**
     * Adds a function that will be called for each object to get the value for
     * {@link PreparedStatement#setObject(int, Object)} parameter for each row in the bulk update
     * to extract the values for fieldName parameter
     */
    @CheckReturnValue
    SELF setField(String fieldName, Function transformer);

    /**
     * Adds a list function that will be called for each object to get the value
     * each of the corresponding parameters in the {@link PreparedStatement}
     */
    @CheckReturnValue
    default > SELF setFields(List fields, Function values) {
        for (int i = 0, fieldsSize = fields.size(); i < fieldsSize; i++) {
            int index = i;
            //noinspection ResultOfMethodCallIgnored
            setField(fields.get(i), o -> values.apply(o).get(index));
        }
        //noinspection unchecked
        return (SELF) this;
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy